From e6d29450e57c2ca85bc7cfc14b8c3221f1c9f462 Mon Sep 17 00:00:00 2001 From: Victor Vrantchan Date: Wed, 9 Jan 2019 13:40:38 -0500 Subject: [PATCH] some tests --- ntrack/listener_test.go | 96 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 ntrack/listener_test.go diff --git a/ntrack/listener_test.go b/ntrack/listener_test.go new file mode 100644 index 0000000..3b0c6f9 --- /dev/null +++ b/ntrack/listener_test.go @@ -0,0 +1,96 @@ +package ntrack + +import ( + "fmt" + "net" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opencensus.io/stats/view" +) + +func TestListener(t *testing.T) { + var tests = []struct { + viewName string + disableKeepalive bool + expectedValue int64 + }{ + { + viewName: "ntrack/listener/accepts", + disableKeepalive: true, + expectedValue: 5, + }, + { + viewName: "ntrack/listener/accepts", + disableKeepalive: false, + expectedValue: 1, + }, + { + viewName: "ntrack/listener/closed", + disableKeepalive: true, + expectedValue: 5, + }, + { + viewName: "ntrack/listener/open", + disableKeepalive: true, + expectedValue: 0, + }, + { + viewName: "ntrack/listener/open", + disableKeepalive: false, + expectedValue: 1, + }, + } + + for _, tt := range tests { + t.Run(tt.viewName, func(t *testing.T) { + lis, err := net.Listen("tcp", "127.0.0.1:0") + require.NoError(t, err) + + ilis, stats := NewInstrumentedListener(lis) + view.Register(stats.views...) + + testClientConnections(t, ilis, tt.disableKeepalive) + + rows, err := view.RetrieveData(tt.viewName) + require.NoError(t, err) + + switch data := rows[0].Data.(type) { + case *view.CountData: + assert.Equal(t, tt.expectedValue, data.Value) + case *view.LastValueData: + assert.Equal(t, float64(tt.expectedValue), data.Value) + } + view.Unregister(stats.views...) + }) + } +} + +func testClientConnections(t *testing.T, lis net.Listener, disableKeepalive bool) { + t.Helper() + + srv := &http.Server{ + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }), + } + + go func() { + if err := srv.Serve(lis); err != nil { + t.Fatal(err) + } + }() + + tr := &http.Transport{DisableKeepAlives: disableKeepalive} + client := &http.Client{Transport: tr} + + requestCount := 5 + for i := 0; i < requestCount; i++ { + resp, err := client.Get(fmt.Sprintf("http://%s", lis.Addr())) + require.NoError(t, err) + resp.Body.Close() + } + +}