Net/Test.pm untested

filter: Statementstmt 0.0% 0 / 207 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 300 CC 6 SCAR 12.6
CC
6
Cov
0%
stop_server
6.0
stop_bot
6.0
run_test
6.0
CRAP 3.5
1 use strict;
2 use warnings;
3  
4 package Bot::Net::Test;
5 use base qw/ Bot::Net::Object Test::More Class::Data::Inheritable /;
6  
7 use Bot::Net;
8 use Bot::Net::Mixin;
9  
10 use Config;
11 use File::Spec;
12 use FindBin;
13 use Hash::Merge ();
14 use POE qw/ Wheel::Run /;
15  
16 __PACKAGE__->mk_classdata('servers' => {});
17 __PACKAGE__->mk_classdata('bots' => {});
18 __PACKAGE__->mk_classdata('waiting' => {});
19  
20 =head1 NAME
21  
22 Bot::Net::Test - helper for building Bot::Net tests
23  
24 =head1 SYNOPSIS
25  
26 use strict;
27 use warnings;
28  
29 # Make this test script a bot
30 use Bot::Net::Bot;
31 use Bot::Net::Mixin::Bot::IRC;
32  
33 use Bot::Net::Test tests => 20;
34  
35 # Start the server in class MyBotNet::Server::Main
36 Bot::Net::Test->start_server('Main');
37  
38 # Connect the bot in class MyBotNet::Bot::Count
39 Bot::Net::Test->start_bot('Count');
40  
41 on bot connected => run {
42 for ( 1 .. 10 ) {
43 yield send_to => count => 'something';
44 }
45 };
46  
47 on bot message_to_me => run {
48 my $event = get ARG0;
49 my $count_expected = (recall('count') || 0) + 1;
50 remember count => $count_expected;
51  
52 is($event->sender_nick, 'count');
53 is($event->message, $count_expected);
54  
55 if ($count_expected == 10) {
56 yield irc => quit => 'Test finished.';
57 }
58 };
59  
60 # Startup this bot
61 Bot::Net::Test->run_test;
62  
63 =head1 DESCRIPTION
64  
65 Provides some tools to make testing your bots and servers a little easier. The typical pattern for using this class is to make your test script into a bot or server by implementing whichever set of mixins you need.
66  
67 You can start orther servers or bots using L</start_bot> or L</start_server>. You can define any states you need to handle for testing. Then, you start that server or bot using L</run_test>.
68  
69 Make sure that you tell your bot to shutdown when you're finished with your tests. (For example, for an IRC bot, you can issue a C<quit> or C<disconnect> state to the IRC POE component as showin the L</SYNOPSIS>.)
70  
71 =head1 METHODS
72  
73 =head2 import_extra
74  
75 Builds a test configuration for your test file.
76  
77 =cut
78  
79 sub import_extra {
80 my $class = shift;
81 my $package = caller;
82  
83 no strict 'refs';
84 ${ $package . '::CONFIG' } ||= {};
85  
86 Test::More->export_to_level(2);
87 $class->export_poe_declarative_to_level(2);
88 }
89  
90 =head2 start_server SERVER
91  
92 Starts the named server. This server will shutdown when the test bot quits or when L</stop_server> is called.
93  
94 =cut
95  
96 sub start_server {
97 my $class = shift;
98 my $server = shift;
99  
100 $class->servers->{ $server } = undef;
101 }
102  
103 =head2 stop_server SERVER
104  
105 Stops the named server.
106  
107 =cut
108  
109 sub stop_server {
110 my $class = shift;
111 my $server = shift;
112  
113 my $wheel = delete $class->servers->{ $server }{ 'wheel' };
114 if ($wheel) {
115 Bot::Net::Test->log->info("Terminating server $server (pid:@{[$wheel->PID]})");
116 $class->waiting->{ $wheel->PID } = 1;
117 $wheel->kill;
118 }
119 }
120  
121 =head2 start_bot BOT
122  
123 Starts the named bot. This bot will shutdown when the test bot quits or when L</stop_bot> is called.
124  
125 =cut
126  
127 sub start_bot {
128 my $class = shift;
129 my $bot = shift;
130  
131 $class->bots->{ $bot } = undef;
132 }
133  
134 =head2 stop_bot BOT
135  
136 Stops the named bot.
137  
138 =cut
139  
140 sub stop_bot {
141 my $class = shift;
142 my $bot = shift;
143  
144 my $wheel = delete $class->bots->{ $bot }{ 'wheel' };
145 if ($wheel) {
146 Bot::Net::Test->log->info("Terminating bot $bot (pid:@{[$wheel->PID]})");
147 $class->waiting->{ $wheel->PID } = 1;
148 $wheel->kill;
149 }
150 }
151  
152 =head2 run_test
153  
154 Tells the test to setup the bot or server and tell the L<POE> kernel to start the event loop.
155  
156 =cut
157  
158 sub run_test {
159 my $class = shift;
160 my $test = caller;
161  
162 my @configs;
163 for my $mixin (@{ Bot::Net::Mixin::_mixins_for_package($test) }) {
164 push @configs, $mixin->default_configuration($test)
165 if $mixin->can('default_configuration');
166 }
167  
168 my $config = Hash::Merge::merge( @configs );
169  
170 # XXX This is a hack, I need to find a better way...
171 $config->{auto_connect} = 0;
172  
173 {
174 no strict 'refs';
175 ${ $test . '::CONFIG' } = $config;
176 }
177  
178 Bot::Net::Test->log->info("Starting test");
179  
180 $test->setup;
181 POE::Kernel->run;
182 }
183  
184 =head1 POE STATES
185  
186 =head2 on _start
187  
188 Sets up a timer which kills the whole test if it doesn't receive any messages within 30 seconds.
189  
190 If you have a test that may run for longer than 30 seconds, make sure your events yield "something_happened":
191  
192 on bot message_to_me => run {
193 yield 'something_happened';
194  
195 # do whatever else you like...
196 };
197  
198 =cut
199  
200 sub _run_that {
201 my @program = @_;
202  
203 sub {
204 $ENV{PERL5LIB} = join ':', @INC;
205 $ENV{BOT_NET_CONFIG_PATH} = File::Spec->catfile(
206 $FindBin::Bin, '..', 't', 'etc'
207 );
208 exec(@program);
209 };
210 }
211  
212 on _start => run {
213 yield 'spawn_all_servers';
214 yield 'spawn_all_bots_after_servers';
215 yield 'connect_after_bots';
216  
217 delay 'shutdown_unless_something_happened', 30;
218  
219 remember something_happened => 0;
220  
221 get(KERNEL)->sig(CHLD => 'child_reaper');
222 };
223  
224 =head2 on spawn_all_servers
225  
226 For each server added in the test file, tell those servers to start.
227  
228 =cut
229  
230 on spawn_all_servers => run {
231 for my $server (keys %{ Bot::Net::Test->servers }) {
232 Bot::Net::Test->log->info("Starting server $server");
233  
234 my $server_name = $server;
235 $server_name =~ s/\W+/_/g;
236  
237 my $wheel = POE::Wheel::Run->new(
238 Program => _run_that(
239 File::Spec->catfile('bin', 'botnet'),
240 qw/ run --server /, $server,
241 ),
242  
243 StdinEvent => "server_${server_name}_stdin",
244 StdoutEvent => "server_${server_name}_stdout",
245 StderrEvent => "server_${server_name}_stderr",
246  
247 ErrorEvent => "server_${server_name}_error",
248 CloseEvent => "server_${server_name}_close",
249 );
250  
251 on "server_${server_name}_stdout" => run {
252 local $_ = get ARG0;
253 recall('log')->debug("$server - $_");
254 return if Bot::Net::Test->servers->{ $server }{ 'ready' } == 1;
255 if (my ($status) = /SERVER READY\s*:\s*(.*)$/m) {
256 my %status = split /\s+/, $status;
257 Bot::Net::Test->servers->{ $server }{ 'ready' } = 1;
258 Bot::Net::Test->servers->{ $server }{ 'status' } = \%status;
259 }
260 };
261  
262 on "server_${server_name}_error" => run {
263 my $op = get ARG0;
264 my $errno = get ARG1;
265 my $error = get ARG2;
266 Test::More::fail("FAILED $server $op ($errno) $error");
267 yield 'bot_quit';
268 };
269  
270 Bot::Net::Test->servers->{ $server }{ 'wheel' } = $wheel;
271 Bot::Net::Test->servers->{ $server }{ 'ready' } = 0;
272 }
273 };
274  
275 =head2 on spawn_all_bots_after_servers
276  
277 Checks to see if all the servers have reported ready status yet. If they have, this handler will tell all the bots to start. Otherwise, it yields the event again to try again in the next time slice.
278  
279 =cut
280  
281 sub _all_servers_are_ready {
282 for my $server (keys %{ Bot::Net::Test->servers }) {
283 my $status = Bot::Net::Test->servers->{ $server };
284 unless (defined $status and $status->{'ready'}) {
285 return '';
286 }
287 }
288  
289 return 1;
290 }
291  
292 on spawn_all_bots_after_servers => run {
293 recall('log')->debug('spawn_all_bots_after_servers');
294 unless (_all_servers_are_ready()) {
295 delay spawn_all_bots_after_servers => 1;
296 return;
297 }
298  
299 for my $bot (keys %{ Bot::Net::Test->bots }) {
300 Bot::Net::Test->log->info("Starting bot $bot");
301  
302 my $bot_name = $bot;
303 $bot_name =~ s/\W+/_/g;
304  
305 my $wheel = POE::Wheel::Run->new(
306 Program => _run_that(
307 File::Spec->catfile('bin', 'botnet'),
308 qw/ run --bot /, $bot,
309 ),
310  
311 StdinEvent => "bot_${bot_name}_stdin",
312 StdoutEvent => "bot_${bot_name}_stdout",
313 StderrEvent => "bot_${bot_name}_stderr",
314  
315 ErrorEvent => "bot_${bot_name}_error",
316 CloseEvent => "bot_${bot_name}_close",
317 StderrFilter => POE::Filter::Line->new,
318 );
319  
320 on "bot_${bot_name}_stdout" => run {
321 local $_ = get ARG0;
322 recall('log')->debug("$bot - $_");
323 return if Bot::Net::Test->bots->{ $bot }{ 'ready' } == 1;
324 if (my ($status) = /BOT READY\s*:\s*(.*)$/m) {
325 my %status = split /\s+/, $status;
326 Bot::Net::Test->bots->{ $bot }{ 'ready' } = 1;
327 Bot::Net::Test->bots->{ $bot }{ 'status' } = \%status;
328 }
329 };
330  
331 on "bot_${bot_name}_error" => run {
332 my $op = get ARG0;
333 my $errno = get ARG1;
334 my $error = get ARG2;
335 Test::More::fail("FAILED $bot $op ($errno) $error");
336 yield 'bot_quit';
337 };
338  
339 Bot::Net::Test->bots->{ $bot }{ 'wheel' } = $wheel;
340 Bot::Net::Test->bots->{ $bot }{ 'ready' } = 0;
341 }
342 };
343  
344 =head2 on connect_after_bots
345  
346 Checks to see if all the bots have spawned yet. If they have, then this emits C<bot connect> to connect the test bot (assuming the test is a test bot). If all the bots have not yet connected, then this will re-emit L</on connect_after_bots> to try again in another time slice.
347  
348 =cut
349  
350 sub _all_bots_are_ready() {
351 for my $bot (keys %{ Bot::Net::Test->bots }) {
352 my $status = Bot::Net::Test->bots->{ $bot };
353 unless (defined $status and $status->{'ready'}) {
354 return '';
355 }
356 }
357  
358 return 1;
359 }
360  
361 on connect_after_bots => run {
362 recall('log')->debug('connect_after_bots');
363 unless (_all_servers_are_ready() and _all_bots_are_ready()) {
364 delay connect_after_bots => 1;
365 return;
366 }
367  
368 yield 'bot_connect';
369 };
370  
371 =head2 on child_reaper
372  
373 Reaps the child bot and server processes.
374  
375 =cut
376  
377 on child_reaper => run {
378 my $pid = get ARG1;
379 my $err = get ARG2;
380  
381 delete Bot::Net::Test->waiting->{ $pid };
382  
383 my $return = $err >> 8;
384 my $signal = $err & 127;
385 my $dump = $err & 128;
386  
387 if (defined $Config{sig_name}) {
388 my $i = 0;
389 my @signame;
390 for my $name (split / /, $Config{sig_name}) {
391 $signame[$i] = $name;
392 $i++;
393 }
394  
395 $signal = $signame[ $signal ] if defined $signame[ $signal ];
396 }
397  
398 my $log = recall 'log';
399  
400 my $message = "Process $pid exited with return $return";
401 $message .= " (signal $signal)" if $signal;
402 $message .= " (core dumped)" if $dump;
403  
404 recall('log')->info($message);
405 };
406  
407 =head2 on shutdown_unless_something_happened
408  
409 Clears the "soemthing_happened" flag if set. If not set, it tells the bot and/or server to quit.
410  
411 =cut
412  
413 on shutdown_unless_something_happened => run {
414 if (recall 'something_happened') {
415 remember something_happened => 0;
416 delay shutdown_unless_something_happened => 30;
417 }
418  
419 else {
420 yield 'server_quit';
421 yield 'bot_quit';
422 }
423 };
424  
425 =head2 on something_happened
426  
427 Sets the "something_happened" flag.
428  
429 =cut
430  
431 on something_happened => run {
432 my $something_happened = recall 'something_happened';
433 remember something_happened => $something_happened + 1;
434 };
435  
436 =head2 on [ bot quit, server quit ]
437  
438 Shutdown any bots and servers that haven't yet been stopped.
439  
440 =cut
441  
442 on [ qw/ bot_quit server_quit / ] => run {
443 Bot::Net::Test->log->warn("Quitting the test.");
444  
445 # Don't have these delayed events anymore
446 delay 'shutdown_unless_something_happened';
447 delay 'spawn_all_bots_after_servers';
448 delay 'connect_after_bots';
449  
450 # Shutdown all bots
451 for my $bot (keys %{ Bot::Net::Test->bots || {} }) {
452 Bot::Net::Test->stop_bot($bot);
453 }
454  
455 # Shutdown all servers
456 for my $server (keys %{ Bot::Net::Test->servers || {} }) {
457 Bot::Net::Test->stop_server($server);
458 }
459  
460 yield 'wait_for_stop';
461 };
462  
463 =head2 on wait_for_stop
464  
465 Called at C<on bot quit> to wait for all the processes to shutdown. It will wait 10 seconds for this before giving. Unless your OS is doing something wonky, that should never happen... if it does, let me know.
466  
467 =cut
468  
469 my $max_wait = 100;
470 on wait_for_stop => run {
471 my $waiting = scalar keys %{ Bot::Net::Test->waiting };
472 delay wait_for_stop => 0.1 if $waiting and $max_wait-- > 0;
473 };
474  
475 =head1 AUTHORS
476  
477 Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
478  
479 =head1 COPYRIGHT AND LICENSE
480  
481 Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
482  
483 This program is free software and may be modified and distributed under the same terms as Perl itself.
484  
485 =cut
486  
487 1;