-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrigger_test.go
142 lines (105 loc) · 1.89 KB
/
trigger_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
// Copyright (c) 2022, Janoš Guljaš <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package feed_test
import (
"testing"
"resenje.org/feed"
)
func TestTrigger_triggerOnce(t *testing.T) {
tr := feed.NewTrigger[int]()
s, cancel := tr.Subscribe(1)
defer cancel()
c := newCond()
var got bool
go func() {
for range s {
signalCond(c, func() {
got = true
})
}
}()
waitCond(c, func() {
n := tr.Trigger(1)
assert(t, "", n, 1)
})
assert(t, "", got, true)
}
func TestTrigger_triggerMultiple(t *testing.T) {
tr := feed.NewTrigger[int]()
s, cancel := tr.Subscribe(1)
defer cancel()
c := newCond()
var gotCount int
stopRead := make(chan struct{})
go func() {
for {
select {
case <-s:
signalCond(c, func() {
gotCount++
})
case <-stopRead:
return
}
}
}()
waitCond(c, func() {
n := tr.Trigger(1)
assert(t, "", n, 1)
})
assert(t, "", gotCount, 1)
waitCond(c, func() {
n := tr.Trigger(1)
assert(t, "", n, 1)
})
assert(t, "", gotCount, 2)
close(stopRead)
for i := 0; i < 10; i++ {
n := tr.Trigger(1)
assert(t, "", n, 1)
}
read := make(chan struct{})
go func() {
for range s {
gotCount++
close(read)
}
}()
<-read
assert(t, "", gotCount, 3)
}
func TestTrigger_multipleTopics(t *testing.T) {
tr := feed.NewTrigger[int]()
s1, cancel1 := tr.Subscribe(1)
defer cancel1()
c1 := newCond()
var got1 bool
go func() {
for range s1 {
signalCond(c1, func() {
got1 = true
})
}
}()
s2, cancel2 := tr.Subscribe(1)
defer cancel2()
c2 := newCond()
var got2 bool
go func() {
for range s2 {
signalCond(c2, func() {
got2 = true
})
}
}()
waitCond(c1, func() {
waitCond(c2, func() {
n := tr.Trigger(1)
assert(t, "", n, 2)
})
})
assert(t, "", got1, true)
assert(t, "", got2, true)
}