File: Fork.pm

package info (click to toggle)
libtest-fork-perl 0.02-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 84 kB
  • sloc: perl: 135; makefile: 2
file content (191 lines) | stat: -rw-r--r-- 4,110 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package Test::Fork;

use strict;
use warnings;

our $VERSION = '0.02';

use base 'Test::Builder::Module';
our @EXPORT = qw(fork_ok);

my $CLASS = __PACKAGE__;


sub note {
    my $msg = shift;
    my $fh = $CLASS->builder->output;

    print $fh "# $msg\n";
}


=head1 NAME

Test::Fork - test code which forks

=head1 SYNOPSIS

    use Test::More tests => 4;
    use Test::Fork;
    
    fork_ok(2, sub{
        pass("Test in the child process");
        pass("Another test in the child process");
    });
    
    pass("Test in the parent");

=head1 DESCRIPTION

B<THIS IS ALPHA CODE!>  The implementation is unreliable and the interface
is subject to change.

Because each test has a number associated with it, testing code which forks
is problematic.  Coordinating the test number amongst the parent and child
processes is complicated.  Test::Fork provides a function to smooth over
the complications.

=head2 Functions

Each function is exported by default.

=head3 B<fork_ok>

    my $child_pid = fork_ok( $num_tests, sub {
        ...child test code...
    });

Runs the given child test code in a forked process.  Returns the pid of the
forked child process, or false if the fork fails.

$num_tests is the number of tests in your child test code.
Consider it to be a sub-plan.

fork_ok() itself is a test, if the fork fails it will fail.  fork_ok()
test does not count towards your $num_tests.

    # This is three tests.
    fork_ok( 2, sub {
        is $foo, $bar;
        ok Something->method;
    });

The children are automatically reaped.

=cut

my %Reaped;
my %Running_Children;
my $Is_Child = 0;

sub fork_ok ($&) {
    my($num_tests, $child_sub) = @_;
    
    my $tb = $CLASS->builder;    
    my $pid = fork;

    # Failed fork
    if( !defined $pid ) {
        return $tb->ok(0, "fork() failed: $!");
    }
    # Parent
    elsif( $pid ) {
        # Avoid race condition where child has run and is reaped before
        # parent even runs.
        $Running_Children{$pid} = 1 unless $Reaped{$pid};

        $tb->use_numbers(0);
        $tb->current_test($tb->current_test + $num_tests);

        $tb->ok(1, "fork() succeeded, child pid $pid");
        return $pid;
    }

    # Child
    $Is_Child = 1;

    $tb->use_numbers(0);
    $tb->no_ending(1);
    
    note("Running child pid $$");
    $child_sub->();
    exit;
}

END {
    while( !$Is_Child and keys %Running_Children ) {
        note("reaper($$) waiting on @{[keys %Running_Children]}");
        _check_kids();
        _reaper();
    }
}

sub _check_kids {
    for my $child (keys %Running_Children) {
        delete $Running_Children{$child} if $Reaped{$child};
        delete $Running_Children{$child} unless kill 0, $child;
        note("Child $child already reaped");
    }
}

sub _reaper {
    local $?;  # wait sets $?

    my $child_pid = wait;
    $Reaped{$child_pid}++;
    delete $Running_Children{$child_pid};

    note("child $child_pid reaped");

    $CLASS->builder->use_numbers(1) unless keys %Running_Children;

    return $child_pid == -1 ? 0 : 1;
}

$SIG{CHLD} = \&_reaper;


=head1 CAVEATS

The failure of tests in a child process cannot be detected by the parent.
Therefore, the normal end-of-test reporting done by Test::Builder will
not notice failed child tests.

Test::Fork turns off test numbering in order to avoid test counter
coordination issues.  It turns it back on once the children are done
running.

Test::Fork will wait for all your child processes to complete at the end of
the parent process.

=head1 SEE ALSO

L<Test::MultiFork>


=head1 AUTHOR

Michael G Schwern E<lt>schwern@pobox.comE<gt>


=head1 BUGS and FEEDBACK

Please send all bugs and feature requests to 
I<bug-Test-Fork> at I<rt.cpan.org> or use the web interface via
L<http://rt.cpan.org>.

If you use it, please send feedback.  I like getting feedback.


=head1 COPYRIGHT and LICENSE

Copyright 2007-2008 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See F<http://www.perl.com/perl/misc/Artistic.html>

=cut

42;