Fix starting test server
[catagits/Catalyst-Engine-STOMP.git] / t / lib / TestServer.pm
1 package StompRole;
2 use Moose::Role;
3 use namespace::autoclean;
4
5 after 'disconnect' => sub {
6     delete shift->{___activemq};
7 };
8
9 package TestServer;
10 use strict;
11 use warnings;
12 use Alien::ActiveMQ;
13 use Test::More;
14 use Exporter qw/import/;
15 use FindBin;
16
17 our $ACTIVEMQ_VERSION = '5.2.0';
18
19 our @EXPORT = qw/ start_server /;
20
21 sub start_server {
22     my ($mq, $stomp);
23     eval {
24         $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } );
25     };
26     if ($@) {
27
28         unless (Alien::ActiveMQ->is_version_installed($ACTIVEMQ_VERSION)) {
29             plan 'skip_all' => 'No ActiveMQ server installed by Alien::ActiveMQ, try running the "install-activemq" command';
30             exit;
31         }
32
33         $mq = Alien::ActiveMQ->run_server($ACTIVEMQ_VERSION);
34
35         eval {
36             $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } );
37         };
38         if ($@) {
39             plan 'skip_all' => 'No ActiveMQ server listening on 61613: ' . $@;
40             exit;
41         }
42     }
43
44     $SIG{CHLD} = 'IGNORE';
45     unless (fork()) {
46             system("$^X -I$FindBin::Bin/lib $FindBin::Bin/script/stomptestapp_stomp.pl --oneshot");
47             exit 0;
48     }
49     print STDERR "server started, waiting for spinup...";
50     sleep 20;
51
52     $stomp->{___activemq} = $mq if $mq;
53     StompRole->meta->apply($stomp);
54     return $stomp;
55 }
56