// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete.
func WithCancel(parent Context) (ctx Context, cancel CancelFunc) {
+ if parent == nil {
+ panic("cannot create context from nil parent")
+ }
c := newCancelCtx(parent)
propagateCancel(parent, &c)
return &c, func() { c.cancel(true, Canceled) }
// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete.
func WithDeadline(parent Context, d time.Time) (Context, CancelFunc) {
+ if parent == nil {
+ panic("cannot create context from nil parent")
+ }
if cur, ok := parent.Deadline(); ok && cur.Before(d) {
// The current deadline is already sooner than the new one.
return WithCancel(parent)
// struct{}. Alternatively, exported context key variables' static
// type should be a pointer or interface.
func WithValue(parent Context, key, val interface{}) Context {
+ if parent == nil {
+ panic("cannot create context from nil parent")
+ }
if key == nil {
panic("nil key")
}
}
}
+func XTestInvalidDerivedFail(t testingT) {
+ panicVal := recoveredValue(func() { WithCancel(nil) })
+ if panicVal == nil {
+ t.Error("expected panic")
+ }
+ panicVal = recoveredValue(func() { WithDeadline(nil, time.Now().Add(shortDuration)) })
+ if panicVal == nil {
+ t.Error("expected panic")
+ }
+ panicVal = recoveredValue(func() { WithValue(nil, "foo", "bar") })
+ if panicVal == nil {
+ t.Error("expected panic")
+ }
+}
+
func recoveredValue(fn func()) (v interface{}) {
defer func() { v = recover() }()
fn()
func TestCancelRemoves(t *testing.T) { XTestCancelRemoves(t) }
func TestWithCancelCanceledParent(t *testing.T) { XTestWithCancelCanceledParent(t) }
func TestWithValueChecksKey(t *testing.T) { XTestWithValueChecksKey(t) }
+func TestInvalidDerivedFail(t *testing.T) { XTestInvalidDerivedFail(t) }
func TestDeadlineExceededSupportsTimeout(t *testing.T) { XTestDeadlineExceededSupportsTimeout(t) }
func TestCustomContextGoroutines(t *testing.T) { XTestCustomContextGoroutines(t) }