Change so that ActiveMQ is only started if not already running
[catagits/Catalyst-Engine-STOMP.git] / t / Catalyst-Engine-Stomp.t
CommitLineData
5c4a7793 1use Test::More;
0a663589 2
3# Tests which expect a STOMP server like ActiveMQ to exist on
4# localhost:61613, which is what you get if you just get the ActiveMQ
5# distro and run its out-of-the-box config.
6
7use Net::Stomp;
8use YAML::XS qw/ Dump Load /;
9use Data::Dumper;
10
6ac87378 11use Alien::ActiveMQ;
12my $ACTIVEMQ_VERSION = '5.2.0';
13
a0c8e885 14my ($stomp, $mq);
5c4a7793 15eval {
16 $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } );
17};
18if ($@) {
a0c8e885 19
20 unless (Alien::ActiveMQ->is_version_installed($ACTIVEMQ_VERSION)) {
21 plan 'skip_all' => 'No ActiveMQ server installed by Alien::ActiveMQ, try running the "install-activemq" command';
22 exit;
23 }
24
25 $mq = Alien::ActiveMQ->run_server($ACTIVEMQ_VERSION);
26
27 eval {
28 $stomp = Net::Stomp->new( { hostname => 'localhost', port => 61613 } );
29 };
30 if ($@) {
31 plan 'skip_all' => 'No ActiveMQ server listening on 61613: ' . $@;
32 exit;
33 }
5c4a7793 34}
35
a0c8e885 36plan tests => 12;
37
0a663589 38# First fire off the server
5c4a7793 39$SIG{CHLD} = 'IGNORE';
ff3b0412 40unless (fork()) {
6ac87378 41 system("$^X -Ilib -Itestapp/lib testapp/script/stomptestapp_stomp.pl --oneshot");
ff3b0412 42 exit 0;
43}
a6b86b35 44print STDERR "server started, waiting for spinup...";
6ac87378 45sleep 20;
0a663589 46
47# Now be a client to that server
a6b86b35 48print STDERR "testing\n";
0a663589 49ok($stomp, 'Net::Stomp object');
50
51my $frame = $stomp->connect();
52ok($frame, 'connect to MQ server ok');
53
54my $reply_to = sprintf '%s:1', $frame->headers->{session};
55ok($frame->headers->{session}, 'got a session');
56ok(length $reply_to > 2, 'valid-looking reply_to queue');
57
58ok($stomp->subscribe( { destination => '/temp-queue/reply' } ), 'subscribe to temp queue');
59
60my $message = {
61 payload => { foo => 1, bar => 2 },
62 reply_to => $reply_to,
63 type => 'testaction',
64 };
65my $text = Dump($message);
66ok($text, 'compose message');
67
68$stomp->send( { destination => '/queue/testcontroller', body => $text } );
69
70my $reply_frame = $stomp->receive_frame();
71ok($reply_frame, 'got a reply');
72ok($reply_frame->headers->{destination} eq "/remote-temp-queue/$reply_to", 'came to correct temp queue');
73ok($reply_frame->body, 'has a body');
74
75my $response = Load($reply_frame->body);
76ok($response, 'YAML response ok');
77ok($response->{type} eq 'testaction_response', 'correct type');
78
79ok($stomp->disconnect, 'disconnected');
80