-
Notifications
You must be signed in to change notification settings - Fork 2
/
loglinear_test
executable file
·113 lines (100 loc) · 2.63 KB
/
loglinear_test
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
#!/bin/sh
MYDIR=$(dirname "$0")
sequence=$1
result=$2
cd "$MYDIR"
await()
{
while [ ! -e "llt_$1.tmp" ]; do
:
done
}
run()
{
./loglinear ./loglinear_test "$1"
touch "llt_e$1.tmp"
}
check_sequence()
{
while read header magic val; do
echo "check: $header $magic $val"
if [ "$magic" = "START" -o "$magic" = "STOP" ]; then
if [ "$val" = "$1" ]; then
shift
else
echo "FAILED: expected '$1', got '$val'" >&2
return 1
fi
fi
done
if [ $# != 0 ]; then
echo "FAILED: remaining sequence not found: '$*'" >&2
return 2
fi
}
main()
{
# This requires a bit of explanation.
#
# First, remember the goal: we want to print logs that pass first, so that
# failures end up near the end. And "inner" failures come after "outer"
# failures: if a parent and his child both fail, we mostly care about the
# child, because the parent probably only failed because the child failed,
# and the child will have the most specific error message. Other than this
# restriction, we want to generally keep things in chronological order.
#
# So our test works like this:
# 1 runs 2a/2b. 2a runs 3a/3b. 2b runs 3c.
# We print logs from steps that succeed first: that's 2b, 3a, 3b.
# 2b is dragged downwards, however, because its child, 3c, fails, so the
# other two are printed before it.
# So 3a and 3b first. 3b is set to wait for 3a, so 3a prints before 3b.
#
# 1, 2a, and 3c all fail. 3c is owned by 2b, though, which didn't fail,
# so 2b is not as important as 2a, which failed. Thus we print 2b
# before 2a. We print parents before their failing children, so 2b
# comes before 3c.
# Now 1 and 2a are left. They both failed, and 1 is 2a's parent, so
# it comes first, followed by 2a.
#
# Obviously, right?
EXPECT="s3a e3a s3b e3b s2b e2b s3c e3c s1 e1 s2a e2a"
echo
echo
rm -f llt_*.tmp
(
echo -n "MAIN "
run 1 2>&1
) | check_sequence $EXPECT
}
echo "START s$sequence"
touch "llt_s$sequence.tmp"
rv=101
case "$sequence" in
1) run 2a & (await s2a; run 2b) & wait; rv=1 ;;
2a) run 3a & (await s3a; run 3b) & wait; rv=2 ;;
2b) run 3c; rv=0 ;;
3a) rv=0 ;;
3b) await e3a; rv=0 ;;
3c) rv=5 ;;
"")
# we were run with no parameters: initial startup.
# Run main() several times in case the test has race conditions.
if main && main && main && main && main; then
echo
echo "Passed!"
exit 0
else
rv=$?
echo
echo "Failed: code $rv"
exit $rv
fi
;;
*)
echo "Unexpected sequence number: '$sequence'" >&2
exit 98
;;
esac
echo "STOP e$sequence"
exit $rv