blob: ceb44bb5f85ef94c3920cea715cfda8a1001e66c [file] [log] [blame]
// 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 (
type ctxKey string
var key = ctxKey("key")
func TestDetach(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 200*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)
// Detached context doesn't time out.
time.Sleep(500 * 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)