-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent_test.go
118 lines (104 loc) · 2.59 KB
/
event_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
package tegenaria
import (
"errors"
"fmt"
"testing"
"github.com/agiledragon/gomonkey/v2"
c "github.com/smartystreets/goconvey/convey" // 别名导入
)
func TestEventWatcher(t *testing.T) {
c.Convey("Watch events", t, func() {
hooker := NewDefaultHooks()
f := func() error {
ch := make(chan EventType, 5)
defer close(ch)
ch <- START
ch <- ERROR
ch <- HEARTBEAT
ch <- EXIT
return hooker.EventsWatcher(ch)
}
c.So(f(), c.ShouldBeNil)
})
}
func TestEventWatcherWithError(t *testing.T) {
c.Convey("Watch start event error", t, func() {
hooker := NewDefaultHooks()
patch := gomonkey.ApplyFunc((*DefaultHooks).Start, func(_ *DefaultHooks, params ...interface{}) error { return errors.New("start error") })
f := func() {
ch := make(chan EventType, 1)
defer close(ch)
ch <- START
err := hooker.EventsWatcher(ch)
if err != nil {
panic(err)
}
}
c.So(f, c.ShouldPanic)
defer patch.Reset()
})
c.Convey("Watch pause event error", t, func() {
patch := gomonkey.ApplyFunc((*DefaultHooks).Pause, func(_ *DefaultHooks, _ ...interface{}) error {
return fmt.Errorf("pause error")
})
hooker := NewDefaultHooks()
f := func() {
ch := make(chan EventType, 1)
ch <- PAUSE
err := hooker.EventsWatcher(ch)
if err != nil {
panic(err)
}
}
c.So(f, c.ShouldPanic)
defer patch.Reset()
})
c.Convey("Watch ERROR event error", t, func() {
patch := gomonkey.ApplyFunc((*DefaultHooks).Error, func(_ *DefaultHooks, _ ...interface{}) error {
return fmt.Errorf("handle error event error")
})
hooker := NewDefaultHooks()
f := func() {
ch := make(chan EventType, 1)
ch <- ERROR
err := hooker.EventsWatcher(ch)
if err != nil {
panic(err)
}
}
c.So(f, c.ShouldNotPanic)
defer patch.Reset()
})
c.Convey("Watch heartbeat event error", t, func() {
patch := gomonkey.ApplyFunc((*DefaultHooks).Heartbeat, func(_ *DefaultHooks, _ ...interface{}) error {
return fmt.Errorf("heartbeat error")
})
hooker := NewDefaultHooks()
f := func() {
ch := make(chan EventType, 1)
ch <- HEARTBEAT
err := hooker.EventsWatcher(ch)
if err != nil {
panic(err)
}
}
c.So(f, c.ShouldPanic)
defer patch.Reset()
})
c.Convey("Watch EXIT event error", t, func() {
patch := gomonkey.ApplyFunc((*DefaultHooks).Exit, func(_ *DefaultHooks, _ ...interface{}) error {
return fmt.Errorf("EXIT error")
})
hooker := NewDefaultHooks()
f := func() {
ch := make(chan EventType, 1)
ch <- EXIT
err := hooker.EventsWatcher(ch)
if err != nil {
panic(err)
}
}
c.So(f, c.ShouldPanic)
defer patch.Reset()
})
}