diff options
author | Erik Dubbelboer <erik@dubbelboer.com> | 2020-09-13 13:58:13 +0200 |
---|---|---|
committer | Erik Dubbelboer <erik@dubbelboer.com> | 2020-09-13 13:58:13 +0200 |
commit | ae8b65fa6279eb1c654d837612f06894384935b1 (patch) | |
tree | 25f2b9d172f39d0b2079aa88d969e3a625cc4fd9 /client_test.go | |
parent | Git commit fix URI parse for urls like host.dm?some/path/to/file (#866) (diff) | |
download | fasthttp-ae8b65fa6279eb1c654d837612f06894384935b1.tar.gz fasthttp-ae8b65fa6279eb1c654d837612f06894384935b1.tar.bz2 fasthttp-ae8b65fa6279eb1c654d837612f06894384935b1.zip |
Add Client.CloseIdleConnections()
Diffstat (limited to 'client_test.go')
-rw-r--r-- | client_test.go | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/client_test.go b/client_test.go index bb04cef..95b102a 100644 --- a/client_test.go +++ b/client_test.go @@ -20,6 +20,50 @@ import ( "github.com/valyala/fasthttp/fasthttputil" ) +func TestCloseIdleConnections(t *testing.T) { + ln := fasthttputil.NewInmemoryListener() + + s := &Server{ + Handler: func(ctx *RequestCtx) { + }, + } + go func() { + if err := s.Serve(ln); err != nil { + t.Error(err) + } + }() + + c := &Client{ + Dial: func(addr string) (net.Conn, error) { + return ln.Dial() + }, + } + + if _, _, err := c.Get(nil, "http://google.com"); err != nil { + t.Fatal(err) + } + + connsLen := func() int { + c.mLock.Lock() + defer c.mLock.Unlock() + + c.m["google.com"].connsLock.Lock() + defer c.m["google.com"].connsLock.Unlock() + + return len(c.m["google.com"].conns) + } + + if conns := connsLen(); conns > 1 { + t.Errorf("expected 1 conns got %d", conns) + } + + c.CloseIdleConnections() + + if conns := connsLen(); conns > 0 { + t.Errorf("expected 0 conns got %d", conns) + } +} + func TestPipelineClientIssue832(t *testing.T) { t.Parallel() |