From d0a299c027fc6b075bed4992329f93e618186a83 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Thu, 2 Jul 2015 11:43:07 -0400 Subject: [PATCH] Send resp immediately on GET /events Signed-off-by: Brian Goff --- api/server/server.go | 4 +++- integration-cli/docker_api_events_test.go | 29 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 integration-cli/docker_api_events_test.go diff --git a/api/server/server.go b/api/server/server.go index 7b24b8c82e..1659f5834e 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -430,7 +430,9 @@ func (s *Server) getEvents(version version.Version, w http.ResponseWriter, r *ht d := s.daemon es := d.EventsService w.Header().Set("Content-Type", "application/json") - enc := json.NewEncoder(ioutils.NewWriteFlusher(w)) + outStream := ioutils.NewWriteFlusher(w) + outStream.Write(nil) // make sure response is sent immediately + enc := json.NewEncoder(outStream) getContainerId := func(cn string) string { c, err := d.Get(cn) diff --git a/integration-cli/docker_api_events_test.go b/integration-cli/docker_api_events_test.go new file mode 100644 index 0000000000..8ae7bf737f --- /dev/null +++ b/integration-cli/docker_api_events_test.go @@ -0,0 +1,29 @@ +package main + +import ( + "net/http" + "time" + + "github.com/go-check/check" +) + +func (s *DockerSuite) TestEventsApiEmptyOutput(c *check.C) { + type apiResp struct { + resp *http.Response + err error + } + chResp := make(chan *apiResp) + go func() { + resp, body, err := sockRequestRaw("GET", "/events", nil, "") + body.Close() + chResp <- &apiResp{resp, err} + }() + + select { + case r := <-chResp: + c.Assert(r.err, check.IsNil) + c.Assert(r.resp.StatusCode, check.Equals, http.StatusOK) + case <-time.After(3 * time.Second): + c.Fatal("timeout waiting for events api to respond, should have responded immediately") + } +}