1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\DBAL\Connection\Connector; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connections\Connector\HighAvailabilityConnector; |
6
|
|
|
use Throwable; |
7
|
|
|
|
8
|
|
|
class HighAvailabilityConnectorTest extends ConnectorTest |
9
|
|
|
{ |
10
|
|
|
public function testConnect() : void |
11
|
|
|
{ |
12
|
|
|
$params = $this->getParams(); |
13
|
|
|
|
14
|
|
|
$connector = new HighAvailabilityConnector($params, $this->failingDriverMock()); |
15
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('master')); |
16
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('slave')); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testConnectFailMaster() : void |
20
|
|
|
{ |
21
|
|
|
$params = $this->getParams(2, 0, true); |
22
|
|
|
|
23
|
|
|
$connector = new HighAvailabilityConnector($params, $this->failingDriverMock()); |
24
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('slave')); |
25
|
|
|
|
26
|
|
|
$failed = false; |
27
|
|
|
try { |
28
|
|
|
$connector->connectTo('master'); |
29
|
|
|
$failed = true; |
30
|
|
|
} catch (Throwable $e) { |
31
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Exception\ConnectionException', $e); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (! $failed) { |
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->fail('Connect has not failed'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testConnectFailSlave() : void |
42
|
|
|
{ |
43
|
|
|
$params = $this->getParams(2, 2); |
44
|
|
|
|
45
|
|
|
$connector = new HighAvailabilityConnector($params, $this->failingDriverMock()); |
46
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('master')); |
47
|
|
|
|
48
|
|
|
$failed = false; |
49
|
|
|
try { |
50
|
|
|
$connector->connectTo('slave'); |
51
|
|
|
$failed = true; |
52
|
|
|
} catch (Throwable $e) { |
53
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Exception\ConnectionException', $e); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (! $failed) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->fail('Connect has not failed'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testConnectRandomFail() : void |
64
|
|
|
{ |
65
|
|
|
for ($i = 0; $i < 100; $i++) { |
66
|
|
|
$params = $this->getParams(2, 1); |
67
|
|
|
|
68
|
|
|
$connector = new HighAvailabilityConnector($params, $this->failingDriverMock()); |
69
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('master')); |
70
|
|
|
$this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connector->connectTo('slave')); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|