Skip to content

Commit 6a3af5c

Browse files
authored
Merge pull request #414 from ramunasd/dbal_ping_only_connected
[bundle] Don't ping DBAL connection if it wasn't opened
2 parents 09b5c48 + df5e4bf commit 6a3af5c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Diff for: pkg/enqueue-bundle/Consumption/Extension/DoctrinePingConnectionExtension.php

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function onPreReceived(Context $context)
3232
{
3333
/** @var Connection $connection */
3434
foreach ($this->registry->getConnections() as $connection) {
35+
if (!$connection->isConnected()) {
36+
continue;
37+
}
38+
3539
if ($connection->ping()) {
3640
return;
3741
}

Diff for: pkg/enqueue-bundle/Tests/Unit/Consumption/Extension/DoctrinePingConnectionExtensionTest.php

+53
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public function testCouldBeConstructedWithRequiredAttributes()
2222
public function testShouldNotReconnectIfConnectionIsOK()
2323
{
2424
$connection = $this->createConnectionMock();
25+
$connection
26+
->expects($this->once())
27+
->method('isConnected')
28+
->will($this->returnValue(true))
29+
;
2530
$connection
2631
->expects($this->once())
2732
->method('ping')
@@ -56,6 +61,11 @@ public function testShouldNotReconnectIfConnectionIsOK()
5661
public function testShouldDoesReconnectIfConnectionFailed()
5762
{
5863
$connection = $this->createConnectionMock();
64+
$connection
65+
->expects($this->once())
66+
->method('isConnected')
67+
->will($this->returnValue(true))
68+
;
5969
$connection
6070
->expects($this->once())
6171
->method('ping')
@@ -93,6 +103,49 @@ public function testShouldDoesReconnectIfConnectionFailed()
93103
$extension->onPreReceived($context);
94104
}
95105

106+
public function testShouldSkipIfConnectionWasNotOpened()
107+
{
108+
$connection1 = $this->createConnectionMock();
109+
$connection1
110+
->expects($this->once())
111+
->method('isConnected')
112+
->will($this->returnValue(false))
113+
;
114+
$connection1
115+
->expects($this->never())
116+
->method('ping')
117+
;
118+
119+
// 2nd connection was opened in the past
120+
$connection2 = $this->createConnectionMock();
121+
$connection2
122+
->expects($this->once())
123+
->method('isConnected')
124+
->will($this->returnValue(true))
125+
;
126+
$connection2
127+
->expects($this->once())
128+
->method('ping')
129+
->will($this->returnValue(true))
130+
;
131+
132+
$context = $this->createPsrContext();
133+
$context->getLogger()
134+
->expects($this->never())
135+
->method('debug')
136+
;
137+
138+
$registry = $this->createRegistryMock();
139+
$registry
140+
->expects($this->once())
141+
->method('getConnections')
142+
->will($this->returnValue([$connection1, $connection2]))
143+
;
144+
145+
$extension = new DoctrinePingConnectionExtension($registry);
146+
$extension->onPreReceived($context);
147+
}
148+
96149
/**
97150
* @return Context
98151
*/

0 commit comments

Comments
 (0)