sql/test/StatementPoolTest.php
Niklas Keller e257237b61 Improve naming
Relates to #1.
2018-09-13 20:51:10 +02:00

88 lines
2.9 KiB
PHP

<?php
namespace Amp\Sql\Test;
use Amp\Delayed;
use Amp\Loop;
use Amp\PHPUnit\TestCase;
use Amp\Sql\Pool;
use Amp\Sql\Statement;
use Amp\Sql\StatementPool;
class StatementPoolTest extends TestCase
{
public function testActiveStatementsRemainAfterTimeout()
{
Loop::run(function () {
$pool = $this->createMock(Pool::class);
$pool->method('isAlive')
->willReturn(true);
$pool->method('getIdleTimeout')
->willReturn(60);
$statement = $this->createMock(Statement::class);
$statement->method('isAlive')
->willReturn(true);
$statement->method('getQuery')
->willReturn('SELECT 1');
$statement->method('lastUsedAt')
->willReturn(\time());
$statement->expects($this->once())
->method('execute');
/** @var StatementPool $statementPool */
$statementPool = $this->getMockBuilder(StatementPool::class)
->setConstructorArgs([$pool, $statement, $this->createCallback(0)])
->getMockForAbstractClass();
$this->assertTrue($statementPool->isAlive());
$this->assertSame(\time(), $statementPool->getLastUsedAt());
yield new Delayed(1500); // Give timeout watcher enough time to execute.
$statementPool->execute();
$this->assertTrue($statementPool->isAlive());
$this->assertSame(\time(), $statementPool->getLastUsedAt());
});
}
public function testIdleStatementsRemovedAfterTimeout()
{
Loop::run(function () {
$pool = $this->createMock(Pool::class);
$pool->method('isAlive')
->willReturn(true);
$pool->method('getIdleTimeout')
->willReturn(1);
$statement = $this->createMock(Statement::class);
$statement->method('isAlive')
->willReturn(true);
$statement->method('getQuery')
->willReturn('SELECT 1');
$statement->method('lastUsedAt')
->willReturn(\time());
$statement->expects($this->once())
->method('execute');
/** @var StatementPool $statementPool */
$statementPool = $this->getMockBuilder(StatementPool::class)
->setConstructorArgs([$pool, $statement, $this->createCallback(1)])
->getMockForAbstractClass();
$this->assertTrue($statementPool->isAlive());
$this->assertSame(\time(), $statementPool->getLastUsedAt());
$statementPool->execute();
yield new Delayed(1500); // Give timeout watcher enough time to execute.
$statementPool->execute();
$this->assertTrue($statementPool->isAlive());
$this->assertSame(\time(), $statementPool->getLastUsedAt());
});
}
}