1package config
2
3import (
4	"fmt"
5	"reflect"
6	"testing"
7	"time"
8)
9
10func TestDedupConfig_Copy(t *testing.T) {
11	t.Parallel()
12
13	cases := []struct {
14		name string
15		a    *DedupConfig
16	}{
17		{
18			"nil",
19			nil,
20		},
21		{
22			"empty",
23			&DedupConfig{},
24		},
25		{
26			"copy",
27			&DedupConfig{
28				Enabled:  Bool(true),
29				MaxStale: TimeDuration(30 * time.Second),
30				Prefix:   String("prefix"),
31				TTL:      TimeDuration(10 * time.Second),
32			},
33		},
34	}
35
36	for i, tc := range cases {
37		t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) {
38			r := tc.a.Copy()
39			if !reflect.DeepEqual(tc.a, r) {
40				t.Errorf("\nexp: %#v\nact: %#v", tc.a, r)
41			}
42		})
43	}
44}
45
46func TestDedupConfig_Merge(t *testing.T) {
47	t.Parallel()
48
49	cases := []struct {
50		name string
51		a    *DedupConfig
52		b    *DedupConfig
53		r    *DedupConfig
54	}{
55		{
56			"nil_a",
57			nil,
58			&DedupConfig{},
59			&DedupConfig{},
60		},
61		{
62			"nil_b",
63			&DedupConfig{},
64			nil,
65			&DedupConfig{},
66		},
67		{
68			"nil_both",
69			nil,
70			nil,
71			nil,
72		},
73		{
74			"empty",
75			&DedupConfig{},
76			&DedupConfig{},
77			&DedupConfig{},
78		},
79		{
80			"enabled_overrides",
81			&DedupConfig{Enabled: Bool(true)},
82			&DedupConfig{Enabled: Bool(false)},
83			&DedupConfig{Enabled: Bool(false)},
84		},
85		{
86			"enabled_empty_one",
87			&DedupConfig{Enabled: Bool(true)},
88			&DedupConfig{},
89			&DedupConfig{Enabled: Bool(true)},
90		},
91		{
92			"enabled_empty_two",
93			&DedupConfig{},
94			&DedupConfig{Enabled: Bool(true)},
95			&DedupConfig{Enabled: Bool(true)},
96		},
97		{
98			"enabled_same",
99			&DedupConfig{Enabled: Bool(true)},
100			&DedupConfig{Enabled: Bool(true)},
101			&DedupConfig{Enabled: Bool(true)},
102		},
103		{
104			"max_stale_overrides",
105			&DedupConfig{MaxStale: TimeDuration(10 * time.Second)},
106			&DedupConfig{MaxStale: TimeDuration(20 * time.Second)},
107			&DedupConfig{MaxStale: TimeDuration(20 * time.Second)},
108		},
109		{
110			"max_stale_empty_one",
111			&DedupConfig{MaxStale: TimeDuration(10 * time.Second)},
112			&DedupConfig{},
113			&DedupConfig{MaxStale: TimeDuration(10 * time.Second)},
114		},
115		{
116			"max_stale_empty_two",
117			&DedupConfig{},
118			&DedupConfig{MaxStale: TimeDuration(10 * time.Second)},
119			&DedupConfig{MaxStale: TimeDuration(10 * time.Second)},
120		},
121		{
122			"max_stale_same",
123			&DedupConfig{MaxStale: TimeDuration(10 * time.Second)},
124			&DedupConfig{MaxStale: TimeDuration(10 * time.Second)},
125			&DedupConfig{MaxStale: TimeDuration(10 * time.Second)},
126		},
127		{
128			"prefix_overrides",
129			&DedupConfig{Prefix: String("prefix")},
130			&DedupConfig{Prefix: String("")},
131			&DedupConfig{Prefix: String("")},
132		},
133		{
134			"prefix_empty_one",
135			&DedupConfig{Prefix: String("prefix")},
136			&DedupConfig{},
137			&DedupConfig{Prefix: String("prefix")},
138		},
139		{
140			"prefix_empty_two",
141			&DedupConfig{},
142			&DedupConfig{Prefix: String("prefix")},
143			&DedupConfig{Prefix: String("prefix")},
144		},
145		{
146			"prefix_same",
147			&DedupConfig{Prefix: String("prefix")},
148			&DedupConfig{Prefix: String("prefix")},
149			&DedupConfig{Prefix: String("prefix")},
150		},
151		{
152			"ttl_overrides",
153			&DedupConfig{TTL: TimeDuration(10 * time.Second)},
154			&DedupConfig{TTL: TimeDuration(0 * time.Second)},
155			&DedupConfig{TTL: TimeDuration(0 * time.Second)},
156		},
157		{
158			"ttl_empty_one",
159			&DedupConfig{TTL: TimeDuration(10 * time.Second)},
160			&DedupConfig{},
161			&DedupConfig{TTL: TimeDuration(10 * time.Second)},
162		},
163		{
164			"ttl_empty_two",
165			&DedupConfig{},
166			&DedupConfig{TTL: TimeDuration(10 * time.Second)},
167			&DedupConfig{TTL: TimeDuration(10 * time.Second)},
168		},
169		{
170			"ttl_same",
171			&DedupConfig{TTL: TimeDuration(10 * time.Second)},
172			&DedupConfig{TTL: TimeDuration(10 * time.Second)},
173			&DedupConfig{TTL: TimeDuration(10 * time.Second)},
174		},
175	}
176
177	for i, tc := range cases {
178		t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) {
179			r := tc.a.Merge(tc.b)
180			if !reflect.DeepEqual(tc.r, r) {
181				t.Errorf("\nexp: %#v\nact: %#v", tc.r, r)
182			}
183		})
184	}
185}
186
187func TestDedupConfig_Finalize(t *testing.T) {
188	t.Parallel()
189
190	cases := []struct {
191		name string
192		i    *DedupConfig
193		r    *DedupConfig
194	}{
195		{
196			"empty",
197			&DedupConfig{},
198			&DedupConfig{
199				Enabled:            Bool(false),
200				MaxStale:           TimeDuration(DefaultDedupMaxStale),
201				Prefix:             String(DefaultDedupPrefix),
202				TTL:                TimeDuration(DefaultDedupTTL),
203				BlockQueryWaitTime: TimeDuration(DefaultDedupBlockQueryWaitTime),
204			},
205		},
206		{
207			"with_max_stale",
208			&DedupConfig{
209				MaxStale: TimeDuration(10 * time.Second),
210			},
211			&DedupConfig{
212				Enabled:            Bool(true),
213				MaxStale:           TimeDuration(10 * time.Second),
214				Prefix:             String(DefaultDedupPrefix),
215				TTL:                TimeDuration(DefaultDedupTTL),
216				BlockQueryWaitTime: TimeDuration(DefaultDedupBlockQueryWaitTime),
217			},
218		},
219		{
220			"with_prefix",
221			&DedupConfig{
222				Prefix: String("prefix"),
223			},
224			&DedupConfig{
225				Enabled:            Bool(true),
226				MaxStale:           TimeDuration(DefaultDedupMaxStale),
227				Prefix:             String("prefix"),
228				TTL:                TimeDuration(DefaultDedupTTL),
229				BlockQueryWaitTime: TimeDuration(DefaultDedupBlockQueryWaitTime),
230			},
231		},
232		{
233			"with_ttl",
234			&DedupConfig{
235				TTL: TimeDuration(10 * time.Second),
236			},
237			&DedupConfig{
238				Enabled:            Bool(true),
239				MaxStale:           TimeDuration(DefaultDedupMaxStale),
240				Prefix:             String(DefaultDedupPrefix),
241				TTL:                TimeDuration(10 * time.Second),
242				BlockQueryWaitTime: TimeDuration(DefaultDedupBlockQueryWaitTime),
243			},
244		},
245		{
246			"with_block_query_wait",
247			&DedupConfig{
248				BlockQueryWaitTime: TimeDuration(60 * time.Second),
249			},
250			&DedupConfig{
251				Enabled:            Bool(true),
252				MaxStale:           TimeDuration(DefaultDedupMaxStale),
253				Prefix:             String(DefaultDedupPrefix),
254				TTL:                TimeDuration(DefaultDedupTTL),
255				BlockQueryWaitTime: TimeDuration(60 * time.Second),
256			},
257		},
258	}
259
260	for i, tc := range cases {
261		t.Run(fmt.Sprintf("%d_%s", i, tc.name), func(t *testing.T) {
262			tc.i.Finalize()
263			if !reflect.DeepEqual(tc.r, tc.i) {
264				t.Errorf("\nexp: %#v\nact: %#v", tc.r, tc.i)
265			}
266		})
267	}
268}
269