forked from dspinellis/dgsh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-kvstore.sh
executable file
·557 lines (468 loc) · 14.8 KB
/
test-kvstore.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/bin/sh
# Helper functions {{{1
# Repeat x times
sequence()
{
jot $1 2>/dev/null || seq $1 2>/dev/null
}
fail()
{
echo "FAIL"
echo "$1"
./dgsh-readval -q -s testsocket 2>/dev/null
if [ "$SERVER_PID" ]
then
stop_server
fi
exit 1
}
# Verify a test result
# -n disables the termination of writeval
check()
{
if [ "$1" != '-n' ]
then
./dgsh-readval -q -s testsocket 2>/dev/null
fi
if [ "$TRY" != "$EXPECT" ]
then
fail "Expected [$EXPECT] got [$TRY]"
else
echo "OK"
fi
}
# A section of the test
section()
{
SECTION="$1"
echo "$1"
}
# A particular test case
testcase()
{
TESTCASE="$1"
echo -n " $1: "
}
# Start the HTTP server
start_server()
{
./dgsh-httpval -p $PORT "$@" &
SERVER_PID=$!
}
# Stop the HTTP server
stop_server()
{
curl -s40 http://localhost:$PORT/.server?quit >/dev/null
sleep 1
kill $SERVER_PID 2>/dev/null
sleep 2
SERVER_PID=''
}
# Simple tests {{{1
section 'Simple tests' # {{{2
testcase "Single record" # {{{3
echo single record | ./dgsh-writeval -s testsocket 2>/dev/null &
TRY="`./dgsh-readval -l -s testsocket 2>/dev/null `"
EXPECT='single record'
check
testcase "Single record fixed width" # {{{3
echo -n 0123456789 | ./dgsh-writeval -l 10 -s testsocket 2>/dev/null &
EXPECT='0123456789'
TRY="`./dgsh-readval -l -s testsocket 2>/dev/null `"
check
testcase "Record separator" # {{{3
echo -n record1:record2 | ./dgsh-writeval -t : -s testsocket 2>/dev/null &
TRY="`./dgsh-readval -l -s testsocket 2>/dev/null `"
EXPECT='record1:'
check
testcase "HTTP interface - text data" # {{{3
PORT=53843
echo single record | ./dgsh-writeval -s testsocket 2>/dev/null &
start_server
# -s40: silent, IPv4 HTTP 1.0
TRY="`curl -s40 http://localhost:$PORT/testsocket`"
EXPECT='single record'
check
stop_server
testcase "HTTP interface - non-blocking empty record" # {{{3
PORT=53843
{ sleep 2 ; echo hi ; } | ./dgsh-writeval -s testsocket 2>/dev/null &
start_server -n
# -s40: silent, IPv4 HTTP 1.0
TRY="`curl -s40 http://localhost:$PORT/testsocket`"
EXPECT=''
check
stop_server
testcase "HTTP interface - non-blocking first record" # {{{3
PORT=53843
echo 'first record' | ./dgsh-writeval -s testsocket 2>/dev/null &
start_server -n
# -s40: silent, IPv4 HTTP 1.0
sleep 1
TRY="`curl -s40 http://localhost:$PORT/testsocket`"
EXPECT='first record'
check
stop_server
testcase "HTTP interface - non-blocking second record" # {{{3
PORT=53843
( echo 'first record' ; sleep 1 ; echo 'second record' ) |
./dgsh-writeval -s testsocket 2>/dev/null &
start_server -n
# -s40: silent, IPv4 HTTP 1.0
sleep 2
TRY="`curl -s40 http://localhost:$PORT/testsocket`"
EXPECT='second record'
check
stop_server
testcase "HTTP interface - binary data" # {{{3
PORT=53843
perl -e 'BEGIN { binmode STDOUT; }
for ($i = 0; $i < 256; $i++) { print chr($i); }' |
./dgsh-writeval -l 256 -s testsocket 2>/dev/null &
start_server -m application/octet-stream
# -s40: silent, IPv4 HTTP 1.0
curl -s40 http://localhost:$PORT/testsocket |
perl -e 'BEGIN { binmode STDIN; }
for ($i = 0; $i < 256; $i++) { $expect .= chr($i); }
read STDIN, $try, 256;
if ($try ne $expect) {
print "FAIL\n";
print "Expected [$expect] got [$try]\n";
} else {
print "OK\n";
}
exit ($try ne $expect);
'
EXITCODE=$?
stop_server
./dgsh-readval -q -s testsocket 2>/dev/null 1>/dev/null
test $EXITCODE = 0 || exit 1
testcase "HTTP interface - large data" # {{{3
PORT=53843
dd if=/dev/zero bs=1M count=1 2>/dev/null |
./dgsh-writeval -l 1000000 -s testsocket 2>/dev/null &
start_server -m application/octet-stream
# -s40: silent, IPv4 HTTP 1.0
BYTES=`curl -s40 http://localhost:$PORT/testsocket |
wc -c`
stop_server
./dgsh-readval -q -s testsocket 2>/dev/null 1>/dev/null
if [ "$BYTES" != 1000000 ]
then
echo FAIL
echo "Expected [1000000 bytes] got [$BYTES bytes]"
exit 1
else
echo "OK"
fi
# Last record tests {{{1
section 'Reading of fixed-length records in stream' # {{{2
(echo -n A12345A7AB; sleep 1; echo -n 12345B7BC; sleep 3; echo -n 12345C7CD) | ./dgsh-writeval -l 9 -s testsocket 2>/dev/null &
testcase "Record one" # {{{3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null`"
EXPECT=A12345A7A
check -n
testcase "Record two" # {{{3
sleep 2
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT=B12345B7B
check -n
testcase "Record three" # {{{3
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null`"
EXPECT=C12345C7C
check
# The socket is no longer there
# Verify that readval doesn't block retrying
./dgsh-readval -nq -s testsocket 2>/dev/null
section 'Reading of newline-separated records in stream' # {{{2
(echo record one; sleep 1; echo record two; sleep 3; echo record three) | ./dgsh-writeval -s testsocket 2>/dev/null &
testcase "Record one" # {{{3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='record one'
check -n
testcase "Record two" # {{{3
sleep 2
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='record two'
check -n
testcase "Record three" # {{{3
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='record three'
check
section 'Non-blocking reading of newline-separated records in stream' # {{{2
(sleep 1 ; echo record one; sleep 2; echo record two; sleep 3; echo record three) | ./dgsh-writeval -s testsocket 2>/dev/null &
testcase "Empty record" # {{{3
TRY="`./dgsh-readval -e -s testsocket 2>/dev/null `"
EXPECT=''
check -n
testcase "Record one" # {{{3
sleep 1
TRY="`./dgsh-readval -e -s testsocket 2>/dev/null `"
EXPECT='record one'
check -n
testcase "Record two" # {{{3
sleep 2
TRY="`./dgsh-readval -e -s testsocket 2>/dev/null `"
EXPECT='record two'
check
section 'Reading last record' # {{{2
testcase "Last record" # {{{3
(echo record one; sleep 1; echo last record) | ./dgsh-writeval -s testsocket 2>/dev/null &
TRY="`./dgsh-readval -l -s testsocket 2>/dev/null `"
EXPECT='last record'
check
testcase "Last record as default" # {{{3
(echo record one; sleep 1; echo last record) | ./dgsh-writeval -s testsocket 2>/dev/null &
TRY="`./dgsh-readval -s testsocket 2>/dev/null `"
EXPECT='last record'
check
testcase "Empty record" # {{{3
./dgsh-writeval -s testsocket 2>/dev/null </dev/null &
TRY="`./dgsh-readval -l -s testsocket 2>/dev/null `"
EXPECT=''
check
testcase "Incomplete record" # {{{3
echo -n unterminated | ./dgsh-writeval -s testsocket 2>/dev/null &
TRY="`./dgsh-readval -l -s testsocket 2>/dev/null `"
EXPECT=''
check
# Window tests {{{1
section 'Window from stream' # {{{2
testcase "Second record" # {{{3
(echo first record ; echo second record ; echo third record; sleep 2) | ./dgsh-writeval -b 2 -e 1 -s testsocket 2>/dev/null &
sleep 1
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='second record'
check
testcase "First record" # {{{3
(echo first record ; echo second record ; echo third record; sleep 2) | ./dgsh-writeval -b 3 -e 2 -s testsocket 2>/dev/null &
sleep 1
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='first record'
check
testcase "Second record" # {{{3
(echo first record ; echo second record ; echo third record; sleep 2) | ./dgsh-writeval -b 3 -e 1 -s testsocket 2>/dev/null &
sleep 1
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='first record
second record'
check
testcase "All records" # {{{3
(echo first record ; echo second record ; echo third record; sleep 2) | ./dgsh-writeval -b 3 -e 0 -s testsocket 2>/dev/null &
sleep 1
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='first record
second record
third record'
check
section 'Window from fixed record stream' # {{{2
testcase "Middle record" # {{{3
(echo -n 000 ; echo -n 011112 ; echo -n 222; sleep 2) | ./dgsh-writeval -l 4 -b 2 -e 1 -s testsocket 2>/dev/null &
sleep 1
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='1111'
check
testcase "First record" # {{{3
(echo -n 000 ; echo -n 011112 ; echo -n 222; sleep 2) | ./dgsh-writeval -l 4 -b 3 -e 2 -s testsocket 2>/dev/null &
sleep 1
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='0000'
check
testcase "First two records" # {{{3
(echo -n 000 ; echo -n 011112 ; echo -n 222; sleep 2) | ./dgsh-writeval -l 4 -b 3 -e 1 -s testsocket 2>/dev/null &
sleep 1
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='00001111'
check
testcase "All records" # {{{3
(echo -n 000 ; echo -n 011112 ; echo -n 222; sleep 2) | ./dgsh-writeval -l 4 -b 3 -e 0 -s testsocket 2>/dev/null &
sleep 1
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='000011112222'
check
# Time window tests {{{1
section 'Time window from terminated record stream' # {{{2
testcase "First record" # {{{3
(echo First record ; sleep 1 ; echo Second--record ; sleep 1 ; echo The third record; sleep 3) | ./dgsh-writeval -u s -b 3.5 -e 2.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='First record'
check
testcase "Middle record" # {{{3
(echo First record ; sleep 1 ; echo Second record ; sleep 1 ; echo The third record; sleep 3) | ./dgsh-writeval -u s -b 2.5 -e 1.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='Second record'
check
testcase "First two records (full buffer)" # {{{3
(echo First record ; sleep 1 ; echo Second--record ; sleep 1 ; echo The third record; sleep 3) | ./dgsh-writeval -u s -b 3.5 -e 1.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='First record
Second--record'
check
testcase "First two records (incomplete buffer)" # {{{3
(echo First record ; sleep 1 ; echo Second record ; sleep 1 ; echo The third record; sleep 3) | ./dgsh-writeval -u s -b 3.5 -e 1.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='First record
Second record'
check
testcase "Last record" # {{{3
(echo First record ; sleep 1 ; echo Second--record ; sleep 1 ; echo The third record; sleep 3) | ./dgsh-writeval -u s -b 1.5 -e 0.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='The third record'
check
testcase "All records" # {{{3
(echo First record ; sleep 1 ; echo Second--record ; sleep 1 ; echo The third record; sleep 3) | ./dgsh-writeval -u s -b 3.5 -e 0.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='First record
Second--record
The third record'
check
testcase "First record after wait" # {{{3
(echo First record ; sleep 1 ; echo Second--record ; sleep 1 ; echo The third record; sleep 3) | ./dgsh-writeval -u s -b 4.5 -e 3.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='First record'
check
section 'Time window from fixed record stream' # {{{2
testcase "Fixed record stream, first record" # {{{3
(echo -n 000 ; sleep 1 ; echo -n 011112 ; sleep 1 ; echo -n 222; sleep 3) | ./dgsh-writeval -l 4 -u s -b 3.5 -e 2.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='0000'
check
testcase "First two records" # {{{3
(echo -n 000 ; sleep 1 ; echo -n 011112 ; sleep 1 ; echo -n 222; sleep 3) | ./dgsh-writeval -l 4 -u s -b 3.5 -e 1.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='000011112222'
check
testcase "Middle record" # {{{3
(echo -n 000 ; sleep 1 ; echo -n 011112 ; sleep 1 ; echo -n 222; sleep 3) | ./dgsh-writeval -l 4 -u s -b 2.5 -e 1.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='11112222'
check
testcase "Last record" # {{{3
(echo -n 000 ; sleep 1 ; echo -n 01111 ; sleep 1 ; echo -n 222; echo -n 2 ; sleep 3) | ./dgsh-writeval -l 4 -u s -b 1.5 -e 0.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='2222'
check
testcase "All records" # {{{3
(echo -n 000 ; sleep 1 ; echo -n 01111 ; sleep 1 ; echo -n 222; echo -n 2 ; sleep 3) | ./dgsh-writeval -l 4 -u s -b 3.5 -e 0.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='000011112222'
check
testcase "First record after wait" # {{{3
(echo -n 000 ; sleep 1 ; echo -n 01111 ; sleep 1 ; echo -n 222; echo -n 2 ; sleep 3) | ./dgsh-writeval -l 4 -u s -b 4.5 -e 3.5 -s testsocket 2>/dev/null &
#Rel 3 2 1
sleep 3
TRY="`./dgsh-readval -c -s testsocket 2>/dev/null `"
EXPECT='0000'
check
section 'Multi-client stress test' # {{{1
echo -n " Running"
if [ ! -r test/sorted-words ]
then
{
cat /usr/share/dict/words 2>/dev/null ||
curl -s https://raw.githubusercontent.com/freebsd/freebsd/master/share/dict/web2
} | head -50000 | sort >test/sorted-words
fi
./dgsh-writeval -s testsocket 2>/dev/null <test/sorted-words &
# Number of parallel clients to run
NUMCLIENTS=5
# Number of read commands to issue
NUMREADS=300
(
for i in `sequence $NUMCLIENTS`
do
for j in `sequence $NUMREADS`
do
./dgsh-readval -c -s testsocket 2>/dev/null
done >read-words-$i &
done
wait
echo
echo " All clients finished"
./dgsh-readval -lq -s testsocket 2>/dev/null 1>/dev/null
echo " Server finished"
)
echo -n " Compare: "
# Wait for the store to terminate
wait
for i in `sequence $NUMCLIENTS`
do
if sort -u read-words-$i | comm -13 test/sorted-words - | grep .
then
fail "Stress test client $i has trash output"
fi
if [ `wc -l < read-words-$i` -ne $NUMREADS ]
then
fail "Stress test client is missing output"
fi
done
rm -f read-words-*
echo "OK"
section 'Time window stress test' # {{{1
echo -n " Running"
# Feed words at a slower pace
perl -pe 'select(undef, undef, undef, 0.001);' test/sorted-words |
./dgsh-writeval -u s -b 3 -e 2 -s testsocket 2>/dev/null &
echo
echo " Server started"
# Number of parallel clients to run
NUMCLIENTS=5
# Number of read commands to issue
NUMREADS=300
echo -n " Running clients"
(
for i in `sequence $NUMCLIENTS`
do
for j in `sequence $NUMREADS`
do
./dgsh-readval -c -s testsocket 2>/dev/null
done >read-words-$i &
done
wait
echo
echo " All clients finished"
./dgsh-readval -q -s testsocket 2>/dev/null 1>/dev/null
echo " Server finished"
)
echo -n " Compare: "
# Wait for the store to terminate
wait
for i in `sequence $NUMCLIENTS`
do
if sort -u read-words-$i | comm -13 test/sorted-words - | grep .
then
fail "Stress test client $i has trash output"
fi
if [ `wc -l < read-words-$i` -lt $NUMREADS ]
then
fail "Stress test client may be missing output"
fi
done
rm -f read-words-*
echo "OK"