@@ -9,16 +9,17 @@ kill_marker=kill_me_d5af0410
9
9
server_port=8377
10
10
srvcfg=" sv_rcon_password rcon;sv_port $server_port ;$kill_marker "
11
11
cl_fifo=" $PWD /$tmpdir /client.fifo"
12
- clcfg=" cl_input_fifo $cl_fifo ;connect 127.0.0.1:$server_port ;$kill_marker "
12
+ clcfg=" cl_input_fifo $cl_fifo ;connect 127.0.0.1:$server_port ;player_name test_client; $kill_marker "
13
13
tw_srv_running=0
14
14
ruby_logfile=ruby_client.txt
15
15
16
+ _client_pid=' '
16
17
_kill_pids=()
17
18
18
19
mkdir -p logs
19
20
mkdir -p tmp
20
21
21
- function start_tw_server() {
22
+ start_tw_server () {
22
23
if [[ -x " $( command -v teeworlds_srv) " ]]
23
24
then
24
25
teeworlds_srv " $srvcfg " & > " $logdir /server.txt"
@@ -37,7 +38,7 @@ function start_tw_server() {
37
38
tw_srv_running=1
38
39
}
39
40
40
- function connect_tw_client() {
41
+ connect_tw_client () {
41
42
if [[ -x " $( command -v teeworlds-headless) " ]]
42
43
then
43
44
teeworlds-headless " $clcfg "
@@ -53,7 +54,7 @@ function connect_tw_client() {
53
54
fi
54
55
}
55
56
56
- function connect_ddnet7_client() {
57
+ connect_ddnet7_client () {
57
58
local clcfg_dd7
58
59
clcfg_dd7=" $( echo " $clcfg " | sed ' s/127.0.0.1/tw-0.7+udp:\/\/127.0.0.1/' ) "
59
60
if [[ -x " $( command -v DDNet7-headless) " ]]
@@ -68,10 +69,10 @@ function connect_ddnet7_client() {
68
69
fi
69
70
}
70
71
71
- function get_test_names() {
72
+ get_test_names () {
72
73
(find client -name " *.rb" ; find server -name " *.rb" ) | tr ' \n' ' '
73
74
}
74
- function invalid_test() {
75
+ invalid_test () {
75
76
local name=" $1 "
76
77
echo " Error: invalid test name '$name '"
77
78
echo " valid tests: $( get_test_names) "
99
100
ruby_logfile=" $logdir /ruby_server.txt"
100
101
fi
101
102
102
- function kill_all_jobs() {
103
+ kill_all_jobs () {
103
104
local i
104
105
local kill_pid
105
106
for i in " ${! _kill_pids[@]} "
@@ -114,7 +115,7 @@ function kill_all_jobs() {
114
115
pkill -f " $kill_marker "
115
116
}
116
117
117
- function cleanup() {
118
+ cleanup () {
118
119
if [ " $tw_srv_running " == " 1" ]
119
120
then
120
121
echo " [*] shutting down server ..."
@@ -127,7 +128,7 @@ function cleanup() {
127
128
128
129
trap cleanup EXIT
129
130
130
- function fail() {
131
+ fail () {
131
132
local msg=" $1 "
132
133
if [ ! -f " $tmpdir /fail.txt" ]
133
134
then
@@ -156,7 +157,7 @@ function fail() {
156
157
exit 1
157
158
}
158
159
159
- function timeout() {
160
+ timeout () {
160
161
local seconds=" $1 "
161
162
sleep " $seconds "
162
163
echo " [-] Timeout -> killing: $testname "
178
179
echo " ddnet7 client log $( date) " > " $logdir /client.txt"
179
180
echo " ruby server log $( date) " > " $ruby_logfile "
180
181
fi
181
- function run_ruby_test() {
182
+ run_ruby_test () {
182
183
if ! ruby " $testname " " $kill_marker " & > " $ruby_logfile "
183
184
then
184
185
fail " test $testname finished with non zero exit code"
196
197
if [[ " $testname " =~ ^server/ ]]
197
198
then
198
199
connect_ddnet7_client " $kill_marker " & >> " $logdir /client.txt" &
199
- _kill_pids+=($! )
200
+ _client_pid=$!
201
+ _kill_pids+=(" $_client_pid " )
200
202
sleep 1
201
203
fi
202
- timeout 6 " $kill_marker " &
204
+ timeout 20 " $kill_marker " &
203
205
_timeout_pid=$!
204
206
205
- function fifo() {
207
+ fifo () {
206
208
local cmd=" $1 "
207
209
local fifo_file=" $2 "
208
210
echo " [*] $cmd >> $fifo_file "
209
211
echo " $cmd " >> " $fifo_file "
210
212
}
213
+ assert_in_log () {
214
+ # usage: assert_in_log string path [num_matches"
215
+ # examples:
216
+ # assert_in_log "string to find" "/path/to/log.txt"
217
+ # assert_in_log "string to find" "/path/to/log.txt" 2
218
+ local needle=" $1 "
219
+ local logfile_path=" $2 "
220
+ local num_matches=" $3 "
221
+ if ! grep -q " $needle " " $logfile_path "
222
+ then
223
+ echo " [-] Error: did not find expected string in logs"
224
+ echo " [-]"
225
+ echo " [-] expected: $needle "
226
+ echo " [-] in file: $ruby_logfile "
227
+ echo " [-]"
228
+ fail " assert failed"
229
+ fi
230
+ if [ " $num_matches " != " " ]
231
+ then
232
+ local actual_matches
233
+ actual_matches=" $( grep -c " $needle " " $logfile_path " ) "
234
+ if [ " $actual_matches " != " $num_matches " ]
235
+ then
236
+ echo " [-] Error: found string unexpected amount of times in log file"
237
+ echo " [-]"
238
+ echo " [-] expected: $needle "
239
+ echo " [-] in file: $ruby_logfile "
240
+ echo " [-]"
241
+ echo " [-] expected num hits: $num_matches "
242
+ echo " [-] got num hits: $actual_matches "
243
+ echo " [-]"
244
+ fail " assert failed"
245
+ fi
246
+ fi
247
+ echo " [*] $needle .. OK"
248
+ }
211
249
212
250
if [ " $testname " == " client/chat.rb" ]
213
251
then
@@ -271,6 +309,11 @@ then
271
309
fifo " rcon shutdown" " $cl_fifo "
272
310
sleep 1
273
311
fifo " quit" " $cl_fifo "
312
+ # ddnet quitting can get stuck so send a kill to ensure it dies
313
+ kill " $_client_pid "
314
+
315
+ assert_in_log " 'test_client' joined the game" " $ruby_logfile " 1
316
+ assert_in_log " rcon='shutdown'" " $ruby_logfile " 1
274
317
else
275
318
echo " Error: unkown test '$testname '"
276
319
exit 1
0 commit comments