From dc47cdcccce0613e97afd9b8b78637588327fc01 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 5 Nov 2017 18:12:12 -0600 Subject: [PATCH] Add extension specific pool tests --- ...onnectionTest.php => AbstractLinkTest.php} | 15 ++--- test/PgSqlConnectionTest.php | 10 +++- test/PgSqlPoolTest.php | 60 +++++++++++++++++++ test/PqConnectionTest.php | 10 +++- test/PqPoolTest.php | 54 +++++++++++++++++ 5 files changed, 134 insertions(+), 15 deletions(-) rename test/{AbstractConnectionTest.php => AbstractLinkTest.php} (97%) create mode 100644 test/PgSqlPoolTest.php create mode 100644 test/PqPoolTest.php diff --git a/test/AbstractConnectionTest.php b/test/AbstractLinkTest.php similarity index 97% rename from test/AbstractConnectionTest.php rename to test/AbstractLinkTest.php index d9252c3..036e961 100644 --- a/test/AbstractConnectionTest.php +++ b/test/AbstractLinkTest.php @@ -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')); diff --git a/test/PgSqlConnectionTest.php b/test/PgSqlConnectionTest.php index 7c22862..769c175 100644 --- a/test/PgSqlConnectionTest.php +++ b/test/PgSqlConnectionTest.php @@ -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()); + } } diff --git a/test/PgSqlPoolTest.php b/test/PgSqlPoolTest.php new file mode 100644 index 0000000..76f1f3a --- /dev/null +++ b/test/PgSqlPoolTest.php @@ -0,0 +1,60 @@ +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"); + } +} diff --git a/test/PqConnectionTest.php b/test/PqConnectionTest.php index 612695a..7364c29 100644 --- a/test/PqConnectionTest.php +++ b/test/PqConnectionTest.php @@ -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()); + } } diff --git a/test/PqPoolTest.php b/test/PqPoolTest.php new file mode 100644 index 0000000..0888ab0 --- /dev/null +++ b/test/PqPoolTest.php @@ -0,0 +1,54 @@ +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"); + } +}