| 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 | |
| 8 | rm -rf testctl |
| 9 | rm -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. |
| 13 | if test "x--old" = "x$1"; then touch testctl; fi |
| 14 | |
| 15 | testwrite() { # 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 | } |
| 18 | testread() { # 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 ) & |
| 26 | sleep 0.5 |
| 27 | testread 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 ) & |
| 32 | sleep 0.5 |
| 33 | testread 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. |
| 38 | testwrite 3pre 1 |
| 39 | ( testread 3 ) & |
| 40 | sleep 0.5 |
| 41 | testwrite 3 0 |
| 42 | sleep 0.5 |
| 43 | |
| 44 | # Test 4: same, but again we have a failure before a success. |
| 45 | testwrite 4pre 1 |
| 46 | ( testread 4 ) & |
| 47 | sleep 0.5 |
| 48 | testwrite 4a 1; sleep 0.5; testwrite 4b 0 |
| 49 | sleep 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. |
| 54 | testread 5 |
| 55 | |
| 56 | if 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 |
| 61 | else |
| 62 | # They don't match. Leave the temporary files lying around and |
| 63 | # return failure. |
| 64 | exit 1 |
| 65 | fi |