|
| 1 | +package batcher |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "sync" |
| 7 | + "sync/atomic" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/launchdarkly/go-sdk-common/v3/ldvalue" |
| 12 | + "github.com/launchdarkly/go-server-sdk/v7/testhelpers/ldtestdata" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/e2b-dev/infra/packages/shared/pkg/featureflags" |
| 16 | +) |
| 17 | + |
| 18 | +// blockFirstFlush returns a BatcherFunc that, on its first call only, signals |
| 19 | +// entered and then blocks until release is closed; every later flush returns |
| 20 | +// immediately. This lets a test pin the processor on a full queue without the |
| 21 | +// Func re-blocking on subsequent batches. |
| 22 | +func blockFirstFlush[T any](entered chan<- struct{}, release <-chan struct{}) func(context.Context, []T) error { |
| 23 | + var once sync.Once |
| 24 | + |
| 25 | + return func(context.Context, []T) error { |
| 26 | + once.Do(func() { |
| 27 | + close(entered) |
| 28 | + <-release |
| 29 | + }) |
| 30 | + |
| 31 | + return nil |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// PushWithContext blocks while the queue is full and succeeds once the |
| 36 | +// processor drains a slot, instead of dropping like the non-blocking Push. |
| 37 | +func TestBatcherPushWithContextBlocksThenSucceeds(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + release := make(chan struct{}) |
| 40 | + entered := make(chan struct{}) |
| 41 | + b, err := NewBatcher(blockFirstFlush[int](entered, release), |
| 42 | + BatcherOptions{MaxDelay: time.Hour, MaxBatchSize: 1, QueueSize: 1}) |
| 43 | + require.NoError(t, err) |
| 44 | + require.NoError(t, b.Start(t.Context())) |
| 45 | + defer b.Stop() |
| 46 | + |
| 47 | + // First item is picked up by the processor and blocks it in Func. |
| 48 | + require.NoError(t, b.Push(1)) |
| 49 | + <-entered |
| 50 | + // Queue (cap 1) now fills. |
| 51 | + require.NoError(t, b.Push(2)) |
| 52 | + require.ErrorIs(t, b.Push(3), ErrBatcherQueueFull, "non-blocking Push drops on a full queue") |
| 53 | + |
| 54 | + // A bounded-blocking push on the full queue succeeds once Func returns and |
| 55 | + // the processor pulls the queued item, freeing a slot. |
| 56 | + done := make(chan error, 1) |
| 57 | + go func() { done <- b.PushWithContext(t.Context(), 4) }() |
| 58 | + time.Sleep(20 * time.Millisecond) |
| 59 | + select { |
| 60 | + case err := <-done: |
| 61 | + t.Fatalf("PushWithContext returned early (%v); it should have blocked on the full queue", err) |
| 62 | + default: |
| 63 | + } |
| 64 | + close(release) |
| 65 | + require.NoError(t, <-done, "PushWithContext should succeed after a slot frees up") |
| 66 | +} |
| 67 | + |
| 68 | +// PushWithContext drops (and does not block forever) when its context expires |
| 69 | +// while the queue stays full. |
| 70 | +func TestBatcherPushWithContextTimesOut(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + release := make(chan struct{}) |
| 73 | + entered := make(chan struct{}) |
| 74 | + b, err := NewBatcher(blockFirstFlush[int](entered, release), |
| 75 | + BatcherOptions{MaxDelay: time.Hour, MaxBatchSize: 1, QueueSize: 1}) |
| 76 | + require.NoError(t, err) |
| 77 | + require.NoError(t, b.Start(t.Context())) |
| 78 | + defer func() { close(release); b.Stop() }() |
| 79 | + |
| 80 | + require.NoError(t, b.Push(1)) |
| 81 | + <-entered |
| 82 | + require.NoError(t, b.Push(2)) // queue full |
| 83 | + |
| 84 | + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Millisecond) |
| 85 | + defer cancel() |
| 86 | + require.ErrorIs(t, b.PushWithContext(ctx, 3), ErrBatcherQueueFull, |
| 87 | + "a terminal push whose deadline elapses on a stuck queue is dropped, not blocked forever") |
| 88 | +} |
| 89 | + |
| 90 | +func TestBatcherPushWithContextNotStarted(t *testing.T) { |
| 91 | + t.Parallel() |
| 92 | + b, err := NewBatcher[int](func(context.Context, []int) error { return nil }, BatcherOptions{}) |
| 93 | + require.NoError(t, err) |
| 94 | + require.ErrorIs(t, b.PushWithContext(t.Context(), 1), ErrBatcherNotStarted) |
| 95 | +} |
| 96 | + |
| 97 | +// A transient flush failure is retried (per the flag) and the batch is |
| 98 | +// delivered rather than dropped on the first error. |
| 99 | +func TestBatcherFlushRetriesThenSucceeds(t *testing.T) { |
| 100 | + t.Parallel() |
| 101 | + source := ldtestdata.DataSource() |
| 102 | + source.Update(source.Flag(featureflags.ClickhouseBatcherFlushMaxRetries.Key()).ValueForAll(ldvalue.Int(3))) |
| 103 | + source.Update(source.Flag(featureflags.ClickhouseBatcherMaxBatchSize.Key()).ValueForAll(ldvalue.Int(1))) |
| 104 | + ff, err := featureflags.NewClientWithDatasource(source) |
| 105 | + require.NoError(t, err) |
| 106 | + defer ff.Close(context.WithoutCancel(t.Context())) |
| 107 | + |
| 108 | + var calls atomic.Int32 |
| 109 | + delivered := make(chan int, 1) |
| 110 | + b, err := NewBatcher(func(_ context.Context, batch []int) error { |
| 111 | + if calls.Add(1) < 3 { |
| 112 | + return errors.New("transient clickhouse error") |
| 113 | + } |
| 114 | + delivered <- len(batch) |
| 115 | + |
| 116 | + return nil |
| 117 | + }, BatcherOptions{Name: "retry-test", FeatureFlags: ff, MaxDelay: time.Hour, MaxBatchSize: 1, QueueSize: 4}) |
| 118 | + require.NoError(t, err) |
| 119 | + require.NoError(t, b.Start(t.Context())) |
| 120 | + defer b.Stop() |
| 121 | + |
| 122 | + require.NoError(t, b.Push(1)) |
| 123 | + select { |
| 124 | + case n := <-delivered: |
| 125 | + require.Equal(t, 1, n) |
| 126 | + case <-time.After(5 * time.Second): |
| 127 | + t.Fatal("batch was not delivered within timeout; retry did not recover the transient failure") |
| 128 | + } |
| 129 | + require.GreaterOrEqual(t, calls.Load(), int32(3), "expected the flush to be retried until success") |
| 130 | +} |
| 131 | + |
| 132 | +// With retries disabled (no flag client), a flush failure drops the batch after |
| 133 | +// a single attempt — the legacy behavior is preserved. |
| 134 | +func TestBatcherFlushNoRetryByDefault(t *testing.T) { |
| 135 | + t.Parallel() |
| 136 | + var calls atomic.Int32 |
| 137 | + handled := make(chan error, 1) |
| 138 | + b, err := NewBatcher(func(_ context.Context, _ []int) error { |
| 139 | + calls.Add(1) |
| 140 | + |
| 141 | + return errors.New("boom") |
| 142 | + }, BatcherOptions{ |
| 143 | + MaxDelay: time.Hour, MaxBatchSize: 1, QueueSize: 4, |
| 144 | + ErrorHandler: func(err error) { handled <- err }, |
| 145 | + }) |
| 146 | + require.NoError(t, err) |
| 147 | + require.NoError(t, b.Start(t.Context())) |
| 148 | + defer b.Stop() |
| 149 | + |
| 150 | + require.NoError(t, b.Push(1)) |
| 151 | + select { |
| 152 | + case <-handled: |
| 153 | + case <-time.After(5 * time.Second): |
| 154 | + t.Fatal("ErrorHandler was not called") |
| 155 | + } |
| 156 | + time.Sleep(50 * time.Millisecond) |
| 157 | + require.Equal(t, int32(1), calls.Load(), "static batcher must not retry (legacy drop-on-first-error)") |
| 158 | +} |
0 commit comments