| 1 | #!perl |
| 2 | |
| 3 | use warningsscip-perl cpan Redis-Fast 0.37 warnings/; |
| 4 | use strictscip-perl cpan Redis-Fast 0.37 strict/; |
| 5 | use Test::Morescip-perl cpan Redis-Fast 0.37 Test/More/; |
| 6 | use Test::Fatalscip-perl cpan Redis-Fast 0.37 Test/Fatal/; |
| 7 | use Redis::Fastscip-perl cpan Redis-Fast 0.37 Redis/Fast/; |
| 8 | use libscip-perl cpan Redis-Fast 0.37 lib/ 't/tlib'; |
| 9 | use Test::SpawnRedisServerscip-perl cpan Redis-Fast 0.37 Test/SpawnRedisServer/; |
| 10 | |
| 11 | use constantscip-perl cpan Redis-Fast 0.37 constant/ SSL_AVAILABLEscip-perl cpan Redis-Fast 0.37 main/SSL_AVAILABLE(). => eval { requirescip-perl cpan Redis-Fast 0.37 main/require(). IO::Socket::SSL }; |
| 12 | |
| 13 | my ($c, $t, $srv) = redisscip-perl cpan Redis-Fast 0.37 main/redis().(timeout => 3); # redis connection timeouts within 3 seconds. |
| 14 | my $use_ssl = $t ? SSL_AVAILABLE : 0; |
| 15 | |
| 16 | END { |
| 17 | $c->()scip-perl cpan Redis-Fast 0.37 main/`->()`(). if $c; |
| 18 | $t->()scip-perl cpan Redis-Fast 0.37 main/`->()`(). if $t; |
| 19 | } |
| 20 | |
| 21 | # Reconnect once |
| 22 | # when |
| 23 | ## 1. reconnect is greater than 0 |
| 24 | ## 2. and reconnect_on_error returns a value greater than and equal to 0 |
| 25 | ## 3. and a specified time seconds elapsed |
| 26 | |
| 27 | my $sync_call_hset = sub { |
| 28 | my ($client, $hint) = @_scip-perl cpan Redis-Fast 0.37 main/`@_`.; |
| 29 | likescip-perl cpan Redis-Fast 0.37 main/like(). (exceptionscip-perl cpan Redis-Fast 0.37 main/exception(). { $client->hsetscip-perl cpan Redis-Fast 0.37 main/hset().(1,1) }, |
| 30 | qr{ERR wrong number of arguments for 'hset' command}, |
| 31 | 'syntax error') |
| 32 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "hint=$hint"; |
| 33 | return 'sync'; |
| 34 | }; |
| 35 | my $async_call_hset = sub { |
| 36 | my ($client, $hint) = @_scip-perl cpan Redis-Fast 0.37 main/`@_`.; |
| 37 | my $_cb_call_count = 0; |
| 38 | my $_cb = sub { |
| 39 | my ($ret, $error) = @_scip-perl cpan Redis-Fast 0.37 main/`@_`.; |
| 40 | isscip-perl cpan Redis-Fast 0.37 main/is(). $error, "ERR wrong number of arguments for 'hset' command" |
| 41 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "_cb_call_count=$_cb_call_count, hint=$hint"; |
| 42 | ok !$ret |
| 43 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "_cb_call_count=$_cb_call_count, hint=$hint"; |
| 44 | $_cb_call_count++; |
| 45 | }; |
| 46 | $client->hsetscip-perl cpan Redis-Fast 0.37 main/hset().(1,1,$_cb); |
| 47 | $client->hsetscip-perl cpan Redis-Fast 0.37 main/hset().(2,2,$_cb); |
| 48 | $client->wait_all_responsesscip-perl cpan Redis-Fast 0.37 main/wait_all_responses().; |
| 49 | isscip-perl cpan Redis-Fast 0.37 main/is(). $_cb_call_count, 2; |
| 50 | return 'async'; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | # Check a condition 1. |
| 55 | |
| 56 | subtestscip-perl cpan Redis-Fast 0.37 main/subtest(). 'reconnect option is 0: reconnect_on_error is not called' => sub { |
| 57 | for my $call_hset ($sync_call_hset, $async_call_hset) { |
| 58 | my $cb_call_count = 0; |
| 59 | my $r = Redis::Fast->newscip-perl cpan Redis-Fast 0.37 Redis/Fast/new().( |
| 60 | reconnect => 0, # do not trigger reconnection. |
| 61 | server => $srv, |
| 62 | ssl => $use_ssl, |
| 63 | SSL_verify_mode => 0, |
| 64 | reconnect_on_error => sub { $cb_call_count++ }, |
| 65 | ); |
| 66 | my $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, "reconnect is 0")scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 67 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 0, 'reconnect_on_error is not called' |
| 68 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "call=$hint"; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | subtestscip-perl cpan Redis-Fast 0.37 main/subtest(). 'reconnect option is 1: reconnect_on_error is called once' => sub { |
| 73 | for my $call_hset ($sync_call_hset, $async_call_hset) { |
| 74 | my $cb_call_count = 0; |
| 75 | my $r = Redis::Fast->newscip-perl cpan Redis-Fast 0.37 Redis/Fast/new().( |
| 76 | reconnect => 1, # trigger reconnection until 1 second elapsed. |
| 77 | server => $srv, |
| 78 | ssl => $use_ssl, |
| 79 | SSL_verify_mode => 0, |
| 80 | reconnect_on_error => sub { |
| 81 | my ($error, $ret, $cmd) = @_scip-perl cpan Redis-Fast 0.37 main/`@_`.; |
| 82 | |
| 83 | # increment a counter to test it later. |
| 84 | $cb_call_count++; |
| 85 | |
| 86 | # tests each argument. |
| 87 | isscip-perl cpan Redis-Fast 0.37 main/is(). $error, "ERR wrong number of arguments for 'hset' command"; |
| 88 | ok !$ret; |
| 89 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cmd, 'HSET'; |
| 90 | return 0; # 0 means invake reconnection as soon as possible. |
| 91 | }, |
| 92 | ); |
| 93 | my $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, "reconnect is 1")scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 94 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 1, 'reconnect_on_error is called once' |
| 95 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "call=$hint"; |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | # Check a condition 2. |
| 101 | |
| 102 | subtestscip-perl cpan Redis-Fast 0.37 main/subtest(). "reconnect_on_error returns -1: redis ERR doesn't trigger reconnection" => sub { |
| 103 | for my $call_hset ($sync_call_hset, $async_call_hset) { |
| 104 | my $connect_count = 0; |
| 105 | my $cb_call_count = 0; |
| 106 | my $r = Redis::Fast->newscip-perl cpan Redis-Fast 0.37 Redis/Fast/new().( |
| 107 | reconnect => 1, |
| 108 | server => $srv, |
| 109 | ssl => $use_ssl, |
| 110 | SSL_verify_mode => 0, |
| 111 | on_connect => sub { $connect_count++ }, |
| 112 | reconnect_on_error => sub { |
| 113 | $cb_call_count++; |
| 114 | return -1; # reconnect_on_error returns -1 |
| 115 | }, |
| 116 | ); |
| 117 | my $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, "reconnect_on_error returns -1")scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 118 | isscip-perl cpan Redis-Fast 0.37 main/is(). $connect_count, 1, "redis ERR doesn't trigger reconnection"; |
| 119 | if ($hint eq 'async') { |
| 120 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 2, 'call reconnect_on_error each async call for hset' |
| 121 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "cb_return_value=-1 hint=$hint"; |
| 122 | } else { |
| 123 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 1, 'call reconnect_on_error once' |
| 124 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "cb_return_value=-1, hint=$hint"; |
| 125 | } |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | subtestscip-perl cpan Redis-Fast 0.37 main/subtest(). "reconnect_on_error returns 0: redis ERR triggers reconnection" => sub { |
| 130 | for my $call_hset ($sync_call_hset, $async_call_hset) { |
| 131 | my $connect_count = 0; |
| 132 | my $cb_call_count = 0; |
| 133 | my $r = Redis::Fast->newscip-perl cpan Redis-Fast 0.37 Redis/Fast/new().( |
| 134 | reconnect => 1, |
| 135 | server => $srv, |
| 136 | ssl => $use_ssl, |
| 137 | SSL_verify_mode => 0, |
| 138 | on_connect => sub { $connect_count++ }, |
| 139 | reconnect_on_error => sub { |
| 140 | $cb_call_count++; |
| 141 | return 0; # reconnect_on_error returns 0 |
| 142 | }, |
| 143 | ); |
| 144 | my $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, "reconnect_on_error returns 0")scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 145 | isscip-perl cpan Redis-Fast 0.37 main/is(). $connect_count, 2, "redis ERR triggers reconnection"; |
| 146 | |
| 147 | # If $cb_return_value is 0 and then $self->need_reconnect is set, |
| 148 | # calling the reconnect_on_error cb again is useless cost. |
| 149 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 1, 'call reconnect_on_error once' |
| 150 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "cb_return_value=-1, hint=$hint"; |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | # Check a condition 3. |
| 156 | |
| 157 | subtestscip-perl cpan Redis-Fast 0.37 main/subtest(). "reconnection will not be triggered until specified seconds elapsed." => sub { |
| 158 | for my $call_hset ($sync_call_hset, $async_call_hset) { |
| 159 | my $hint; |
| 160 | my $cb_call_count = 0; |
| 161 | my $cb_return_value = 0; |
| 162 | my $r = Redis::Fast->newscip-perl cpan Redis-Fast 0.37 Redis/Fast/new().( |
| 163 | reconnect => 1, |
| 164 | server => $srv, |
| 165 | ssl => $use_ssl, |
| 166 | SSL_verify_mode => 0, |
| 167 | reconnect_on_error => sub { |
| 168 | $cb_call_count++; |
| 169 | return $cb_return_value; |
| 170 | }, |
| 171 | ); |
| 172 | |
| 173 | # reconnect if the redis returns ERR, |
| 174 | # and next reconnection will be triggered. |
| 175 | $cb_return_value = 0; |
| 176 | |
| 177 | $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, scip-perl cpan Redis-Fast 0.37 main/`->()`().$cb_return_value)scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 178 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 1, 'call reconnect_on_error once' |
| 179 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "cb_return_value=$cb_return_value, call=$hint"; |
| 180 | |
| 181 | # reconnect if the redis returns ERR, |
| 182 | # and next reconnection will not be triggered until 1 second elapsed. |
| 183 | $cb_return_value = 1; |
| 184 | |
| 185 | $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, scip-perl cpan Redis-Fast 0.37 main/`->()`().$cb_return_value)scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 186 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 2, 'call reconnect_on_error twice' |
| 187 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "cb_return_value=$cb_return_value, call=$hint"; |
| 188 | |
| 189 | # reconnection is not triggered |
| 190 | # because $cb_return_value seconds have not passed since the last reconnection. |
| 191 | $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, scip-perl cpan Redis-Fast 0.37 main/`->()`().$cb_return_value)scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 192 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 2, 'call reconnect_on_error twice' |
| 193 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "cb_return_value=$cb_return_value, call=$hint"; |
| 194 | } |
| 195 | }; |
| 196 | |
| 197 | subtestscip-perl cpan Redis-Fast 0.37 main/subtest(). "reconnection will be triggered after specified seconds elapsed." => sub { |
| 198 | for my $call_hset ($sync_call_hset, $async_call_hset) { |
| 199 | my $hint; |
| 200 | my $cb_call_count = 0; |
| 201 | my $cb_return_value = 0; |
| 202 | my $r = Redis::Fast->newscip-perl cpan Redis-Fast 0.37 Redis/Fast/new().( |
| 203 | reconnect => 1, |
| 204 | server => $srv, |
| 205 | ssl => $use_ssl, |
| 206 | SSL_verify_mode => 0, |
| 207 | reconnect_on_error => sub { |
| 208 | $cb_call_count++; |
| 209 | return $cb_return_value; |
| 210 | }, |
| 211 | ); |
| 212 | |
| 213 | # reconnect if the redis returns ERR, |
| 214 | # and next reconnection will be triggered. |
| 215 | $cb_return_value = 0; |
| 216 | |
| 217 | $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, scip-perl cpan Redis-Fast 0.37 main/`->()`().$cb_return_value)scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 218 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 1, 'call reconnect_on_error once' |
| 219 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "cb_return_value=$cb_return_value, call=$hint"; |
| 220 | |
| 221 | # reconnect if the redis returns ERR, |
| 222 | # and next reconnection will not be triggered until 1 second elapsed. |
| 223 | $cb_return_value = 1; |
| 224 | |
| 225 | $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, scip-perl cpan Redis-Fast 0.37 main/`->()`().$cb_return_value)scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 226 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 2, 'call reconnect_on_error twice' |
| 227 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "cb_return_value=$cb_return_value, call=$hint"; |
| 228 | |
| 229 | # wait for $cb_return_value seconds to pass since the last reconnection. |
| 230 | # +1 second is a buffer. |
| 231 | sleep $cb_return_value + 1; |
| 232 | |
| 233 | # reconnection is triggered |
| 234 | # because $cb_return_value seconds have passed since the last reconnection. |
| 235 | $hint = $call_hset->(scip-perl cpan Redis-Fast 0.37 main/`->()`().$r, scip-perl cpan Redis-Fast 0.37 main/`->()`().$cb_return_value)scip-perl cpan Redis-Fast 0.37 main/`->()`().; |
| 236 | isscip-perl cpan Redis-Fast 0.37 main/is(). $cb_call_count, 3, 'call reconnect_on_error twice' |
| 237 | or diagscip-perl cpan Redis-Fast 0.37 main/diag(). "cb_return_value=$cb_return_value, call=$hint"; |
| 238 | } |
| 239 | }; |
| 240 | |
| 241 | done_testingscip-perl cpan Redis-Fast 0.37 main/done_testing().(); |