File: 06_close.t

package info (click to toggle)
libanyevent-rabbitmq-perl 1.17~dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 276 kB
  • ctags: 199
  • sloc: perl: 2,740; makefile: 27
file content (109 lines) | stat: -rw-r--r-- 2,536 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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;
}

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

    return scalar keys %{$ar->channels};
}

sub open_channel {
    my ($ar,) = @_;
    
    my $done = AnyEvent->condvar;
    $ar->open_channel(
        on_success => sub {$done->send(shift)},
        on_failure => sub {$done->send()},
        on_return  => sub {die 'Receive return'},
        on_close   => \&handle_close,
    );
    my $ch = $done->recv;
    die 'Open channel failure' if !$ch;

    return $ch;
}

sub handle_close {
    my $method_frame = shift->method_frame;
    die $method_frame->reply_code, $method_frame->reply_text
      if $method_frame->reply_code;
}