1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 // Each XTestFoo in context_test.go must be called from a TestFoo here to run.
20 func TestParentFinishesChild(t *testing.T) {
21 XTestParentFinishesChild(t) // uses unexported context types
23 func TestChildFinishesFirst(t *testing.T) {
24 XTestChildFinishesFirst(t) // uses unexported context types
26 func TestCancelRemoves(t *testing.T) {
27 XTestCancelRemoves(t) // uses unexported context types
29 func TestCustomContextGoroutines(t *testing.T) {
30 XTestCustomContextGoroutines(t) // reads the context.goroutines counter
33 // The following are regular tests in package context_test.
35 // otherContext is a Context that's not one of the types defined in context.go.
36 // This lets us test code paths that differ based on the underlying type of the
38 type otherContext struct {
43 shortDuration = 1 * time.Millisecond // a reasonable duration to block in a test
44 veryLongDuration = 1000 * time.Hour // an arbitrary upper bound on the test's running time
47 // quiescent returns an arbitrary duration by which the program should have
48 // completed any remaining work and reached a steady (idle) state.
49 func quiescent(t *testing.T) time.Duration {
50 deadline, ok := t.Deadline()
52 return 5 * time.Second
55 const arbitraryCleanupMargin = 1 * time.Second
56 return time.Until(deadline) - arbitraryCleanupMargin
58 func TestBackground(t *testing.T) {
61 t.Fatalf("Background returned nil")
65 t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
68 if got, want := fmt.Sprint(c), "context.Background"; got != want {
69 t.Errorf("Background().String() = %q want %q", got, want)
73 func TestTODO(t *testing.T) {
76 t.Fatalf("TODO returned nil")
80 t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
83 if got, want := fmt.Sprint(c), "context.TODO"; got != want {
84 t.Errorf("TODO().String() = %q want %q", got, want)
88 func TestWithCancel(t *testing.T) {
89 c1, cancel := WithCancel(Background())
91 if got, want := fmt.Sprint(c1), "context.Background.WithCancel"; got != want {
92 t.Errorf("c1.String() = %q want %q", got, want)
96 c2, _ := WithCancel(o)
97 contexts := []Context{c1, o, c2}
99 for i, c := range contexts {
100 if d := c.Done(); d == nil {
101 t.Errorf("c[%d].Done() == %v want non-nil", i, d)
103 if e := c.Err(); e != nil {
104 t.Errorf("c[%d].Err() == %v want nil", i, e)
108 case x := <-c.Done():
109 t.Errorf("<-c.Done() == %v want nothing (it should block)", x)
114 cancel() // Should propagate synchronously.
115 for i, c := range contexts {
119 t.Errorf("<-c[%d].Done() blocked, but shouldn't have", i)
121 if e := c.Err(); e != Canceled {
122 t.Errorf("c[%d].Err() == %v want %v", i, e, Canceled)
127 func testDeadline(c Context, name string, t *testing.T) {
130 timer := time.NewTimer(d)
134 t.Fatalf("%s: context not timed out after %v", name, d)
137 if e := c.Err(); e != DeadlineExceeded {
138 t.Errorf("%s: c.Err() == %v; want %v", name, e, DeadlineExceeded)
142 func TestDeadline(t *testing.T) {
145 c, _ := WithDeadline(Background(), time.Now().Add(shortDuration))
146 if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
147 t.Errorf("c.String() = %q want prefix %q", got, prefix)
149 testDeadline(c, "WithDeadline", t)
151 c, _ = WithDeadline(Background(), time.Now().Add(shortDuration))
153 testDeadline(o, "WithDeadline+otherContext", t)
155 c, _ = WithDeadline(Background(), time.Now().Add(shortDuration))
157 c, _ = WithDeadline(o, time.Now().Add(veryLongDuration))
158 testDeadline(c, "WithDeadline+otherContext+WithDeadline", t)
160 c, _ = WithDeadline(Background(), time.Now().Add(-shortDuration))
161 testDeadline(c, "WithDeadline+inthepast", t)
163 c, _ = WithDeadline(Background(), time.Now())
164 testDeadline(c, "WithDeadline+now", t)
167 func TestTimeout(t *testing.T) {
170 c, _ := WithTimeout(Background(), shortDuration)
171 if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
172 t.Errorf("c.String() = %q want prefix %q", got, prefix)
174 testDeadline(c, "WithTimeout", t)
176 c, _ = WithTimeout(Background(), shortDuration)
178 testDeadline(o, "WithTimeout+otherContext", t)
180 c, _ = WithTimeout(Background(), shortDuration)
182 c, _ = WithTimeout(o, veryLongDuration)
183 testDeadline(c, "WithTimeout+otherContext+WithTimeout", t)
186 func TestCanceledTimeout(t *testing.T) {
187 c, _ := WithTimeout(Background(), time.Second)
189 c, cancel := WithTimeout(o, veryLongDuration)
190 cancel() // Should propagate synchronously.
194 t.Errorf("<-c.Done() blocked, but shouldn't have")
196 if e := c.Err(); e != Canceled {
197 t.Errorf("c.Err() == %v want %v", e, Canceled)
205 var k2 = key2(1) // same int as k1, different type
206 var k3 = key2(3) // same type as k2, different int
208 func TestValues(t *testing.T) {
209 check := func(c Context, nm, v1, v2, v3 string) {
210 if v, ok := c.Value(k1).(string); ok == (len(v1) == 0) || v != v1 {
211 t.Errorf(`%s.Value(k1).(string) = %q, %t want %q, %t`, nm, v, ok, v1, len(v1) != 0)
213 if v, ok := c.Value(k2).(string); ok == (len(v2) == 0) || v != v2 {
214 t.Errorf(`%s.Value(k2).(string) = %q, %t want %q, %t`, nm, v, ok, v2, len(v2) != 0)
216 if v, ok := c.Value(k3).(string); ok == (len(v3) == 0) || v != v3 {
217 t.Errorf(`%s.Value(k3).(string) = %q, %t want %q, %t`, nm, v, ok, v3, len(v3) != 0)
222 check(c0, "c0", "", "", "")
224 c1 := WithValue(Background(), k1, "c1k1")
225 check(c1, "c1", "c1k1", "", "")
227 if got, want := fmt.Sprint(c1), `context.Background.WithValue(type context_test.key1, val c1k1)`; got != want {
228 t.Errorf("c.String() = %q want %q", got, want)
231 c2 := WithValue(c1, k2, "c2k2")
232 check(c2, "c2", "c1k1", "c2k2", "")
234 c3 := WithValue(c2, k3, "c3k3")
235 check(c3, "c2", "c1k1", "c2k2", "c3k3")
237 c4 := WithValue(c3, k1, nil)
238 check(c4, "c4", "", "c2k2", "c3k3")
240 o0 := otherContext{Background()}
241 check(o0, "o0", "", "", "")
243 o1 := otherContext{WithValue(Background(), k1, "c1k1")}
244 check(o1, "o1", "c1k1", "", "")
246 o2 := WithValue(o1, k2, "o2k2")
247 check(o2, "o2", "c1k1", "o2k2", "")
249 o3 := otherContext{c4}
250 check(o3, "o3", "", "c2k2", "c3k3")
252 o4 := WithValue(o3, k3, nil)
253 check(o4, "o4", "", "c2k2", "")
256 func TestAllocs(t *testing.T) {
258 for _, test := range []struct {
265 desc: "Background()",
266 f: func() { Background() },
271 desc: fmt.Sprintf("WithValue(bg, %v, nil)", k1),
273 c := WithValue(bg, k1, nil)
280 desc: "WithTimeout(bg, 1*time.Nanosecond)",
282 c, _ := WithTimeout(bg, 1*time.Nanosecond)
289 desc: "WithCancel(bg)",
291 c, cancel := WithCancel(bg)
299 desc: "WithTimeout(bg, 5*time.Millisecond)",
301 c, cancel := WithTimeout(bg, 5*time.Millisecond)
310 if runtime.Compiler == "gccgo" {
311 // gccgo does not yet do escape analysis.
312 // TODO(iant): Remove this when gccgo does do escape analysis.
313 limit = test.gccgoLimit
319 if n := testing.AllocsPerRun(numRuns, test.f); n > limit {
320 t.Errorf("%s allocs = %f want %d", test.desc, n, int(limit))
325 func TestSimultaneousCancels(t *testing.T) {
326 root, cancel := WithCancel(Background())
327 m := map[Context]CancelFunc{root: cancel}
329 // Create a tree of contexts.
330 for len(q) != 0 && len(m) < 100 {
333 for i := 0; i < 4; i++ {
334 ctx, cancel := WithCancel(parent)
339 // Start all the cancels in a random order.
340 var wg sync.WaitGroup
342 for _, cancel := range m {
343 go func(cancel CancelFunc) {
350 stuck := make(chan struct{})
351 timer := time.AfterFunc(d, func() { close(stuck) })
354 // Wait on all the contexts in a random order.
359 buf := make([]byte, 10<<10)
360 n := runtime.Stack(buf, true)
361 t.Fatalf("timed out after %v waiting for <-ctx.Done(); stacks:\n%s", d, buf[:n])
364 // Wait for all the cancel functions to return.
365 done := make(chan struct{})
373 buf := make([]byte, 10<<10)
374 n := runtime.Stack(buf, true)
375 t.Fatalf("timed out after %v waiting for cancel functions; stacks:\n%s", d, buf[:n])
379 func TestInterlockedCancels(t *testing.T) {
380 parent, cancelParent := WithCancel(Background())
381 child, cancelChild := WithCancel(parent)
388 timer := time.NewTimer(d)
393 buf := make([]byte, 10<<10)
394 n := runtime.Stack(buf, true)
395 t.Fatalf("timed out after %v waiting for child.Done(); stacks:\n%s", d, buf[:n])
399 func TestLayersCancel(t *testing.T) {
400 testLayers(t, time.Now().UnixNano(), false)
403 func TestLayersTimeout(t *testing.T) {
404 testLayers(t, time.Now().UnixNano(), true)
407 func testLayers(t *testing.T, seed int64, testTimeout bool) {
410 r := rand.New(rand.NewSource(seed))
411 errorf := func(format string, a ...any) {
412 t.Errorf(fmt.Sprintf("seed=%d: %s", seed, format), a...)
424 for i := 0; i < minLayers || numTimers == 0 || len(cancels) == 0 || len(vals) == 0; i++ {
428 ctx = WithValue(ctx, v, v)
429 vals = append(vals, v)
431 var cancel CancelFunc
432 ctx, cancel = WithCancel(ctx)
433 cancels = append(cancels, cancel)
435 var cancel CancelFunc
436 d := veryLongDuration
440 ctx, cancel = WithTimeout(ctx, d)
441 cancels = append(cancels, cancel)
445 checkValues := func(when string) {
446 for _, key := range vals {
447 if val := ctx.Value(key).(*value); key != val {
448 errorf("%s: ctx.Value(%p) = %p want %p", when, key, val, key)
455 errorf("ctx should not be canceled yet")
459 if s, prefix := fmt.Sprint(ctx), "context.Background."; !strings.HasPrefix(s, prefix) {
460 t.Errorf("ctx.String() = %q want prefix %q", s, prefix)
463 checkValues("before cancel")
466 timer := time.NewTimer(d)
471 errorf("ctx should have timed out after %v", d)
473 checkValues("after timeout")
475 cancel := cancels[r.Intn(len(cancels))]
480 errorf("ctx should be canceled")
482 checkValues("after cancel")
486 func TestWithCancelCanceledParent(t *testing.T) {
487 parent, pcancel := WithCancelCause(Background())
488 cause := fmt.Errorf("Because!")
491 c, _ := WithCancel(parent)
495 t.Errorf("child not done immediately upon construction")
497 if got, want := c.Err(), Canceled; got != want {
498 t.Errorf("child not canceled; got = %v, want = %v", got, want)
500 if got, want := Cause(c), cause; got != want {
501 t.Errorf("child has wrong cause; got = %v, want = %v", got, want)
505 func TestWithCancelSimultaneouslyCanceledParent(t *testing.T) {
506 // Cancel the parent goroutine concurrently with creating a child.
507 for i := 0; i < 100; i++ {
508 parent, pcancel := WithCancelCause(Background())
509 cause := fmt.Errorf("Because!")
512 c, _ := WithCancel(parent)
514 if got, want := c.Err(), Canceled; got != want {
515 t.Errorf("child not canceled; got = %v, want = %v", got, want)
517 if got, want := Cause(c), cause; got != want {
518 t.Errorf("child has wrong cause; got = %v, want = %v", got, want)
523 func TestWithValueChecksKey(t *testing.T) {
524 panicVal := recoveredValue(func() { WithValue(Background(), []byte("foo"), "bar") })
526 t.Error("expected panic")
528 panicVal = recoveredValue(func() { WithValue(Background(), nil, "bar") })
529 if got, want := fmt.Sprint(panicVal), "nil key"; got != want {
530 t.Errorf("panic = %q; want %q", got, want)
534 func TestInvalidDerivedFail(t *testing.T) {
535 panicVal := recoveredValue(func() { WithCancel(nil) })
537 t.Error("expected panic")
539 panicVal = recoveredValue(func() { WithDeadline(nil, time.Now().Add(shortDuration)) })
541 t.Error("expected panic")
543 panicVal = recoveredValue(func() { WithValue(nil, "foo", "bar") })
545 t.Error("expected panic")
549 func recoveredValue(fn func()) (v any) {
550 defer func() { v = recover() }()
555 func TestDeadlineExceededSupportsTimeout(t *testing.T) {
556 i, ok := DeadlineExceeded.(interface {
560 t.Fatal("DeadlineExceeded does not support Timeout interface")
563 t.Fatal("wrong value for timeout")
566 func TestCause(t *testing.T) {
568 forever = 1e6 * time.Second
569 parentCause = fmt.Errorf("parentCause")
570 childCause = fmt.Errorf("childCause")
571 tooSlow = fmt.Errorf("tooSlow")
572 finishedEarly = fmt.Errorf("finishedEarly")
574 for _, test := range []struct {
594 ctx: func() Context {
595 ctx, cancel := WithCancel(Background())
603 name: "WithCancelCause",
604 ctx: func() Context {
605 ctx, cancel := WithCancelCause(Background())
613 name: "WithCancelCause nil",
614 ctx: func() Context {
615 ctx, cancel := WithCancelCause(Background())
623 name: "WithCancelCause: parent cause before child",
624 ctx: func() Context {
625 ctx, cancelParent := WithCancelCause(Background())
626 ctx, cancelChild := WithCancelCause(ctx)
627 cancelParent(parentCause)
628 cancelChild(childCause)
635 name: "WithCancelCause: parent cause after child",
636 ctx: func() Context {
637 ctx, cancelParent := WithCancelCause(Background())
638 ctx, cancelChild := WithCancelCause(ctx)
639 cancelChild(childCause)
640 cancelParent(parentCause)
647 name: "WithCancelCause: parent cause before nil",
648 ctx: func() Context {
649 ctx, cancelParent := WithCancelCause(Background())
650 ctx, cancelChild := WithCancel(ctx)
651 cancelParent(parentCause)
659 name: "WithCancelCause: parent cause after nil",
660 ctx: func() Context {
661 ctx, cancelParent := WithCancelCause(Background())
662 ctx, cancelChild := WithCancel(ctx)
664 cancelParent(parentCause)
671 name: "WithCancelCause: child cause after nil",
672 ctx: func() Context {
673 ctx, cancelParent := WithCancel(Background())
674 ctx, cancelChild := WithCancelCause(ctx)
676 cancelChild(childCause)
683 name: "WithCancelCause: child cause before nil",
684 ctx: func() Context {
685 ctx, cancelParent := WithCancel(Background())
686 ctx, cancelChild := WithCancelCause(ctx)
687 cancelChild(childCause)
696 ctx: func() Context {
697 ctx, cancel := WithTimeout(Background(), 0)
701 err: DeadlineExceeded,
702 cause: DeadlineExceeded,
705 name: "WithTimeout canceled",
706 ctx: func() Context {
707 ctx, cancel := WithTimeout(Background(), forever)
715 name: "WithTimeoutCause",
716 ctx: func() Context {
717 ctx, cancel := WithTimeoutCause(Background(), 0, tooSlow)
721 err: DeadlineExceeded,
725 name: "WithTimeoutCause canceled",
726 ctx: func() Context {
727 ctx, cancel := WithTimeoutCause(Background(), forever, tooSlow)
735 name: "WithTimeoutCause stacked",
736 ctx: func() Context {
737 ctx, cancel := WithCancelCause(Background())
738 ctx, _ = WithTimeoutCause(ctx, 0, tooSlow)
739 cancel(finishedEarly)
742 err: DeadlineExceeded,
746 name: "WithTimeoutCause stacked canceled",
747 ctx: func() Context {
748 ctx, cancel := WithCancelCause(Background())
749 ctx, _ = WithTimeoutCause(ctx, forever, tooSlow)
750 cancel(finishedEarly)
754 cause: finishedEarly,
757 name: "WithoutCancel",
758 ctx: func() Context {
759 return WithoutCancel(Background())
765 name: "WithoutCancel canceled",
766 ctx: func() Context {
767 ctx, cancel := WithCancelCause(Background())
768 ctx = WithoutCancel(ctx)
769 cancel(finishedEarly)
776 name: "WithoutCancel timeout",
777 ctx: func() Context {
778 ctx, cancel := WithTimeoutCause(Background(), 0, tooSlow)
779 ctx = WithoutCancel(ctx)
788 t.Run(test.name, func(t *testing.T) {
791 if got, want := ctx.Err(), test.err; want != got {
792 t.Errorf("ctx.Err() = %v want %v", got, want)
794 if got, want := Cause(ctx), test.cause; want != got {
795 t.Errorf("Cause(ctx) = %v want %v", got, want)
801 func TestCauseRace(t *testing.T) {
802 cause := errors.New("TestCauseRace")
803 ctx, cancel := WithCancelCause(Background())
808 // Poll Cause, rather than waiting for Done, to test that
809 // access to the underlying cause is synchronized properly.
810 if err := Cause(ctx); err != nil {
812 t.Errorf("Cause returned %v, want %v", err, cause)
820 func TestWithoutCancel(t *testing.T) {
821 key, value := "key", "value"
822 ctx := WithValue(Background(), key, value)
823 ctx = WithoutCancel(ctx)
824 if d, ok := ctx.Deadline(); !d.IsZero() || ok != false {
825 t.Errorf("ctx.Deadline() = %v, %v want zero, false", d, ok)
827 if done := ctx.Done(); done != nil {
828 t.Errorf("ctx.Deadline() = %v want nil", done)
830 if err := ctx.Err(); err != nil {
831 t.Errorf("ctx.Err() = %v want nil", err)
833 if v := ctx.Value(key); v != value {
834 t.Errorf("ctx.Value(%q) = %q want %q", key, v, value)
838 type customDoneContext struct {
843 func (c *customDoneContext) Done() <-chan struct{} {
847 func TestCustomContextPropagation(t *testing.T) {
848 cause := errors.New("TestCustomContextPropagation")
849 donec := make(chan struct{})
850 ctx1, cancel1 := WithCancelCause(Background())
851 ctx2 := &customDoneContext{
855 ctx3, cancel3 := WithCancel(ctx2)
862 if got, want := ctx3.Err(), Canceled; got != want {
863 t.Errorf("child not canceled; got = %v, want = %v", got, want)
865 if got, want := Cause(ctx3), cause; got != want {
866 t.Errorf("child has wrong cause; got = %v, want = %v", got, want)
870 func TestAfterFuncCalledAfterCancel(t *testing.T) {
871 ctx, cancel := WithCancel(Background())
872 donec := make(chan struct{})
873 stop := AfterFunc(ctx, func() {
878 t.Fatalf("AfterFunc called before context is done")
879 case <-time.After(shortDuration):
884 case <-time.After(veryLongDuration):
885 t.Fatalf("AfterFunc not called after context is canceled")
888 t.Fatalf("stop() = true, want false")
892 func TestAfterFuncCalledAfterTimeout(t *testing.T) {
893 ctx, cancel := WithTimeout(Background(), shortDuration)
895 donec := make(chan struct{})
896 AfterFunc(ctx, func() {
901 case <-time.After(veryLongDuration):
902 t.Fatalf("AfterFunc not called after context is canceled")
906 func TestAfterFuncCalledImmediately(t *testing.T) {
907 ctx, cancel := WithCancel(Background())
909 donec := make(chan struct{})
910 AfterFunc(ctx, func() {
915 case <-time.After(veryLongDuration):
916 t.Fatalf("AfterFunc not called for already-canceled context")
920 func TestAfterFuncNotCalledAfterStop(t *testing.T) {
921 ctx, cancel := WithCancel(Background())
922 donec := make(chan struct{})
923 stop := AfterFunc(ctx, func() {
927 t.Fatalf("stop() = false, want true")
932 t.Fatalf("AfterFunc called for already-canceled context")
933 case <-time.After(shortDuration):
936 t.Fatalf("stop() = true, want false")
940 // This test verifies that cancelling a context does not block waiting for AfterFuncs to finish.
941 func TestAfterFuncCalledAsynchronously(t *testing.T) {
942 ctx, cancel := WithCancel(Background())
943 donec := make(chan struct{})
944 stop := AfterFunc(ctx, func() {
945 // The channel send blocks until donec is read from.
950 // After cancel returns, read from donec and unblock the AfterFunc.
953 case <-time.After(veryLongDuration):
954 t.Fatalf("AfterFunc not called after context is canceled")