forked from azazeal/singleflight
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingleflight_test.go
136 lines (99 loc) · 2.24 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
package singleflight
import (
"context"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const (
longPause = time.Second >> (1 + iota)
mediumPause
shortPause
)
func Test(t *testing.T) {
t.Parallel()
const key = "key"
var (
caller Caller[string, bool]
executions int64
)
fn := func(ctx context.Context) (bool, error) {
time.Sleep(shortPause)
_ = atomic.AddInt64(&executions, 1)
return caller.KeyFromContext(ctx) == key, assert.AnError
}
var wg sync.WaitGroup
doCall := func(r *bool, err *error) {
wg.Add(1)
go func() {
defer wg.Done()
*r, *err = caller.Call(context.Background(), key, fn)
}()
}
// start the first caller
var r1 bool
var err1 error
doCall(&r1, &err1)
// start the second caller
var r2 bool
var err2 error
doCall(&r2, &err2)
wg.Wait()
assert.True(t, r1)
assert.Same(t, assert.AnError, err1)
assert.True(t, r2)
assert.Same(t, assert.AnError, err2)
assert.Equal(t, int64(1), executions)
// ensure further executions once concurrent callers finish
r3, err3 := caller.Call(context.Background(), key+"1", fn)
assert.False(t, r3)
assert.Same(t, assert.AnError, err3)
assert.Equal(t, int64(2), executions)
}
func TestSecondaryContextCancellation(t *testing.T) {
t.Parallel()
fn := func(ctx context.Context) (bool, error) {
time.Sleep(longPause)
return true, nil
}
const key = "key"
var (
caller Caller[string, bool]
got1, got2, got3 bool
err1, err2, err3 error
wg sync.WaitGroup
)
wg.Add(1)
go func() {
defer wg.Done()
got1, err1 = caller.Call(context.Background(), key, fn)
}()
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(shortPause)
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(shortPause)
cancel()
}()
got2, err2 = caller.Call(ctx, key, fn)
}()
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(shortPause)
ctx, cancel := context.WithTimeout(context.Background(), mediumPause)
defer cancel()
got3, err3 = caller.Call(ctx, key, fn)
}()
wg.Wait()
assert.True(t, got1)
assert.NoError(t, err1)
assert.False(t, got2)
assert.ErrorIs(t, err2, context.Canceled)
assert.False(t, got3)
assert.ErrorIs(t, err3, context.DeadlineExceeded)
}