|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package queuebatch |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + |
| 14 | + "go.opentelemetry.io/collector/component/componenttest" |
| 15 | + "go.opentelemetry.io/collector/exporter/exporterhelper/internal/request" |
| 16 | + "go.opentelemetry.io/collector/exporter/exporterhelper/internal/requesttest" |
| 17 | +) |
| 18 | + |
| 19 | +func TestMultiBatcher_NoTimeout(t *testing.T) { |
| 20 | + cfg := BatchConfig{ |
| 21 | + FlushTimeout: 0, |
| 22 | + MinSize: 10, |
| 23 | + } |
| 24 | + sink := requesttest.NewSink() |
| 25 | + |
| 26 | + type partitionKey struct{} |
| 27 | + |
| 28 | + ba := newMultiBatcher(cfg, batcherSettings[request.Request]{ |
| 29 | + sizerType: request.SizerTypeItems, |
| 30 | + sizer: request.NewItemsSizer(), |
| 31 | + partitioner: NewPartitioner(func(ctx context.Context, _ request.Request) string { |
| 32 | + return ctx.Value(partitionKey{}).(string) |
| 33 | + }), |
| 34 | + next: sink.Export, |
| 35 | + maxWorkers: 1, |
| 36 | + }) |
| 37 | + |
| 38 | + require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost())) |
| 39 | + t.Cleanup(func() { |
| 40 | + require.NoError(t, ba.Shutdown(context.Background())) |
| 41 | + }) |
| 42 | + |
| 43 | + done := newFakeDone() |
| 44 | + ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done) |
| 45 | + ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done) |
| 46 | + |
| 47 | + // Neither batch should be flushed since they haven't reached min threshold. |
| 48 | + assert.Equal(t, 0, sink.RequestsCount()) |
| 49 | + assert.Equal(t, 0, sink.ItemsCount()) |
| 50 | + |
| 51 | + ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done) |
| 52 | + ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done) |
| 53 | + |
| 54 | + assert.Eventually(t, func() bool { |
| 55 | + return sink.RequestsCount() == 2 && sink.ItemsCount() == 28 |
| 56 | + }, 1*time.Second, 10*time.Millisecond) |
| 57 | + // Check that done callback is called for the right amount of times. |
| 58 | + assert.EqualValues(t, 0, done.errors.Load()) |
| 59 | + assert.EqualValues(t, 4, done.success.Load()) |
| 60 | + |
| 61 | + require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost())) |
| 62 | +} |
| 63 | + |
| 64 | +func TestMultiBatcher_Timeout(t *testing.T) { |
| 65 | + cfg := BatchConfig{ |
| 66 | + FlushTimeout: 100 * time.Millisecond, |
| 67 | + MinSize: 100, |
| 68 | + } |
| 69 | + sink := requesttest.NewSink() |
| 70 | + |
| 71 | + type partitionKey struct{} |
| 72 | + |
| 73 | + ba := newMultiBatcher(cfg, batcherSettings[request.Request]{ |
| 74 | + sizerType: request.SizerTypeItems, |
| 75 | + sizer: request.NewItemsSizer(), |
| 76 | + partitioner: NewPartitioner(func(ctx context.Context, _ request.Request) string { |
| 77 | + return ctx.Value(partitionKey{}).(string) |
| 78 | + }), |
| 79 | + next: sink.Export, |
| 80 | + maxWorkers: 1, |
| 81 | + }) |
| 82 | + |
| 83 | + require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost())) |
| 84 | + t.Cleanup(func() { |
| 85 | + require.NoError(t, ba.Shutdown(context.Background())) |
| 86 | + }) |
| 87 | + |
| 88 | + done := newFakeDone() |
| 89 | + ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done) |
| 90 | + ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done) |
| 91 | + |
| 92 | + // Neither batch should be flushed since they haven't reached min threshold. |
| 93 | + assert.Equal(t, 0, sink.RequestsCount()) |
| 94 | + assert.Equal(t, 0, sink.ItemsCount()) |
| 95 | + |
| 96 | + ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done) |
| 97 | + ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done) |
| 98 | + |
| 99 | + assert.Eventually(t, func() bool { |
| 100 | + return sink.RequestsCount() == 2 && sink.ItemsCount() == 28 |
| 101 | + }, 1*time.Second, 10*time.Millisecond) |
| 102 | + // Check that done callback is called for the right amount of times. |
| 103 | + assert.EqualValues(t, 0, done.errors.Load()) |
| 104 | + assert.EqualValues(t, 4, done.success.Load()) |
| 105 | + |
| 106 | + require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost())) |
| 107 | +} |
0 commit comments