6 use RT::Test tests => 35;
9 use RT::Util qw(safe_run_child);
15 my $res = safe_run_child { return 1 };
16 is $res, 1, "correct return value";
25 $context = 'array'; return 1, 2, 3;
26 } elsif ( defined wantarray ) {
27 $context = 'scalar'; return 'foo';
28 } elsif ( !wantarray ) {
29 $context = 'void'; return;
32 is_deeply [ safe_run_child { $sub->(@_) } ], [1, 2, 3];
36 is scalar safe_run_child {$sub->(@_)}, 'foo';
37 is $context, 'scalar';
40 safe_run_child {$sub->(@_)};
47 my $res = safe_run_child {
48 if (fork) { wait; return 'parent' }
50 open my $fh, '>', RT::Test->temp_directory .'/tttt';
56 is $res, 'parent', "correct return value";
57 is( RT::Test->file_content([RT::Test->temp_directory, 'tttt'], unlink => 1 ),
59 'correct file content',
67 my $res = safe_run_child {
68 if (fork) { wait; return 'parent' }
70 open my $fh, '>', RT::Test->temp_directory .'/tttt';
76 is $res, 'parent', "correct return value";
77 is( RT::Test->file_content([RT::Test->temp_directory, 'tttt'], unlink => 1 ),
79 'correct file content',
81 } qr/System Error: child/;
87 my $res = safe_run_child {
88 if (fork) { wait; return 'parent' }
90 open my $fh, '>', RT::Test->temp_directory .'/tttt';
96 is $res, 'parent', "correct return value";
97 is( RT::Test->file_content([RT::Test->temp_directory, 'tttt'], unlink => 1 ),
99 'correct file content',
106 my $res = eval { safe_run_child { die 'parent'; } };
107 is $res, undef, "correct return value";
108 like $@, qr'System Error: parent', "correct error message value";
114 my $script = RT::Test->temp_directory .'/true.pl';
115 open my $fh, '>', $script;
119 open my \$fh, '>', '$script.res';
128 my $res = safe_run_child {
129 if (fork) { wait; return 'parent' }
132 is $res, 'parent', "correct return value";
133 is( RT::Test->file_content([$script .'.res'], unlink => 1 ),
135 'correct file content',
140 # fork+parent that doesn't wait()
143 my $start = Time::HiRes::time();
146 # Set up a poor man's semaphore
148 $SIG{USR1} = sub {$all_set++};
150 my $res = safe_run_child {
151 if ($pid = fork) { return 'parent' }
153 open my $fh, '>', RT::Test->temp_directory .'/first';
156 # Signal that the first file is now all set; we need to do this
157 # to avoid a race condition
158 kill POSIX::SIGUSR1(), getppid();
162 open $fh, '>', RT::Test->temp_directory .'/second';
168 ok( Time::HiRes::time() - $start < 5, "Didn't wait until child finished" );
170 # Wait for up to 3 seconds to get signaled that the child has made
171 # the file (the USR1 will break out of the sleep()). This _should_
172 # be immediate, but there's a race between the parent and child
173 # here, since there's no wait()'ing. There's still a tiny race
174 # where the signal could come in betwene the $all_set check and the
175 # sleep, but that just means we sleep for 3 seconds uselessly.
176 sleep 3 unless $all_set;
178 is $res, 'parent', "correct return value";
179 is( RT::Test->file_content([RT::Test->temp_directory, 'first'], unlink => 1 ),
181 'correct file content',
183 ok( not(-f RT::Test->temp_directory.'/second'), "Second file does not exist yet");
186 ok(waitpid($pid,0), "Waited until child finished to reap");
187 is( RT::Test->file_content([RT::Test->temp_directory, 'second'], unlink => 1 ),
189 'correct file content',
195 local $Test::Builder::Level = $Test::Builder::Level + 1;
196 my $test = $RT::Handle->dbh->selectall_arrayref(
197 "SELECT id FROM Users WHERE Name = 'Nobody'"
199 ok $test && $test->[0][0], "selected, DB is there";