-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpubsub.t
194 lines (181 loc) · 7.33 KB
/
pubsub.t
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use Mojo::Base -strict;
BEGIN { $ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll' }
use Test::More;
plan skip_all => 'set TEST_ONLINE to enable this test' unless $ENV{TEST_ONLINE};
use Mojo::IOLoop;
use Mojo::JSON qw(true);
use Mojo::Pg;
subtest 'Notifications with event loop' => sub {
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
my ($db, @all, @test);
$pg->pubsub->on(reconnect => sub { $db = pop });
$pg->pubsub->listen(
pstest => sub {
my ($pubsub, $payload) = @_;
push @test, $payload;
Mojo::IOLoop->next_tick(sub { $pubsub->pg->db->notify(pstest => 'stop') });
Mojo::IOLoop->stop if $payload eq 'stop';
}
);
$db->on(notification => sub { push @all, [@_[1, 3]] });
$pg->db->notify(pstest => '♥test♥');
Mojo::IOLoop->start;
is_deeply \@test, ['♥test♥', 'stop'], 'right messages';
is_deeply \@all, [['pstest', '♥test♥'], ['pstest', 'stop']], 'right notifications';
};
subtest 'JSON' => sub {
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
my (@json, @raw);
$pg->pubsub->json('pstest')->listen(
pstest => sub {
my ($pubsub, $payload) = @_;
push @json, $payload;
Mojo::IOLoop->stop if ref $payload eq 'HASH' && $payload->{msg} eq 'stop';
}
);
$pg->pubsub->listen(
pstest2 => sub {
my ($pubsub, $payload) = @_;
push @raw, $payload;
}
);
Mojo::IOLoop->next_tick(sub {
$pg->db->notify(pstest => 'fail');
$pg->pubsub->notify('pstest')->notify(pstest => {msg => '♥works♥'})->notify(pstest => [1, 2, 3])
->notify(pstest => true)->notify(pstest2 => '♥works♥')->notify(pstest => {msg => 'stop'});
});
Mojo::IOLoop->start;
is_deeply \@json, [undef, undef, {msg => '♥works♥'}, [1, 2, 3], true, {msg => 'stop'}], 'right data structures';
is_deeply \@raw, ['♥works♥'], 'right messages';
};
subtest 'Unsubscribe' => sub {
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
my $db;
$pg->pubsub->on(reconnect => sub { $db = pop });
my (@all, @test);
my $first = $pg->pubsub->listen(pstest => sub { push @test, pop });
my $second = $pg->pubsub->listen(pstest => sub { push @test, pop });
$db->on(notification => sub { push @all, [@_[1, 3]] });
$pg->pubsub->notify('pstest')->notify(pstest => 'first');
is_deeply \@test, ['', '', 'first', 'first'], 'right messages';
is_deeply \@all, [['pstest', ''], ['pstest', 'first']], 'right notifications';
$pg->pubsub->unlisten(pstest => $first)->notify(pstest => 'second');
is_deeply \@test, ['', '', 'first', 'first', 'second'], 'right messages';
is_deeply \@all, [['pstest', ''], ['pstest', 'first'], ['pstest', 'second']], 'right notifications';
$pg->pubsub->unlisten(pstest => $second)->notify(pstest => 'third');
is_deeply \@test, ['', '', 'first', 'first', 'second'], 'right messages';
is_deeply \@all, [['pstest', ''], ['pstest', 'first'], ['pstest', 'second']], 'right notifications';
@all = @test = ();
my $third = $pg->pubsub->listen(pstest => sub { push @test, pop });
my $fourth = $pg->pubsub->listen(pstest => sub { push @test, pop });
$pg->pubsub->notify(pstest => 'first');
is_deeply \@test, ['first', 'first'], 'right messages';
$pg->pubsub->notify(pstest => 'second');
is_deeply \@test, ['first', 'first', 'second', 'second'], 'right messages';
$pg->pubsub->unlisten('pstest')->notify(pstest => 'third');
is_deeply \@test, ['first', 'first', 'second', 'second'], 'right messages';
};
subtest 'Reconnect while listening' => sub {
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
my (@dbhs, @test);
$pg->pubsub->on(reconnect => sub { push @dbhs, pop->dbh });
$pg->pubsub->listen(pstest => sub { push @test, pop });
ok $dbhs[0], 'database handle';
is_deeply \@test, [], 'no messages';
{
local $dbhs[0]{Warn} = 0;
$pg->pubsub->on(reconnect => sub { shift->notify(pstest => 'works'); Mojo::IOLoop->stop });
$pg->db->query('select pg_terminate_backend(?)', $dbhs[0]{pg_pid});
Mojo::IOLoop->start;
ok $dbhs[1], 'database handle';
isnt $dbhs[0], $dbhs[1], 'different database handles';
is_deeply \@test, ['works'], 'right messages';
};
};
subtest 'Reconnect while listening multiple retries' => sub {
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
my (@dbhs, @test, @test3, @test4);
$pg->pubsub->reconnect_interval(0.1);
$pg->pubsub->on(reconnect => sub { push @dbhs, pop->dbh });
$pg->pubsub->listen(pstest => sub { push @test, pop });
$pg->pubsub->listen(pstest4 => sub { push @test4, pop });
ok $dbhs[0], 'database handle';
is_deeply \@test, [], 'no messages';
{
local $dbhs[0]{Warn} = 0;
$pg->pubsub->on(
reconnect => sub {
shift->notify(pstest => 'works')->notify(pstest3 => 'works too')->notify(pstest4 => 'failed');
Mojo::IOLoop->stop;
}
);
my $dsn = $pg->dsn;
$pg->pubsub->on(
disconnect => sub {
my $pubsub = shift;
Mojo::IOLoop->timer(0.2 => sub { $pg->dsn($dsn) });
$pubsub->listen(pstest3 => sub { push @test3, pop });
$pubsub->unlisten('pstest4');
}
);
$pg->db->query('SELECT PG_TERMINATE_BACKEND(?)', $dbhs[0]{pg_pid});
$pg->dsn('dbi:Pg:badoption=1');
Mojo::IOLoop->start;
ok $dbhs[1], 'database handle';
isnt $dbhs[0], $dbhs[1], 'different database handles';
is_deeply \@test, ['works'], 'right messages';
is_deeply \@test3, ['works too'], 'right messages';
is_deeply \@test4, [], 'no messages';
};
};
subtest 'Reconnect while not listening' => sub {
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
my (@dbhs, @test);
$pg->pubsub->on(reconnect => sub { push @dbhs, pop->dbh });
$pg->pubsub->notify(pstest => 'fail');
ok $dbhs[0], 'database handle';
is_deeply \@test, [], 'no messages';
{
local $dbhs[0]{Warn} = 0;
$pg->pubsub->on(reconnect => sub { Mojo::IOLoop->stop });
$pg->db->query('SELECT PG_TERMINATE_BACKEND(?)', $dbhs[0]{pg_pid});
Mojo::IOLoop->start;
ok $dbhs[1], 'database handle';
isnt $dbhs[0], $dbhs[1], 'different database handles';
$pg->pubsub->listen(pstest => sub { push @test, pop });
$pg->pubsub->notify(pstest => 'works too');
is_deeply \@test, ['works too'], 'right messages';
};
};
subtest 'Reset' => sub {
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
my (@dbhs, @test);
$pg->pubsub->on(reconnect => sub { push @dbhs, pop->dbh });
$pg->pubsub->listen(pstest => sub { push @test, pop });
ok $dbhs[0], 'database handle';
$pg->pubsub->notify(pstest => 'first');
is_deeply \@test, ['first'], 'right messages';
{
$pg->pubsub->reset;
$pg->pubsub->notify(pstest => 'second');
ok $dbhs[1], 'database handle';
isnt $dbhs[0], $dbhs[1], 'different database handles';
is_deeply \@test, ['first'], 'right messages';
$pg->pubsub->listen(pstest => sub { push @test, pop });
$pg->pubsub->notify(pstest => 'third');
ok !$dbhs[2], 'no database handle';
is_deeply \@test, ['first', 'third'], 'right messages';
};
};
subtest 'Call listen/unlisten immediately after notify' => sub {
my $pg = Mojo::Pg->new($ENV{TEST_ONLINE});
my @test;
$pg->pubsub->listen(pstest => sub { push @test, pop });
$pg->db->notify(pstest => 'works');
$pg->pubsub->listen(pstest2 => sub { });
is_deeply \@test, ['works'], 'right messages';
$pg->db->notify(pstest => 'works too');
$pg->pubsub->unlisten(pstest3 => sub { });
is_deeply \@test, ['works', 'works too'], 'right messages';
};
done_testing();