fix util.py/FileLock and lock_test
Do not pass os.O_TRUNC to open(), that immediately empties out the lock file. Fix lock_test to be able to catch this error. So far things were happening too fast to notice it. Caught by running 'make check' on a main unit APU that has different timing behavior. Change-Id: I872a3d2548ac84097ac7acf13cb12c36822e076e
This commit is contained in:
parent
c28769488e
commit
936a81ec1a
|
@ -1,4 +1,14 @@
|
||||||
|
creating files
|
||||||
|
launch a program that locks a given file, it will create $dir/lock_test
|
||||||
|
wait until this lock_test lock file was created by program
|
||||||
|
expecting the lock file to reflect "long name"
|
||||||
launched first, locked by: 'long name'
|
launched first, locked by: 'long name'
|
||||||
|
launching second program, should find the lock intact and wait
|
||||||
launched second, locked by: 'long name'
|
launched second, locked by: 'long name'
|
||||||
|
drop the first lock, $f1 removal signals the first process to stop locking
|
||||||
|
wait for first program to carry out the lock release
|
||||||
|
now expecting second program to lock
|
||||||
waited, locked by: 'shorter'
|
waited, locked by: 'shorter'
|
||||||
|
release the second program also
|
||||||
|
expecting the lock to be gone
|
||||||
waited more, locked by: ''
|
waited more, locked by: ''
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
echo 'creating files'
|
||||||
dir="$(mktemp -d)"
|
dir="$(mktemp -d)"
|
||||||
n1="long name"
|
n1="long name"
|
||||||
f1="$dir/$n1"
|
f1="$dir/$n1"
|
||||||
|
@ -7,21 +9,45 @@ n2="shorter"
|
||||||
f2="$dir/$n2"
|
f2="$dir/$n2"
|
||||||
touch "$f2"
|
touch "$f2"
|
||||||
sync
|
sync
|
||||||
|
|
||||||
|
echo 'launch a program that locks a given file, it will create $dir/lock_test'
|
||||||
python3 ./lock_test_help.py "$dir" "$n1" &
|
python3 ./lock_test_help.py "$dir" "$n1" &
|
||||||
|
|
||||||
|
echo 'wait until this lock_test lock file was created by program'
|
||||||
while [ ! -f "$dir/lock_test" ]; do
|
while [ ! -f "$dir/lock_test" ]; do
|
||||||
sleep .1
|
sleep .1
|
||||||
done
|
done
|
||||||
|
sync
|
||||||
|
|
||||||
|
echo 'expecting the lock file to reflect "long name"'
|
||||||
echo "launched first, locked by: '$(cat "$dir/lock_test")'"
|
echo "launched first, locked by: '$(cat "$dir/lock_test")'"
|
||||||
|
|
||||||
|
echo 'launching second program, should find the lock intact and wait'
|
||||||
python3 ./lock_test_help.py "$dir" "$n2" &
|
python3 ./lock_test_help.py "$dir" "$n2" &
|
||||||
|
while [ ! -f "$f2.ready" ]; do
|
||||||
|
sleep .1
|
||||||
|
done
|
||||||
|
sleep 1
|
||||||
|
sync
|
||||||
echo "launched second, locked by: '$(cat "$dir/lock_test")'"
|
echo "launched second, locked by: '$(cat "$dir/lock_test")'"
|
||||||
|
|
||||||
|
echo 'drop the first lock, $f1 removal signals the first process to stop locking'
|
||||||
rm "$f1"
|
rm "$f1"
|
||||||
|
|
||||||
|
echo 'wait for first program to carry out the lock release'
|
||||||
while [ ! -f "$f1.done" ]; do
|
while [ ! -f "$f1.done" ]; do
|
||||||
sleep .1
|
sleep .1
|
||||||
done
|
done
|
||||||
|
|
||||||
|
echo 'now expecting second program to lock'
|
||||||
echo "waited, locked by: '$(cat "$dir/lock_test")'"
|
echo "waited, locked by: '$(cat "$dir/lock_test")'"
|
||||||
|
|
||||||
|
echo 'release the second program also'
|
||||||
rm "$f2"
|
rm "$f2"
|
||||||
while [ ! -f "$f2.done" ]; do
|
while [ ! -f "$f2.done" ]; do
|
||||||
sleep .1
|
sleep .1
|
||||||
done
|
done
|
||||||
|
|
||||||
|
echo 'expecting the lock to be gone'
|
||||||
echo "waited more, locked by: '$(cat "$dir/lock_test")'"
|
echo "waited more, locked by: '$(cat "$dir/lock_test")'"
|
||||||
rm -rf "$dir"
|
rm -rf "$dir"
|
||||||
|
|
|
@ -8,11 +8,15 @@ from osmo_gsm_tester.util import FileLock, touch_file
|
||||||
|
|
||||||
testdir, name = sys.argv[1:]
|
testdir, name = sys.argv[1:]
|
||||||
stop_signalling_file = os.path.join(testdir, name)
|
stop_signalling_file = os.path.join(testdir, name)
|
||||||
assert os.path.isfile(stop_signalling_file)
|
if not os.path.isfile(stop_signalling_file):
|
||||||
|
print('expected a stop-file %r' % stop_signalling_file)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
lockfile_path = os.path.join(testdir, 'lock_test')
|
lockfile_path = os.path.join(testdir, 'lock_test')
|
||||||
fl = FileLock(lockfile_path, name)
|
fl = FileLock(lockfile_path, name)
|
||||||
|
|
||||||
|
touch_file(stop_signalling_file + '.ready')
|
||||||
|
|
||||||
with fl:
|
with fl:
|
||||||
while os.path.exists(stop_signalling_file):
|
while os.path.exists(stop_signalling_file):
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
|
|
|
@ -101,7 +101,7 @@ class FileLock:
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
if self.f is not None:
|
if self.f is not None:
|
||||||
return
|
return
|
||||||
self.fd = os.open(self.path, os.O_CREAT | os.O_WRONLY | os.O_TRUNC)
|
self.fd = os.open(self.path, os.O_CREAT | os.O_WRONLY)
|
||||||
fcntl.flock(self.fd, fcntl.LOCK_EX)
|
fcntl.flock(self.fd, fcntl.LOCK_EX)
|
||||||
os.truncate(self.fd, 0)
|
os.truncate(self.fd, 0)
|
||||||
os.write(self.fd, str(self.owner).encode('utf-8'))
|
os.write(self.fd, str(self.owner).encode('utf-8'))
|
||||||
|
|
Loading…
Reference in New Issue