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 <hukeping@huawei.com>
Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
Derek McGowan 2017-01-09 17:10:49 -08:00
parent 129ad8ea0c
commit efc32091e7
No known key found for this signature in database
GPG Key ID: F58C5D0A4405ACDB

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"mime" "mime"
"net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"reflect" "reflect"
@ -94,6 +95,21 @@ func TestHTTPSink(t *testing.T) {
var expectedMetrics EndpointMetrics var expectedMetrics EndpointMetrics
expectedMetrics.Statuses = make(map[string]int) 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 { for _, tc := range []struct {
events []Event // events to send events []Event // events to send
url string url string
@ -121,8 +137,8 @@ func TestHTTPSink(t *testing.T) {
failure: true, failure: true,
}, },
{ {
// Case where connection never goes through. // Case where connection is immediately closed
url: "http://shoudlntresolve/", url: closeL.Addr().String(),
failure: true, failure: true,
}, },
} { } {