-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstream_test.go
256 lines (237 loc) · 5.8 KB
/
stream_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
package observer
import (
"context"
"fmt"
"testing"
"time"
)
func TestStreamInitialValue(t *testing.T) {
state := newState(10)
stream := &stream[int]{state: state}
if val := stream.Value(); val != 10 {
t.Fatalf("Expecting 10 but got %#v\n", val)
}
}
func TestStreamUpdate(t *testing.T) {
state1 := newState(10)
state2 := state1.update(15)
stream := &stream[int]{state: state1}
if val := stream.Value(); val != 10 {
t.Fatalf("Expecting 10 but got %#v\n", val)
}
state2.update(15)
if val := stream.Value(); val != 10 {
t.Fatalf("Expecting 10 but got %#v\n", val)
}
}
func TestStreamNextValue(t *testing.T) {
state1 := newState(10)
stream := &stream[int]{state: state1}
state2 := state1.update(15)
if val := stream.Next(); val != 15 {
t.Fatalf("Expecting 15 but got %#v\n", val)
}
state2.update(20)
if val := stream.Next(); val != 20 {
t.Fatalf("Expecting 20 but got %#v\n", val)
}
}
func TestStreamDetectsChanges(t *testing.T) {
state := newState(10)
stream := &stream[int]{state: state}
select {
case <-stream.Changes():
t.Fatalf("Expecting no changes\n")
default:
}
go func() {
time.Sleep(1 * time.Second)
state.update(15)
}()
select {
case <-stream.Changes():
case <-time.After(2 * time.Second):
t.Fatalf("Expecting changes\n")
}
select {
case <-stream.Changes():
default:
t.Fatalf("Expecting changes\n")
}
if val := stream.Next(); val != 15 {
t.Fatalf("Expecting 15 but got %#v\n", val)
}
select {
case <-stream.Changes():
t.Fatalf("Expecting no changes\n")
default:
}
}
func TestStreamHasChanges(t *testing.T) {
state := newState(10)
stream := &stream[int]{state: state}
if stream.HasNext() {
t.Fatalf("Expecting no changes\n")
}
state.update(15)
if !stream.HasNext() {
t.Fatalf("Expecting changes\n")
}
if val := stream.Next(); val != 15 {
t.Fatalf("Expecting 15 but got %#v\n", val)
}
}
func TestStreamWaitsNext(t *testing.T) {
state := newState(10)
stream := &stream[int]{state: state}
for i := 15; i <= 100; i++ {
state = state.update(i)
if val := stream.WaitNext(); val != i {
t.Fatalf("Expecting %#v but got %#v\n", i, val)
}
}
if stream.HasNext() {
t.Fatalf("Expecting no changes\n")
}
}
func TestStreamWaitNextBackgroundCtx(t *testing.T) {
state := newState(0)
stream := &stream[int]{state: state}
for i := 7; i <= 133; i++ {
state = state.update(i)
got, err := stream.WaitNextCtx(context.Background())
if err != nil {
t.Fatalf("Expecting no error\n")
}
if got != i {
t.Fatalf("Expecting %#v but got %#v\n", i, got)
}
}
if stream.HasNext() {
t.Fatalf("Expecting no changes\n")
}
}
func TestStreamWaitNextCanceledCtx(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
initialVal := 99
state := newState(initialVal)
stream := &stream[int]{state: state}
// cancel the context
cancel()
updateVal := initialVal + 17
state = state.update(updateVal)
for i := 0; i < 100; i++ {
// ensure the method returns an error when a canceled context is used and doesn't advance the stream
_, err := stream.WaitNextCtx(ctx)
if err == nil {
t.Fatalf("Expecting error but got none\n")
}
if stream.Value() != initialVal {
t.Fatalf("Expecting stream's current value to be %#v but it is %#v\n", initialVal, stream.Value())
}
}
// check that a call with a non-canceled context works
got, err := stream.WaitNextCtx(context.Background())
if err != nil {
t.Fatalf("Expecting no error\n")
}
if got != updateVal {
t.Fatalf("Expecting %#v but got %#v\n", updateVal, got)
}
if stream.HasNext() {
t.Fatalf("Expecting no changes\n")
}
}
func TestStreamClone(t *testing.T) {
state := newState(10)
stream1 := &stream[int]{state: state}
stream2 := stream1.Clone()
if stream2.HasNext() {
t.Fatalf("Expecting no changes\n")
}
if val := stream2.Value(); val != 10 {
t.Fatalf("Expecting 10 but got %#v\n", val)
}
state.update(15)
if !stream1.HasNext() {
t.Fatalf("Expecting changes\n")
}
if !stream2.HasNext() {
t.Fatalf("Expecting changes\n")
}
stream1.Next()
if val := stream1.Value(); val != 15 {
t.Fatalf("Expecting 15 but got %#v\n", val)
}
if val := stream2.Value(); val != 10 {
t.Fatalf("Expecting 10 but got %#v\n", val)
}
stream2.Next()
if val := stream2.Value(); val != 15 {
t.Fatalf("Expecting 15 but got %#v\n", val)
}
if stream1.HasNext() {
t.Fatalf("Expecting no changes\n")
}
if stream2.HasNext() {
t.Fatalf("Expecting no changes\n")
}
}
func TestStreamPeek(t *testing.T) {
state := newState(10)
stream := &stream[int]{state: state}
state = state.update(15)
if val := stream.Peek(); val != 15 {
t.Fatalf("Expecting 15 but got %#v\n", val)
}
state = state.update(20)
if val := stream.Peek(); val != 15 {
t.Fatalf("Expecting 15 but got %#v\n", val)
}
stream.Next()
if val := stream.Peek(); val != 20 {
t.Fatalf("Expecting 20 but got %#v\n", val)
}
}
func TestStreamConcurrencyWithClones(t *testing.T) {
initial := 1000
final := 2000
prop := NewProperty(initial)
stream := prop.Observe()
var cherrs []chan error
for i := 0; i < 1000; i++ {
cherr := make(chan error, 1)
cherrs = append(cherrs, cherr)
go testStreamRead(stream.Clone(), initial, final, cherr)
}
done := make(chan bool)
go func(prop Property[int], initial, final int, done chan bool) {
defer close(done)
for i := initial + 1; i <= final; i++ {
prop.Update(i)
}
}(prop, initial, final, done)
for _, cherr := range cherrs {
if err := <-cherr; err != nil {
t.Fatal(err)
}
}
<-done
}
func testStreamRead(s Stream[int], initial, final int, err chan error) {
val := s.Value()
if val != initial {
err <- fmt.Errorf("Expecting %#v but got %#v\n", initial, val)
return
}
for i := initial + 1; i <= final; i++ {
prevVal := val
val = s.WaitNext()
expected := prevVal + 1
if val != expected {
err <- fmt.Errorf("Expecting %#v but got %#v\n", expected, val)
return
}
}
close(err)
}