-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix reconnect if initial connect fails
Fixes #70.
- Loading branch information
Showing
5 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace Amp\Redis; | ||
|
||
use Amp\Redis\Trainer\ConnectorTrainer; | ||
|
||
class RemoteExecutorTest extends IntegrationTest | ||
{ | ||
private $connectorTrainer; | ||
|
||
public function testAutomaticReconnectIfConnectFails(): \Generator | ||
{ | ||
// Create new instance, because flushAll() is executed first otherwise | ||
$this->redis = $this->createInstance(); | ||
|
||
$this->connectorTrainer->givenConnectFails(); | ||
|
||
try { | ||
yield $this->redis->get('foobar'); | ||
|
||
$this->fail('Expected exception'); | ||
} catch (\Throwable $e) { | ||
$this->assertStringStartsWith('Failed to connect to redis instance', $e->getMessage()); | ||
} | ||
|
||
$this->connectorTrainer->givenConnectIsNotIntercepted(); | ||
|
||
// Should not throw the same error again but retry | ||
$this->assertNull(yield $this->redis->get('foobar')); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->connectorTrainer = new ConnectorTrainer; | ||
|
||
parent::setUp(); | ||
} | ||
|
||
protected function createInstance(): Redis | ||
{ | ||
return new Redis(new RemoteExecutor(Config::fromUri($this->getUri()), $this->connectorTrainer)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Amp\Redis\Trainer; | ||
|
||
use Amp\CancellationToken; | ||
use Amp\Failure; | ||
use Amp\Promise; | ||
use Amp\Socket\ConnectContext; | ||
use Amp\Socket\Connector; | ||
use Amp\Socket\SocketException; | ||
use function Amp\Socket\connector; | ||
|
||
final class ConnectorTrainer implements Connector | ||
{ | ||
/** @var Connector */ | ||
private $connector; | ||
|
||
public function __construct() | ||
{ | ||
$this->givenConnectIsNotIntercepted(); | ||
} | ||
|
||
public function givenConnector(Connector $connector): void | ||
{ | ||
$this->connector = $connector; | ||
} | ||
|
||
public function givenConnectFails(): void | ||
{ | ||
$this->givenConnector(new class implements Connector { | ||
public function connect( | ||
string $uri, | ||
?ConnectContext $context = null, | ||
?CancellationToken $token = null | ||
): Promise { | ||
return new Failure(new SocketException('Connect to ' . $uri . ' failed')); | ||
} | ||
}); | ||
} | ||
|
||
public function givenConnectIsNotIntercepted(): void | ||
{ | ||
$this->givenConnector(connector()); | ||
} | ||
|
||
public function connect(string $uri, ?ConnectContext $context = null, ?CancellationToken $token = null): Promise | ||
{ | ||
return $this->connector->connect($uri, $context, $token); | ||
} | ||
} |