Debian Codegraph @

exporter/exporterhelper/internal/queuebatch/config_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 "path/filepath"
8 "testing"
9 "time"
10
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/require"
13
14 "go.opentelemetry.io/collector/component"
15 "go.opentelemetry.io/collector/config/configoptional"
16 "go.opentelemetry.io/collector/confmap/confmaptest"
17 "go.opentelemetry.io/collector/confmap/xconfmap"
18 "go.opentelemetry.io/collector/exporter/exporterhelper/internal/request"
19 )
20
21 func TestConfig_Validate(t *testing.T) {
22 cfg := newTestConfig()
23 require.NoError(t, xconfmap.Validate(cfg))
24
25 cfg.NumConsumers = 0
26 require.EqualError(t, xconfmap.Validate(cfg), "`num_consumers` must be positive")
27
28 cfg = newTestConfig()
29 cfg.QueueSize = 0
30 require.EqualError(t, xconfmap.Validate(cfg), "`queue_size` must be positive")
31
32 cfg = newTestConfig()
33 cfg.QueueSize = 0
34 require.EqualError(t, xconfmap.Validate(cfg), "`queue_size` must be positive")
35
36 storageID := component.MustNewID("test")
37 cfg = newTestConfig()
38 cfg.WaitForResult = true
39 cfg.StorageID = &storageID
40 require.EqualError(t, xconfmap.Validate(cfg), "`wait_for_result` is not supported with a persistent queue configured with `storage`")
41
42 cfg = newTestConfig()
43 cfg.QueueSize = cfg.Batch.Get().MinSize - 1
44 require.EqualError(t, xconfmap.Validate(cfg), "`min_size` must be less than or equal to `queue_size`")
45
46 cfg = newTestConfig()
47 cfg.Batch.Get().Sizer = request.SizerType{}
48 require.EqualError(t, xconfmap.Validate(cfg), "batch: `batch` supports only `items` or `bytes` sizer, found \"\"")
49
50 cfg = newTestConfig()
51 cfg.Sizer = request.SizerTypeBytes
52 require.NoError(t, xconfmap.Validate(cfg))
53
54 // Confirm Validate doesn't return error with invalid config when feature is disabled
55 cfg.Enabled = false
56 assert.NoError(t, xconfmap.Validate(cfg))
57 }
58
59 func TestBatchConfig_Validate(t *testing.T) {
60 cfg := newTestBatchConfig()
61 require.NoError(t, xconfmap.Validate(cfg))
62
63 cfg = newTestBatchConfig()
64 cfg.FlushTimeout = 0
65 require.EqualError(t, xconfmap.Validate(cfg), "`flush_timeout` must be positive, found 0")
66
67 cfg = newTestBatchConfig()
68 cfg.MinSize = -1
69 require.EqualError(t, xconfmap.Validate(cfg), "`min_size` must be non-negative, found -1")
70
71 cfg = newTestBatchConfig()
72 cfg.MaxSize = -1
73 require.EqualError(t, xconfmap.Validate(cfg), "`max_size` must be non-negative, found -1")
74
75 cfg = newTestBatchConfig()
76 cfg.Sizer = request.SizerTypeRequests
77 require.EqualError(t, xconfmap.Validate(cfg), "`batch` supports only `items` or `bytes` sizer, found \"requests\"")
78
79 cfg = newTestBatchConfig()
80 cfg.Sizer = request.SizerType{}
81 require.EqualError(t, xconfmap.Validate(cfg), "`batch` supports only `items` or `bytes` sizer, found \"\"")
82
83 cfg = newTestBatchConfig()
84 cfg.MinSize = 2048
85 cfg.MaxSize = 1024
86 require.EqualError(t, xconfmap.Validate(cfg), "`max_size` (1024) must be greater or equal to `min_size` (2048)")
87 }
88
89 func newTestBatchConfig() BatchConfig {
90 return BatchConfig{
91 FlushTimeout: 200 * time.Millisecond,
92 Sizer: request.SizerTypeItems,
93 MinSize: 2048,
94 MaxSize: 0,
95 }
96 }
97
98 func TestUnmarshal(t *testing.T) {
99 newBaseCfg := func() Config {
100 return Config{
101 Enabled: true,
102 Sizer: request.SizerTypeRequests,
103 NumConsumers: 10,
104 QueueSize: 1_000,
105 Batch: configoptional.Default(BatchConfig{
106 FlushTimeout: 200 * time.Millisecond,
107 Sizer: request.SizerTypeItems,
108 MinSize: 8192,
109 }),
110 }
111 }
112 tests := []struct {
113 path string
114 expectedErr string
115 expectedCfg func() Config
116 }{
117 {
118 path: "batch_set_empty_explicit_sizer.yaml",
119 expectedCfg: func() Config {
120 cfg := newBaseCfg()
121 cfg.Sizer = request.SizerTypeBytes
122 // Batch is set, sizer is not overridden
123 cfg.Batch.GetOrInsertDefault()
124 return cfg
125 },
126 },
127 {
128 path: "batch_set_empty_no_explicit_sizer.yaml",
129 expectedCfg: func() Config {
130 cfg := newBaseCfg()
131 cfg.Batch.GetOrInsertDefault()
132 return cfg
133 },
134 },
135 {
136 path: "batch_set_nonempty_explicit_sizer.yaml",
137 expectedCfg: func() Config {
138 cfg := newBaseCfg()
139 cfg.Sizer = request.SizerTypeBytes
140 cfg.QueueSize = 2000
141 cfg.Batch = configoptional.Some(BatchConfig{
142 FlushTimeout: 200 * time.Millisecond,
143 // Sizer has been overridden by parent sizer
144 Sizer: request.SizerTypeBytes,
145 MinSize: 100,
146 })
147 return cfg
148 },
149 },
150 {
151 path: "batch_set_nonempty_no_explicit_sizer.yaml",
152 expectedCfg: func() Config {
153 cfg := newBaseCfg()
154 cfg.QueueSize = 2000
155 cfg.Batch = configoptional.Some(BatchConfig{
156 FlushTimeout: 200 * time.Millisecond,
157 // Sizer has NOT been overridden by parent sizer
158 Sizer: request.SizerTypeItems,
159 MinSize: 100,
160 })
161 return cfg
162 },
163 },
164 {
165 path: "batch_unset.yaml",
166 // Batch remains unset, sizer override does not apply.
167 expectedCfg: newBaseCfg,
168 },
169 }
170
171 for _, tt := range tests {
172 t.Run(tt.path, func(t *testing.T) {
173 cm, err := confmaptest.LoadConf(filepath.Join("testdata", tt.path))
174 require.NoError(t, err)
175
176 cfg := newBaseCfg()
177 err = cfg.Unmarshal(cm)
178 if tt.expectedErr != "" {
179 assert.ErrorContains(t, err, tt.expectedErr)
180 return
181 }
182 require.NoError(t, err)
183 assert.Equal(t, tt.expectedCfg(), cfg)
184 assert.NoError(t, xconfmap.Validate(cfg))
185 })
186 }
187 }