forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents_test.go
More file actions
202 lines (183 loc) · 5.8 KB
/
events_test.go
File metadata and controls
202 lines (183 loc) · 5.8 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
"testing"
"time"
)
func TestEventSerial(t *testing.T) {
v1 := nextEventSerial()
v2 := nextEventSerial()
if v2 < v1 {
t.Error("Fail to get next serial")
}
}
func TestEventPoolSerial(t *testing.T) {
val := eventPoolSerial.nextSerial("test1")
if val != 1 {
t.Error("Fail to get next serial")
}
val = eventPoolSerial.nextSerial("test1")
if val != 2 {
t.Error("Fail to get next serial")
}
val = eventPoolSerial.nextSerial("test2")
if val != 1 {
t.Error("Fail to get next serial")
}
}
func readEvent(reader *bufio.Reader) (string, string) {
header, err := reader.ReadString('\n')
if err != nil {
return "", ""
} else {
tmp := strings.Split(header[0:len(header)-1], ":")
len, _ := strconv.Atoi(tmp[len(tmp)-1])
b := make([]byte, len)
io.ReadFull(reader, b)
return header, string(b)
}
}
func TestEventListener(t *testing.T) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()
reader := bufio.NewReader(r1)
listener := NewEventListener("pool-1",
"supervisor",
r2,
w1,
10)
eventListenerManager.registerEventListener("pool-1",
[]string{"REMOTE_COMMUNICATION"},
listener)
emitEvent(NewRemoteCommunicationEvent("type-1", "this is a remote communication event test"))
fmt.Printf("start to write READY\n")
w2.Write([]byte("READY\n"))
_, body := readEvent(reader)
if body != "type:type-1\nthis is a remote communication event test" {
t.Error("The body is not expect")
}
w2.Write([]byte("RESULT 4\nFAIL"))
w2.Write([]byte("READY\n"))
_, body = readEvent(reader)
if body != "type:type-1\nthis is a remote communication event test" {
t.Error("The body is not expect")
}
w2.Write([]byte("RESULT 2\nOK"))
time.Sleep(2 * time.Second)
w2.Close()
r2.Close()
r1.Close()
w1.Close()
eventListenerManager.unregisterEventListener("pool-1")
}
func TestProcCommEventCapture(t *testing.T) {
r1, w1 := io.Pipe()
r2, w2 := io.Pipe()
reader := bufio.NewReader(r1)
capture_reader, capture_writer := io.Pipe()
eventCapture := NewProcCommEventCapture(capture_reader,
10240,
"PROCESS_COMMUNICATION_STDOUT",
"proc-1",
"group-1")
eventCapture.SetPid(99)
listener := NewEventListener("pool-1",
"supervisor",
r2,
w1,
10)
eventListenerManager.registerEventListener("pool-1",
[]string{"PROCESS_COMMUNICATION"},
listener)
w2.Write([]byte("READY\n"))
capture_writer.Write([]byte(`this is unuseful information, seems it is very
long and not useful, just used for testing purpose.
let's input more unuseful information, ok.....
haha...<!--XSUPERVISOR:BEGIN-->this is a proc event test<!--XSUPERVISOR:END--> also
add some other unuseful`))
_, body := readEvent(reader)
expect_body := "processname:proc-1 groupname:group-1 pid:99\nthis is a proc event test"
if body != expect_body {
t.Error("Fail to get the process communication event")
}
w2.Close()
r2.Close()
r1.Close()
w1.Close()
}
func TestProcessStartingEvent(t *testing.T) {
event := createProcessStartingEvent("proc-1", "group-1", "STOPPED", 0)
if event.GetType() != "PROCESS_STATE_STARTING" {
t.Error("Fail to creating the process starting event")
}
if event.GetBody() != "processname:proc-1 groupname:group-1 from_state:STOPPED tries:0" {
t.Error("Fail to encode the process starting event")
}
}
func TestProcessRunningEvent(t *testing.T) {
event := createProcessRunningEvent("proc-1", "group-1", "STARTING", 2766)
if event.GetType() != "PROCESS_STATE_RUNNING" {
t.Error("Fail to creating the process running event")
}
if event.GetBody() != "processname:proc-1 groupname:group-1 from_state:STARTING pid:2766" {
t.Error("Fail to encode the process running event")
}
}
func TestProcessBackoffEvent(t *testing.T) {
event := createProcessBackoffEvent("proc-1", "group-1", "STARTING", 1)
if event.GetType() != "PROCESS_STATE_BACKOFF" {
t.Error("Fail to creating the process backoff event")
}
if event.GetBody() != "processname:proc-1 groupname:group-1 from_state:STARTING tries:1" {
t.Error("Fail to encode the process backoff event")
}
}
func TestProcessStoppingEvent(t *testing.T) {
event := createProcessStoppingEvent("proc-1", "group-1", "STARTING", 2766)
if event.GetType() != "PROCESS_STATE_STOPPING" {
t.Error("Fail to creating the process stopping event")
}
if event.GetBody() != "processname:proc-1 groupname:group-1 from_state:STARTING pid:2766" {
t.Error("Fail to encode the process stopping event")
}
}
func TestProcessExitedEvent(t *testing.T) {
event := createProcessExitedEvent("proc-1", "group-1", "RUNNING", 1, 2766)
if event.GetType() != "PROCESS_STATE_EXITED" {
t.Error("Fail to creating the process exited event")
}
if event.GetBody() != "processname:proc-1 groupname:group-1 from_state:RUNNING expected:1 pid:2766" {
t.Error("Fail to encode the process exited event")
}
}
func TestProcessStoppedEvent(t *testing.T) {
event := createProcessStoppedEvent("proc-1", "group-1", "STOPPING", 2766)
if event.GetType() != "PROCESS_STATE_STOPPED" {
t.Error("Fail to creating the process stopped event")
}
if event.GetBody() != "processname:proc-1 groupname:group-1 from_state:STOPPING pid:2766" {
t.Error("Fail to encode the process stopped event")
}
}
func TestProcessFatalEvent(t *testing.T) {
event := createProcessFatalEvent("proc-1", "group-1", "BACKOFF")
if event.GetType() != "PROCESS_STATE_FATAL" {
t.Error("Fail to creating the process fatal event")
}
if event.GetBody() != "processname:proc-1 groupname:group-1 from_state:BACKOFF" {
t.Error("Fail to encode the process fatal event")
}
}
func TestProcessUnknownEvent(t *testing.T) {
event := createProcessUnknownEvent("proc-1", "group-1", "BACKOFF")
if event.GetType() != "PROCESS_STATE_UNKNOWN" {
t.Error("Fail to creating the process unknown event")
}
if event.GetBody() != "processname:proc-1 groupname:group-1 from_state:BACKOFF" {
t.Error("Fail to encode the process unknown event")
}
}