From b370f6b2329af14098e2ec35c19fb15fd6e455dd Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Tue, 10 Jul 2018 18:36:30 -0500 Subject: [PATCH] Add pool tests --- phpunit.xml.dist | 2 +- test/AbstractPoolTest.php | 76 +++++++++++++++++++++++++++++++++ test/StatementPoolTest.php | 87 ++++++++++++++++++++++++++++++++++++++ test/bootstrap.php | 3 -- 4 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 test/AbstractPoolTest.php create mode 100644 test/StatementPoolTest.php delete mode 100644 test/bootstrap.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 228d633..41a6525 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -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" diff --git a/test/AbstractPoolTest.php b/test/AbstractPoolTest.php new file mode 100644 index 0000000..9a26b1c --- /dev/null +++ b/test/AbstractPoolTest.php @@ -0,0 +1,76 @@ +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()); + }); + } +} diff --git a/test/StatementPoolTest.php b/test/StatementPoolTest.php new file mode 100644 index 0000000..6d433a0 --- /dev/null +++ b/test/StatementPoolTest.php @@ -0,0 +1,87 @@ +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()); + }); + } +} diff --git a/test/bootstrap.php b/test/bootstrap.php deleted file mode 100644 index 991ea43..0000000 --- a/test/bootstrap.php +++ /dev/null @@ -1,3 +0,0 @@ -