2ce73a2f9c764fb1762ead6ea4d529b65d11181a
[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
16 our $ACTIVEMQ_VERSION = '5.2.0';
17
18 our @EXPORT = qw/ start_server /;
19
20 sub start_server {
21     my ($mq, $stomp);
22     eval {
23         $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } );
24     };
25     if ($@) {
26
27         unless (Alien::ActiveMQ->is_version_installed($ACTIVEMQ_VERSION)) {
28             plan 'skip_all' => 'No ActiveMQ server installed by Alien::ActiveMQ, try running the "install-activemq" command';
29             exit;
30         }
31
32         $mq = Alien::ActiveMQ->run_server($ACTIVEMQ_VERSION);
33
34         eval {
35             $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } );
36         };
37         if ($@) {
38             plan 'skip_all' => 'No ActiveMQ server listening on 61613: ' . $@;
39             exit;
40         }
41     }
42
43     $SIG{CHLD} = 'IGNORE';
44     unless (fork()) {
45             system("$^X -Ilib -Itestapp/lib testapp/script/stomptestapp_stomp.pl --oneshot");
46             exit 0;
47     }
48     print STDERR "server started, waiting for spinup...";
49     sleep 20;
50
51     $stomp->{___activemq} = $mq if $mq;
52     StompRole->meta->apply($stomp);
53     return $stomp;
54 }
55