AnyEvent-RabbitMQ

 view release on metacpan or  search on metacpan

xt/06_close.t  view on Meta::CPAN

use Test::More;
use Test::Exception;

my %conf = (
    host  => 'localhost',
    port  => 5672,
    user  => 'guest',
    pass  => 'guest',
    vhost => '/',
);

eval {
    use IO::Socket::INET;

    my $socket = IO::Socket::INET->new(
        Proto    => 'tcp',
        PeerAddr => $conf{host},
        PeerPort => $conf{port},
        Timeout  => 1,
    ) or die 'Error connecting to AMQP Server!';

    close $socket;
};

plan skip_all => 'Connection failure: '
               . $conf{host} . ':' . $conf{port} if $@;
plan tests => 2;

use AnyEvent::RabbitMQ;

subtest 'No channels', sub {
    my $ar = connect_ar();
    ok $ar->is_open, 'connection is open';
    is channel_count($ar), 0, 'no channels open';

    close_ar($ar);
    ok !$ar->is_open, 'connection closed';
    is channel_count($ar), 0, 'no channels open';
};

subtest 'channels', sub {
    my $ar = connect_ar();
    ok $ar->is_open, 'connection is open';
    is channel_count($ar), 0, 'no channels open';

    my $ch = open_channel($ar);
    ok $ch->is_open, 'channel is open';
    is channel_count($ar), 1, 'no channels open';

    close_ar($ar);
    ok !$ar->is_open, 'connection closed';
    is channel_count($ar), 0, 'no channels open';
    ok !$ch->is_open, 'channel closed';
};

sub connect_ar {
    my $done = AnyEvent->condvar;
    my $ar = AnyEvent::RabbitMQ->new()->load_xml_spec()->connect(
        (map {$_ => $conf{$_}} qw(host port user pass vhost)),
        timeout    => 1,
        on_success => sub {$done->send(1)},
        on_failure => sub { diag @_; $done->send()},
        on_close   => \&handle_close,
    );
    die 'Connection failure' if !$done->recv;
    return $ar;
}

sub close_ar {
    my ($ar,) = @_;

    my $done = AnyEvent->condvar;
    $ar->close(
        on_success => sub {$done->send(1)},
        on_failure => sub { diag @_; $done->send()},
    );
    die 'Close failure' if !$done->recv;

    return;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.627 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )