Skip to content

Commit

Permalink
Merge pull request #74 from foodora/remove-redundant-unit-test
Browse files Browse the repository at this point in the history
remove redundant TestClient_MaxIdleConns unit test
  • Loading branch information
Muharem Ismailov committed Sep 5, 2019
2 parents 7d6318b + 1150499 commit 21f21dd
Showing 1 changed file with 3 additions and 85 deletions.
88 changes: 3 additions & 85 deletions fdhttp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,12 @@ package fdhttp_test

import (
"bytes"
"context"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/foodora/go-ranger/fdhttp"
"github.com/foodora/go-ranger/fdhttp/fdmiddleware"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

func newClientMiddleware(called *bool) fdmiddleware.ClientMiddleware {
Expand Down Expand Up @@ -48,77 +40,3 @@ func TestClient_CallMiddleware(t *testing.T) {
assert.True(t, called)
assert.True(t, srvCalled)
}

type testConn struct {
net.Conn
closeFn func() error
}

func (c *testConn) Close() error {
return c.closeFn()
}

func middlewareConnCount(activeConns *int32) fdmiddleware.ClientMiddleware {
return fdmiddleware.ClientMiddlewareFunc(func(next http.RoundTripper) http.RoundTripper {
nextTr := next.(*http.Transport)
tr := &http.Transport{
MaxIdleConns: nextTr.MaxIdleConns,
MaxIdleConnsPerHost: nextTr.MaxIdleConnsPerHost,
IdleConnTimeout: nextTr.IdleConnTimeout,
TLSHandshakeTimeout: nextTr.TLSHandshakeTimeout,
ExpectContinueTimeout: nextTr.ExpectContinueTimeout,
}

tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
atomic.AddInt32(activeConns, 1)

conn, _ := next.(*http.Transport).DialContext(ctx, network, addr)
tConn := &testConn{
Conn: conn,
closeFn: func() error {
atomic.AddInt32(activeConns, -1)
return conn.Close()
},
}
return tConn, nil
}
return tr
})
}

func httpGetParallel(t *testing.T, c fdhttp.Client, url string, times int) {
var wg sync.WaitGroup
for i := 0; i < times; i++ {
wg.Add(1)
go func() {
resp, err := c.Get(url)
assert.NoError(t, err)
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
wg.Done()
}()
}

wg.Wait()
}

func TestClient_MaxIdleConns(t *testing.T) {
tests := []int{1, 2, 5}

for _, expectedActiveConns := range tests {
c := fdhttp.NewClient()
c.SetMaxIdleConns(expectedActiveConns)
c.SetMaxIdleConnsPerHost(expectedActiveConns)

var activeConns int32
c.Use(middlewareConnCount(&activeConns))

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Millisecond)
}))
defer ts.Close()

httpGetParallel(t, c, ts.URL, 10)
assert.EqualValues(t, expectedActiveConns, atomic.LoadInt32(&activeConns))
}
}

0 comments on commit 21f21dd

Please sign in to comment.