Debian Codegraph @

src/util/eventbox_test.go

raw
1 package util
2
3 import "testing"
4
5 // fzf events
6 const (
7 EvtReadNew EventType = iota
8 EvtReadFin
9 EvtSearchNew
10 EvtSearchProgress
11 EvtSearchFin
12 )
13
14 func TestEventBox(t *testing.T) {
15 eb := NewEventBox()
16
17 // Wait should return immediately
18 ch := make(chan bool)
19
20 go func() {
21 eb.Set(EvtReadNew, 10)
22 ch <- true
23 <-ch
24 eb.Set(EvtSearchNew, 10)
25 eb.Set(EvtSearchNew, 15)
26 eb.Set(EvtSearchNew, 20)
27 eb.Set(EvtSearchProgress, 30)
28 ch <- true
29 <-ch
30 eb.Set(EvtSearchFin, 40)
31 ch <- true
32 <-ch
33 }()
34
35 count := 0
36 sum := 0
37 looping := true
38 for looping {
39 <-ch
40 eb.Wait(func(events *Events) {
41 for _, value := range *events {
42 switch val := value.(type) {
43 case int:
44 sum += val
45 looping = sum < 100
46 }
47 }
48 events.Clear()
49 })
50 ch <- true
51 count++
52 }
53
54 if count != 3 {
55 t.Error("Invalid number of events", count)
56 }
57 if sum != 100 {
58 t.Error("Invalid sum", sum)
59 }
60 }