From 6242da34fbe20940e3e258d2ec80e9aeef0438e1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 3 Oct 2019 07:21:59 +0000 Subject: init: implement locking First, we use flock(2) to wait on parallel public-inbox-init(1) invocations while we make multiple changes using git-config(1). This flock allows -init processes to wait on each other if using reasonable POSIX filesystems. Then, we also need a git-config(1)-compatible lock to prevent user-invoked git-config(1) processes from clobbering our changes while we're holding the flock. --- t/init.t | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 't') diff --git a/t/init.t b/t/init.t index 667b09fe..0cd6f31f 100644 --- a/t/init.t +++ b/t/init.t @@ -10,6 +10,8 @@ my $tmpdir = tempdir('pi-init-XXXXXX', TMPDIR => 1, CLEANUP => 1); use constant pi_init => 'blib/script/public-inbox-init'; use PublicInbox::Import; use File::Basename; +use PublicInbox::Spawn qw(spawn); +use Cwd qw(getcwd); open my $null, '>>', '/dev/null'; my $rdr = { 2 => fileno($null) }; sub quiet_fail { @@ -47,6 +49,16 @@ sub quiet_fail { @cmd = (pi_init, 'clist', '-V2', "$tmpdir/clist", qw(http://example.com/clist clist@example.com)); quiet_fail(\@cmd, 'attempting to init V2 from V1 fails'); + + open my $lock, '+>', "$cfgfile.lock" or die; + @cmd = (getcwd(). '/'. pi_init, 'lock', "$tmpdir/lock", + qw(http://example.com/lock lock@example.com)); + ok(-e "$cfgfile.lock", 'lock exists'); + my $pid = spawn(\@cmd, undef, $rdr); + is(waitpid($pid, 0), $pid, 'lock init failed'); + is($? >> 8, 255, 'got expected exit code on lock failure'); + ok(unlink("$cfgfile.lock"), + '-init did not unlink lock on failure'); } SKIP: { -- cgit v1.2.3-24-ge0c7