forked from cerc-io/plugeth
Merge pull request #1967 from karalabe/fix-filter-test-datarace
event/filter: fix data race in the test
This commit is contained in:
commit
6d09468cab
@ -21,35 +21,40 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Simple test to check if baseline matching/mismatching filtering works.
|
||||||
func TestFilters(t *testing.T) {
|
func TestFilters(t *testing.T) {
|
||||||
var success bool
|
|
||||||
var failure bool
|
|
||||||
|
|
||||||
fm := New()
|
fm := New()
|
||||||
fm.Start()
|
fm.Start()
|
||||||
|
|
||||||
|
// Register two filters to catch posted data
|
||||||
|
first := make(chan struct{})
|
||||||
fm.Install(Generic{
|
fm.Install(Generic{
|
||||||
Str1: "hello",
|
Str1: "hello",
|
||||||
Fn: func(data interface{}) {
|
Fn: func(data interface{}) {
|
||||||
success = data.(bool)
|
first <- struct{}{}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
second := make(chan struct{})
|
||||||
fm.Install(Generic{
|
fm.Install(Generic{
|
||||||
Str1: "hello1",
|
Str1: "hello1",
|
||||||
Str2: "hello",
|
Str2: "hello",
|
||||||
Fn: func(data interface{}) {
|
Fn: func(data interface{}) {
|
||||||
failure = true
|
second <- struct{}{}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
// Post an event that should only match the first filter
|
||||||
fm.Notify(Generic{Str1: "hello"}, true)
|
fm.Notify(Generic{Str1: "hello"}, true)
|
||||||
fm.Stop()
|
fm.Stop()
|
||||||
|
|
||||||
time.Sleep(10 * time.Millisecond) // yield to the notifier
|
// Ensure only the mathcing filters fire
|
||||||
|
select {
|
||||||
if !success {
|
case <-first:
|
||||||
t.Error("expected 'hello' to be posted")
|
case <-time.After(100 * time.Millisecond):
|
||||||
|
t.Error("matching filter timed out")
|
||||||
}
|
}
|
||||||
|
select {
|
||||||
if failure {
|
case <-second:
|
||||||
t.Error("hello1 was triggered")
|
t.Error("mismatching filter fired")
|
||||||
|
case <-time.After(100 * time.Millisecond):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user