1
0
mirror of https://github.com/danog/postgres.git synced 2024-11-30 04:29:12 +01:00

Add extension specific pool tests

This commit is contained in:
Aaron Piotrowski 2017-11-05 18:12:12 -06:00
parent eb965b30dd
commit dc47cdcccc
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
5 changed files with 134 additions and 15 deletions

View File

@ -6,7 +6,7 @@ use Amp\Coroutine;
use Amp\Delayed;
use Amp\Loop;
use Amp\Postgres\CommandResult;
use Amp\Postgres\Connection;
use Amp\Postgres\Link;
use Amp\Postgres\Listener;
use Amp\Postgres\QueryError;
use Amp\Postgres\Statement;
@ -15,7 +15,7 @@ use Amp\Postgres\TransactionError;
use Amp\Postgres\TupleResult;
use PHPUnit\Framework\TestCase;
abstract class AbstractConnectionTest extends TestCase {
abstract class AbstractLinkTest extends TestCase {
/** @var \Amp\Postgres\Connection */
protected $connection;
@ -34,16 +34,12 @@ abstract class AbstractConnectionTest extends TestCase {
/**
* @param string $connectionString
*
* @return \Amp\Postgres\Connection Connection object to be tested.
* @return \Amp\Postgres\Link Connection or Link object to be tested.
*/
abstract public function createConnection(string $connectionString): Connection;
abstract public function createLink(string $connectionString): Link;
public function setUp() {
$this->connection = $this->createConnection('host=localhost user=postgres');
}
public function testIsAlive() {
$this->assertTrue($this->connection->isAlive());
$this->connection = $this->createLink('host=localhost user=postgres');
}
public function testQueryWithTupleResult() {
@ -411,6 +407,7 @@ abstract class AbstractConnectionTest extends TestCase {
$listener = yield $this->connection->listen($channel);
$this->assertInstanceOf(Listener::class, $listener);
$this->assertSame($channel, $listener->getChannel());
Loop::delay(100, function () use ($channel) {
yield $this->connection->query(\sprintf("NOTIFY %s, '%s'", $channel, '0'));

View File

@ -2,17 +2,17 @@
namespace Amp\Postgres\Test;
use Amp\Postgres\Connection;
use Amp\Postgres\Link;
use Amp\Postgres\PgSqlConnection;
/**
* @requires extension pgsql
*/
class PgSqlConnectionTest extends AbstractConnectionTest {
class PgSqlConnectionTest extends AbstractLinkTest {
/** @var resource PostgreSQL connection resource. */
protected $handle;
public function createConnection(string $connectionString): Connection {
public function createLink(string $connectionString): Link {
$this->handle = \pg_connect($connectionString);
$socket = \pg_socket($this->handle);
@ -40,4 +40,8 @@ class PgSqlConnectionTest extends AbstractConnectionTest {
\pg_query($this->handle, "ROLLBACK");
\pg_query($this->handle, "DROP TABLE test");
}
public function testIsAlive() {
$this->assertTrue($this->connection->isAlive());
}
}

60
test/PgSqlPoolTest.php Normal file
View File

@ -0,0 +1,60 @@
<?php
namespace Amp\Postgres\Test;
use Amp\Postgres\AggregatePool;
use Amp\Postgres\Link;
use Amp\Postgres\PgSqlConnection;
/**
* @requires extension pgsql
*/
class PgSqlPoolTest extends AbstractLinkTest {
/** @var resource[] PostgreSQL connection resources. */
protected $handles = [];
public function createLink(string $connectionString): Link {
$pool = new AggregatePool;
$handle = \pg_connect($connectionString, \PGSQL_CONNECT_FORCE_NEW);
$socket = \pg_socket($handle);
\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.');
}
}
$this->handles[] = $handle;
$pool->addConnection(new PgSqlConnection($handle, $socket));
$handle = \pg_connect($connectionString, \PGSQL_CONNECT_FORCE_NEW);
$socket = \pg_socket($handle);
$this->handles[] = $handle;
$pool->addConnection(new PgSqlConnection($handle, $socket));
return $pool;
}
public function tearDown() {
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");
}
}

View File

@ -2,17 +2,17 @@
namespace Amp\Postgres\Test;
use Amp\Postgres\Connection;
use Amp\Postgres\Link;
use Amp\Postgres\PqConnection;
/**
* @requires extension pq
*/
class PqConnectionTest extends AbstractConnectionTest {
class PqConnectionTest extends AbstractLinkTest {
/** @var resource PostgreSQL connection resource. */
protected $handle;
public function createConnection(string $connectionString): Connection {
public function createLink(string $connectionString): Link {
$this->handle = new \pq\Connection($connectionString);
$this->handle->exec("DROP TABLE IF EXISTS test");
@ -38,4 +38,8 @@ class PqConnectionTest extends AbstractConnectionTest {
$this->handle->exec("ROLLBACK");
$this->handle->exec("DROP TABLE test");
}
public function testIsAlive() {
$this->assertTrue($this->connection->isAlive());
}
}

54
test/PqPoolTest.php Normal file
View File

@ -0,0 +1,54 @@
<?php
namespace Amp\Postgres\Test;
use Amp\Postgres\AggregatePool;
use Amp\Postgres\Link;
use Amp\Postgres\PqConnection;
/**
* @requires extension pq
*/
class PqPoolTest extends AbstractLinkTest {
/** @var \pq\Connection[] */
protected $handles = [];
public function createLink(string $connectionString): Link {
$pool = new AggregatePool;
$handle = new \pq\Connection($connectionString);
$handle->exec("DROP TABLE IF EXISTS test");
$result = $handle->exec("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 = $handle->execParams("INSERT INTO test VALUES (\$1, \$2)", $row);
if (!$result) {
$this->fail('Could not insert test data.');
}
}
$this->handles[] = $handle;
$pool->addConnection(new PqConnection($handle));
$handle = new \pq\Connection($connectionString);
$this->handles[] = $handle;
$pool->addConnection(new PqConnection($handle));
return $pool;
}
public function tearDown() {
$this->handles[0]->exec("ROLLBACK");
$this->handles[0]->exec("DROP TABLE test");
}
}