]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: prepare flock for testing --fcntl
authorRasmus Villemoes <rasmus.villemoes@prevas.dk>
Wed, 8 May 2024 13:16:10 +0000 (15:16 +0200)
committerRasmus Villemoes <rasmus.villemoes@prevas.dk>
Wed, 8 May 2024 13:48:41 +0000 (15:48 +0200)
Prepare the test script for doing all the same things, but with
--fcntl in effect. The diff is much smaller when viewed with
git show -w.

tests/expected/misc/flock
tests/ts/misc/flock

index 9c973962ac065d0ff1efc32dabb6552779f83bcb..913e35ef9d7295b4c78e0c3543eeec859ff4b3c2 100644 (file)
@@ -1,3 +1,4 @@
+API: flock(2)
 Locking
 Unlocking
 Unlocked
index 0c6ac0bec544520f61153c9475d001d1413d2713..974e1ba089f261c4c93ff3f6ad2b66a1304e451c 100755 (executable)
@@ -41,84 +41,94 @@ function do_lock {
        fi
 }
 
-# general lock
-GEN_OUTPUT="$TS_OUTPUT"
-START=$(date '+%s')
-# running flock in background is not the best usage example
-$TS_CMD_FLOCK --shared $TS_OUTDIR/lockfile \
-       bash -c 'echo "Locking"; sleep 3; echo "Unlocking"' \
-       > $GEN_OUTPUT 2>&1 &
-pid=$!
-
-# check for running background process
-if [ "$pid" -le "0" ] || ! kill -s 0 "$pid" &>/dev/null; then
-       ts_die "unable to run flock"
-fi
-# the lock should be established when flock has a child
-timeout 1s bash -c "while ! pgrep -P $pid >/dev/null; do sleep 0.1 ;done" \
-       || ts_die "timeout waiting for flock child"
+for api in flock ; do
+       case $api in
+               (flock)
+                       subtest_prefix=""
+                       api_arg="" ;;
+       esac
+
+       # general lock
+       GEN_OUTPUT="$TS_OUTPUT"
+       START=$(date '+%s')
+       # running flock in background is not the best usage example
+       $TS_CMD_FLOCK $api_arg --shared $TS_OUTDIR/lockfile \
+                     bash -c "echo 'API: $api(2)'; echo 'Locking'; sleep 3; echo 'Unlocking'" \
+                     >> $GEN_OUTPUT 2>&1 &
+       pid=$!
+
+       # check for running background process
+       if [ "$pid" -le "0" ] || ! kill -s 0 "$pid" &>/dev/null; then
+               ts_die "unable to run flock"
+       fi
+       # the lock should be established when flock has a child
+       timeout 1s bash -c "while ! pgrep -P $pid >/dev/null; do sleep 0.1 ;done" \
+               || ts_die "timeout waiting for flock child"
 
