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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
#!perl
use warnings;
use strict;
use Test::More;
use Test::Fatal;
use Time::HiRes qw(gettimeofday tv_interval);
use Redis::Fast;
use lib 't/tlib';
use Test::SpawnRedisServer;
use Net::EmptyPort qw(empty_port);
my ($c, $srv) = redis(timeout => 1);
END { $c->() if $c }
subtest 'Command without connection, no reconnect' => sub {
ok(my $r = Redis::Fast->new(reconnect => 0, server => $srv), 'connected to our test redis-server');
ok($r->quit, 'close connection to the server');
like(exception { $r->set(reconnect => 1) }, qr{Not connected to any server}, 'send ping without reconnect',);
};
subtest 'Command without connection or timeout, with database change, with reconnect' => sub {
ok(my $r = Redis::Fast->new(reconnect => 2, server => $srv), 'connected to our test redis-server');
ok($r->select(4), 'send command with reconnect');
ok($r->set(reconnect => $$), 'send command with reconnect');
ok($r->quit, 'close connection to the server');
is($r->get('reconnect'), $$, 'reconnect with read errors before write');
};
subtest 'Reconnection discards pending commands' => sub {
ok(my $r = Redis::Fast->new(reconnect => 2, server => $srv), 'connected to our test redis-server');
my $processed_pending = 0;
$r->dbsize(sub { $processed_pending++ });
_wait_for_redis_timeout();
ok($r->set(foo => 'bar'), 'send command with reconnect');
is($processed_pending, 0, 'pending command discarded on reconnect');
};
subtest 'INFO commands with extra logic triggers reconnect' => sub {
ok(my $r = Redis::Fast->new(reconnect => 2, server => $srv), 'connected to our test redis-server');
ok($r->quit, 'close connection to the server');
my $info = $r->info;
is(ref $info, 'HASH', 'reconnect on INFO command');
};
subtest 'KEYS commands with extra logic triggers reconnect' => sub {
ok(my $r = Redis::Fast->new(reconnect => 2, server => $srv), 'connected to our test redis-server');
ok($r->flushdb, 'delete all keys');
ok($r->set(reconnect => $$), 'set known key');
ok($r->quit, 'close connection to the server');
my @keys = $r->keys('*');
is_deeply(\@keys, ['reconnect'], 'reconnect on KEYS command');
};
subtest 'PING commands with extra logic triggers reconnect' => sub {
ok(my $r = Redis::Fast->new(reconnect => 2, server => $srv), 'connected to our test redis-server');
_wait_for_redis_timeout();
my $res = $r->ping;
ok($res, 'reconnect on PING command');
ok($r->quit, 'close connection to the server');
$res = $r->ping;
ok(!$res, 'QUIT command disables reconnect on PING command');
};
subtest "Bad commands don't trigger reconnect" => sub {
ok(my $r = Redis::Fast->new(reconnect => 2, server => $srv), 'connected to our test redis-server');
my $prev_sock = $r->__sock;
like(
exception { $r->set(bad => reconnect => 1) },
qr{ERR wrong number of arguments for 'set' command|ERR syntax error},
'Bad commands still die',
);
is($r->__sock, $prev_sock, "... and don't trigger a reconnect");
};
subtest 'Reconnect code clears sockect ASAP' => sub {
ok(my $r = Redis::Fast->new(reconnect => 3, server => $srv), 'connected to our test redis-server');
_wait_for_redis_timeout();
is(exception { $r->quit }, undef, "Quit doesn't die if we are already disconnected");
};
subtest "Reconnect gives up after timeout" => sub {
ok(my $r = Redis::Fast->new(reconnect => 3, server => $srv), 'connected to our test redis-server');
$c->(); ## Make sure the server is dead
my $t0 = [gettimeofday];
like(
exception { $r->set(reconnect => 1) },
qr{Could not connect to Redis server at},
'Eventually it gives up and dies',
);
ok(tv_interval($t0) > 3, '... minimum value for the reconnect reached');
};
subtest "Reconnect during transaction" => sub {
$c->(); ## Make previous server is dead
my $port = empty_port();
ok(($c, $srv) = redis(port => $port, timeout => 1), "spawn redis on port $port");
ok(my $r = Redis::Fast->new(reconnect => 3, server => $srv), 'connected to our test redis-server');
ok($r->multi(), 'start transacion');
ok($r->set('reconnect_1' => 1), 'set first key');
$c->();
ok(($c, $srv) = redis(port => $port, timeout => 1), "respawn redis on port $port");
like(exception { $r->set('reconnect_2' => 2) }, qr{reconnect disabled inside transaction}, 'set second key');
$r->connect(); #reconnect
is($r->exists('reconnect_1'), 0, 'key "reconnect_1" should not exist');
is($r->exists('reconnect_2'), 0, 'key "reconnect_2" should not exist');
};
subtest "exec does not trigger reconnect" => sub {
$c->(); ## Make previous server is dead
my $port = empty_port();
ok(($c, $srv) = redis(port => $port, timeout => 1), "spawn redis on port $port");
ok(my $r = Redis::Fast->new(reconnect => 3, server => $srv), 'connected to our test redis-server');
ok($r->multi(), 'start transacion');
ok($r->set('reconnect_1' => 1), 'set first key');
$c->();
ok(($c, $srv) = redis(port => $port, timeout => 1), "respawn redis on port $port");
like(exception { $r->exec() }, qr{reconnect disabled inside transaction}, 'exec');
$r->connect(); #reconnect
is($r->exists('reconnect_1'), 0, 'key "reconnect_1" should not exist');
};
subtest "multi should trigger reconnect" => sub {
$c->(); ## Make previous server is dead
my $port = empty_port();
ok(($c, $srv) = redis(port => $port, timeout => 1), "spawn redis on port $port");
ok(my $r = Redis::Fast->new(reconnect => 3, server => $srv), 'connected to our test redis-server');
$c->();
ok(($c, $srv) = redis(port => $port, timeout => 1), "respawn redis on port $port");
ok($r->multi(), 'start transacion');
ok($r->set('reconnect' => 1), 'set key');
ok($r->exec(), 'execute transaction');
$c->();
ok(($c, $srv) = redis(port => $port, timeout => 1), "respawn redis on port $port");
ok($r->set('reconnect' => 1), 'setting key should not fail');
};
subtest "Reconnect works after WATCH + MULTI + EXEC" => sub {
$c->(); ## Make previous server is dead
my $port = empty_port();
ok(($c, $srv) = redis(port => $port, timeout => 1), "spawn redis on port $port");
ok(my $r = Redis::Fast->new(reconnect => 3, server => $srv), 'connected to our test redis-server');
ok($r->set('watch' => 'watch'), 'set watch key');
ok($r->watch('watch'), 'start watching key');
ok($r->multi(), 'start transacion');
ok($r->set('reconnect' => 1), 'set key');
ok($r->exec(), 'execute transaction');
$c->();
ok(($c, $srv) = redis(port => $port, timeout => 1), "respawn redis on port $port");
ok($r->set('reconnect' => 1), 'setting key should not fail');
};
subtest "Reconnect works after WATCH + MULTI + DISCARD" => sub {
$c->(); ## Make previous server is dead
my $port = empty_port();
ok(($c, $srv) = redis(port => $port, timeout => 1), "spawn redis on port $port");
ok(my $r = Redis::Fast->new(reconnect => 3, server => $srv), 'connected to our test redis-server');
ok($r->set('watch' => 'watch'), 'set watch key');
ok($r->watch('watch'), 'start watching key');
ok($r->multi(), 'start transacion');
ok($r->set('reconnect' => 1), 'set key');
ok($r->discard(), 'dscard transaction');
$c->();
ok(($c, $srv) = redis(port => $port, timeout => 1), "respawn redis on port $port");
ok($r->set('reconnect' => 1), 'setting second key should not fail');
};
subtest "Reconnect behaviour differs from cpan Redis module #73" => sub {
$c->(); ## Make previous server is dead
my $port = empty_port();
ok(($c, $srv) = redis(port => $port, timeout => 1), "spawn redis on port $port");
ok(my $r = Redis::Fast->new(
reconnect => 1,
every => 100_000,
cnx_timeout => 3,
read_timeout => 1,
write_timeout => 1,
server => $srv
), 'connected to our test redis-server');
ok($r->set(reconnect => $$), 'send command');
$c->(); ## Make sure the server is dead
like(
exception { $r->set(reconnect => $$) },
qr{Could not connect to Redis server at},
'Eventually it gives up and dies (first try)',
);
like(
exception { $r->set(reconnect => $$) },
qr{Could not connect to Redis server at},
'Eventually it gives up and dies (second try)',
);
ok(($c, $srv) = redis(port => $port, timeout => 1), "respawn redis on port $port");
ok($r->set(reconnect => $$), 'a command works after respawn');
};
done_testing();
sub _wait_for_redis_timeout {
## Redis will timeout clients after 100 internal server loops, at
## least 10 seconds (even with a timeout 1 on the config) so we sleep
## a bit more hoping the timeout did happen. Not perfect, patches
## welcome
diag('Sleeping 11 seconds, waiting for Redis to timeout...');
sleep(11);
}
|