Skip to content

Commit

Permalink
#53 - cancel query before close connection (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg1540 authored Jan 6, 2023
1 parent c3d2490 commit 8e45076
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/PgSqlHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ private function fetchTypes(string $id): Promise
public function close(): void
{
if ($this->handle instanceof \PgSql\Connection || \is_resource($this->handle)) {
\pg_cancel_query($this->handle);
\pg_close($this->handle);
$this->handle = null;
}
Expand Down
12 changes: 12 additions & 0 deletions test/AbstractConnectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ public function testConnectInvalidUser(): Promise

return $this->connect(PostgresConnectionConfig::fromString('host=localhost user=invalid'), new TimeoutCancellationToken(100));
}

public function testConnectionClose(): \Generator
{
$connection = yield $this->connect(PostgresConnectionConfig::fromString('host=localhost user=postgres'));
$this->assertInstanceOf(Connection::class, $connection);

$connection->execute('SELECT pg_sleep(10)');

$start = microtime(true);
$connection->close();
$this->assertEquals(0, round(microtime(true) - $start));
}
}

0 comments on commit 8e45076

Please sign in to comment.