Files
fasthttp/server_race_test.go
T
不插电 f203307887 fix: ShutdownWithContext and ctx.Done() exist race. (#1908)
* fix: ShutdownWithContext and ctx.Done() exist race.

* fix: Even if ln.Close() err, the Shutdown process should still proceed.

* refactor: remove END label.
2024-12-08 12:52:06 +01:00

47 lines
896 B
Go

//go:build race
package fasthttp
import (
"context"
"github.com/valyala/fasthttp/fasthttputil"
"math"
"testing"
)
func TestServerDoneRace(t *testing.T) {
t.Parallel()
s := &Server{
Handler: func(ctx *RequestCtx) {
for i := 0; i < math.MaxInt; i++ {
ctx.Done()
}
},
}
ln := fasthttputil.NewInmemoryListener()
defer ln.Close()
go func() {
if err := s.Serve(ln); err != nil {
t.Errorf("unexpected error: %v", err)
}
}()
c, err := ln.Dial()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
defer c.Close()
if _, err = c.Write([]byte("POST / HTTP/1.1\r\nHost: go.dev\r\nContent-Length: 3\r\n\r\nABC" +
"\r\n\r\n" + // <-- this stuff is bogus, but we'll ignore it
"GET / HTTP/1.1\r\nHost: go.dev\r\n\r\n")); err != nil {
t.Fatal(err)
}
ctx, cancelFunc := context.WithCancel(context.Background())
cancelFunc()
s.ShutdownWithContext(ctx)
}