-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathconfig_test.go
183 lines (160 loc) · 5.86 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package httpclient
import (
"bytes"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/acronis/go-appkit/config"
"github.com/acronis/go-appkit/retry"
)
func TestConfigWithLoader(t *testing.T) {
yamlData := testYamlData(nil)
actualConfig := &Config{}
err := config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.NoError(t, err, "load configuration")
expectedConfig := &Config{
Retries: RetriesConfig{
Enabled: true,
MaxAttempts: 30,
Policy: RetryPolicyExponential,
ExponentialBackoff: ExponentialBackoffConfig{
InitialInterval: 3 * time.Second,
Multiplier: 2,
},
},
RateLimits: RateLimitsConfig{
Enabled: true,
Limit: 300,
Burst: 3000,
WaitTimeout: 3 * time.Second,
},
Log: LogConfig{
Enabled: true,
SlowRequestThreshold: 5 * time.Second,
Mode: "all",
},
Metrics: MetricsConfig{Enabled: true},
Timeout: 30 * time.Second,
}
require.Equal(t, expectedConfig, actualConfig, "configuration does not match expected")
}
func TestConfigRateLimit(t *testing.T) {
yamlData := testYamlData([][]string{{"limit: 300", "limit: -300"}})
actualConfig := &Config{}
err := config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "rateLimits.limit: must be positive", err.Error())
yamlData = testYamlData([][]string{{"burst: 3000", "burst: -3"}})
actualConfig = &Config{}
err = config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "rateLimits.burst: must be positive", err.Error())
yamlData = testYamlData([][]string{{"waitTimeout: 3s", "waitTimeout: -3s"}})
actualConfig = &Config{}
err = config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "rateLimits.waitTimeout: must be positive", err.Error())
}
func TestConfigRetries(t *testing.T) {
yamlData := testYamlData([][]string{{"maxAttempts: 30", "maxAttempts: -30"}})
actualConfig := &Config{}
err := config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "retries.maxAttempts: must be positive", err.Error())
}
func TestConfigLogger(t *testing.T) {
yamlData := testYamlData([][]string{{"slowRequestThreshold: 5s", "slowRequestThreshold: -5s"}})
actualConfig := &Config{}
err := config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "log.slowRequestThreshold: can not be negative", err.Error())
yamlData = testYamlData([][]string{{"mode: all", "mode: invalid"}})
actualConfig = &Config{}
err = config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "log.mode: choose one of: [all, failed]", err.Error())
}
func TestConfigRetriesPolicy(t *testing.T) {
yamlData := testYamlData([][]string{{"policy: exponential", "policy: invalid"}})
actualConfig := &Config{}
err := config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "retries.policy: must be one of: [exponential, constant]", err.Error())
yamlData = testYamlData([][]string{
{"initialInterval: 3s", "initialInterval: -1s"},
})
err = config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "retries.exponentialBackoff.initialInterval: must be positive", err.Error())
yamlData = testYamlData([][]string{{"multiplier: 2", "multiplier: 1"}})
err = config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "retries.exponentialBackoff.multiplier: must be greater than 1", err.Error())
yamlData = testYamlData([][]string{
{"policy: exponential", "policy: constant"},
{"interval: 2s", "interval: -3s"},
})
err = config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.Error(t, err)
require.Equal(t, "retries.constantBackoff.interval: must be positive", err.Error())
yamlData = testYamlData([][]string{
{"policy: exponential", "policy:"},
})
err = config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.NoError(t, err)
require.Nil(t, actualConfig.Retries.GetPolicy())
yamlData = testYamlData(nil)
err = config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.NoError(t, err)
require.Implements(t, (*retry.Policy)(nil), actualConfig.Retries.GetPolicy())
}
func TestConfigDisableWithLoader(t *testing.T) {
yamlData := []byte(`
retries:
enabled: false
rateLimits:
enabled: false
logger:
enabled: false
metrics:
enabled: false
timeout: 30s
`)
actualConfig := &Config{}
err := config.NewDefaultLoader("").LoadFromReader(bytes.NewReader(yamlData), config.DataTypeYAML, actualConfig)
require.NoError(t, err)
}
func testYamlData(replacements [][]string) []byte {
yamlData := `
retries:
enabled: true
maxAttempts: 30
policy: exponential
exponentialBackoff:
initialInterval: 3s
multiplier: 2
constantBackoff:
interval: 2s
rateLimits:
enabled: true
limit: 300
burst: 3000
waitTimeout: 3s
log:
enabled: true
slowRequestThreshold: 5s
mode: all
metrics:
enabled: true
timeout: 30s
`
for i := range replacements {
yamlData = strings.Replace(yamlData, replacements[i][0], replacements[i][1], 1)
}
return []byte(yamlData)
}