-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifier_test.go
195 lines (161 loc) · 3.63 KB
/
notifier_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
package collections_test
import (
"context"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/arg0net/collections"
)
func TestNotifier(t *testing.T) {
sn := collections.NewStatefulNotifier(0)
sn.Store(1)
sn.Store(2)
sn.Store(2)
sn.Store(3)
v, ch := sn.Load()
require.Equal(t, 3, v)
require.NotNil(t, ch)
sn.Store(4)
<-ch
v, _ = sn.Load()
require.Equal(t, 4, v)
}
func TestNotifierUpdate(t *testing.T) {
sn := collections.NewStatefulNotifier(0)
start := make(chan struct{})
incr := func(in int) int {
return in + 1
}
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
select {
case <-start:
}
sn.Update(incr)
}()
}
close(start)
wg.Wait()
v, _ := sn.Load()
require.Equal(t, 10, v)
}
func TestNotifierWait(t *testing.T) {
ctx := context.Background()
done := make(chan int, 1)
sn := collections.NewStatefulNotifier(0)
go func() {
v, _ := sn.Wait(ctx, func(v int) bool {
return v == 3
})
done <- v
}()
// give time for wait to start.
time.Sleep(10 * time.Millisecond)
sn.Store(1)
require.Empty(t, done)
sn.Store(2)
require.Empty(t, done)
sn.Store(3)
v := <-done
require.Equal(t, 3, v)
}
func TestWaitCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
result := make(chan error, 1)
sn := collections.NewStatefulNotifier(0)
go func() {
_, err := sn.Wait(ctx, func(v int) bool {
return v == 42
})
result <- err
}()
// give time for wait to start.
time.Sleep(10 * time.Millisecond)
cancel()
err := <-result
require.ErrorIs(t, err, context.Canceled)
}
func TestWatch(t *testing.T) {
sn := collections.NewStatefulNotifier(0)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var lastValue atomic.Int32
var done atomic.Bool
recv := sn.Watch(ctx)
go func() {
for v := range recv {
lastValue.Store(int32(v))
}
done.Store(true)
}()
sn.Store(42)
require.Eventually(t, func() bool {
return lastValue.Load() == 42
}, 2*time.Second, 10*time.Millisecond)
sn.Store(999)
require.Eventually(t, func() bool {
return lastValue.Load() == 999
}, 2*time.Second, 10*time.Millisecond)
cancel()
require.Eventually(t, func() bool {
return done.Load()
}, 2*time.Second, 10*time.Millisecond)
}
func TestNotifierWaitAny(t *testing.T) {
ctx := context.Background()
done := make(chan int, 1)
sn := make([]*collections.StatefulNotifier[int], 5)
for i := range sn {
sn[i] = collections.NewStatefulNotifier(0)
}
go func() {
_, idx := collections.WaitAny(ctx, func(v int) bool {
return v == 42
}, sn...)
done <- idx
}()
// give time for wait to start.
time.Sleep(10 * time.Millisecond)
expected := rand.Intn(4)
sn[0].Store(0)
sn[expected].Store(42)
got := <-done
require.Equal(t, expected, got)
}
func TestNotifierWaitAnyCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
result := make(chan int, 1)
sn := make([]*collections.StatefulNotifier[int], 5)
for i := range sn {
sn[i] = collections.NewStatefulNotifier(0)
}
go func() {
_, idx := collections.WaitAny(ctx, func(v int) bool {
return v == 42
}, sn...)
result <- idx
}()
// give time for wait to start.
time.Sleep(10 * time.Millisecond)
cancel()
idx := <-result
require.Equal(t, -1, idx)
}
func TestNotifierWaitAnyImmediate(t *testing.T) {
ctx := context.Background()
sn := make([]*collections.StatefulNotifier[int], 5)
for i := range sn {
sn[i] = collections.NewStatefulNotifier(i)
}
got, idx := collections.WaitAny(ctx, func(v int) bool {
return v == 1
}, sn...)
require.Equal(t, 1, idx)
require.Equal(t, 1, got)
}