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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
package tui
import (
"sync"
"testing"
"time"
)
func drainEvents(eventQueue <-chan func(), timeout time.Duration) {
deadline := time.After(timeout)
for {
select {
case fn := <-eventQueue:
fn()
case <-deadline:
return
}
}
}
func TestWatch_CreatesWatcher(t *testing.T) {
ch := make(chan string)
handler := func(s string) {}
watcher := Watch(ch, handler)
if watcher == nil {
t.Error("Watch() should return a non-nil Watcher")
}
}
func TestWatch_ReceivesChannelValues(t *testing.T) {
ch := make(chan string, 10)
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
defer close(stopCh)
var received []string
handler := func(s string) {
received = append(received, s)
}
watcher := Watch(ch, handler)
watcher.Start(eventQueue, stopCh)
ch <- "hello"
ch <- "world"
for i := range 2 {
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatalf("timed out waiting for event %d", i)
}
}
if len(received) != 2 {
t.Fatalf("received %d values, want 2", len(received))
}
if received[0] != "hello" {
t.Errorf("received[0] = %q, want %q", received[0], "hello")
}
if received[1] != "world" {
t.Errorf("received[1] = %q, want %q", received[1], "world")
}
}
func TestWatch_ExitsWhenChannelCloses(t *testing.T) {
ch := make(chan string)
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
handler := func(s string) {}
watcher := Watch(ch, handler)
watcher.Start(eventQueue, stopCh)
close(ch)
select {
case fn := <-eventQueue:
_ = fn
t.Error("unexpected event after channel close")
case <-time.After(200 * time.Millisecond):
}
}
func TestWatch_ExitsWhenStopChCloses(t *testing.T) {
ch := make(chan string, 10)
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
var received []string
var mu sync.Mutex
handler := func(s string) {
mu.Lock()
received = append(received, s)
mu.Unlock()
}
watcher := Watch(ch, handler)
watcher.Start(eventQueue, stopCh)
ch <- "first"
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatal("timed out waiting for first event")
}
close(stopCh)
select {
case ch <- "second":
default:
}
drainEvents(eventQueue, 200*time.Millisecond)
mu.Lock()
defer mu.Unlock()
if len(received) > 1 {
t.Errorf("received %d values, want at most 1", len(received))
}
}
func TestOnTimer_CreatesWatcher(t *testing.T) {
handler := func() {}
watcher := OnTimer(time.Second, handler)
if watcher == nil {
t.Error("OnTimer() should return a non-nil Watcher")
}
}
func TestOnTimer_FiresAtInterval(t *testing.T) {
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
defer close(stopCh)
var count int
handler := func() {
count++
}
watcher := OnTimer(20*time.Millisecond, handler)
watcher.Start(eventQueue, stopCh)
for i := range 2 {
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatalf("timed out waiting for tick %d", i+1)
}
}
if count < 2 {
t.Errorf("timer fired %d times, want >= 2", count)
}
}
func TestOnTimer_ExitsWhenStopChCloses(t *testing.T) {
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
var count int
var mu sync.Mutex
handler := func() {
mu.Lock()
count++
mu.Unlock()
}
watcher := OnTimer(10*time.Millisecond, handler)
watcher.Start(eventQueue, stopCh)
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatal("timed out waiting for first tick")
}
mu.Lock()
countBeforeStop := count
mu.Unlock()
close(stopCh)
drainEvents(eventQueue, 200*time.Millisecond)
mu.Lock()
finalCount := count
mu.Unlock()
if finalCount > countBeforeStop+2 {
t.Errorf("timer fired %d times (was %d before stop), should have stopped",
finalCount, countBeforeStop)
}
}
func TestWatcher_HandlerCalledOnMainLoop(t *testing.T) {
ch := make(chan int, 1)
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
defer close(stopCh)
handlerCalled := make(chan struct{})
handler := func(v int) {
close(handlerCalled)
}
watcher := Watch(ch, handler)
watcher.Start(eventQueue, stopCh)
ch <- 42
select {
case <-handlerCalled:
t.Error("handler should not be called until event is dequeued")
case <-time.After(20 * time.Millisecond):
}
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatal("no event in queue")
}
select {
case <-handlerCalled:
case <-time.After(time.Second):
t.Error("handler was not called after draining queue")
}
}
func TestNewChannelWatcher(t *testing.T) {
ch := make(chan string, 1)
var received string
w := NewChannelWatcher(ch, func(s string) {
received = s
})
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
w.Start(eventQueue, stopCh)
ch <- "hello"
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatal("timed out waiting for event")
}
close(stopCh)
if received != "hello" {
t.Fatalf("expected 'hello', got '%s'", received)
}
}
func TestNewChannelWatcher_StopsOnStopCh(t *testing.T) {
ch := make(chan string, 10)
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
var received []string
var mu sync.Mutex
w := NewChannelWatcher(ch, func(s string) {
mu.Lock()
received = append(received, s)
mu.Unlock()
})
w.Start(eventQueue, stopCh)
ch <- "first"
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatal("timed out waiting for first event")
}
close(stopCh)
select {
case ch <- "second":
default:
}
drainEvents(eventQueue, 200*time.Millisecond)
mu.Lock()
defer mu.Unlock()
if len(received) > 1 {
t.Errorf("received %d values, want at most 1 (watcher should have stopped)", len(received))
}
}
func TestNewChannelWatcher_ExitsWhenChannelCloses(t *testing.T) {
ch := make(chan string)
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
w := NewChannelWatcher(ch, func(s string) {})
w.Start(eventQueue, stopCh)
close(ch)
select {
case fn := <-eventQueue:
_ = fn
t.Error("unexpected event after channel close")
case <-time.After(200 * time.Millisecond):
}
}
func TestOnChange_CreatesWatcher(t *testing.T) {
state := NewState(0)
watcher := OnChange(state, func(v int) {})
if watcher == nil {
t.Error("OnChange() should return a non-nil Watcher")
}
}
func TestOnChange_FiresOnStart(t *testing.T) {
state := NewState(42)
state.BindApp(testApp)
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
defer close(stopCh)
var received []int
watcher := OnChange(state, func(v int) {
received = append(received, v)
})
watcher.Start(eventQueue, stopCh)
if len(received) != 1 {
t.Fatalf("received %d values on start, want 1", len(received))
}
if received[0] != 42 {
t.Errorf("received[0] = %d, want 42", received[0])
}
}
func TestOnChange_FiresOnStateChange(t *testing.T) {
state := NewState(0)
state.BindApp(testApp)
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
defer close(stopCh)
var received []int
watcher := OnChange(state, func(v int) {
received = append(received, v)
})
watcher.Start(eventQueue, stopCh)
received = nil
state.Set(10)
state.Set(20)
if len(received) != 2 {
t.Fatalf("received %d values, want 2", len(received))
}
if received[0] != 10 {
t.Errorf("received[0] = %d, want 10", received[0])
}
if received[1] != 20 {
t.Errorf("received[1] = %d, want 20", received[1])
}
}
func TestOnChange_UnbindsOnStop(t *testing.T) {
state := NewState(0)
state.BindApp(testApp)
eventQueue := make(chan func(), 10)
stopCh := make(chan struct{})
var received []int
sw := &stateWatcher[int]{
state: state,
handler: func(v int) { received = append(received, v) },
stopped: make(chan struct{}),
}
sw.Start(eventQueue, stopCh)
received = nil
state.Set(1)
if len(received) != 1 {
t.Fatalf("received %d values before stop, want 1", len(received))
}
close(stopCh)
<-sw.stopped
state.Set(2)
if len(received) != 1 {
t.Errorf("received %d values after stop, want 1 (binding should be removed)", len(received))
}
}
func TestWatch_WithDifferentTypes(t *testing.T) {
type tc struct {
testFunc func(t *testing.T)
}
tests := map[string]tc{
"string channel": {
testFunc: func(t *testing.T) {
ch := make(chan string, 1)
eventQueue := make(chan func(), 1)
stopCh := make(chan struct{})
defer close(stopCh)
var received string
watcher := Watch(ch, func(s string) { received = s })
watcher.Start(eventQueue, stopCh)
ch <- "test"
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatal("timed out waiting for event")
}
if received != "test" {
t.Errorf("received = %q, want %q", received, "test")
}
},
},
"int channel": {
testFunc: func(t *testing.T) {
ch := make(chan int, 1)
eventQueue := make(chan func(), 1)
stopCh := make(chan struct{})
defer close(stopCh)
var received int
watcher := Watch(ch, func(i int) { received = i })
watcher.Start(eventQueue, stopCh)
ch <- 42
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatal("timed out waiting for event")
}
if received != 42 {
t.Errorf("received = %d, want %d", received, 42)
}
},
},
"struct channel": {
testFunc: func(t *testing.T) {
type data struct {
Name string
Value int
}
ch := make(chan data, 1)
eventQueue := make(chan func(), 1)
stopCh := make(chan struct{})
defer close(stopCh)
var received data
watcher := Watch(ch, func(d data) { received = d })
watcher.Start(eventQueue, stopCh)
ch <- data{Name: "test", Value: 123}
select {
case fn := <-eventQueue:
fn()
case <-time.After(time.Second):
t.Fatal("timed out waiting for event")
}
if received.Name != "test" || received.Value != 123 {
t.Errorf("received = %+v, want {Name:test Value:123}", received)
}
},
},
}
for name, tt := range tests {
t.Run(name, tt.testFunc)
}
}
|