3 package Test::MooseX::Daemonize;
6 use Sub::Exporter::ForMethods 'method_installer';
7 use Sub::Exporter -setup => {
8 exports => [ qw(daemonize_ok check_test_output) ],
9 groups => { default => [ qw(daemonize_ok check_test_output) ] },
10 installer => method_installer,
15 our $Test = Test::Builder->new;
18 my ( $daemon, $msg ) = @_;
19 unless ( my $pid = fork ) {
24 sleep(1); # Punt on sleep time, 1 seconds should be enough
25 $Test->ok( $daemon->pidfile->does_file_exist, $msg )
27 'Pidfile (' . $daemon->pidfile->file . ') not found.' );
31 sub check_test_output {
33 open( my $stdout_in, '<', $app->test_output )
34 or die "can't open test output: $!";
35 while ( my $line = <$stdout_in> ) {
38 if ( $line =~ /\A((not\s+)?ok)(?:\s+-)(?:\s+(.*))\z/ ) {
39 my ( $status, $not, $text ) = ( $1, $2, $3 );
42 # We don't just call ok(!$not), because that generates diagnostics of
43 # its own for failures. We only want the diagnostics from the child.
44 my $orig_no_diag = $Test->no_diag;
46 $Test->ok(!$not, $text);
47 $Test->no_diag($orig_no_diag);
49 elsif ( $line =~ s/\A#\s?// ) {
53 $Test->diag("$label: $line (unrecognised)\n");
58 package Test::MooseX::Daemonize::Testable;
68 after daemonize => sub {
69 $Test->use_numbers(0);
71 open my $out, '>', $_[0]->test_output or die "Cannot open test output: $!";
72 my $fileno = fileno $out;
73 open STDERR, ">&=", $fileno
74 or die "Can't redirect STDERR";
76 open STDOUT, ">&=", $fileno
77 or die "Can't redirect STDOUT";
80 $Test->failure_output($out);
81 $Test->todo_output($out);
91 Test::MooseX::Daemonize - Tool to help test MooseX::Daemonize applications
95 use File::Spec::Functions;
96 use File::Temp qw(tempdir);
98 my $dir = tempdir( CLEANUP => 1 );
100 ## Try to make sure we are in the test directory
102 my $file = catfile( $dir, "im_alive" );
103 my $daemon = FileMaker->new( pidbase => $dir, filename => $file );
105 daemonize_ok( $daemon, 'child forked okay' );
106 ok( -e $file, "$file exists" );
110 This module provides some basic Test::Builder compatible test methods to
111 use when writing tests for you MooseX::Daemonize based modules.
113 =head1 EXPORTED FUNCTIONS
117 =item B<daemonize_ok ( $daemon, ?$msg )>
119 This will attempt to daemonize your C<$daemon> returning ok on
120 success and not ok on failure.
122 =item B<check_test_output ( $daemon )>
124 This is expected to be used with a C<$daemon> which does the
125 B<Test::MooseX::Daemonize::Testable> role (included in this package
126 see the source for more info). It will collect the test output
127 from your daemon and apply it in the parent process by mucking
128 around with L<Test::Builder> stuff, again, read the source for
129 more info. If we get time we will document this more thoroughly.
133 =head1 INCOMPATIBILITIES
137 =head1 BUGS AND LIMITATIONS
139 No bugs have been reported.
141 Please report any bugs or feature requests to
142 C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at
143 L<http://rt.cpan.org>.
151 Chris Prather C<< <perigrin@cpan.org> >>
153 =head1 LICENCE AND COPYRIGHT
155 Copyright (c) 2007-2011, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved.
157 This module is free software; you can redistribute it and/or
158 modify it under the same terms as Perl itself. See L<perlartistic>.
160 =head1 DISCLAIMER OF WARRANTY
162 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
163 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
164 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
165 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
166 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
167 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
168 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
169 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
170 NECESSARY SERVICING, REPAIR, OR CORRECTION.
172 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
173 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
174 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
175 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
176 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
177 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
178 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
179 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
180 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF