sql/test/AbstractPoolTest.php

77 lines
2.1 KiB
PHP
Raw Normal View History

2018-07-11 01:36:30 +02:00
<?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());
});
}
}