#!/usr/bin/perl
#
# test script for case #01782776
#

use strict;
use warnings qw(FATAL all);
use Net::STOMP::Client qw();

sub stomp () {
    my($stomp);

    $stomp = Net::STOMP::Client->new(host => "localhost", port => 3080);
    $stomp->connect(login => "admin", passcode => "admin");
    return($stomp);
}

sub test ($) {
    my($subscription) = @_;
    my($prod, $cons, $frame);

    # connect
    print("connect\n");
    $prod = stomp();
    $cons = stomp();
    # subscribe
    printf("subscribe %s\n", $subscription);
    $cons->message_callback(sub { return(1) });
    $cons->subscribe(
        destination => $subscription,
        id          => $cons->uuid(),
        receipt     => $cons->uuid(),
        ack         => "auto",
    );
    $cons->wait_for_receipts(timeout => 1);
    # send messages
    foreach my $n (1 .. 2) {
        $prod->send(
            destination => "/topic/test.$n",
            body        => $n,
        );
    }
    # receive messages
    while (1) {
        $frame = $cons->wait_for_frames(timeout => 1);
        last unless $frame;
        printf("received %s: %s\n", $frame->command(), $frame->body());
    }
    # disconnect
    print("disconnect\n");
    $prod->disconnect();
    $cons->disconnect();
    print("\n");
}

# NOTE: uncomment the next line to see the STOMP traffic
#$Net::STOMP::Client::Debug::Flags = -1;
test("/topic/test.1,/topic/test.2");
test("/topic/test.1,test.2");
