|
| 1 | +package background |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "runtime" |
| 6 | + "sync/atomic" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/empty" |
| 13 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xatomic" |
| 14 | + "github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest" |
| 15 | +) |
| 16 | + |
| 17 | +func TestWorkerContext(t *testing.T) { |
| 18 | + t.Run("Empty", func(t *testing.T) { |
| 19 | + w := Worker{} |
| 20 | + require.NotNil(t, w.Context()) |
| 21 | + require.NotNil(t, w.ctx) |
| 22 | + require.NotNil(t, w.stop) |
| 23 | + }) |
| 24 | + |
| 25 | + t.Run("Dedicated", func(t *testing.T) { |
| 26 | + ctx := context.WithValue(context.Background(), "1", "2") |
| 27 | + w := NewWorker(ctx) |
| 28 | + require.Equal(t, "2", w.Context().Value("1")) |
| 29 | + }) |
| 30 | + |
| 31 | + t.Run("Stop", func(t *testing.T) { |
| 32 | + w := Worker{} |
| 33 | + ctx := w.Context() |
| 34 | + require.NoError(t, ctx.Err()) |
| 35 | + |
| 36 | + _ = w.Close(context.Background(), nil) |
| 37 | + require.Error(t, ctx.Err()) |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +func TestWorkerStart(t *testing.T) { |
| 42 | + t.Run("Started", func(t *testing.T) { |
| 43 | + w := NewWorker(xtest.Context(t)) |
| 44 | + started := make(empty.Chan) |
| 45 | + w.Start("test", func(ctx context.Context) { |
| 46 | + close(started) |
| 47 | + }) |
| 48 | + xtest.WaitChannelClosed(t, started) |
| 49 | + }) |
| 50 | + t.Run("Stopped", func(t *testing.T) { |
| 51 | + ctx := xtest.Context(t) |
| 52 | + w := NewWorker(ctx) |
| 53 | + _ = w.Close(ctx, nil) |
| 54 | + |
| 55 | + started := make(empty.Chan) |
| 56 | + w.Start("test", func(ctx context.Context) { |
| 57 | + close(started) |
| 58 | + }) |
| 59 | + |
| 60 | + // expected: no close channel |
| 61 | + time.Sleep(time.Second / 100) |
| 62 | + select { |
| 63 | + case <-started: |
| 64 | + t.Fatal() |
| 65 | + default: |
| 66 | + // pass |
| 67 | + } |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func TestWorkerClose(t *testing.T) { |
| 72 | + t.Run("StopBackground", func(t *testing.T) { |
| 73 | + ctx := xtest.Context(t) |
| 74 | + w := NewWorker(ctx) |
| 75 | + |
| 76 | + started := make(empty.Chan) |
| 77 | + stopped := xatomic.Bool{} |
| 78 | + w.Start("test", func(innerCtx context.Context) { |
| 79 | + close(started) |
| 80 | + <-innerCtx.Done() |
| 81 | + stopped.Store(true) |
| 82 | + }) |
| 83 | + |
| 84 | + xtest.WaitChannelClosed(t, started) |
| 85 | + require.NoError(t, w.Close(ctx, nil)) |
| 86 | + require.True(t, stopped.Load()) |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("DoubleClose", func(t *testing.T) { |
| 90 | + ctx := xtest.Context(t) |
| 91 | + w := NewWorker(ctx) |
| 92 | + require.NoError(t, w.Close(ctx, nil)) |
| 93 | + require.Error(t, w.Close(ctx, nil)) |
| 94 | + }) |
| 95 | +} |
| 96 | + |
| 97 | +func TestWorkerConcurrentStartAndClose(t *testing.T) { |
| 98 | + targetClose := int64(10000) |
| 99 | + parallel := 10 |
| 100 | + |
| 101 | + var counter int64 |
| 102 | + |
| 103 | + ctx := xtest.Context(t) |
| 104 | + w := NewWorker(ctx) |
| 105 | + |
| 106 | + closeIndex := int64(0) |
| 107 | + closed := make(empty.Chan) |
| 108 | + go func() { |
| 109 | + xtest.SpinWaitCondition(t, nil, func() bool { |
| 110 | + return atomic.LoadInt64(&counter) > targetClose |
| 111 | + }) |
| 112 | + require.NoError(t, w.Close(ctx, nil)) |
| 113 | + closeIndex = atomic.LoadInt64(&counter) |
| 114 | + close(closed) |
| 115 | + }() |
| 116 | + |
| 117 | + stopNewStarts := xatomic.Bool{} |
| 118 | + for i := 0; i < parallel; i++ { |
| 119 | + go func() { |
| 120 | + for { |
| 121 | + if stopNewStarts.Load() { |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + go func() { |
| 126 | + w.Start("test", func(ctx context.Context) { |
| 127 | + atomic.AddInt64(&counter, 1) |
| 128 | + }) |
| 129 | + }() |
| 130 | + } |
| 131 | + }() |
| 132 | + } |
| 133 | + |
| 134 | + xtest.WaitChannelClosed(t, closed) |
| 135 | + runtime.Gosched() |
| 136 | + require.Equal(t, closeIndex, atomic.LoadInt64(&counter)) |
| 137 | + stopNewStarts.Store(true) |
| 138 | +} |
0 commit comments