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
|
#!perl
use warnings;
use strict;
use Test::More;
use Test::Fatal;
use Redis::Fast;
use lib 't/tlib';
use Test::SpawnRedisServer;
use constant SSL_AVAILABLE => eval { require IO::Socket::SSL };
my ($c, $t, $srv) = redis(timeout => 3); # redis connection timeouts within 3 seconds.
my $use_ssl = $t ? SSL_AVAILABLE : 0;
END {
$c->() if $c;
$t->() if $t;
}
# Reconnect once
# when
## 1. reconnect is greater than 0
## 2. and reconnect_on_error returns a value greater than and equal to 0
## 3. and a specified time seconds elapsed
my $sync_call_hset = sub {
my ($client, $hint) = @_;
like (exception { $client->hset(1,1) },
qr{ERR wrong number of arguments for 'hset' command},
'syntax error')
or diag "hint=$hint";
return 'sync';
};
my $async_call_hset = sub {
my ($client, $hint) = @_;
my $_cb_call_count = 0;
my $_cb = sub {
my ($ret, $error) = @_;
is $error, "ERR wrong number of arguments for 'hset' command"
or diag "_cb_call_count=$_cb_call_count, hint=$hint";
ok !$ret
or diag "_cb_call_count=$_cb_call_count, hint=$hint";
$_cb_call_count++;
};
$client->hset(1,1,$_cb);
$client->hset(2,2,$_cb);
$client->wait_all_responses;
is $_cb_call_count, 2;
return 'async';
};
# Check a condition 1.
subtest 'reconnect option is 0: reconnect_on_error is not called' => sub {
for my $call_hset ($sync_call_hset, $async_call_hset) {
my $cb_call_count = 0;
my $r = Redis::Fast->new(
reconnect => 0, # do not trigger reconnection.
server => $srv,
ssl => $use_ssl,
SSL_verify_mode => 0,
reconnect_on_error => sub { $cb_call_count++ },
);
my $hint = $call_hset->($r, "reconnect is 0");
is $cb_call_count, 0, 'reconnect_on_error is not called'
or diag "call=$hint";
}
};
subtest 'reconnect option is 1: reconnect_on_error is called once' => sub {
for my $call_hset ($sync_call_hset, $async_call_hset) {
my $cb_call_count = 0;
my $r = Redis::Fast->new(
reconnect => 1, # trigger reconnection until 1 second elapsed.
server => $srv,
ssl => $use_ssl,
SSL_verify_mode => 0,
reconnect_on_error => sub {
my ($error, $ret, $cmd) = @_;
# increment a counter to test it later.
$cb_call_count++;
# tests each argument.
is $error, "ERR wrong number of arguments for 'hset' command";
ok !$ret;
is $cmd, 'HSET';
return 0; # 0 means invake reconnection as soon as possible.
},
);
my $hint = $call_hset->($r, "reconnect is 1");
is $cb_call_count, 1, 'reconnect_on_error is called once'
or diag "call=$hint";
}
};
# Check a condition 2.
subtest "reconnect_on_error returns -1: redis ERR doesn't trigger reconnection" => sub {
for my $call_hset ($sync_call_hset, $async_call_hset) {
my $connect_count = 0;
my $cb_call_count = 0;
my $r = Redis::Fast->new(
reconnect => 1,
server => $srv,
ssl => $use_ssl,
SSL_verify_mode => 0,
on_connect => sub { $connect_count++ },
reconnect_on_error => sub {
$cb_call_count++;
return -1; # reconnect_on_error returns -1
},
);
my $hint = $call_hset->($r, "reconnect_on_error returns -1");
is $connect_count, 1, "redis ERR doesn't trigger reconnection";
if ($hint eq 'async') {
is $cb_call_count, 2, 'call reconnect_on_error each async call for hset'
or diag "cb_return_value=-1 hint=$hint";
} else {
is $cb_call_count, 1, 'call reconnect_on_error once'
or diag "cb_return_value=-1, hint=$hint";
}
}
};
subtest "reconnect_on_error returns 0: redis ERR triggers reconnection" => sub {
for my $call_hset ($sync_call_hset, $async_call_hset) {
my $connect_count = 0;
my $cb_call_count = 0;
my $r = Redis::Fast->new(
reconnect => 1,
server => $srv,
ssl => $use_ssl,
SSL_verify_mode => 0,
on_connect => sub { $connect_count++ },
reconnect_on_error => sub {
$cb_call_count++;
return 0; # reconnect_on_error returns 0
},
);
my $hint = $call_hset->($r, "reconnect_on_error returns 0");
is $connect_count, 2, "redis ERR triggers reconnection";
# If $cb_return_value is 0 and then $self->need_reconnect is set,
# calling the reconnect_on_error cb again is useless cost.
is $cb_call_count, 1, 'call reconnect_on_error once'
or diag "cb_return_value=-1, hint=$hint";
}
};
# Check a condition 3.
subtest "reconnection will not be triggered until specified seconds elapsed." => sub {
for my $call_hset ($sync_call_hset, $async_call_hset) {
my $hint;
my $cb_call_count = 0;
my $cb_return_value = 0;
my $r = Redis::Fast->new(
reconnect => 1,
server => $srv,
ssl => $use_ssl,
SSL_verify_mode => 0,
reconnect_on_error => sub {
$cb_call_count++;
return $cb_return_value;
},
);
# reconnect if the redis returns ERR,
# and next reconnection will be triggered.
$cb_return_value = 0;
$hint = $call_hset->($r, $cb_return_value);
is $cb_call_count, 1, 'call reconnect_on_error once'
or diag "cb_return_value=$cb_return_value, call=$hint";
# reconnect if the redis returns ERR,
# and next reconnection will not be triggered until 1 second elapsed.
$cb_return_value = 1;
$hint = $call_hset->($r, $cb_return_value);
is $cb_call_count, 2, 'call reconnect_on_error twice'
or diag "cb_return_value=$cb_return_value, call=$hint";
# reconnection is not triggered
# because $cb_return_value seconds have not passed since the last reconnection.
$hint = $call_hset->($r, $cb_return_value);
is $cb_call_count, 2, 'call reconnect_on_error twice'
or diag "cb_return_value=$cb_return_value, call=$hint";
}
};
subtest "reconnection will be triggered after specified seconds elapsed." => sub {
for my $call_hset ($sync_call_hset, $async_call_hset) {
my $hint;
my $cb_call_count = 0;
my $cb_return_value = 0;
my $r = Redis::Fast->new(
reconnect => 1,
server => $srv,
ssl => $use_ssl,
SSL_verify_mode => 0,
reconnect_on_error => sub {
$cb_call_count++;
return $cb_return_value;
},
);
# reconnect if the redis returns ERR,
# and next reconnection will be triggered.
$cb_return_value = 0;
$hint = $call_hset->($r, $cb_return_value);
is $cb_call_count, 1, 'call reconnect_on_error once'
or diag "cb_return_value=$cb_return_value, call=$hint";
# reconnect if the redis returns ERR,
# and next reconnection will not be triggered until 1 second elapsed.
$cb_return_value = 1;
$hint = $call_hset->($r, $cb_return_value);
is $cb_call_count, 2, 'call reconnect_on_error twice'
or diag "cb_return_value=$cb_return_value, call=$hint";
# wait for $cb_return_value seconds to pass since the last reconnection.
# +1 second is a buffer.
sleep $cb_return_value + 1;
# reconnection is triggered
# because $cb_return_value seconds have passed since the last reconnection.
$hint = $call_hset->($r, $cb_return_value);
is $cb_call_count, 3, 'call reconnect_on_error twice'
or diag "cb_return_value=$cb_return_value, call=$hint";
}
};
done_testing();
|