File: failover.t

package info (click to toggle)
libnet-stomp-perl 0.62-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 224 kB
  • sloc: perl: 1,311; makefile: 7
file content (138 lines) | stat: -rw-r--r-- 4,547 bytes parent folder | download | duplicates (3)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!perl
use lib 't/lib';
use TestHelp;
use Test::Fatal;
use Log::Any::Adapter::Test;

subtest 'default reconnect attempts' => sub {
    Log::Any::Adapter->set(
        { lexically => \(my $guard) },
        'Test',
    );
    Log::Any::Adapter::Test->clear;

    my ($s,$fh) = mkstomp_testsocket(
        hosts => [
            {hostname=>'one',port=>1},
            {hostname=>'two',port=>2},
            {hostname=>'three',port=>3},
        ],
    );

    my @connected_hosts;
    {no warnings 'redefine';
     *Net::Stomp::_get_socket = sub {
         my ($self) = @_;
         push @connected_hosts,$self->current_host;
         if (@connected_hosts>4) {
             $fh->{connected}=1;
             return $fh;
         }
         else {
             return undef;
         }
     }
    };
    $fh->{to_read} = sub {
        return Net::Stomp::Frame->new({
            command => 'CONNECTED',
            headers => {session=>'foo'},
        })->as_string;
    };

    $fh->{connected}=undef; # fake a disconnect
    $s->send({destination=>'here',body=>'string'});
    cmp_deeply(
        \@connected_hosts,
        [1,2,0,1,2],
        'tried all hosts, round-robin, re-starting',
    );

    cmp_deeply(
        Log::Any::Adapter::Test->msgs,
        [
            superhashof({ message => re(qr{sending}) }),
            superhashof({ message => re(qr{closing}) }),
            superhashof({ message => re(qr{reconnecting}) }),
            superhashof({ message => re(qr{error connecting to two}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
            superhashof({ message => re(qr{error connecting to three}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
            superhashof({ message => re(qr{error connecting to one}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
            superhashof({ message => re(qr{error connecting to two}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
            superhashof({ message => re(qr{connecting}i) }),
            superhashof({ message => re(qr{waiting}i) }),
            superhashof({ message => re(qr{connected}i) }),
        ],
        'reconnecting should be logged',
    );
};

subtest 'limited reconnect attempts' => sub {
    Log::Any::Adapter->set(
        { lexically => \(my $guard) },
        'Test',
    );
    Log::Any::Adapter::Test->clear;

    my ($s,$fh) = mkstomp_testsocket(
        hosts => [
            {hostname=>'one',port=>1},
            {hostname=>'two',port=>2},
            {hostname=>'three',port=>3},
        ],
        reconnect_attempts => 2,
    );

    my @connected_hosts;
    {no warnings 'redefine';
     *Net::Stomp::_get_socket = sub {
         my ($self) = @_;
         push @connected_hosts,$self->current_host;
         return undef;
     }
    };
    $fh->{to_read} = sub {
        return Net::Stomp::Frame->new({
            command => 'CONNECTED',
            headers => {session=>'foo'},
        })->as_string;
    };

    $fh->{connected}=undef; # fake a disconnect
    my $e = exception { $s->send({destination=>'here',body=>'string'}) };
    ok(defined $e,'died ok');
    like($e,qr{giving up},'correct exception');
    cmp_deeply(
        \@connected_hosts,
        [1,2,0,1,2,0],
        'tried all hosts, round-robin, re-starting, then stopped',
    );

    cmp_deeply(
        Log::Any::Adapter::Test->msgs,
        [
            superhashof({ message => re(qr{sending}) }),
            superhashof({ message => re(qr{closing}) }),
            superhashof({ message => re(qr{reconnecting}) }),
            superhashof({ message => re(qr{error connecting to two}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
            superhashof({ message => re(qr{error connecting to three}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
            superhashof({ message => re(qr{error connecting to one}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
            superhashof({ message => re(qr{error connecting to two}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
            superhashof({ message => re(qr{error connecting to three}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
            superhashof({ message => re(qr{error connecting to one}i) }),
            superhashof({ message => re(qr{failed to connect}i) }),
        ],
        'reconnecting should be logged',
    );
};


done_testing;