| // Copyright 2020 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package xcontext |
| |
| import ( |
| "context" |
| "testing" |
| "time" |
| ) |
| |
| type ctxKey string |
| |
| var key = ctxKey("key") |
| |
| func TestDetach(t *testing.T) { |
| ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond) |
| defer cancel() |
| |
| ctx = context.WithValue(ctx, key, "value") |
| dctx := Detach(ctx) |
| // Detached context has the same values. |
| got, ok := dctx.Value(key).(string) |
| if !ok || got != "value" { |
| t.Errorf("Value: got (%v, %t), want 'value', true", got, ok) |
| } |
| |
| // Wait for the parent context to time out, and then give it an arbitrary |
| // amount of extra time for the cancellation to propagate if it's going to. |
| <-ctx.Done() |
| time.Sleep(5 * time.Millisecond) |
| |
| if err := ctx.Err(); err != context.DeadlineExceeded { |
| t.Fatalf("original context Err: got %v, want DeadlineExceeded", err) |
| } |
| if err := dctx.Err(); err != nil { |
| t.Errorf("detached context Err: got %v, want nil", err) |
| } |
| } |