From efc32091e7da4fcb18ad47b6484efb6406d0b69f Mon Sep 17 00:00:00 2001 From: Derek McGowan Date: Mon, 9 Jan 2017 17:10:49 -0800 Subject: [PATCH] Fix unit test If running test behide a proxy, we may get the error code 403 Forbidden which will fail line 135 for the last testcase. Detail: ``` metrics not as expected: notifications.EndpointMetrics{Pending:0, Events:0, Successes:4, Failures:0, Errors:0, Statuses:map[string]int{"307 Temporary Redirect":0, "400 Bad Request":0, "403 Forbidden":0, "200 OK":4}} != notifications.EndpointMetrics{Pending:0, Events:0, Successes:4, Failures:0, Errors:0, Statuses:map[string]int{"400 Bad Request":0, "200 OK":4, "307 Temporary Redirect":0}} ``` Immediate close will fix that Signed-off-by: Hu Keping Signed-off-by: Derek McGowan --- notifications/http_test.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/notifications/http_test.go b/notifications/http_test.go index e0469362..de47f789 100644 --- a/notifications/http_test.go +++ b/notifications/http_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "mime" + "net" "net/http" "net/http/httptest" "reflect" @@ -94,6 +95,21 @@ func TestHTTPSink(t *testing.T) { var expectedMetrics EndpointMetrics expectedMetrics.Statuses = make(map[string]int) + closeL, err := net.Listen("tcp", "localhost:0") + if err != nil { + t.Fatalf("unexpected error creating listener: %v", err) + } + defer closeL.Close() + go func() { + for { + c, err := closeL.Accept() + if err != nil { + return + } + c.Close() + } + }() + for _, tc := range []struct { events []Event // events to send url string @@ -121,8 +137,8 @@ func TestHTTPSink(t *testing.T) { failure: true, }, { - // Case where connection never goes through. - url: "http://shoudlntresolve/", + // Case where connection is immediately closed + url: closeL.Addr().String(), failure: true, }, } {