|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | 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; |
4 | 9 |
|
5 | 10 | /**
|
6 | 11 | * @group redis
|
@@ -101,20 +106,68 @@ public function testConnectionTimeout()
|
101 | 106 | sleep(1);
|
102 | 107 | $this->assertTrue($db->ping());
|
103 | 108 | 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 | + } |
105 | 114 | $this->assertTrue($db->ping());
|
106 | 115 | }
|
107 | 116 |
|
108 | 117 | public function testConnectionTimeoutRetry()
|
109 | 118 | {
|
| 119 | + $logger = new Logger(); |
| 120 | + Yii::setLogger($logger); |
| 121 | + |
110 | 122 | $db = $this->getConnection(false);
|
111 | 123 | $db->retries = 1;
|
112 | 124 | $db->configSet('timeout', 1);
|
| 125 | + $this->assertCount(3, $logger->messages, 'log of connection and init commands.'); |
| 126 | + |
113 | 127 | $this->assertTrue($db->ping());
|
114 |
| - sleep(1); |
| 128 | + $this->assertCount(4, $logger->messages, 'log +1 ping command.'); |
| 129 | + usleep(500000); // 500ms |
| 130 | + |
115 | 131 | $this->assertTrue($db->ping());
|
| 132 | + $this->assertCount(5, $logger->messages, 'log +1 ping command.'); |
116 | 133 | sleep(2);
|
| 134 | + |
| 135 | + // reconnect should happen here |
| 136 | + |
117 | 137 | $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)); |
118 | 171 | }
|
119 | 172 |
|
120 | 173 | /**
|
|
0 commit comments