How to improve this context
cascading test:
func TestParentTimedoutContext(t *testing.T) {
ctx1, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()
ctx2, cancel2 := context.WithTimeout(ctx1, 1000*time.Millisecond)
defer cancel2()
time.Sleep(15 * time.Millisecond) // make ctx1 timeout
if ctx2.Err() == nil {
t.Error("failed")
}
}
Command:
go test -timeout 30s -count=1 -run ^TestParentTimedoutContext$ my/internal/services
Output:
ok
Sometimes:
failed
go version go1.16.6