diff options
author | kinggo <lilong.21@bytedance.com> | 2022-11-20 19:26:36 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-20 13:26:36 +0200 |
commit | 49951353c83ac7b3ca9a78570dbea0c970b6d801 (patch) | |
tree | 8f3be03c091834f7c7f11eb8b84ba6c2fd7e1c7a /server_test.go | |
parent | style: modify typo and remove repeated type conversions (#1437) (diff) | |
download | fasthttp-49951353c83ac7b3ca9a78570dbea0c970b6d801.tar.gz fasthttp-49951353c83ac7b3ca9a78570dbea0c970b6d801.tar.bz2 fasthttp-49951353c83ac7b3ca9a78570dbea0c970b6d801.zip |
Diffstat (limited to 'server_test.go')
-rw-r--r-- | server_test.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/server_test.go b/server_test.go index 333a866..0c44a23 100644 --- a/server_test.go +++ b/server_test.go @@ -16,6 +16,7 @@ import ( "runtime" "strings" "sync" + "sync/atomic" "testing" "time" @@ -3594,6 +3595,57 @@ func TestShutdownCloseIdleConns(t *testing.T) { } } +func TestShutdownWithContext(t *testing.T) { + t.Parallel() + + ln := fasthttputil.NewInmemoryListener() + s := &Server{ + Handler: func(ctx *RequestCtx) { + time.Sleep(5 * time.Second) + ctx.Success("aaa/bbb", []byte("real response")) + }, + } + go func() { + if err := s.Serve(ln); err != nil { + t.Errorf("unexepcted error: %v", err) + } + }() + time.Sleep(1 * time.Second) + go func() { + conn, err := ln.Dial() + if err != nil { + t.Errorf("unexepcted error: %v", err) + } + + if _, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: google.com\r\n\r\n")); err != nil { + t.Errorf("unexpected error: %v", err) + } + br := bufio.NewReader(conn) + verifyResponse(t, br, StatusOK, "aaa/bbb", "real response") + }() + + time.Sleep(1 * time.Second) + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond) + defer cancel() + shutdownErr := make(chan error) + go func() { + shutdownErr <- s.ShutdownWithContext(ctx) + }() + + timer := time.NewTimer(time.Second) + select { + case <-timer.C: + t.Fatal("idle connections not closed on shutdown") + case err := <-shutdownErr: + if err == nil || err != context.DeadlineExceeded { + t.Fatalf("unexpected err %v. Expecting %v", err, context.DeadlineExceeded) + } + } + if atomic.LoadInt32(&s.open) != 1 { + t.Fatalf("unexpected open connection num: %#v. Expecting %#v", atomic.LoadInt32(&s.open), 1) + } +} + func TestMultipleServe(t *testing.T) { t.Parallel() |