Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72ac5bf

Browse files
goetasfabpot
authored andcommitted
Always attempt to listen for notifications
1 parent 0ad8230 commit 72ac5bf

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

src/Symfony/Component/Messenger/Bridge/Doctrine/Tests/Transport/PostgreSqlConnectionTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
namespace Symfony\Component\Messenger\Bridge\Doctrine\Tests\Transport;
1313

14+
use Doctrine\DBAL\Cache\ArrayResult;
15+
use Doctrine\DBAL\Cache\ArrayStatement;
16+
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
17+
use Doctrine\DBAL\Query\QueryBuilder;
18+
use Doctrine\DBAL\Result;
1419
use Doctrine\DBAL\Schema\Table;
1520
use PHPUnit\Framework\TestCase;
1621
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\PostgreSqlConnection;
@@ -42,6 +47,68 @@ public function testUnserialize()
4247
$connection->__wakeup();
4348
}
4449

50+
public function testListenOnConnection()
51+
{
52+
$driverConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
53+
54+
$driverConnection
55+
->expects(self::any())
56+
->method('getDatabasePlatform')
57+
->willReturn(new PostgreSQLPlatform());
58+
59+
$driverConnection
60+
->expects(self::any())
61+
->method('createQueryBuilder')
62+
->willReturn(new QueryBuilder($driverConnection));
63+
64+
$wrappedConnection = new class() {
65+
private $notifyCalls = 0;
66+
67+
public function pgsqlGetNotify()
68+
{
69+
++$this->notifyCalls;
70+
71+
return false;
72+
}
73+
74+
public function countNotifyCalls()
75+
{
76+
return $this->notifyCalls;
77+
}
78+
};
79+
80+
// dbal 2.x
81+
if (interface_exists(Result::class)) {
82+
$driverConnection
83+
->expects(self::exactly(2))
84+
->method('getWrappedConnection')
85+
->willReturn($wrappedConnection);
86+
87+
$driverConnection
88+
->expects(self::any())
89+
->method('executeQuery')
90+
->willReturn(new ArrayStatement([]));
91+
} else {
92+
// dbal 3.x
93+
$driverConnection
94+
->expects(self::exactly(2))
95+
->method('getNativeConnection')
96+
->willReturn($wrappedConnection);
97+
98+
$driverConnection
99+
->expects(self::any())
100+
->method('executeQuery')
101+
->willReturn(new Result(new ArrayResult([]), $driverConnection));
102+
}
103+
$connection = new PostgreSqlConnection(['table_name' => 'queue_table'], $driverConnection);
104+
105+
$connection->get(); // first time we have queueEmptiedAt === null, fallback on the parent implementation
106+
$connection->get();
107+
$connection->get();
108+
109+
$this->assertSame(2, $wrappedConnection->countNotifyCalls());
110+
}
111+
45112
public function testGetExtraSetupSql()
46113
{
47114
$driverConnection = $this->createMock(\Doctrine\DBAL\Connection::class);

src/Symfony/Component/Messenger/Bridge/Doctrine/Transport/PostgreSqlConnection.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ final class PostgreSqlConnection extends Connection
3333
'get_notify_timeout' => 0,
3434
];
3535

36-
private $listening = false;
37-
3836
public function __sleep(): array
3937
{
4038
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
@@ -62,12 +60,9 @@ public function get(): ?array
6260
return parent::get();
6361
}
6462

65-
if (!$this->listening) {
66-
// This is secure because the table name must be a valid identifier:
67-
// https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
68-
$this->executeStatement(sprintf('LISTEN "%s"', $this->configuration['table_name']));
69-
$this->listening = true;
70-
}
63+
// This is secure because the table name must be a valid identifier:
64+
// https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
65+
$this->executeStatement(sprintf('LISTEN "%s"', $this->configuration['table_name']));
7166

7267
if (method_exists($this->driverConnection, 'getNativeConnection')) {
7368
$wrappedConnection = $this->driverConnection->getNativeConnection();
@@ -150,11 +145,6 @@ private function createTriggerFunctionName(): string
150145

151146
private function unlisten()
152147
{
153-
if (!$this->listening) {
154-
return;
155-
}
156-
157148
$this->executeStatement(sprintf('UNLISTEN "%s"', $this->configuration['table_name']));
158-
$this->listening = false;
159149
}
160150
}

0 commit comments

Comments
 (0)