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