File: role_connectionmanager.t

package info (click to toggle)
libmessage-passing-perl 0.117-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 544 kB
  • sloc: perl: 2,742; makefile: 2; sh: 1
file content (120 lines) | stat: -rw-r--r-- 2,486 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
110
111
112
113
114
115
116
117
118
119
120
use strict;
use warnings;
use Test::More;
use AnyEvent;

{
    package Connection::Subscriber;
    use Moo;
    use namespace::clean -except => 'meta';

    has am_connected => ( is => 'rw' );

    sub connected {
        shift->am_connected(1);
    }

    sub disconnected {
        shift->am_connected(0);
    }
}
{
    package Some::Shonky::Async::Code;
    use Moo;
    use namespace::clean -except => 'meta';

}

{
    package My::Connection::Wrapper;
    use Moo;
    use Scalar::Util qw/ weaken /;
    use namespace::clean -except => 'meta';

    with 'Message::Passing::Role::ConnectionManager';

    has '+timeout' => (
        default => sub { 0.1 },
    );

    has '+reconnect_after' => (
        default => sub { 0.1 },
    );

    sub _build_connection {
        my $self = shift;
        weaken($self);
        my $client = Some::Shonky::Async::Code->new;
        # Real code now has something like:
        # $client->add_connect_callback(sub {
        #   $self->_set_connected(1);
        # });
        # instead we'll simulate that below..
        return $client;
    }
}

my $sub = Connection::Subscriber->new;
ok !exists($sub->{am_connected});

my $i = My::Connection::Wrapper->new;
ok $i;
ok $i->{connection};
isa_ok $i->{connection}, 'Some::Shonky::Async::Code';

$i->subscribe_to_connect($sub);
ok !exists($sub->{am_connected});

$i->_set_connected(1);
ok exists($sub->{am_connected});
ok $sub->{am_connected};
Scalar::Util::weaken($sub);

my $sub2 = Connection::Subscriber->new;
$i->subscribe_to_connect($sub2);
ok $sub2->{am_connected};

is_deeply $i->_connect_subscribers, [$sub2];
ok !$sub;

# Test connectiomn timeout
$i = My::Connection::Wrapper->new;
my $cv = AnyEvent->condvar;
{
    my $t; $t = AnyEvent->timer(
        after => 0.11,
        cb => sub { undef $t; $cv->send },
    );
}
ok $i->{connection};
$cv->recv;
ok !$i->{connection};

# Test reconnect
$cv = AnyEvent->condvar;
{
    my $t; $t = AnyEvent->timer(
        after => 0.11,
        cb => sub { undef $t; $cv->send },
    );
}
$cv->recv;
$i->_set_connected(1);
ok $i->{connection};
my ($c, $d) = (0,0);
no warnings 'redefine';
*My::Connection::Wrapper::_build_timeout_timer = sub { $c++; shift->next::method(@_) };
*My::Connection::Wrapper::_build_reconnect_timer = sub { $d++; shift->next::method(@_) };
$cv = AnyEvent->condvar;
{
    my $t; $t = AnyEvent->timer(
        after => 0.5,
        cb => sub { undef $t; $cv->send },
    );
}
$cv->recv;
is $c, 0;
is $d, 0;

done_testing;