From de6ffbdebf264f248b1c78257358d4d69e3db15e Mon Sep 17 00:00:00 2001 From: Fiona Ebner Date: Thu, 14 Nov 2024 16:07:32 +0100 Subject: [PATCH] test: lock file: get rid of END block that made test always pass The exit code of the test would be the exit code of the 'rm' system call, no matter if the test itself failed or not. Use an eval block instead of the END block and propagate the error correctly. Signed-off-by: Fiona Ebner --- test/lock_file.pl | 90 +++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/test/lock_file.pl b/test/lock_file.pl index 6d151ce..4cb8b15 100755 --- a/test/lock_file.pl +++ b/test/lock_file.pl @@ -11,10 +11,6 @@ use PVE::Tools 'lock_file_full'; my $name = "test.lockfile.$$-"; -END { - system("rm $name*"); -}; - # Utilities: sub forked($$) { @@ -78,43 +74,6 @@ sub assert_not { die "code shouldn't have run: $what\n" if $_ran{$what}; } -# Regular lock: -new(); -lock_file_full($name, 10, 0, sub { ran('single lock') }); -assert('single lock'); - -# Lock multiple times in a row: -new(); -lock_file_full($name, 10, 0, sub { ran('lock A') }); -assert('lock A'); -lock_file_full($name, 10, 0, sub { ran('lock B') }); -assert('lock B'); - -# Nested lock: -new(); -lock_file_full($name, 10, 0, sub { - ran('lock A'); - lock_file_full($name, 10, 0, sub { ran('lock B') }); - assert('lock B'); - ran('lock C'); -}); -assert('lock A'); -assert('lock B'); -assert('lock C'); - -# Independent locks: -new(); -lock_file_full($name, 10, 0, sub { - ran('lock A'); - # locks file "${name}2" - lock_file_full($name.2, 10, 0, sub { ran('lock B') }); - assert('lock B'); - ran('lock C'); -}); -assert('lock A'); -assert('lock B'); -assert('lock C'); - # Does it actually lock? (shared=0) # Can we get two simultaneous shared locks? (shared=1) sub forktest1($) { @@ -157,5 +116,50 @@ sub forktest1($) { }; close($fmain); } -forktest1(0); -forktest1(1); + +eval { + # Regular lock: + new(); + lock_file_full($name, 10, 0, sub { ran('single lock') }); + assert('single lock'); + + # Lock multiple times in a row: + new(); + lock_file_full($name, 10, 0, sub { ran('lock A') }); + assert('lock A'); + lock_file_full($name, 10, 0, sub { ran('lock B') }); + assert('lock B'); + + # Nested lock: + new(); + lock_file_full($name, 10, 0, sub { + ran('lock A'); + lock_file_full($name, 10, 0, sub { ran('lock B') }); + assert('lock B'); + ran('lock C'); + }); + assert('lock A'); + assert('lock B'); + assert('lock C'); + + # Independent locks: + new(); + lock_file_full($name, 10, 0, sub { + ran('lock A'); + # locks file "${name}2" + lock_file_full($name.2, 10, 0, sub { ran('lock B') }); + assert('lock B'); + ran('lock C'); + }); + assert('lock A'); + assert('lock B'); + assert('lock C'); + + # Does it actually lock? (shared=0) + # Can we get two simultaneous shared locks? (shared=1) + forktest1(0); + forktest1(1); +}; +my $err = $@; +system("rm $name*"); +die $err if $err;