Commit | Line | Data |
3c3db18c |
1 | package Test::MooseX::Daemonize; |
fe0eeebc |
2 | use strict; |
7d7115e5 |
3 | |
4 | # BEGIN CARGO CULTING |
5 | use Sub::Exporter; |
6 | use Test::Builder; |
fe0eeebc |
7 | |
8 | our $VERSION = '0.03'; |
7d7115e5 |
9 | our $AUTHORITY = 'cpan:PERIGRIN'; |
10 | |
b3cd9b56 |
11 | { |
12 | my @exports = qw[ |
13 | daemonize_ok |
14 | check_test_output |
15 | ]; |
16 | |
17 | Sub::Exporter::setup_exporter( |
18 | { |
19 | exports => \@exports, |
20 | groups => { default => \@exports } |
21 | } |
22 | ); |
23 | } |
7d7115e5 |
24 | |
25 | our $Test = Test::Builder->new; |
26 | |
27 | sub daemonize_ok { |
28 | my ( $daemon, $msg ) = @_; |
d02fc704 |
29 | unless ( my $pid = fork ) { |
7d7115e5 |
30 | $daemon->start(); |
31 | exit; |
32 | } |
33 | else { |
3543c999 |
34 | sleep(1); # Punt on sleep time, 1 seconds should be enough |
fe0eeebc |
35 | $Test->ok( $daemon->pidfile->does_file_exist, $msg ) |
b3cd9b56 |
36 | || $Test->diag( |
37 | 'Pidfile (' . $daemon->pidfile->file . ') not found.' ); |
3543c999 |
38 | } |
39 | } |
40 | |
41 | sub check_test_output { |
42 | my ($app) = @_; |
43 | open( my $stdout_in, '<', $app->test_output ) |
44 | or die "can't open test output: $!"; |
45 | while ( my $line = <$stdout_in> ) { |
46 | $line =~ s/\s+\z//; |
47 | my $label; |
48 | if ( $line =~ /\A((not\s+)?ok)(?:\s+-)(?:\s+(.*))\z/ ) { |
49 | my ( $status, $not, $text ) = ( $1, $2, $3 ); |
50 | $text ||= ''; |
51 | |
52 | # We don't just call ok(!$not), because that generates diagnostics of |
53 | # its own for failures. We only want the diagnostics from the child. |
54 | my $num = $Test->current_test; |
55 | $Test->current_test( ++$num ); |
56 | $Test->_print("$status $num - $text\n"); |
57 | } |
58 | elsif ( $line =~ s/\A#\s?// ) { |
59 | $Test->diag($line); |
60 | } |
61 | else { |
62 | $Test->_print_diag("$label: $line (unrecognised)\n"); |
63 | } |
7d7115e5 |
64 | } |
65 | } |
66 | |
3c3db18c |
67 | package Test::MooseX::Daemonize::Testable; |
68 | use Moose::Role; |
69 | |
70 | has test_output => ( |
3543c999 |
71 | isa => 'Str', |
72 | is => 'ro', |
3c3db18c |
73 | required => 1, |
74 | ); |
75 | |
76 | after daemonize => sub { |
77 | $Test->use_numbers(0); |
78 | $Test->no_ending(1); |
3543c999 |
79 | open my $out, '>', $_[0]->test_output or die "Cannot open test output: $!"; |
80 | my $fileno = fileno $out; |
81 | open STDERR, ">&=", $fileno |
3c3db18c |
82 | or die "Can't redirect STDERR"; |
83 | |
3543c999 |
84 | open STDOUT, ">&=", $fileno |
3c3db18c |
85 | or die "Can't redirect STDOUT"; |
86 | |
3543c999 |
87 | $Test->output($out); |
88 | $Test->failure_output($out); |
89 | $Test->todo_output($out); |
3c3db18c |
90 | }; |
91 | |
7d7115e5 |
92 | 1; |
93 | __END__ |
94 | |
fe0eeebc |
95 | =pod |
7d7115e5 |
96 | |
97 | =head1 NAME |
98 | |
fe0eeebc |
99 | Test::MooseX::Daemonize - Tool to help test MooseX::Daemonize applications |
7d7115e5 |
100 | |
101 | =head1 VERSION |
102 | |
103 | This document describes MooseX::Daemonize version 0.0.1 |
104 | |
7d7115e5 |
105 | =head1 SYNOPSIS |
106 | |
380acf65 |
107 | use File::Spec::Functions; |
108 | use File::Temp qw(tempdir); |
109 | |
110 | my $dir = tempdir( CLEANUP => 1 ); |
7d7115e5 |
111 | |
112 | ## Try to make sure we are in the test directory |
7d7115e5 |
113 | |
380acf65 |
114 | my $file = catfile( $dir, "im_alive" ); |
115 | my $daemon = FileMaker->new( pidbase => $dir, filename => $file ); |
7d7115e5 |
116 | |
117 | daemonize_ok( $daemon, 'child forked okay' ); |
118 | ok( -e $file, "$file exists" ); |
7d7115e5 |
119 | |
120 | =head1 DESCRIPTION |
121 | |
fe0eeebc |
122 | This module provides some basic Test::Builder compatible test methods to |
123 | use when writing tests for you MooseX::Daemonize based modules. |
7d7115e5 |
124 | |
fe0eeebc |
125 | =head1 EXPORTED FUNCTIONS |
7d7115e5 |
126 | |
fe0eeebc |
127 | =over 4 |
7d7115e5 |
128 | |
fe0eeebc |
129 | =item B<daemonize_ok ( $daemon, ?$msg )> |
7d7115e5 |
130 | |
fe0eeebc |
131 | This will attempt to daemonize your C<$daemon> returning ok on |
132 | success and not ok on failure. |
7d7115e5 |
133 | |
fe0eeebc |
134 | =item B<check_test_output ( $daemon )> |
7d7115e5 |
135 | |
fe0eeebc |
136 | This is expected to be used with a C<$daemon> which does the |
137 | B<Test::MooseX::Daemonize::Testable> role (included in this package |
138 | see the source for more info). It will collect the test output |
139 | from your daemon and apply it in the parent process by mucking |
140 | around with L<Test::Builder> stuff, again, read the source for |
141 | more info. If we get time we will document this more thoroughly. |
3543c999 |
142 | |
7d7115e5 |
143 | =back |
144 | |
7d7115e5 |
145 | =head1 INCOMPATIBILITIES |
146 | |
7d7115e5 |
147 | None reported. |
148 | |
7d7115e5 |
149 | =head1 BUGS AND LIMITATIONS |
150 | |
7d7115e5 |
151 | No bugs have been reported. |
152 | |
153 | Please report any bugs or feature requests to |
154 | C<bug-acme-dahut-call@rt.cpan.org>, or through the web interface at |
155 | L<http://rt.cpan.org>. |
156 | |
157 | =head1 SEE ALSO |
158 | |
fe0eeebc |
159 | L<MooseX::Daemonize> |
7d7115e5 |
160 | |
161 | =head1 AUTHOR |
162 | |
163 | Chris Prather C<< <perigrin@cpan.org> >> |
164 | |
7d7115e5 |
165 | =head1 LICENCE AND COPYRIGHT |
166 | |
df3c463b |
167 | Copyright (c) 2007-2008, Chris Prather C<< <perigrin@cpan.org> >>. All rights reserved. |
7d7115e5 |
168 | |
169 | This module is free software; you can redistribute it and/or |
170 | modify it under the same terms as Perl itself. See L<perlartistic>. |
171 | |
7d7115e5 |
172 | =head1 DISCLAIMER OF WARRANTY |
173 | |
174 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
175 | FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
176 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
177 | PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
178 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
179 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
180 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
181 | YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
182 | NECESSARY SERVICING, REPAIR, OR CORRECTION. |
183 | |
184 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
185 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
186 | REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE |
187 | LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
188 | OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
189 | THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
190 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
191 | FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
192 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
3543c999 |
193 | SUCH DAMAGES. |
fe0eeebc |
194 | |
195 | =cut |