line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Fork; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
1893
|
use strict; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
87
|
|
4
|
3
|
|
|
3
|
|
9
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
159
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.01_01'; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
36
|
use base 'Test::Builder::Module'; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
1011
|
|
9
|
|
|
|
|
|
|
our @EXPORT = qw(fork_ok); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $CLASS = __PACKAGE__; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Test::Fork - test code which forks |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Test::More tests => 5; |
21
|
|
|
|
|
|
|
use Test::Fork; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
fork_ok(2, sub{ |
24
|
|
|
|
|
|
|
is 23, 42; |
25
|
|
|
|
|
|
|
is 42, 23; |
26
|
|
|
|
|
|
|
}); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
is 99, 23; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 DESCRIPTION |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
B The implementation is unreliable and the interface |
33
|
|
|
|
|
|
|
is subject to change. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Because each test has a number associated with it, testing code which forks |
36
|
|
|
|
|
|
|
is problematic. Coordinating the test number amongst the parent and child |
37
|
|
|
|
|
|
|
processes is complicated. Test::Fork provides a function to smooth over |
38
|
|
|
|
|
|
|
the complications. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=head2 Functions |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Each function is exported by default. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head3 B |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
fork_ok( $num_tests, sub { |
47
|
|
|
|
|
|
|
...child test code... |
48
|
|
|
|
|
|
|
}); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
Runs the given child test code in a forked process. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
$num_tests is the number of tests in your child test code. |
53
|
|
|
|
|
|
|
Consider it to be a sub-plan. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
fork_ok() itself is a test, if the fork fails it will fail. fork_ok() |
56
|
|
|
|
|
|
|
test does not count towards your $num_tests. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
fork_ok( 2, sub { |
59
|
|
|
|
|
|
|
is $foo, $bar; |
60
|
|
|
|
|
|
|
ok Something->method; |
61
|
|
|
|
|
|
|
}); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=cut |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my %running_children; |
66
|
|
|
|
|
|
|
my $Is_Child = 0; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub fork_ok ($&) { |
69
|
5
|
|
|
5
|
1
|
2497
|
my($num_tests, $child_sub) = @_; |
70
|
|
|
|
|
|
|
|
71
|
5
|
|
|
|
|
16
|
my $tb = $CLASS->builder; |
72
|
5
|
|
|
|
|
4773
|
my $pid = fork; |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
# Failed fork |
75
|
5
|
50
|
|
|
|
256
|
if( !defined $pid ) { |
|
|
100
|
|
|
|
|
|
76
|
0
|
|
|
|
|
0
|
return $tb->ok(0, "fork() failed: $!"); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
# Parent |
79
|
|
|
|
|
|
|
elsif( $pid ) { |
80
|
3
|
|
|
|
|
160
|
$tb->use_numbers(0); |
81
|
3
|
|
|
|
|
131
|
$running_children{$pid}++; |
82
|
3
|
|
|
|
|
60
|
$tb->current_test($tb->current_test + $num_tests); |
83
|
|
|
|
|
|
|
|
84
|
3
|
|
|
|
|
442
|
return $tb->ok(1, "fork() succeeded"); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# Child |
88
|
2
|
|
|
|
|
45
|
$Is_Child = 1; |
89
|
|
|
|
|
|
|
|
90
|
2
|
|
|
|
|
84
|
$tb->use_numbers(0); |
91
|
2
|
|
|
|
|
63
|
$tb->no_ending(1); |
92
|
|
|
|
|
|
|
|
93
|
2
|
|
|
|
|
58
|
$child_sub->(); |
94
|
2
|
|
|
|
|
1862
|
exit; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
END { |
98
|
|
|
|
|
|
|
while( !$Is_Child and keys %running_children ) { |
99
|
|
|
|
|
|
|
_reaper(); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _reaper { |
104
|
2
|
|
|
2
|
|
87314
|
my $child_pid = wait; |
105
|
2
|
|
|
|
|
16
|
delete $running_children{$child_pid}; |
106
|
|
|
|
|
|
|
|
107
|
2
|
100
|
|
|
|
27752
|
$CLASS->builder->use_numbers(1) unless keys %running_children; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$SIG{CHLD} = \&_reaper; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 CAVEATS |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
The failure of tests in a child process cannot be detected by the parent. |
116
|
|
|
|
|
|
|
Therefore, the normal end-of-test reporting done by Test::Builder will |
117
|
|
|
|
|
|
|
not notice failed child tests. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Test::Fork turns off test numbering in order to avoid test counter |
120
|
|
|
|
|
|
|
coordination issues. It turns it back on once the children are done |
121
|
|
|
|
|
|
|
running. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 SEE ALSO |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
L |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 AUTHOR |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Michael G Schwern Eschwern@pobox.comE |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 BUGS and FEEDBACK |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Please send all bugs and feature requests to |
137
|
|
|
|
|
|
|
I at I or use the web interface via |
138
|
|
|
|
|
|
|
L. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head1 COPYRIGHT and LICENSE |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
Copyright 2007 by Michael G Schwern Eschwern@pobox.comE. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or |
146
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
See F |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
42; |