context: fix removal of cancelled timer contexts from parent
Change-Id: Iee673c97e6a3b779c3d8ba6bb1b5f2b2e2032b86
Reviewed-on: https://go-review.googlesource.com/3911
Reviewed-by: Sameer Ajmani <sameer@golang.org>
diff --git a/context/context_test.go b/context/context_test.go
index 82d2494..faf6772 100644
--- a/context/context_test.go
+++ b/context/context_test.go
@@ -551,3 +551,25 @@
checkValues("after cancel")
}
}
+
+func TestCancelRemoves(t *testing.T) {
+ checkChildren := func(when string, ctx Context, want int) {
+ if got := len(ctx.(*cancelCtx).children); got != want {
+ t.Errorf("%s: context has %d children, want %d", when, got, want)
+ }
+ }
+
+ ctx, _ := WithCancel(Background())
+ checkChildren("after creation", ctx, 0)
+ _, cancel := WithCancel(ctx)
+ checkChildren("with WithCancel child ", ctx, 1)
+ cancel()
+ checkChildren("after cancelling WithCancel child", ctx, 0)
+
+ ctx, _ = WithCancel(Background())
+ checkChildren("after creation", ctx, 0)
+ _, cancel = WithTimeout(ctx, 60*time.Minute)
+ checkChildren("with WithTimeout child ", ctx, 1)
+ cancel()
+ checkChildren("after cancelling WithTimeout child", ctx, 0)
+}