From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id BB58A1FF16B
	for <inbox@lore.proxmox.com>; Thu, 14 Nov 2024 16:08:25 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id B01D533D61;
	Thu, 14 Nov 2024 16:08:05 +0100 (CET)
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu, 14 Nov 2024 16:07:34 +0100
Message-Id: <20241114150754.374376-8-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20241114150754.374376-1-f.ebner@proxmox.com>
References: <20241114150754.374376-1-f.ebner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.055 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH common v4 07/27] test: have lock file test use
 run_fork() helper
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.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