Make tests use strict and warnings. Cleanup the killing of activemq so that it happen...
Tomas Doran (t0m) [Sat, 6 Jun 2009 23:21:37 +0000 (00:21 +0100)]
lib/Catalyst/Test/MessageDriven.pm
t/01-good-message.t
t/02-bad-action.t
t/03-json-message.t
t/04-message-driven-request.t
t/lib/TestServer.pm [new file with mode: 0644]
t/server.pl [deleted file]

index 75c0f3b..06dec69 100644 (file)
@@ -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;
+
index eabb7cb..0d60f37 100644 (file)
@@ -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;
 
index 0b119eb..173bad6 100644 (file)
@@ -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;
 
index 16375c9..40d5d08 100644 (file)
@@ -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;
 
index 5e5ebb5..f7299db 100644 (file)
@@ -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 (file)
index 0000000..2ce73a2
--- /dev/null
@@ -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 (file)
index bc77af9..0000000
+++ /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;
-