-
Notifications
You must be signed in to change notification settings - Fork 77
/
singleflight_test.go
294 lines (263 loc) · 7.6 KB
/
singleflight_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//go:build !race
//
// Copyright (C) 2019-2023 vdaas.org vald team <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Package singleflight represents zero time caching
package singleflight
import (
"context"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/test/goleak"
)
func TestNew(t *testing.T) {
type want struct {
want Group[any]
}
type test struct {
name string
want want
checkFunc func(want, Group[any]) error
beforeFunc func()
afterFunc func()
}
defaultCheckFunc := func(w want, got Group[any]) error {
if !reflect.DeepEqual(got, w.want) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", got, w.want)
}
return nil
}
tests := []test{
{
name: "returns Group implementation",
want: want{
want: &group[any]{},
},
},
}
for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(tt)
if test.beforeFunc != nil {
test.beforeFunc()
}
if test.afterFunc != nil {
defer test.afterFunc()
}
checkFunc := test.checkFunc
if test.checkFunc == nil {
checkFunc = defaultCheckFunc
}
got := New[any]()
if err := checkFunc(test.want, got); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}
func Test_group_Do(t *testing.T) {
type args[V any] struct {
ctx context.Context
key string
fn func() (V, error)
}
type want[V any] struct {
wantV V
wantShared bool
err error
}
type test[V any] struct {
name string
args args[V]
want want[V]
beforeFunc func(args[V])
execFunc func(*testing.T, args[V]) (V, bool, error)
checkFunc func(want[V], V, bool, error) error
afterFunc func(args[V])
}
tests := []test[string]{
func() test[string] {
// routine1
key1 := "req_1"
var cnt1 uint32
// the unparam lint rule is disabled here because we need to match the interface to singleflight implementation.
// if this rule is not disabled, if will warns that the error will always return null.
//nolint:unparam
fn1 := func() (string, error) {
atomic.AddUint32(&cnt1, 1)
return "res_1", nil
}
// routine 2
key2 := "req_2"
var cnt2 uint32
// the unparam lint rule is disabled here because we need to match the interface to singleflight implementation.
// if this rule is not disabled, if will warns that the error will always return null.
//nolint:unparam
fn2 := func() (string, error) {
atomic.AddUint32(&cnt2, 1)
return "res_2", nil
}
return test[string]{
name: "returns (v, false, nil) when Do is called with another key",
args: args[string]{
key: key1,
ctx: context.Background(),
fn: fn1,
},
want: want[string]{
wantV: "res_1",
wantShared: false,
err: nil,
},
execFunc: func(t *testing.T, a args[string]) (got string, gotShared bool, err error) {
t.Helper()
g := New[string]()
wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
got, gotShared, err = g.Do(a.ctx, a.key, a.fn)
wg.Done()
}()
wg.Add(1)
go func() {
_, _, _ = g.Do(a.ctx, key2, fn2)
wg.Done()
}()
wg.Wait()
return got, gotShared, err
},
checkFunc: func(w want[string], gotV string, gotShared bool, err error) error {
if got, want := int(atomic.LoadUint32(&cnt1)), 1; got != want {
return errors.Errorf("cnt got = %d, want = %d", got, want)
}
if got, want := int(atomic.LoadUint32(&cnt2)), 1; got != want {
return errors.Errorf("cnt got = %d, want = %d", got, want)
}
if !errors.Is(err, w.err) {
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
if !reflect.DeepEqual(gotV, w.wantV) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotV, w.wantV)
}
if !reflect.DeepEqual(gotShared, w.wantShared) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotShared, w.wantShared)
}
return nil
},
}
}(),
func() test[string] {
// routine1
var cnt1 uint32
// the unparam lint rule is disabled here because we need to match the interface to singleflight implementation.
// if this rule is not disabled, if will warns that the error will always return null.
//nolint:unparam
fn1 := func() (string, error) {
atomic.AddUint32(&cnt1, 1)
time.Sleep(time.Millisecond * 500)
return "res_1", nil
}
// routine 2
var cnt2 uint32
// the unparam lint rule is disabled here because we need to match the interface to singleflight implementation.
// if this rule is not disabled, if will warns that the error will always return null.
//nolint:unparam
fn2 := func() (string, error) {
atomic.AddUint32(&cnt2, 1)
return "res_2", nil
}
w := want[string]{
wantV: "res_1",
wantShared: true,
err: nil,
}
checkFunc := func(w want[string], gotV string, gotShared bool, err error) error {
c1 := int(atomic.LoadUint32(&cnt1))
c2 := int(atomic.LoadUint32(&cnt2))
// since there is a chance that the go routine 2 is executed before routine 1, we need to check if either one is executed
if !((c1 == 1 && c2 == 0) || (c1 == 0 && c2 == 1)) {
return errors.Errorf("cnt1 and cnt2 is executed, %d, %d", c1, c2)
}
if !errors.Is(err, w.err) {
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
if !reflect.DeepEqual(gotV, w.wantV) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotV, w.wantV)
}
if !reflect.DeepEqual(gotShared, w.wantShared) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotShared, w.wantShared)
}
return nil
}
return test[string]{
name: "returns (v, true, nil) when Do is called with the same key",
args: args[string]{
key: "req_1",
ctx: context.Background(),
fn: fn1,
},
want: w,
execFunc: func(t *testing.T, a args[string]) (string, bool, error) {
t.Helper()
g := New[string]()
wg := new(sync.WaitGroup)
var got, got1 string
var gotShared, gotShared1 bool
var err, err1 error
wg.Add(1)
go func() {
got, gotShared, err = g.Do(a.ctx, a.key, fn1)
wg.Done()
}()
// call with the same key but with another function
wg.Add(1)
time.Sleep(time.Millisecond * 100)
go func() {
got1, gotShared1, err1 = g.Do(a.ctx, a.key, fn2)
wg.Done()
}()
wg.Wait()
if err := checkFunc(w, got1, gotShared1, err1); err != nil {
t.Fatal(err)
}
return got, gotShared, err
},
checkFunc: checkFunc,
}
}(),
}
for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
defer goleak.VerifyNone(tt)
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
gotV, gotShared, err := test.execFunc(t, test.args)
if err := test.checkFunc(test.want, gotV, gotShared, err); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}
// NOT IMPLEMENTED BELOW