-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_concurrent.sh
328 lines (294 loc) · 7.97 KB
/
test_concurrent.sh
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
# Usage: ./test_concurrent.sh [port] [iterations] [settle]
#############################################
# globals section
#############################################
PORT=$1
COUNTS=$2
TIMEOUT=$3
MSG_STEM="Sending a gratuitously long message to try trigger problems in the aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa server "
ROOM="partytime"
TEMP_DIR="/tmp/${RANDOM}"
REF_SENDER="reference/ref-sender"
REF_RECEIVER="reference/ref-receiver"
SERVER_PID=0
RECEIVER_PID=0
declare -a CLIENT_PIDS
declare -a PIPE_RES_PIDS
#############################################
# functions section
#############################################
cleanup() {
local FLAGS=$1
local PID=0
for PID in "${PIPE_RES_PIDS[@]}"; do
kill ${FLAGS} ${PID} > /dev/null 2>&1
wait ${PID} 2> /dev/null
done
for PID in "${CLIENT_PIDS[@]}"; do
kill ${FLAGS} ${PID} > /dev/null 2>&1
wait ${PID} 2> /dev/null
done
if [[ ${RECEIVER_PID} -ne 0 ]]; then
kill ${FLAGS} ${RECEIVER_PID} > /dev/null 2>&1
wait ${RECEIVER_PID} 2> /dev/null
fi
if [[ ${SERVER_PID} -ne 0 ]]; then
kill ${FLAGS} ${SERVER_PID} > /dev/null 2>&1
wait ${SERVER_PID} 2> /dev/null
fi
rm -rf ${TEMP_DIR}
}
# cleanup all resources on error
error_cleanup () {
echo $1
cleanup -9
exit 1
}
# make a pipe and hold it open using a subprocess
makepipe() {
local NAME=$1
mkfifo ${NAME}
while sleep 10; do :; done > ${NAME} &
PIPE_RES_PIDS+=($!)
}
# Main worker that sends data concurrently to the same room
send_worker() {
local FIFO=$1
local NAME=$2
local IDX=0
nc localhost ${PORT} \
< ${FIFO} \
> /dev/null 2>&1 &
local PID=$!
echo "slogin:${NAME}" > ${FIFO}
while [[ ${IDX} -lt ${COUNTS} ]]; do
echo "join:${ROOM}" > ${FIFO}
echo "sendall:${MSG_STEM}${IDX}" > ${FIFO}
echo "leave:dummy" > ${FIFO}
IDX=$((IDX+1))
done
echo "quit:done" > ${FIFO}
}
# worker that joins and leaves the server in a tight loop
send_join_worker() {
local FIFO=$1
local NAME=$2
mkfifo ${FIFO}
while true; do
nc localhost ${PORT} \
< ${FIFO} \
> /dev/null 2>&1 &
(
echo "slogin:${NAME}"
sleep 0.05
echo "join:${ROOM}"
echo "leave:dummy"
echo "join:room1"
echo "sendall:THIS IS A A REALLLLLLLLLLLLLLLYYYYYYYYYYYY LONNNNNNNNNNNNGGGGGGGG MESSSSSSSSSAAAAAAAAAAAGGGGGGE"
echo "join:pineapple"
echo "sendall:THIS IS A A REALLLLLLLLLLLLLLLYYYYYYYYYYYY LONNNNNNNNNNNNGGGGGGGG MESSSSSSSSSAAAAAAAAAAAGGGGGGE"
echo "quit:dummy"
) > ${FIFO}
done
}
# worker that joins and leaves a room in a tight loop
send_stress_worker() {
local FIFO=$1
local NAME=$2
nc localhost ${PORT} \
< ${FIFO} \
> /dev/null 2>&1 &
echo "slogin:${NAME}" > ${FIFO}
while true; do
echo "join:${ROOM}" > ${FIFO}
echo "join:bar" > ${FIFO}
echo "join:foo" > ${FIFO}
done
}
# receiver worker that joins and leaves a server in a tight loop
recv_join_worker() {
local FIFO=$1
local NAME=$2
local PID
while true; do
nc localhost ${PORT} \
< ${FIFO} \
> /dev/null 2>&1 &
PID=$!
echo "rlogin:${NAME}" > ${FIFO}
echo "join:${ROOM}" > ${FIFO}
sleep 0.1
kill -9 ${PID} > /dev/null 2>&1
wait ${PID} > /dev/null 2>&1
done
}
# function to verify a file produced by verify
verify_file() {
local FILE=$1
local USER=$2
local IDX=0
while read LINE; do
EXPECTED="${USER}: ${MSG_STEM}${IDX}"
if [[ ${IDX} -ge ${COUNTS} ]]; then
echo "too many lines in file ${FILE}"
return 1
fi
if [[ "${LINE}" != "${EXPECTED}" ]]; then
echo "expected ${EXPECTED} but got ${LINE}"
return 1
fi
IDX=$((IDX+1))
done < "${FILE}"
if [[ ${IDX} -ne ${COUNTS} ]]; then
echo "too few lines for file ${FILE}"
return 1
fi
return 0
}
# function to verify an ouutput file
verify() {
local FILE=$1
# split up files by username
grep "^bob:" ${FILE} > bob.out
grep "^alice:" ${FILE} > alice.out
grep "^mallory:" ${FILE} > mallory.out
grep -Ev "^(bob|alice|mallory):" ${FILE} > other.out
# verify non-empty files
verify_file bob.out bob
if [[ $? -ne 0 ]]; then
echo "Failed to verify first sender"
return 1;
fi
verify_file alice.out alice
if [[ $? -ne 0 ]]; then
echo "Failed to verify second sender"
return 1;
fi
verify_file mallory.out mallory
if [[ $? -ne 0 ]]; then
echo "Failed to verify third sender"
return 1;
fi
# Verify that no other output was generated
if [[ -s other.out ]]; then
echo "Verification error: Server sent unexpected messages"
return 1
fi
if [[ -s ${FILE%.out}.err ]]; then
echo "Verification error: Server sent unexpected errors"
return 1
fi
}
# spinner to make stuff look pretty in the terminal
spinner() {
local TEXT=$1
local TIME=0.1
while true; do
echo -ne "${TEXT} ⠋\x1b[0G"
sleep ${TIME}
echo -ne "${TEXT} ⠙\x1b[0G"
sleep ${TIME}
echo -ne "${TEXT} ⠹\x1b[0G"
sleep ${TIME}
echo -ne "${TEXT} ⠸\x1b[0G"
sleep ${TIME}
echo -ne "${TEXT} ⠼\x1b[0G"
sleep ${TIME}
echo -ne "${TEXT} ⠴\x1b[0G"
sleep ${TIME}
echo -ne "${TEXT} ⠦\x1b[0G"
sleep ${TIME}
echo -ne "${TEXT} ⠧\x1b[0G"
sleep ${TIME}
echo -ne "${TEXT} ⠇\x1b[0G"
sleep ${TIME}
echo -ne "${TEXT} ⠏\x1b[0G"
sleep ${TIME}
done
}
#############################################
# Script body
#############################################
if [[ "$#" -ne 3 ]]; then
echo "Usage: ./$0 [port] [iterations] [settle]"
exit 1
fi
# configure traps
trap "error_cleanup 'cleanup on ERR...'" ERR
trap "error_cleanup 'cleanup on SIGINT...'" SIGINT
trap "error_cleanup 'cleanup on SIGTERM...'" SIGTERM
# setup
rm -rf ${TEMP_DIR}
mkdir ${TEMP_DIR}
# start server
echo "spawning server"
if [[ ${VALGRIND_ENABLE} -eq 1 ]]; then
valgrind --leak-check=full --track-origins=yes ./server ${PORT} &
SERVER_PID=$!
else
./server ${PORT} &
SERVER_PID=$!
fi
# wait for server to come up
sleep 0.5
# spawn receiver
echo "spawning receiver"
stdbuf -oL -eL \
${REF_RECEIVER} localhost ${PORT} eva ${ROOM} \
1> "concur.out" \
2> "concur.err" &
RECEIVER_PID=$!
# wait for receiver to come up
sleep 0.5
# spawn send workers
echo "spawning workers"
makepipe ${TEMP_DIR}/concur_1.in
send_worker ${TEMP_DIR}/concur_1.in bob &
CLIENT_PIDS+=($!)
makepipe ${TEMP_DIR}/concur_2.in
send_worker ${TEMP_DIR}/concur_2.in alice &
CLIENT_PIDS+=($!)
makepipe ${TEMP_DIR}/concur_3.in
send_worker ${TEMP_DIR}/concur_3.in mallory &
CLIENT_PIDS+=($!)
send_join_worker ${TEMP_DIR}/concur_4.in bad1 &
CLIENT_PIDS+=($!)
send_join_worker ${TEMP_DIR}/concur_5.in bad2 &
CLIENT_PIDS+=($!)
makepipe ${TEMP_DIR}/concur_6.in
recv_join_worker ${TEMP_DIR}/concur_6.in bad3 &
CLIENT_PIDS+=($!)
makepipe ${TEMP_DIR}/concur_7.in
send_stress_worker ${TEMP_DIR}/concur_7.in bad4 &
CLIENT_PIDS+=($!)
makepipe ${TEMP_DIR}/concur_8.in
send_stress_worker ${TEMP_DIR}/concur_8.in bad5 &
CLIENT_PIDS+=($!)
# wait for workers to start
sleep 0.5
# show spinner while waiting for messages to settle
spinner "running workers" &
SPINNER_PID=$!
# wait for messages to settle
sleep ${TIMEOUT}
kill ${SPINNER_PID}
# check that server is still up
kill -0 ${SERVER_PID}
echo ""
if [[ $? -ne 0 ]]; then
echo "Server died when it was not supposed to!"
exit 1
fi
# clean up everything
echo "cleaning up run"
cleanup
trap - ERR
echo "verifying outputs"
# verify outputs
verify concur.out
if [[ $? -eq 0 ]]; then
echo "Tests passed successfully!"
fi
# exit with correct code
exit $?