(t *testing.T)
| 50 | } |
| 51 | |
| 52 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…