New utility for the collection: 'buildrun', a rewrite of my previous
[sgt/utils] / buildrun / test.sh
CommitLineData
31f0fd76 1#!/bin/sh
2
3# Test script for buildrun.
4
5# Can be used with my original inotify-based implementation too,
6# provided you pass --old on the command line.
7
8rm -rf testctl
9rm -f testlog
10
11# The inotify-based version wouldn't cope if the control file didn't
12# exist to begin with. Create an empty one.
13if test "x--old" = "x$1"; then touch testctl; fi
14
15testwrite() { # usage: testwrite <identifier> <exitstatus>
16 buildrun -w testctl sh -c "echo write start $1 >> testlog; sleep 2; echo write end $1 returning $2 >> testlog; exit $2"
17}
18testread() { # usage: testread <identifier>
19 echo read start $1 >> testlog
20 buildrun -r testctl sh -c "echo read end $1 >> testlog"
21}
22
23# Test 1: start buildrun -w first, then buildrun -r. The former
24# completes successfully, and the latter should then finish.
25( testwrite 1 0 ) &
26sleep 0.5
27testread 1
28
29# Test 2: same, but this time the first -w fails and we need a second
30# one that succeeds.
31( testwrite 2a 1; sleep 0.5; testwrite 2b 0 ) &
32sleep 0.5
33testread 2
34
35# Test 3: this time we arrange a failure, and then start up a buildrun
36# -r while no -w is running at all, demonstrating that it can block
37# even when there's nothing to block on.
38testwrite 3pre 1
39( testread 3 ) &
40sleep 0.5
41testwrite 3 0
42sleep 0.5
43
44# Test 4: same, but again we have a failure before a success.
45testwrite 4pre 1
46( testread 4 ) &
47sleep 0.5
48testwrite 4a 1; sleep 0.5; testwrite 4b 0
49sleep 0.5
50
51# Test 5: now that no -w run is active and the last one was
52# successful, do a -r run on its own and check that it returns success
53# without blocking.
54testread 5
55
56if diff -u testout.txt testlog; then
57 # Files match. Report success and clear up.
58 echo "Tests passed."
59 rm -rf testctl
60 rm -f testlog
61else
62 # They don't match. Leave the temporary files lying around and
63 # return failure.
64 exit 1
65fi