-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_bus_test.go
More file actions
50 lines (38 loc) · 860 Bytes
/
event_bus_test.go
File metadata and controls
50 lines (38 loc) · 860 Bytes
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
package eventd_test
import (
"fmt"
"math"
"testing"
"time"
. "github.com/symphony09/eventd"
)
func TestEventBus(t *testing.T) {
bus := new(EventBus[int])
cancelDivide, err := bus.Subscribe(func(event string, i int) bool {
fmt.Println("divide", event, i, "to", i/2)
return true
}, On("put"))
if err != nil {
t.Error(err)
}
cancelCheck, _ := bus.Subscribe(func(event string, i int) bool {
fmt.Println("check", event, i)
if i != 0 {
return true
} else {
bus.Emit("check failed", i)
return false
}
}, On("put"), Weight(math.MaxInt))
_, _ = bus.Subscribe(func(event string, i int) bool {
fmt.Println("log", event, i)
return true
}, On(".*"), Async)
bus.Emit("put", 0)
bus.Emit("put", 2)
time.Sleep(time.Millisecond * 10)
cancelDivide()
cancelCheck()
bus.Emit("put", 4)
time.Sleep(time.Millisecond * 10)
}