Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 1 | #!/bin/sh |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 2 | |
| 3 | echo 'creating files' |
Neels Hofmeyr | 417a03d | 2017-05-04 14:48:49 +0200 | [diff] [blame] | 4 | dir="$(mktemp -d)" |
| 5 | n1="long name" |
| 6 | f1="$dir/$n1" |
| 7 | touch "$f1" |
| 8 | n2="shorter" |
| 9 | f2="$dir/$n2" |
| 10 | touch "$f2" |
| 11 | sync |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 12 | |
| 13 | echo 'launch a program that locks a given file, it will create $dir/lock_test' |
Neels Hofmeyr | 417a03d | 2017-05-04 14:48:49 +0200 | [diff] [blame] | 14 | python3 ./lock_test_help.py "$dir" "$n1" & |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 15 | |
| 16 | echo 'wait until this lock_test lock file was created by program' |
Neels Hofmeyr | 417a03d | 2017-05-04 14:48:49 +0200 | [diff] [blame] | 17 | while [ ! -f "$dir/lock_test" ]; do |
| 18 | sleep .1 |
| 19 | done |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 20 | sync |
| 21 | |
| 22 | echo 'expecting the lock file to reflect "long name"' |
Neels Hofmeyr | a6278b7 | 2017-05-10 13:46:11 +0200 | [diff] [blame] | 23 | echo "launched first, locked by: '$(cat "$dir/lock_test")'" |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 24 | |
| 25 | echo 'launching second program, should find the lock intact and wait' |
Neels Hofmeyr | 417a03d | 2017-05-04 14:48:49 +0200 | [diff] [blame] | 26 | python3 ./lock_test_help.py "$dir" "$n2" & |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 27 | while [ ! -f "$f2.ready" ]; do |
| 28 | sleep .1 |
| 29 | done |
| 30 | sleep 1 |
| 31 | sync |
Neels Hofmeyr | a6278b7 | 2017-05-10 13:46:11 +0200 | [diff] [blame] | 32 | echo "launched second, locked by: '$(cat "$dir/lock_test")'" |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 33 | |
| 34 | echo 'drop the first lock, $f1 removal signals the first process to stop locking' |
Neels Hofmeyr | 417a03d | 2017-05-04 14:48:49 +0200 | [diff] [blame] | 35 | rm "$f1" |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 36 | |
| 37 | echo 'wait for first program to carry out the lock release' |
Neels Hofmeyr | 417a03d | 2017-05-04 14:48:49 +0200 | [diff] [blame] | 38 | while [ ! -f "$f1.done" ]; do |
| 39 | sleep .1 |
| 40 | done |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 41 | |
| 42 | echo 'now expecting second program to lock' |
Neels Hofmeyr | a6278b7 | 2017-05-10 13:46:11 +0200 | [diff] [blame] | 43 | echo "waited, locked by: '$(cat "$dir/lock_test")'" |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 44 | |
| 45 | echo 'release the second program also' |
Neels Hofmeyr | 417a03d | 2017-05-04 14:48:49 +0200 | [diff] [blame] | 46 | rm "$f2" |
| 47 | while [ ! -f "$f2.done" ]; do |
| 48 | sleep .1 |
| 49 | done |
Neels Hofmeyr | 936a81e | 2017-09-14 01:31:41 +0200 | [diff] [blame] | 50 | |
| 51 | echo 'expecting the lock to be gone' |
Neels Hofmeyr | a6278b7 | 2017-05-10 13:46:11 +0200 | [diff] [blame] | 52 | echo "waited more, locked by: '$(cat "$dir/lock_test")'" |
Neels Hofmeyr | 417a03d | 2017-05-04 14:48:49 +0200 | [diff] [blame] | 53 | rm -rf "$dir" |