2017-11-06 01:12:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Postgres\Test;
|
|
|
|
|
2018-10-13 16:56:46 +02:00
|
|
|
use Amp\Loop;
|
2018-07-01 19:33:12 +02:00
|
|
|
use Amp\Postgres\ConnectionConfig;
|
2017-11-06 01:12:12 +01:00
|
|
|
use Amp\Postgres\Link;
|
2018-07-01 19:34:55 +02:00
|
|
|
use Amp\Postgres\PgSqlConnection;
|
2018-07-01 19:33:12 +02:00
|
|
|
use Amp\Postgres\Pool;
|
2018-01-03 04:55:59 +01:00
|
|
|
use Amp\Promise;
|
2018-07-01 19:33:12 +02:00
|
|
|
use Amp\Sql\Connector;
|
2018-01-03 04:55:59 +01:00
|
|
|
use Amp\Success;
|
2017-11-06 01:12:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @requires extension pgsql
|
|
|
|
*/
|
2019-04-03 21:02:26 +02:00
|
|
|
class PgSqlPoolTest extends AbstractLinkTest
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2018-01-03 04:55:59 +01:00
|
|
|
const POOL_SIZE = 3;
|
|
|
|
|
2017-11-06 01:12:12 +01:00
|
|
|
/** @var resource[] PostgreSQL connection resources. */
|
|
|
|
protected $handles = [];
|
|
|
|
|
2018-07-01 19:33:12 +02:00
|
|
|
public function createLink(string $connectionString): Link
|
|
|
|
{
|
2018-10-13 16:56:46 +02:00
|
|
|
if (Loop::get()->getHandle() instanceof \EvLoop) {
|
|
|
|
$this->markTestSkipped("ext-pgsql is not compatible with pecl-ev");
|
|
|
|
}
|
|
|
|
|
2018-01-03 04:55:59 +01:00
|
|
|
for ($i = 0; $i < self::POOL_SIZE; ++$i) {
|
|
|
|
$this->handles[] = \pg_connect($connectionString, \PGSQL_CONNECT_FORCE_NEW);
|
|
|
|
}
|
|
|
|
|
2018-03-27 05:08:46 +02:00
|
|
|
$connector = $this->createMock(Connector::class);
|
|
|
|
$connector->method('connect')
|
2018-01-03 04:55:59 +01:00
|
|
|
->will($this->returnCallback(function (): Promise {
|
|
|
|
static $count = 0;
|
|
|
|
if (!isset($this->handles[$count])) {
|
|
|
|
$this->fail("createConnection called too many times");
|
|
|
|
}
|
2018-07-01 19:34:55 +02:00
|
|
|
$handle = $this->handles[$count];
|
2018-01-03 04:55:59 +01:00
|
|
|
++$count;
|
2018-07-01 19:34:55 +02:00
|
|
|
return new Success(new PgSqlConnection($handle, \pg_socket($handle)));
|
2018-01-03 04:55:59 +01:00
|
|
|
}));
|
|
|
|
|
2018-07-17 18:20:07 +02:00
|
|
|
$pool = new Pool(new ConnectionConfig('localhost'), \count($this->handles), Pool::DEFAULT_IDLE_TIMEOUT, true, $connector);
|
2018-03-27 05:08:46 +02:00
|
|
|
|
2018-01-03 04:55:59 +01:00
|
|
|
$handle = \reset($this->handles);
|
2017-11-06 01:12:12 +01:00
|
|
|
|
|
|
|
\pg_query($handle, "DROP TABLE IF EXISTS test");
|
|
|
|
|
|
|
|
$result = \pg_query($handle, "CREATE TABLE test (domain VARCHAR(63), tld VARCHAR(63), PRIMARY KEY (domain, tld))");
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$this->fail('Could not create test table.');
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->getData() as $row) {
|
|
|
|
$result = \pg_query_params($handle, "INSERT INTO test VALUES (\$1, \$2)", $row);
|
|
|
|
|
|
|
|
if (!$result) {
|
|
|
|
$this->fail('Could not insert test data.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $pool;
|
|
|
|
}
|
|
|
|
|
2020-02-06 23:34:49 +01:00
|
|
|
public function tearDown(): void
|
2018-07-01 19:33:12 +02:00
|
|
|
{
|
2017-11-06 01:12:12 +01:00
|
|
|
foreach ($this->handles as $handle) {
|
|
|
|
\pg_get_result($handle); // Consume any leftover results from test.
|
|
|
|
}
|
|
|
|
|
|
|
|
\pg_query($this->handles[0], "ROLLBACK");
|
|
|
|
\pg_query($this->handles[0], "DROP TABLE test");
|
2018-01-03 04:55:59 +01:00
|
|
|
|
|
|
|
foreach ($this->handles as $handle) {
|
|
|
|
\pg_close($handle);
|
|
|
|
}
|
2017-11-06 01:12:12 +01:00
|
|
|
}
|
|
|
|
}
|