-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary_test.sh
executable file
·59 lines (50 loc) · 1.23 KB
/
library_test.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
#!/bin/bash
source ./library.sh
MAX_ITERATIONS=3
prepare() {
TMP_DIR=`mktemp -d -p /tmp/`
trap 'cleanup' EXIT
}
cleanup() {
rm -rf $TMP_DIR
}
# Test Cases
should_handle_fixed_delay() {
before=`date +%s`
wait_for --function fail_nth_times
after=`date +%s`
diff=$((after-before))
if [ $diff -lt 3 ] || [ $diff -gt 4 ]; then
echo "FAIL: Should take almost 3 seconds, but took: $diff!"
exit 1
fi
}
should_handle_exponential_backoff() {
# With exponential backoff of 2: 1 + 2 + 4 + 8 = 15.
before=`date +%s`
wait_for --function fail_nth_times --backoff-multiplier 2 --retry-delay 1
after=`date +%s`
diff=$((after-before))
if [ $diff -lt 15 ] || [ $diff -gt 16 ]; then
echo "FAIL: Should take almost 15 seconds, but took: $diff!"
exit 1
fi
}
# Utilities
fail_nth_times() {
if [ ! -f "$TMP_DIR/iterations" ]; then
echo 0 > $TMP_DIR/iterations
else
value=`cat $TMP_DIR/iterations`
if [ $value -eq $MAX_ITERATIONS ];then
echo "true"
else
value=$((value+1))
echo $value > $TMP_DIR/iterations
fi
fi
}
prepare
should_handle_fixed_delay
should_handle_exponential_backoff
cleanup