Add pool tests

This commit is contained in:
Aaron Piotrowski 2018-07-10 18:36:30 -05:00
parent 1d33364698
commit b370f6b232
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
4 changed files with 164 additions and 4 deletions

View File

@ -4,7 +4,7 @@
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="test/bootstrap.php"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"

76
test/AbstractPoolTest.php Normal file
View File

@ -0,0 +1,76 @@
<?php
namespace Amp\Sql\Test;
use Amp\Delayed;
use Amp\Loop;
use Amp\Promise;
use Amp\Sql\AbstractPool;
use Amp\Sql\ConnectionConfig;
use Amp\Sql\Connector;
use Amp\Sql\Link;
use Amp\Success;
use PHPUnit\Framework\TestCase;
class AbstractPoolTest extends TestCase
{
/**
* @expectedException \Error
* @expectedExceptionMessage Pool must contain at least one connection
*/
public function testInvalidMaxConnections()
{
$mock = $this->getMockBuilder(AbstractPool::class)
->setConstructorArgs([$this->createMock(ConnectionConfig::class), 0])
->getMock();
}
public function testIdleConnectionsRemovedAfterTimeout()
{
Loop::run(function () {
$now = \time();
$connector = $this->createMock(Connector::class);
$connector->method('connect')
->willReturnCallback(function () use ($now): Promise {
$link = $this->createMock(Link::class);
$link->method('lastUsedAt')
->willReturn($now);
$link->method('isAlive')
->willReturn(true);
$link->method('query')
->willReturnCallback(function () {
return new Delayed(100);
});
return new Success($link);
});
/** @var AbstractPool $pool */
$pool = $this->getMockBuilder(AbstractPool::class)
->setConstructorArgs([$this->createMock(ConnectionConfig::class), 100, 2, $connector])
->getMockForAbstractClass();
$count = 3;
$promises = [];
for ($i = 0; $i < $count; ++$i) {
$promises[] = $pool->query("SELECT $i");
}
$results = yield $promises;
$this->assertSame($count, $pool->getConnectionCount());
yield new Delayed(1000);
$this->assertSame($count, $pool->getConnectionCount());
yield new Delayed(1000);
$this->assertSame(0, $pool->getConnectionCount());
});
}
}

View File

@ -0,0 +1,87 @@
<?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->lastUsedAt());
yield new Delayed(1500); // Give timeout watcher enough time to execute.
$statementPool->execute();
$this->assertTrue($statementPool->isAlive());
$this->assertSame(\time(), $statementPool->lastUsedAt());
});
}
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->lastUsedAt());
$statementPool->execute();
yield new Delayed(1500); // Give timeout watcher enough time to execute.
$statementPool->execute();
$this->assertTrue($statementPool->isAlive());
$this->assertSame(\time(), $statementPool->lastUsedAt());
});
}
}

View File

@ -1,3 +0,0 @@
<?php
require __DIR__.'/../vendor/autoload.php';