-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventserver_test.go
253 lines (223 loc) · 7.46 KB
/
eventserver_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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"io"
"net"
"strconv"
"testing"
"github.com/sahildua2305/go-eventserver/config"
"os/exec"
"strings"
)
///// Unit tests /////
// Tests parseEvent function extensively for various event messages that
// the server might need to handle. Contains a good number of valid and
// invalid event messages. Feel free to add more as you come across more
// edge cases to make sure this function is robust.
func TestParseEvent(t *testing.T) {
want := map[string]bool{
// Random bad message cases
"": false,
"123224": false,
"abcdkjdhfjsdf": false,
"|": false,
"12|||": false,
"|||": false,
"1|L|2|3": false,
// Follow message cases
"12|F|21212|31212": true,
"1234567890|F|1234567890|1234567891": true,
"12|F": false,
"12|F|12": false,
"|F|12|13": false,
"1|F|12|12|12|12": false,
"1|F||2": false,
"1|F|2|": false,
// Unfollow message cases
"2|U|4|5": true,
"2|U|4": false,
"2|U|4|": false,
"2|U||4": false,
"|U|4|5": false,
"|U||": false,
"1|U|2|3|4": false,
// Broadcast message cases
"1|B": true,
"1234567890|B": true,
"|B": false,
"1|B|2": false,
"1|B|2|3": false,
// Private message cases
"1|P|12|11": true,
"1|P||": false,
"|P||": false,
"1|P|2|3|": false,
"|P": false,
"1|P": false,
"1|P|2|": false,
// Status message cases
"12|S|23": true,
"12|S|23|23": false,
"12|S|": false,
}
for msg := range want {
_, err := parseEvent(msg)
if err != nil && want[msg] {
t.Errorf("parseEvent with event - %v: want ok, got %+v", msg, err)
}
if err == nil && !want[msg] {
t.Errorf("parseEvent with event - %v: want not ok, got ok", msg)
}
}
}
///// Functional tests /////
// Tests whether the server can be started and gracefully stopped
// without any errors.
func TestEventServer_startAndStop(t *testing.T) {
cfg, err := config.LoadEventServerConfig("./config/testdata/config_valid.json")
if err != nil {
t.Error("Couldn't load server config, got error: ", err)
}
es, err := startServer(cfg)
if err != nil {
t.Error("Server couldn't be started, got error: ", err)
}
err = es.gracefulStop()
if err != nil {
t.Error("Server couldn't be stopped gracefully, got error: ", err)
}
if !es.hasStopped {
t.Error("Server is still running, expected to be stopped")
}
if err = es.gracefulStop(); err == nil {
t.Error("gracefulStop() ran successfully, expected it to throw error")
}
}
// Tests the event server with the given jar test program for 1000 events.
func TestEventServer_runWithJarHarness(t *testing.T) {
cfg := &config.EventServerConfig{EventListenerPort: 8080, ClientListenerPort: 8088}
es, err := startServer(cfg)
if err != nil {
t.Fatal("Server couldn't be started, got error: ", err)
}
c := exec.Command("time", "java", "-server", "-Xmx1G", "-jar", "./extras/follower-maze-2.0.jar")
c.Env = []string{"totalEvents=1000", "eventListenerPort=8080", "clientListenerPort=8088"}
out, err := c.CombinedOutput()
if err != nil {
t.Error("Got error: ", err)
}
if !strings.Contains(string(out), "ALL NOTIFICATIONS RECEIVED") {
t.Error("Test failed with jar harness, got incorrect output")
}
err = es.gracefulStop()
if err != nil {
t.Error("Server couldn't be stopped gracefully, got error: ", err)
}
}
// Tests the behaviour of server with some raw messages including
// some bad ones.
func TestEventServer_withRawEventMessages(t *testing.T) {
cfg := &config.EventServerConfig{EventListenerPort: 6060, ClientListenerPort: 6066}
es, err := startServer(cfg)
if err != nil {
t.Fatal("Server couldn't be started, got error:", err)
}
conn, err := net.Dial("tcp", "localhost:"+strconv.Itoa(cfg.EventListenerPort))
if err != nil {
t.Error("Couldn't open port for event source, got error: ", err)
}
eventMsgs := []string{
"123224",
"",
"abcdkjdhfjsdf",
"2|U|4|5",
"12|S|23",
"1|U|2|3|4",
}
for _, msg := range eventMsgs {
_, err = io.WriteString(conn, msg)
if err != nil {
t.Errorf("Couldn't send event: %+v, got error: %+v", msg, err)
}
}
conn.Close()
if es.hasStopped {
t.Error("Server has stopped, expected to be running")
}
err = es.gracefulStop()
if err != nil {
t.Error("Server couldn't be stopped gracefully, got error: ", err)
}
}
// Tests the behaviour when one of the ports is already open and hence,
// the server can't be started.
func TestEventServer_alreadyBusyPort(t *testing.T) {
cfg := &config.EventServerConfig{EventListenerPort: 5050, ClientListenerPort: 5055}
listener, err := net.Listen("tcp", ":"+strconv.Itoa(cfg.EventListenerPort))
if err != nil {
t.Fatal("Couldn't open port for event source, got error: ", err)
}
es, err := startServer(cfg)
if err == nil {
t.Error("Server started without error, expected error")
}
listener.Close()
listener, err = net.Listen("tcp", ":"+strconv.Itoa(cfg.ClientListenerPort))
if err != nil {
t.Error("Couldn't open port for user clients, got error: ", err)
}
es, err = startServer(cfg)
if err == nil {
t.Error("Server started without error, expected error")
}
listener.Close()
err = es.gracefulStop()
if err == nil {
t.Error("gracefulStop() ran successfully, expected it to throw error")
}
}
///// Benchmark tests /////
func benchmarkEventServer(args []string, b *testing.B) {
// run server with events b.N times
for n := 0; n < b.N; n++ {
cfg := &config.EventServerConfig{EventListenerPort: 7070, ClientListenerPort: 7077}
es, err := startServer(cfg)
if err != nil {
b.Fatal("Server couldn't be started, got error:", err)
}
c := exec.Command("time", "java", "-server", "-Xmx1G", "-jar", "./extras/follower-maze-2.0.jar")
c.Env = []string{"eventListenerPort=7070", "clientListenerPort=7077"}
c.Env = append(c.Env, args...)
out, err := c.CombinedOutput()
if err != nil {
b.Error("Got error: ", err)
}
if !strings.Contains(string(out), "ALL NOTIFICATIONS RECEIVED") {
b.Error("Test failed with jar harness, got incorrect output")
}
err = es.gracefulStop()
if err != nil {
b.Error("Server couldn't be stopped gracefully, got error: ", err)
}
}
}
func BenchmarkEventServer_1000Events1Batch10ConnectedUsers(b *testing.B) {
benchmarkEventServer([]string{"totalEvents=1000", "maxEventSourceBatchSize=1", "concurrencyLevel=10"}, b)
}
func BenchmarkEventServer_1000Events10Batch10ConnectedUsers(b *testing.B) {
benchmarkEventServer([]string{"totalEvents=1000", "maxEventSourceBatchSize=10", "concurrencyLevel=10"}, b)
}
func BenchmarkEventServer_1000Events100Batch10ConnectedUsers(b *testing.B) {
benchmarkEventServer([]string{"totalEvents=1000", "maxEventSourceBatchSize=100", "concurrencyLevel=10"}, b)
}
func BenchmarkEventServer_10000Events100Batch100ConnectedUsers(b *testing.B) {
benchmarkEventServer([]string{"totalEvents=10000", "maxEventSourceBatchSize=100", "concurrencyLevel=100"}, b)
}
func BenchmarkEventServer_10000Events1000Batch100ConnectedUsers(b *testing.B) {
benchmarkEventServer([]string{"totalEvents=10000", "maxEventSourceBatchSize=1000", "concurrencyLevel=100"}, b)
}
func BenchmarkEventServer_10000Events10000Batch100ConnectedUsers(b *testing.B) {
benchmarkEventServer([]string{"totalEvents=10000", "maxEventSourceBatchSize=10000", "concurrencyLevel=100"}, b)
}
func BenchmarkEventServer_100000Events10000Batch1000ConnectedUsers(b *testing.B) {
benchmarkEventServer([]string{"totalEvents=100000", "maxEventSourceBatchSize=10000", "concurrencyLevel=1000"}, b)
}