public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH common v4 07/27] test: have lock file test use run_fork() helper
Date: Thu, 14 Nov 2024 16:07:34 +0100	[thread overview]
Message-ID: <20241114150754.374376-8-f.ebner@proxmox.com> (raw)
In-Reply-To: <20241114150754.374376-1-f.ebner@proxmox.com>

There are differences for error handling in the old and new helper, in
particular when the child is signaled. But the test here does not
explicitly check for specific error conditions or messages and is
therefore still correct with the new helper.

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
---

New in v4.

Diff is better viewed with
--color-moved=zebra --color-moved-ws=ignore-all-space

 test/lock_file.pl | 110 +++++++++++++++-------------------------------
 1 file changed, 35 insertions(+), 75 deletions(-)

diff --git a/test/lock_file.pl b/test/lock_file.pl
index 4cb8b15..cd3f433 100755
--- a/test/lock_file.pl
+++ b/test/lock_file.pl
@@ -11,50 +11,6 @@ use PVE::Tools 'lock_file_full';
 
 my $name = "test.lockfile.$$-";
 
-# Utilities:
-
-sub forked($$) {
-    my ($code1, $code2) = @_;
-
-    pipe(my $except_r, my $except_w) or die "pipe: $!\n";
-
-    my $pid = fork();
-    die "fork failed: $!\n" if !defined($pid);
-
-    if ($pid == 0) {
-	close($except_r);
-	eval { $code1->() };
-	if ($@) {
-	    print {$except_w} $@;
-	    $except_w->flush();
-	    POSIX::_exit(1);
-	}
-	POSIX::_exit(0);
-    }
-    close($except_w);
-
-    eval { $code2->() };
-    my $err = $@;
-    if ($err) {
-	kill(15, $pid);
-    } else {
-	my $err = do { local $/ = undef; <$except_r> };
-    }
-    die "interrupted\n" if waitpid($pid, 0) != $pid;
-    die $err if $err;
-
-    # Check exit code:
-    my $status = POSIX::WEXITSTATUS($?);
-    if ($? == -1) {
-	die "failed to execute\n";
-    } elsif (POSIX::WIFSIGNALED($?)) {
-	my $sig = POSIX::WTERMSIG($?);
-	die "got signal $sig\n";
-    } elsif ($status != 0) {
-	die "exit code $status\n";
-    }
-}
-
 # Book-keeping:
 
 my %_ran;
@@ -82,38 +38,42 @@ sub forktest1($) {
     # socket pair for synchronization
     socketpair(my $fmain, my $fother, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
 	or die "socketpair(): $!\n";
-    forked sub {
-	    # other side
-	    close($fmain);
-	    my $line;
-	    lock_file_full($name, 60, $shared, sub {
-		ran('other side');
-		# tell parent we've acquired the lock
-		print {$fother} "1\n";
-		$fother->flush();
-		# wait for parent to be done trying to lock
-		$line = <$fother>;
-	    });
-	    die $@ if $@;
-	    die "parent failed\n" if !$line || $line ne "2\n";
-	    assert('other side');
-    }, sub {
-	    # main process
-	    # Wait for our child to lock:
-	    close($fother);
-	    my $line = <$fmain>;
-	    die "child failed to acquire a lock\n" if !$line || $line ne "1\n";
-	    lock_file_full($name, 1, $shared, sub {
-		ran('local side');
-	    });
-	    if ($shared) {
-		assert('local side');
-	    } else {
-		assert_not('local side');
-	    }
-	    print {$fmain} "2\n";
-	    $fmain->flush();
+    my $other = sub {
+	# other side
+	close($fmain);
+	my $line;
+	lock_file_full($name, 60, $shared, sub {
+	    ran('other side');
+	    # tell parent we've acquired the lock
+	    print {$fother} "1\n";
+	    $fother->flush();
+	    # wait for parent to be done trying to lock
+	    $line = <$fother>;
+	});
+	die $@ if $@;
+	die "parent failed\n" if !$line || $line ne "2\n";
+	assert('other side');
+	return;
     };
+    my $main = sub {
+	# main process
+	# Wait for our child to lock:
+	close($fother);
+	my $line = <$fmain>;
+	die "child failed to acquire a lock\n" if !$line || $line ne "1\n";
+	lock_file_full($name, 1, $shared, sub {
+	    ran('local side');
+	});
+	if ($shared) {
+	    assert('local side');
+	} else {
+	    assert_not('local side');
+	}
+	print {$fmain} "2\n";
+	$fmain->flush();
+    };
+
+    PVE::Tools::run_fork($other, { afterfork => $main });
     close($fmain);
 }
 
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2024-11-14 15:08 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-14 15:07 [pve-devel] [PATCH-SERIES qemu/common/storage/qemu-server/container/manager v4 00/27] backup provider API Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu v4 01/27] PVE backup: add target ID in backup state Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu v4 02/27] PVE backup: get device info: allow caller to specify filter for which devices use fleecing Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu v4 03/27] PVE backup: implement backup access setup and teardown API for external providers Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu v4 04/27] PVE backup: implement bitmap support for external backup access Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH common v4 05/27] test: lock file: get rid of END block that made test always pass Fiona Ebner
2024-11-14 19:46   ` [pve-devel] applied: " Thomas Lamprecht
2024-11-14 15:07 ` [pve-devel] [PATCH common v4 06/27] tools: run fork: allow running code in parent after fork Fiona Ebner
2024-11-14 19:46   ` [pve-devel] applied: " Thomas Lamprecht
2024-11-14 15:07 ` Fiona Ebner [this message]
2024-11-14 19:46   ` [pve-devel] applied: [PATCH common v4 07/27] test: have lock file test use run_fork() helper Thomas Lamprecht
2024-11-14 15:07 ` [pve-devel] [PATCH storage v4 08/27] add storage_has_feature() helper function Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH storage v4 09/27] plugin: introduce new_backup_provider() method Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH storage v4 10/27] extract backup config: delegate to backup provider for storages that support it Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [POC storage v4 11/27] add backup provider example Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [POC storage v4 12/27] WIP Borg plugin Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu-server v4 13/27] backup: keep track of block-node size for fleecing Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu-server v4 14/27] module config: load nbd module at boot Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu-server v4 15/27] backup: allow adding fleecing images also for EFI and TPM Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu-server v4 16/27] backup: implement backup for external providers Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu-server v4 17/27] backup: implement restore " Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH qemu-server v4 18/27] backup restore: external: hardening check for untrusted source image Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH container v4 19/27] add LXC::Namespaces module Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH container v4 20/27] backup: implement backup for external providers Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH container v4 21/27] backup: implement restore " Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH container v4 22/27] external restore: don't use 'one-file-system' tar flag when restoring from a directory Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH container v4 23/27] create: factor out compression option helper Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH container v4 24/27] restore tar archive: check potentially untrusted archive Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH container v4 25/27] api: add early check against restoring privileged container from external source Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH manager v4 26/27] ui: backup: also check for backup subtype to classify archive Fiona Ebner
2024-11-14 15:07 ` [pve-devel] [PATCH manager v4 27/27] backup: implement backup for external providers Fiona Ebner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241114150754.374376-8-f.ebner@proxmox.com \
    --to=f.ebner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal