-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PooledStatement now holds a collection of statements created from the pool, releasing them at the same frequency as connections are released from the pool. Added a lastUsedAt() method to Statement.
- Loading branch information
Showing
6 changed files
with
168 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Amp\Postgres\Test; | ||
|
||
use Amp\Delayed; | ||
use Amp\Loop; | ||
use Amp\Postgres\Pool; | ||
use Amp\Postgres\PooledStatement; | ||
use Amp\Postgres\ResultSet; | ||
use Amp\Postgres\Statement; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class PooledStatementTest extends TestCase { | ||
public function testActiveStatementsRemainAfterTimeout() { | ||
Loop::run(function () { | ||
$pool = new Pool('host=localhost user=postgres'); | ||
|
||
$statement = $this->createMock(Statement::class); | ||
$statement->method('getQuery') | ||
->willReturn('SELECT 1'); | ||
$statement->method('lastUsedAt') | ||
->willReturn(\time()); | ||
$statement->expects($this->once()) | ||
->method('execute'); | ||
|
||
$pooledStatement = new PooledStatement($pool, $statement); | ||
|
||
$this->assertTrue($pooledStatement->isAlive()); | ||
$this->assertSame(\time(), $pooledStatement->lastUsedAt()); | ||
|
||
yield new Delayed(1500); // Give timeout watcher enough time to execute. | ||
|
||
$pooledStatement->execute(); | ||
|
||
$this->assertTrue($pooledStatement->isAlive()); | ||
$this->assertSame(\time(), $pooledStatement->lastUsedAt()); | ||
}); | ||
} | ||
|
||
public function testIdleStatementsRemovedAfterTimeout() { | ||
Loop::run(function () { | ||
$pool = new Pool('host=localhost user=postgres'); | ||
|
||
$statement = $this->createMock(Statement::class); | ||
$statement->method('getQuery') | ||
->willReturn('SELECT 1'); | ||
$statement->method('lastUsedAt') | ||
->willReturn(0); | ||
$statement->expects($this->never()) | ||
->method('execute'); | ||
|
||
$pooledStatement = new PooledStatement($pool, $statement); | ||
|
||
$this->assertTrue($pooledStatement->isAlive()); | ||
$this->assertSame(\time(), $pooledStatement->lastUsedAt()); | ||
|
||
yield new Delayed(1500); // Give timeout watcher enough time to execute and remove mock statement object. | ||
|
||
$result = yield $pooledStatement->execute(); | ||
|
||
$this->assertInstanceOf(ResultSet::class, $result); | ||
|
||
$this->assertTrue($pooledStatement->isAlive()); | ||
$this->assertSame(\time(), $pooledStatement->lastUsedAt()); | ||
}); | ||
} | ||
} |