Skip to content

Commit b989e99

Browse files
committed
CBG-4373: implement flusher interface on counted and non counted response writer
1 parent 88edb9a commit b989e99

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

rest/api_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,3 +2857,37 @@ func TestAllDbs(t *testing.T) {
28572857
RequireStatus(t, resp, http.StatusOK)
28582858
require.Equal(t, fmt.Sprintf(`[{"db_name":"%s","bucket":"%s","state":"Online"}]`, rt.GetDatabase().Name, rt.GetDatabase().Bucket.GetName()), resp.Body.String())
28592859
}
2860+
2861+
// TestBufferFlush will test for http.ResponseWriter implements Flusher interface
2862+
func TestBufferFlush(t *testing.T) {
2863+
rt := NewRestTester(t, &RestTesterConfig{
2864+
SyncFn: channels.DocChannelsSyncFunction,
2865+
})
2866+
defer rt.Close()
2867+
ctx := base.TestCtx(t)
2868+
2869+
a := rt.ServerContext().Database(ctx, "db").Authenticator(ctx)
2870+
2871+
// Create a test user
2872+
user, err := a.NewUser("foo", "letmein", channels.BaseSetOf(t, "foo"))
2873+
require.NoError(t, err)
2874+
require.NoError(t, a.Save(user))
2875+
2876+
// create some changes
2877+
for i := 0; i < 10; i++ {
2878+
rt.PutDoc(fmt.Sprint(i), `{"some":"doc", "channels":["foo"]}`)
2879+
}
2880+
2881+
var wg sync.WaitGroup
2882+
var resp *TestResponse
2883+
wg.Add(1)
2884+
go func() {
2885+
defer wg.Done()
2886+
resp = rt.SendUserRequest(http.MethodGet, "/{{.keyspace}}/_changes?feed=continuous&since=0&timeout=5000&include_docs=true", "", "foo")
2887+
RequireStatus(t, resp, http.StatusOK)
2888+
}()
2889+
wg.Wait()
2890+
2891+
// assert that the response is a flushed response
2892+
assert.True(t, resp.Flushed)
2893+
}

rest/counted_response_writer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,10 @@ func (w *CountedResponseWriter) isHijackable() bool {
9292
_, ok := w.writer.(http.Hijacker)
9393
return ok
9494
}
95+
96+
func (w *CountedResponseWriter) Flush() {
97+
f, ok := w.writer.(http.Flusher)
98+
if ok {
99+
f.Flush()
100+
}
101+
}

rest/non_counted_response_writer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,10 @@ func (w *NonCountedResponseWriter) isHijackable() bool {
4545
_, ok := w.ResponseWriter.(http.Hijacker)
4646
return ok
4747
}
48+
49+
func (w *NonCountedResponseWriter) Flush() {
50+
f, ok := w.ResponseWriter.(http.Flusher)
51+
if ok {
52+
f.Flush()
53+
}
54+
}

0 commit comments

Comments
 (0)