2 # tests for SQlite-specific features
9 use MogileFS::Util qw(error_code);
12 use POSIX qw(:sys_wait_h);
14 my ($fh, $filename) = File::Temp::tempfile();
16 MogileFS::Config->set_config('db_dsn', "DBI:SQLite:$filename");
17 MogileFS::Config->set_config('db_user', '');
18 MogileFS::Config->set_config('db_pass', '');
19 MogileFS::Config->set_config('max_handles', 0xffffffff);
21 my ($r, $w, $pid, $buf);
22 my $sto = eval { MogileFS::Store->new };
26 plan skip_all => "Can't create temporary test database: $@";
31 is(ref($sto), "MogileFS::Store::SQLite", "store is sane");
32 is($sto->setup_database, 1, "setup database");
34 is(1, pipe($r, $w), "IPC pipe is ready");
36 # normal lock contention
38 fail("fork failed: $!") unless defined $pid;
40 $sto = Mgd::get_store(); # fork-safe
42 $sto->release_lock("test-lock") == 1 or die "released bad lock";
45 $sto->get_lock("test-lock", 1) == 1 or die "child failed to get_lock";
47 syswrite($w, ".") == 1 or die "child failed to wake parent";
52 is(sysread($r, $buf, 1), 1, "child wakes us up");
53 is($buf, ".", "child wakes parent up properly");
54 ok(! $sto->get_lock("test-lock", 1), "fails to lock while child has lock");
55 is(kill(TERM => $pid), 1, "kill successful");
56 is(waitpid($pid, 0), $pid, "waitpid successful");
57 is($?, 0, "child dies correctly");
58 is($sto->get_lock("test-lock", 1), 1, "acquire lock when child dies");
61 # detects recursive lock
62 ok(! eval { $sto->get_lock("test-lock", 1); }, "recursion fails");
63 like($@, qr/Lock recursion detected/i, "proper error on failed lock");
64 is($sto->release_lock("test-lock"), 1, "lock release");
66 is($sto->get_lock("test-lock", 0), 1, "acquire lock with 0 timeout");
67 is($sto->release_lock("test-lock"), 1, "lock release");
68 is($sto->release_lock("test-lock") + 0, 0, "redundant lock release");
72 fail("fork failed: $!") unless defined $pid;
74 $sto = Mgd::get_store(); # fork-safe
75 $sto->get_lock("test-lock", 1) or die "child failed to get_lock";
77 syswrite($w, ".") == 1 or die "child failed to wake parent";
79 $sto->release_lock("test-lock") == 1 or die "child failed to release";
83 is(sysread($r, $buf, 1), 1, "parent woken up");
84 is($buf, ".", "child wakes parent up properly");
85 ok($sto->get_lock("test-lock", 6), "acquire lock eventually");
86 is(waitpid($pid, 0), $pid, "waitpid successful");
87 is($?, 0, "child dies correctly");
88 is($sto->release_lock("test-lock"), 1, "lock release");
92 # kill -9 a lock holder
94 fail("fork failed: $!") unless defined $pid;
96 $sto = Mgd::get_store(); # fork-safe
97 $sto->get_lock("test-lock", 1) or die "child failed to get_lock";
99 syswrite($w, ".") == 1 or die "child failed to wake parent";
104 is(sysread($r, $buf, 1), 1, "parent woken up");
105 is($buf, ".", "child wakes parent up properly");
106 is(kill(KILL => $pid), 1, "kill -9 successful");
107 is(waitpid($pid, 0), $pid, "waitpid successful");
108 ok(WIFSIGNALED($?) && WTERMSIG($?) == 9, "child was SIGKILL-ed");
109 ok($sto->get_lock("test-lock", 1), "acquire lock in parent");