(t *testing.T)
| 25 | ) |
| 26 | |
| 27 | func TestDo(t *testing.T) { |
| 28 | t.Parallel() |
| 29 | |
| 30 | ctx, cancel := context.WithCancel(context.Background()) |
| 31 | var k struct{} |
| 32 | v := "incontext" |
| 33 | ctx = context.WithValue(ctx, k, v) //nolint:staticcheck |
| 34 | |
| 35 | assert.Nil(t, contextError(ctx)) |
| 36 | assert.Equal(t, ctx.Value(k), v) |
| 37 | |
| 38 | cancel() |
| 39 | assert.Error(t, contextError(ctx)) |
| 40 | assert.Equal(t, ctx.Value(k), v) |
| 41 | |
| 42 | t.Run("with canceled context", func(t *testing.T) { |
| 43 | t.Parallel() |
| 44 | Do(ctx, func(ctx context.Context) { |
| 45 | assert.NoError(t, contextError(ctx)) |
| 46 | assert.Equal(t, ctx.Value(k), v) |
| 47 | }) |
| 48 | }) |
| 49 | |
| 50 | t.Run("wrap with cancel", func(t *testing.T) { |
| 51 | t.Parallel() |
| 52 | Do(ctx, func(ctx context.Context) { |
| 53 | ctx, cancelFn := context.WithCancel(ctx) |
| 54 | cancelFn() |
| 55 | assert.ErrorIs(t, contextError(ctx), context.Canceled) |
| 56 | assert.Equal(t, ctx.Value(k), v) |
| 57 | }) |
| 58 | }) |
| 59 | |
| 60 | // canceled after 10 seconds |
| 61 | t.Run("timeout", func(t *testing.T) { |
| 62 | t.Parallel() |
| 63 | |
| 64 | done := make(chan struct{}) |
| 65 | called := make(chan struct{}) |
| 66 | |
| 67 | Do(context.Background(), func(ctx context.Context) { |
| 68 | close(called) |
| 69 | select { |
| 70 | case <-ctx.Done(): |
| 71 | close(done) |
| 72 | case <-time.After(11 * time.Second): |
| 73 | t.Error("context was not canceled in time") |
| 74 | } |
| 75 | }) |
| 76 | |
| 77 | select { |
| 78 | case <-done: |
| 79 | // success |
| 80 | case <-time.After(1 * time.Second): |
| 81 | t.Error("timeout waiting for context cancellation") |
| 82 | } |
| 83 | |
| 84 | select { |
nothing calls this directly
no test coverage detected
searching dependent graphs…