Skip to content

Commit 6d09468

Browse files
committed
Merge pull request #1967 from karalabe/fix-filter-test-datarace
event/filter: fix data race in the test
2 parents 2334ee9 + 8e2bf42 commit 6d09468

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

event/filter/filter_test.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,40 @@ import (
2121
"time"
2222
)
2323

24+
// Simple test to check if baseline matching/mismatching filtering works.
2425
func TestFilters(t *testing.T) {
25-
var success bool
26-
var failure bool
27-
2826
fm := New()
2927
fm.Start()
28+
29+
// Register two filters to catch posted data
30+
first := make(chan struct{})
3031
fm.Install(Generic{
3132
Str1: "hello",
3233
Fn: func(data interface{}) {
33-
success = data.(bool)
34+
first <- struct{}{}
3435
},
3536
})
37+
second := make(chan struct{})
3638
fm.Install(Generic{
3739
Str1: "hello1",
3840
Str2: "hello",
3941
Fn: func(data interface{}) {
40-
failure = true
42+
second <- struct{}{}
4143
},
4244
})
45+
// Post an event that should only match the first filter
4346
fm.Notify(Generic{Str1: "hello"}, true)
4447
fm.Stop()
4548

46-
time.Sleep(10 * time.Millisecond) // yield to the notifier
47-
48-
if !success {
49-
t.Error("expected 'hello' to be posted")
49+
// Ensure only the mathcing filters fire
50+
select {
51+
case <-first:
52+
case <-time.After(100 * time.Millisecond):
53+
t.Error("matching filter timed out")
5054
}
51-
52-
if failure {
53-
t.Error("hello1 was triggered")
55+
select {
56+
case <-second:
57+
t.Error("mismatching filter fired")
58+
case <-time.After(100 * time.Millisecond):
5459
}
5560
}

0 commit comments

Comments
 (0)