8000 Always attempt to listen for notifications by goetas · Pull Request #47209 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Always attempt to listen for notifications #47209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
8000 Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

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

use Doctrine\DBAL\Cache\ArrayResult;
use Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\Table;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Bridge\Doctrine\Transport\PostgreSqlConnection;
Expand Down Expand Up @@ -42,6 +47,68 @@ public function testUnserialize()
$connection->__wakeup();
}

public function testListenOnConnection()
{
$driverConnection = $this->createMock(\Doctrine\DBAL\Connection::class);

$driverConnection
->expects(self::any())
->method('getDatabasePlatform')
->willReturn(new PostgreSQLPlatform());

$driverConnection
->expects(self::any())
->method('createQueryBuilder')
->willReturn(new QueryBuilder($driverConnection));

$wrappedConnection = new class() {
private $notifyCalls = 0;

public function pgsqlGetNotify()
{
++$this->notifyCalls;

return false;
}

public function countNotifyCalls()
{
return $this->notifyCalls;
}
};

// dbal 2.x
if (interface_exists(Result::class)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do this weird thing to test against dbal 2.x and dbal 3.x. I'm not proud of it but i do not have a better idea

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could maybe duplicate the test to remove the if (and skip if not using the good version). But IMHO that's good enough as-is.

$driverConnection
->expects(self::exactly(2))
->method('getWrappedConnection')
->willReturn($wrappedConnection);

$driverConnection
->expects(self::any())
->method('executeQuery')
->willReturn(new ArrayStatement([]));
} else {
// dbal 3.x
$driverConnection
->expects(self::exactly(2))
->method('getNativeConnection')
->willReturn($wrappedConnection);

$driverConnection
->expects(self::any())
->method('executeQuery')
->willReturn(new Result(new ArrayResult([]), $driverConnection));
}
$connection = new PostgreSqlConnection(['table_name' => 'queue_table'], $driverConnection);

$connection->get(); // first time we have queueEmptiedAt === null, fallback on the parent implementation
$connection->get();
$connection->get();

$this->assertSame(2, $wrappedConnection->countNotifyCalls());
}

public function testGetExtraSetupSql()
{
$driverConnection = $this->createMock(\Doctrine\DBAL\Connection::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ final class PostgreSqlConnection extends Connection
'get_notify_timeout' => 0,
];

private $listening = false;

public function __sleep(): array
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
Expand Down Expand Up @@ -62,12 +60,9 @@ public function get(): ?array
return parent::get();
}

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

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

private function unlisten()
{
if (!$this->listening) {
return;
}

$this->executeStatement(sprintf('UNLISTEN "%s"', $this->configuration['tabl 400D e_name']));
$this->listening = false;
}
}
0