MCPcopy
hub / github.com/gliderlabs/ssh / TestSetValueConcurrency

Function TestSetValueConcurrency

context_test.go:52–85  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

50}
51
52func TestSetValueConcurrency(t *testing.T) {
53 ctx, cancel := newContext(nil)
54 defer cancel()
55
56 go func() {
57 for { // use a loop to access context.Context functions to make sure they are thread-safe with SetValue
58 _, _ = ctx.Deadline()
59 _ = ctx.Err()
60 _ = ctx.Value("foo")
61 select {
62 case <-ctx.Done():
63 break
64 default:
65 }
66 }
67 }()
68 ctx.SetValue("bar", -1) // a context value which never changes
69 now := time.Now()
70 var cnt int64
71 go func() {
72 for time.Since(now) < 100*time.Millisecond {
73 cnt++
74 ctx.SetValue("foo", cnt) // a context value which changes a lot
75 }
76 cancel()
77 }()
78 <-ctx.Done()
79 if ctx.Value("foo") != cnt {
80 t.Fatal("context.Value(foo) doesn't match latest SetValue")
81 }
82 if ctx.Value("bar") != -1 {
83 t.Fatal("context.Value(bar) doesn't match latest SetValue")
84 }
85}

Callers

nothing calls this directly

Calls 3

newContextFunction · 0.85
ValueMethod · 0.80
SetValueMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…