Debian Codegraph @

exporter/exporterhelper/internal/queuebatch/multi_batcher_test.go

raw symbols SCIP
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 "go.uber.org/zap"
14
15 "go.opentelemetry.io/collector/component/componenttest"
16 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/request"
17 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/requesttest"
18 )
19
20 func TestMultiBatcher_NoTimeout(t *testing.T) {
21 cfg := BatchConfig{
22 FlushTimeout: 0,
23 Sizer: request.SizerTypeItems,
24 MinSize: 10,
25 }
26 sink := requesttest.NewSink()
27
28 type partitionKey struct{}
29
30 ba := newMultiBatcher(cfg,
31 request.NewItemsSizer(),
32 newWorkerPool(1),
33 NewPartitioner(func(ctx context.Context, _ request.Request) string {
34 return ctx.Value(partitionKey{}).(string)
35 }),
36 nil,
37 sink.Export,
38 zap.NewNop(),
39 )
40
41 require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost()))
42 t.Cleanup(func() {
43 require.NoError(t, ba.Shutdown(context.Background()))
44 })
45
46 done := newFakeDone()
47 ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done)
48 ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done)
49
50 // Neither batch should be flushed since they haven't reached min threshold.
51 assert.Equal(t, 0, sink.RequestsCount())
52 assert.Equal(t, 0, sink.ItemsCount())
53
54 ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done)
55
56 assert.Eventually(t, func() bool {
57 return sink.RequestsCount() == 1 && sink.ItemsCount() == 16
58 }, 500*time.Millisecond, 10*time.Millisecond)
59
60 ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done)
61
62 assert.Eventually(t, func() bool {
63 return sink.RequestsCount() == 2 && sink.ItemsCount() == 28
64 }, 500*time.Millisecond, 10*time.Millisecond)
65
66 // Check that done callback is called for the right amount of times.
67 assert.EqualValues(t, 0, done.errors.Load())
68 assert.EqualValues(t, 4, done.success.Load())
69
70 require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost()))
71 }
72
73 func TestMultiBatcher_Timeout(t *testing.T) {
74 cfg := BatchConfig{
75 FlushTimeout: 100 * time.Millisecond,
76 Sizer: request.SizerTypeItems,
77 MinSize: 100,
78 }
79 sink := requesttest.NewSink()
80
81 type partitionKey struct{}
82
83 ba := newMultiBatcher(cfg,
84 request.NewItemsSizer(),
85 newWorkerPool(1),
86 NewPartitioner(func(ctx context.Context, _ request.Request) string {
87 return ctx.Value(partitionKey{}).(string)
88 }),
89 nil,
90 sink.Export,
91 zap.NewNop(),
92 )
93
94 require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost()))
95 t.Cleanup(func() {
96 require.NoError(t, ba.Shutdown(context.Background()))
97 })
98
99 done := newFakeDone()
100 ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done)
101 ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done)
102
103 // Neither batch should be flushed since they haven't reached min threshold.
104 assert.Equal(t, 0, sink.RequestsCount())
105 assert.Equal(t, 0, sink.ItemsCount())
106
107 ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p1"), &requesttest.FakeRequest{Items: 8}, done)
108 ba.Consume(context.WithValue(context.Background(), partitionKey{}, "p2"), &requesttest.FakeRequest{Items: 6}, done)
109
110 assert.Eventually(t, func() bool {
111 return sink.RequestsCount() == 2 && sink.ItemsCount() == 28
112 }, 1*time.Second, 10*time.Millisecond)
113 // Check that done callback is called for the right amount of times.
114 assert.EqualValues(t, 0, done.errors.Load())
115 assert.EqualValues(t, 4, done.success.Load())
116
117 require.NoError(t, ba.Start(context.Background(), componenttest.NewNopHost()))
118 }