Skip to content

Commit 91dbd89

Browse files
committed
add test to verify number of retries
1 parent 77495a2 commit 91dbd89

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

Connection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,17 +660,17 @@ public function executeCommand($name, $params = [])
660660
\Yii::trace("Executing Redis Command: {$name}", __METHOD__);
661661
if ($this->retries > 0) {
662662
$tries = $this->retries;
663-
while ($tries-- >= 0) {
663+
while ($tries-- > 0) {
664664
try {
665665
return $this->sendCommandInternal($command, $params);
666666
} catch (SocketException $e) {
667+
\Yii::error($e, __METHOD__);
667668
// backup retries, fail on commands that fail inside here
668669
$retries = $this->retries;
669670
$this->retries = 0;
670671
$this->close();
671672
$this->open();
672673
$this->retries = $retries;
673-
\Yii::error($e, __METHOD__);
674674
}
675675
}
676676
}

tests/RedisConnectionTest.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
namespace yiiunit\extensions\redis;
4+
use Yii;
5+
use yii\helpers\ArrayHelper;
6+
use yii\log\Logger;
7+
use yii\redis\Connection;
8+
use yii\redis\SocketException;
49

510
/**
611
* @group redis
@@ -101,20 +106,68 @@ public function testConnectionTimeout()
101106
sleep(1);
102107
$this->assertTrue($db->ping());
103108
sleep(2);
104-
$this->expectException('\yii\redis\SocketException');
109+
if (method_exists($this, 'setExpectedException')) {
110+
$this->setExpectedException('\yii\redis\SocketException');
111+
} else {
112+
$this->expectException('\yii\redis\SocketException');
113+
}
105114
$this->assertTrue($db->ping());
106115
}
107116

108117
public function testConnectionTimeoutRetry()
109118
{
119+
$logger = new Logger();
120+
Yii::setLogger($logger);
121+
110122
$db = $this->getConnection(false);
111123
$db->retries = 1;
112124
$db->configSet('timeout', 1);
125+
$this->assertCount(3, $logger->messages, 'log of connection and init commands.');
126+
113127
$this->assertTrue($db->ping());
114-
sleep(1);
128+
$this->assertCount(4, $logger->messages, 'log +1 ping command.');
129+
usleep(500000); // 500ms
130+
115131
$this->assertTrue($db->ping());
132+
$this->assertCount(5, $logger->messages, 'log +1 ping command.');
116133
sleep(2);
134+
135+
// reconnect should happen here
136+
117137
$this->assertTrue($db->ping());
138+
$this->assertCount(11, $logger->messages, 'log +1 ping command, and reconnection.'
139+
. print_r(array_map(function($s) { return (string) $s; }, ArrayHelper::getColumn($logger->messages, 0)), true));
140+
}
141+
142+
/**
143+
* Retry connecting 2 times
144+
*/
145+
public function testConnectionTimeoutRetryCount()
146+
{
147+
$logger = new Logger();
148+
Yii::setLogger($logger);
149+
150+
$db = $this->getConnection(false);
151+
$db->retries = 2;
152+
$db->configSet('timeout', 1);
153+
$db->on(Connection::EVENT_AFTER_OPEN, function() {
154+
// sleep 2 seconds after connect to make every command time out
155+
sleep(2);
156+
});
157+
$this->assertCount(3, $logger->messages, 'log of connection and init commands.');
158+
159+
$exception = false;
160+
try {
161+
// should try to reconnect 2 times, before finally failing
162+
// results in 3 times sending the PING command to redis
163+
sleep(2);
164+
$db->ping();
165+
} catch (SocketException $e) {
166+
$exception = true;
167+
}
168+
$this->assertTrue($exception, 'SocketException should have been thrown.');
169+
$this->assertCount(14, $logger->messages, 'log +1 ping command, and reconnection.'
170+
. print_r(array_map(function($s) { return (string) $s; }, ArrayHelper::getColumn($logger->messages, 0)), true));
118171
}
119172

120173
/**

0 commit comments

Comments
 (0)