From 3d07e3b6094c8a2fb5dbbf0d195617e8e9239449 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 13 Jan 2015 10:31:21 -0700 Subject: [PATCH] Refactor response count check. --- collectd/collectd_test.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/collectd/collectd_test.go b/collectd/collectd_test.go index fc1ed39949..4bc7d01e5a 100644 --- a/collectd/collectd_test.go +++ b/collectd/collectd_test.go @@ -34,6 +34,21 @@ func (testServer) WriteSeries(database, retentionPolicy, name string, tags map[s return 0, nil } +func (testServer) ResponseN(n int) ([]*serverResponse, error) { + var a []*serverResponse + for { + select { + case r := <-responses: + a = append(a, r) + if len(a) == n { + return a, nil + } + case <-time.After(time.Second): + return a, fmt.Errorf("unexpected response count: %d", len(a)) + } + } +} + func TestServer_ListenAndServe_ErrBindAddressRequired(t *testing.T) { var ( ts testServer @@ -166,19 +181,8 @@ func TestServer_Serve_Success(t *testing.T) { t.Fatalf("err does not match. expected %v, got %v", nil, e) } - expectedRespCnt := 33 - var resps []*serverResponse - for { - select { - case r := <-responses: - resps = append(resps, r) - if len(resps) == expectedRespCnt { - return - } - case <-time.After(time.Second): - t.Fatalf("timed out after %d of %d expected responses", len(resps), expectedRespCnt) - return - } + if _, err := ts.ResponseN(33); err != nil { + t.Fatal(err) } }