-ts_init_subtest "non-block"
-do_lock "--nonblock --conflict-exit-code 123" 123 "You will never see this!"
-ts_finalize_subtest
+       ts_init_subtest "${subtest_prefix}non-block"
+       do_lock "${api_arg} --nonblock --conflict-exit-code 123" 123 "You will never see this!"
+       ts_finalize_subtest
 
 
-ts_init_subtest "no-fork"
-do_lock "--no-fork --nonblock --conflict-exit-code 123" 123 "You will never see this!"
-ts_finalize_subtest
+       ts_init_subtest "${subtest_prefix}no-fork"
+       do_lock "${api_arg} --no-fork --nonblock --conflict-exit-code 123" 123 "You will never see this!"
+       ts_finalize_subtest
 
 
-ts_init_subtest "shared"
-do_lock "--shared" 0 "Have shared lock"
-ts_finalize_subtest
+       ts_init_subtest "${subtest_prefix}shared"
+       do_lock "${api_arg} --shared" 0 "Have shared lock"
+       ts_finalize_subtest
 
 
-# this is the same as non-block test (exclusive lock is the default), but here
-# we explicitly specify --exclusive on command line
-ts_init_subtest "exclusive"
-do_lock "--nonblock --exclusive --conflict-exit-code 123" 123 "You will never see this!"
-ts_finalize_subtest
+       # this is the same as non-block test (exclusive lock is the default), but here
+       # we explicitly specify --exclusive on command line
+       ts_init_subtest "${subtest_prefix}exclusive"
+       do_lock "${api_arg} --nonblock --exclusive --conflict-exit-code 123" 123 "You will never see this!"
+       ts_finalize_subtest
 
 
-ts_init_subtest "fd"
-cd "$TS_OUTDIR"
-rm 4 2> /dev/null
-exec 4<>$TS_OUTDIR/lockfile || ts_log "Could not open lockfile"
-$TS_CMD_FLOCK --nonblock --exclusive --conflict-exit-code 123 4 \
-       >> $TS_OUTPUT 2>> $TS_ERRLOG
+       ts_init_subtest "${subtest_prefix}fd"
+       cd "$TS_OUTDIR"
+       rm 4 2> /dev/null
+       exec 4<>$TS_OUTDIR/lockfile || ts_log "Could not open lockfile"
+       $TS_CMD_FLOCK $api_arg --nonblock --exclusive --conflict-exit-code 123 4 \
+                     >> $TS_OUTPUT 2>> $TS_ERRLOG
 
-rc="$?"
+       rc="$?"
 
-if [  "$rc" == "123" ]; then
-       ts_log "Success"
-else
-       ts_log "Failed [rc=$rc]"
-fi
-[ -f 4 ] && ts_log "fd file should not exist"
-ts_finalize_subtest
+       if [  "$rc" == "123" ]; then
+               ts_log "Success"
+       else
+               ts_log "Failed [rc=$rc]"
+       fi
+       [ -f 4 ] && ts_log "fd file should not exist"
+       ts_finalize_subtest
+
+
+       ts_init_subtest "${subtest_prefix}timeout"
+       do_lock "${api_arg} --timeout 5 --conflict-exit-code 5" 0 "After timeout."
+       END=$(date '+%s')
+       ts_finalize_subtest
+
+
+       # expected is 3 seconds (see "sleep 3" for the general lock), but we should not
+       # rely on exact number due to scheduler, machine load, etc. Let's check for
+       # inmterval <3,5>.
+       #
+       ts_init_subtest "${subtest_prefix}time-check"
+       TIMEDIFF=$(( $END - $START ))
+       if [ $TIMEDIFF -lt 3 ]; then
+               ts_log "general lock failed [$TIMEDIFF sec]"
+       elif [ $TIMEDIFF -gt 5 ]; then
+               ts_log "wait too long [$TIMEDIFF sec]"
+       else
+               ts_log "success"
+       fi
+       ts_finalize_subtest "diff ${TIMEDIFF} sec"
 
 
-ts_init_subtest "timeout"
-do_lock "--timeout 5 --conflict-exit-code 5" 0 "After timeout."
-END=$(date '+%s')
-ts_finalize_subtest
+       echo "Unlocked" >> $GEN_OUTPUT
 
+done
 
-# expected is 3 seconds (see "sleep 3" for the general lock), but we should not
-# rely on exact number due to scheduler, machine load, etc. Let's check for
-# inmterval <3,5>.
-#
-ts_init_subtest "time-check"
-TIMEDIFF=$(( $END - $START ))
-if [ $TIMEDIFF -lt 3 ]; then
-       ts_log "general lock failed [$TIMEDIFF sec]"
-elif [ $TIMEDIFF -gt 5 ]; then
-       ts_log "wait too long [$TIMEDIFF sec]"
-else
-       ts_log "success"
-fi
-ts_finalize_subtest "diff ${TIMEDIFF} sec"
-
-
-echo "Unlocked" >> $GEN_OUTPUT
 ts_finalize