diff options
author | mopeneko <mopeneko@lem0n.cc> | 2024-02-10 18:26:36 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-10 10:26:36 +0100 |
commit | b430b88e7880ba596e093acf8e53186729e58882 (patch) | |
tree | b962536fed219128bcc848b4103d566ca68111ca /server_test.go | |
parent | add DisableDNSResolution for TCPDialer. Sometimes, users do not need to use D... (diff) | |
download | fasthttp-b430b88e7880ba596e093acf8e53186729e58882.tar.gz fasthttp-b430b88e7880ba596e093acf8e53186729e58882.tar.bz2 fasthttp-b430b88e7880ba596e093acf8e53186729e58882.zip |
Implement `GetRejectedConnectionsCount` function (#1704)
* Implement `GetRejectedConnectionsCount`
* Implement test for `GetRejectedConnectionsCount`
Diffstat (limited to 'server_test.go')
-rw-r--r-- | server_test.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/server_test.go b/server_test.go index efc126f..58454db 100644 --- a/server_test.go +++ b/server_test.go @@ -1016,6 +1016,62 @@ func TestServerConcurrencyLimit(t *testing.T) { } } +func TestRejectedRequestsCount(t *testing.T) { + t.Parallel() + + s := &Server{ + Handler: func(ctx *RequestCtx) { + ctx.WriteString("OK") //nolint:errcheck + }, + Concurrency: 1, + Logger: &testLogger{}, + } + + ln := fasthttputil.NewInmemoryListener() + + serverCh := make(chan struct{}) + go func() { + if err := s.Serve(ln); err != nil { + t.Errorf("unexpected error: %v", err) + } + close(serverCh) + }() + + clientCh := make(chan struct{}) + expectedCount := 5 + go func() { + for i := 0; i < expectedCount+1; i++ { + _, err := ln.Dial() + if err != nil { + t.Errorf("unexpected error: %v", err) + } + } + + if cnt := s.GetRejectedConnectionsCount(); cnt != uint32(expectedCount) { + t.Errorf("unexpected rejected connections count: %d. Expecting %d", + cnt, expectedCount) + } + + close(clientCh) + }() + + select { + case <-clientCh: + case <-time.After(time.Second): + t.Fatal("timeout") + } + + if err := ln.Close(); err != nil { + t.Fatalf("unexpected error: %v", err) + } + + select { + case <-serverCh: + case <-time.After(time.Second): + t.Fatal("timeout") + } +} + func TestServerWriteFastError(t *testing.T) { t.Parallel() |