-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventbus_example_test.go
60 lines (51 loc) · 1.23 KB
/
eventbus_example_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
package eventbus_test
import (
"fmt"
"time"
"github.com/montag451/go-eventbus"
)
type ProcessStarted struct {
Pid int
}
func (ProcessStarted) Name() eventbus.EventName {
return "process.started"
}
type ProcessSignaled struct {
Pid int
Signal int
}
func (ProcessSignaled) Name() eventbus.EventName {
return "process.signaled"
}
type ProcessExited struct {
Pid int
ExitCode int
}
func (ProcessExited) Name() eventbus.EventName {
return "process.exited"
}
func Example() {
closed := make(chan struct{})
b := eventbus.New(eventbus.WithClosedHandler(func() {
close(closed)
}))
b.Subscribe(eventbus.WildcardPattern("process.*"), func(e eventbus.Event, t time.Time) {
switch e := e.(type) {
case ProcessStarted:
fmt.Printf("Process %d started\n", e.Pid)
case ProcessSignaled:
fmt.Printf("Process %d has been signaled with signal %d\n", e.Pid, e.Signal)
case ProcessExited:
fmt.Printf("Process %d exited with code %d\n", e.Pid, e.ExitCode)
}
})
b.Publish(ProcessStarted{12000})
b.PublishSync(ProcessSignaled{12000, 9})
b.PublishAsync(ProcessExited{12000, 0})
b.Close()
<-closed
// Output:
// Process 12000 started
// Process 12000 has been signaled with signal 9
// Process 12000 exited with code 0
}