From: Tomas Doran (t0m) Date: Sat, 6 Jun 2009 23:21:37 +0000 (+0100) Subject: Make tests use strict and warnings. Cleanup the killing of activemq so that it happen... X-Git-Tag: 0.05~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Engine-STOMP.git;a=commitdiff_plain;h=8edb2b469643bd6e63e67d4846bafd3ac3927ba8 Make tests use strict and warnings. Cleanup the killing of activemq so that it happens once you disconnect from stomp, rather than in global destruction, which can fail. --- diff --git a/lib/Catalyst/Test/MessageDriven.pm b/lib/Catalyst/Test/MessageDriven.pm index 75c0f3b..06dec69 100644 --- a/lib/Catalyst/Test/MessageDriven.pm +++ b/lib/Catalyst/Test/MessageDriven.pm @@ -16,7 +16,7 @@ Catalyst::Test::MessageDriven - test message-driven Catalyst apps Derived from Catalyst::Test, this module provides a way to run tests against message-driven Catalyst applications - those with Catalyst::Controller::MessageDriven-based controllers, and expect to -run with Catalyst::Engine::Stomp. +run with Catalyst::Engine::Stomp. =head1 SYNOPSIS @@ -32,7 +32,7 @@ run with Catalyst::Engine::Stomp. =head2 request(queue, message) This function accepts a queue and a message, and runs the request in -that context. Returns a response object. +that context. Returns a response object. =head1 TODO @@ -58,7 +58,7 @@ my $build_exports = sub { my $app = $class->run(); $request = sub { message_driven_request( $app, @_ ) }; } - + return { request => $request, }; @@ -85,7 +85,7 @@ sub message_driven_request { $request->content($req_message); $request->content_length(length $req_message); $request->content_type('application/octet-stream'); - + my $response; $app->handle_request($request, \$response); @@ -101,3 +101,4 @@ sub run { } 1; + diff --git a/t/01-good-message.t b/t/01-good-message.t index eabb7cb..0d60f37 100644 --- a/t/01-good-message.t +++ b/t/01-good-message.t @@ -1,3 +1,5 @@ +use strict; +use warnings; use Test::More; # Tests which expect a STOMP server like ActiveMQ to exist on @@ -9,8 +11,10 @@ use YAML::XS qw/ Dump Load /; use Data::Dumper; use FindBin; -use lib "$FindBin::Bin"; -require 'server.pl'; +use lib "$FindBin::Bin/lib"; +use TestServer; + +my $stomp = start_server(); plan tests => 11; diff --git a/t/02-bad-action.t b/t/02-bad-action.t index 0b119eb..173bad6 100644 --- a/t/02-bad-action.t +++ b/t/02-bad-action.t @@ -1,16 +1,19 @@ +use strict; +use warnings; use Test::More; # Tests which expect a STOMP server like ActiveMQ to exist on # localhost:61613, which is what you get if you just get the ActiveMQ # distro and run its out-of-the-box config. -use Net::Stomp; use YAML::XS qw/ Dump Load /; use Data::Dumper; use FindBin; -use lib "$FindBin::Bin"; -require 'server.pl'; +use lib "$FindBin::Bin/lib"; +use TestServer; + +my $stomp = start_server(); plan tests => 12; diff --git a/t/03-json-message.t b/t/03-json-message.t index 16375c9..40d5d08 100644 --- a/t/03-json-message.t +++ b/t/03-json-message.t @@ -1,21 +1,24 @@ +use strict; +use warnings; use Test::More; # Tests which expect a STOMP server like ActiveMQ to exist on # localhost:61613, which is what you get if you just get the ActiveMQ # distro and run its out-of-the-box config. -use Net::Stomp; - eval { use JSON; }; if ($@) { plan 'skip_all' => 'JSON not installed, skipping JSON-format test'; + exit; } use FindBin; -use lib "$FindBin::Bin"; -require 'server.pl'; +use lib "$FindBin::Bin/lib"; +use TestServer; + +my $stomp = start_server(); plan tests => 11; diff --git a/t/04-message-driven-request.t b/t/04-message-driven-request.t index 5e5ebb5..f7299db 100644 --- a/t/04-message-driven-request.t +++ b/t/04-message-driven-request.t @@ -1,12 +1,13 @@ use strict; use warnings; -use Test::More tests => 6; +use Test::More tests => 5; use FindBin; use lib "$FindBin::Bin/../testapp/lib"; -BEGIN { use_ok 'Catalyst::Test::MessageDriven' or die; - use_ok 'StompTestApp' or die; }; +BEGIN { + use_ok 'Catalyst::Test::MessageDriven', 'StompTestApp' or die; +}; # successful request - type is minimum attributes my $req = "---\ntype: ping\n"; diff --git a/t/lib/TestServer.pm b/t/lib/TestServer.pm new file mode 100644 index 0000000..2ce73a2 --- /dev/null +++ b/t/lib/TestServer.pm @@ -0,0 +1,55 @@ +package StompRole; +use Moose::Role; +use namespace::autoclean; + +after 'disconnect' => sub { + delete shift->{___activemq}; +}; + +package TestServer; +use strict; +use warnings; +use Alien::ActiveMQ; +use Test::More; +use Exporter qw/import/; + +our $ACTIVEMQ_VERSION = '5.2.0'; + +our @EXPORT = qw/ start_server /; + +sub start_server { + my ($mq, $stomp); + eval { + $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } ); + }; + if ($@) { + + unless (Alien::ActiveMQ->is_version_installed($ACTIVEMQ_VERSION)) { + plan 'skip_all' => 'No ActiveMQ server installed by Alien::ActiveMQ, try running the "install-activemq" command'; + exit; + } + + $mq = Alien::ActiveMQ->run_server($ACTIVEMQ_VERSION); + + eval { + $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } ); + }; + if ($@) { + plan 'skip_all' => 'No ActiveMQ server listening on 61613: ' . $@; + exit; + } + } + + $SIG{CHLD} = 'IGNORE'; + unless (fork()) { + system("$^X -Ilib -Itestapp/lib testapp/script/stomptestapp_stomp.pl --oneshot"); + exit 0; + } + print STDERR "server started, waiting for spinup..."; + sleep 20; + + $stomp->{___activemq} = $mq if $mq; + StompRole->meta->apply($stomp); + return $stomp; +} + diff --git a/t/server.pl b/t/server.pl deleted file mode 100644 index bc77af9..0000000 --- a/t/server.pl +++ /dev/null @@ -1,32 +0,0 @@ -use Alien::ActiveMQ; -my $ACTIVEMQ_VERSION = '5.2.0'; - -eval { - $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } ); -}; -if ($@) { - - unless (Alien::ActiveMQ->is_version_installed($ACTIVEMQ_VERSION)) { - plan 'skip_all' => 'No ActiveMQ server installed by Alien::ActiveMQ, try running the "install-activemq" command'; - exit; - } - - $mq = Alien::ActiveMQ->run_server($ACTIVEMQ_VERSION); - - eval { - $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } ); - }; - if ($@) { - plan 'skip_all' => 'No ActiveMQ server listening on 61613: ' . $@; - exit; - } -} - -$SIG{CHLD} = 'IGNORE'; -unless (fork()) { - system("$^X -Ilib -Itestapp/lib testapp/script/stomptestapp_stomp.pl --oneshot"); - exit 0; -} -print STDERR "server started, waiting for spinup..."; -sleep 20; -