Skip to content

Commit

Permalink
refactor tests of connection pool to create pools within same parent …
Browse files Browse the repository at this point in the history
…class
  • Loading branch information
deminy committed Mar 5, 2024
1 parent 121a5f0 commit ff2cccb
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 96 deletions.
68 changes: 62 additions & 6 deletions tests/DatabaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Swoole\Tests;

use PHPUnit\Framework\TestCase;
use Swoole\ConnectionPool;
use Swoole\Database\MysqliConfig;
use Swoole\Database\MysqliPool;
use Swoole\Database\PDOConfig;
Expand All @@ -27,7 +28,26 @@
*/
class DatabaseTestCase extends TestCase
{
protected function getMysqliPool(): MysqliPool
protected static string $sqliteDatabaseFile;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
static::$sqliteDatabaseFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('swoole_pdo_pool_sqlite_test_', true);
if (file_exists(static::$sqliteDatabaseFile)) {
unlink(static::$sqliteDatabaseFile);
}
}

public static function tearDownAfterClass(): void
{
if (file_exists(static::$sqliteDatabaseFile)) {
unlink(static::$sqliteDatabaseFile);
}
parent::tearDownAfterClass();
}

protected static function getMysqliPool(int $size = ConnectionPool::DEFAULT_SIZE): MysqliPool
{
$config = (new MysqliConfig())
->withHost(MYSQL_SERVER_HOST)
Expand All @@ -38,10 +58,10 @@ protected function getMysqliPool(): MysqliPool
->withPassword(MYSQL_SERVER_PWD)
;

return new MysqliPool($config);
return new MysqliPool($config, $size);
}

protected function getPdoPool(): PDOPool
protected static function getPdoMysqlPool(int $size = ConnectionPool::DEFAULT_SIZE): PDOPool
{
$config = (new PDOConfig())
->withHost(MYSQL_SERVER_HOST)
Expand All @@ -52,13 +72,49 @@ protected function getPdoPool(): PDOPool
->withPassword(MYSQL_SERVER_PWD)
;

return new PDOPool($config);
return new PDOPool($config, $size);
}

protected static function getPdoPgsqlPool(int $size = ConnectionPool::DEFAULT_SIZE): PDOPool
{
$config = (new PDOConfig())
->withDriver('pgsql')
->withHost(PGSQL_SERVER_HOST)
->withPort(PGSQL_SERVER_PORT)
->withDbName(PGSQL_SERVER_DB)
->withUsername(PGSQL_SERVER_USER)
->withPassword(PGSQL_SERVER_PWD)
;

return new PDOPool($config, $size);
}

protected static function getPdoOraclePool(int $size = ConnectionPool::DEFAULT_SIZE): PDOPool
{
$config = (new PDOConfig())
->withDriver('oci')
->withHost(ORACLE_SERVER_HOST)
->withPort(ORACLE_SERVER_PORT)
->withDbName(ORACLE_SERVER_DB)
->withCharset('AL32UTF8')
->withUsername(ORACLE_SERVER_USER)
->withPassword(ORACLE_SERVER_PWD)
;

return new PDOPool($config, $size);
}

protected static function getPdoSqlitePool(int $size = ConnectionPool::DEFAULT_SIZE): PDOPool
{
$config = (new PDOConfig())->withDriver('sqlite')->withDbname(static::$sqliteDatabaseFile);

return new PDOPool($config, $size);
}

protected function getRedisPool(): RedisPool
protected static function getRedisPool(int $size = ConnectionPool::DEFAULT_SIZE): RedisPool
{
$config = (new RedisConfig())->withHost(REDIS_SERVER_HOST)->withPort(REDIS_SERVER_PORT);

return new RedisPool($config);
return new RedisPool($config, $size);
}
}
7 changes: 4 additions & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@

declare(strict_types=1);

use Swoole\Constant;
use Swoole\Coroutine;

Coroutine::set([
'log_level' => SWOOLE_LOG_INFO,
'trace_flags' => 0,
Constant::OPTION_LOG_LEVEL => SWOOLE_LOG_INFO,
Constant::OPTION_TRACE_FLAGS => 0,
]);

if (!defined('SWOOLE_LIBRARY')) {
require dirname(__DIR__) . '/vendor/autoload.php';
require_once dirname(__DIR__) . '/vendor/autoload.php';
}

if (!defined('MYSQL_SERVER_HOST')) {
Expand Down
78 changes: 10 additions & 68 deletions tests/unit/Database/PDOPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

namespace Swoole\Database;

use PHPUnit\Framework\TestCase;
use Swoole\Coroutine;
use Swoole\Coroutine\WaitGroup;
use Swoole\Tests\DatabaseTestCase;
use Swoole\Tests\HookFlagsTrait;

use function Swoole\Coroutine\go;
Expand All @@ -25,46 +25,18 @@
* @internal
* @coversNothing
*/
class PDOPoolTest extends TestCase
class PDOPoolTest extends DatabaseTestCase
{
use HookFlagsTrait;

protected static string $sqliteDatabaseFile;

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
self::$sqliteDatabaseFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'swoole_pdo_pool_sqlite_test.db';
if (file_exists(self::$sqliteDatabaseFile)) {
unlink(self::$sqliteDatabaseFile);
}
}

public static function tearDownAfterClass(): void
{
if (file_exists(self::$sqliteDatabaseFile)) {
unlink(self::$sqliteDatabaseFile);
}
parent::tearDownAfterClass();
}

public function testPutWhenErrorHappens()
{
self::saveHookFlags();
self::setHookFlags(SWOOLE_HOOK_ALL);
$expect = ['0', '1', '2', '3', '4'];
$actual = [];
Coroutine\run(function () use (&$actual) {
$config = (new PDOConfig())
->withHost(MYSQL_SERVER_HOST)
->withPort(MYSQL_SERVER_PORT)
->withDbName(MYSQL_SERVER_DB)
->withCharset('utf8mb4')
->withUsername(MYSQL_SERVER_USER)
->withPassword(MYSQL_SERVER_PWD)
;

$pool = new PDOPool($config, 2);
$pool = self::getPdoMysqlPool(2);
for ($n = 5; $n--;) {
Coroutine::create(function () use ($pool, $n, &$actual) {
$pdo = $pool->get();
Expand Down Expand Up @@ -95,17 +67,8 @@ public function testPostgres(): void
self::saveHookFlags();
self::setHookFlags(SWOOLE_HOOK_ALL);
run(function () {
$config = (new PDOConfig())
->withDriver('pgsql')
->withHost(PGSQL_SERVER_HOST)
->withPort(PGSQL_SERVER_PORT)
->withDbName(PGSQL_SERVER_DB)
->withUsername(PGSQL_SERVER_USER)
->withPassword(PGSQL_SERVER_PWD)
;
$pool = new PDOPool($config, 10);

$pdo = $pool->get();
$pool = self::getPdoPgsqlPool(10);
$pdo = $pool->get();
$pdo->exec('CREATE TABLE IF NOT EXISTS test(id INT);');
$pool->put($pdo);

Expand Down Expand Up @@ -137,18 +100,8 @@ public function testOracle(): void
self::saveHookFlags();
self::setHookFlags(SWOOLE_HOOK_ALL);
run(function () {
$config = (new PDOConfig())
->withDriver('oci')
->withHost(ORACLE_SERVER_HOST)
->withPort(ORACLE_SERVER_PORT)
->withDbName(ORACLE_SERVER_DB)
->withCharset('AL32UTF8')
->withUsername(ORACLE_SERVER_USER)
->withPassword(ORACLE_SERVER_PWD)
;
$pool = new PDOPool($config, 10);

$pdo = $pool->get();
$pool = self::getPdoOraclePool(10);
$pdo = $pool->get();
try {
$pdo->exec('DROP TABLE test PURGE');
} catch (\PDOException $e) {
Expand Down Expand Up @@ -187,10 +140,8 @@ public function testSqlite(): void
self::saveHookFlags();
self::setHookFlags(SWOOLE_HOOK_ALL);
run(function () {
$config = (new PDOConfig())->withDriver('sqlite')->withDbname(self::$sqliteDatabaseFile);
$pool = new PDOPool($config, 10);

$pdo = $pool->get();
$pool = self::getPdoSqlitePool(10);
$pdo = $pool->get();
$pdo->exec('CREATE TABLE IF NOT EXISTS test(id INT);');
$pool->put($pdo);

Expand Down Expand Up @@ -222,16 +173,7 @@ public function testTimeoutException(): void
self::saveHookFlags();
self::setHookFlags(SWOOLE_HOOK_ALL);
run(function () {
$config = (new PDOConfig())
->withHost(MYSQL_SERVER_HOST)
->withPort(MYSQL_SERVER_PORT)
->withDbName(MYSQL_SERVER_DB)
->withCharset('utf8mb4')
->withUsername(MYSQL_SERVER_USER)
->withPassword(MYSQL_SERVER_PWD)
;

$pool = new PDOPool($config, 1);
$pool = self::getPdoMysqlPool(1);
$waitGroup = new WaitGroup(2); // A wait group to wait for the next 2 coroutines to finish.

go(function () use ($pool, $waitGroup) {
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Database/PDOStatementProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testRun()
{
Coroutine\run(function () {
self::assertFalse(
$this->getPdoPool()->get()->query("SHOW TABLES like 'NON_EXISTING_TABLE_NAME'")->fetch(\PDO::FETCH_ASSOC),
self::getPdoMysqlPool()->get()->query("SHOW TABLES like 'NON_EXISTING_TABLE_NAME'")->fetch(\PDO::FETCH_ASSOC),
'FALSE is returned if no results found.'
);
});
Expand Down Expand Up @@ -75,7 +75,7 @@ public static function dataSetFetchMode(): array
public function testSetFetchMode(array $expected, array $args, string $message)
{
Coroutine\run(function () use ($expected, $args, $message) {
$stmt = $this->getPdoPool()->get()->query(
$stmt = self::getPdoMysqlPool()->get()->query(
'SELECT
*
FROM (
Expand All @@ -95,7 +95,7 @@ public function testSetFetchMode(array $expected, array $args, string $message)
public function testBindParam()
{
Coroutine\run(function () {
$stmt = $this->getPdoPool()->get()->prepare('SHOW TABLES like ?');
$stmt = self::getPdoMysqlPool()->get()->prepare('SHOW TABLES like ?');
$table = 'NON_EXISTING_TABLE_NAME';
$stmt->bindParam(1, $table, \PDO::PARAM_STR);
$stmt->execute();
Expand Down
44 changes: 28 additions & 16 deletions tests/unit/ObjectProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Swoole;

use Swoole\Database\MysqliProxy;
use Swoole\Database\ObjectProxy;
use Swoole\Database\PDOProxy;
use Swoole\Tests\DatabaseTestCase;

/**
Expand All @@ -21,30 +24,39 @@
*/
class ObjectProxyTest extends DatabaseTestCase
{
public function tearDown(): void
/**
* @return array<array<ObjectProxy>>
*/
public static function dateClone(): array
{
parent::tearDown();
$this->addToAssertionCount(2); // Add those in method $this->testClone().
return [
[[self::class, 'getMysqliPool'], MysqliProxy::class],
[[self::class, 'getPdoMysqlPool'], PDOProxy::class],
[[self::class, 'getPdoOraclePool'], PDOProxy::class],
[[self::class, 'getPdoPgsqlPool'], PDOProxy::class],
[[self::class, 'getPdoSqlitePool'], PDOProxy::class],
];
}

/**
* @param class-string<ObjectProxy> $class
* @dataProvider dateClone
* @covers \Swoole\Database\ObjectProxy::__clone()
*/
public function testClone()
public function testClone(callable $callback, string $class): void
{
Coroutine\run(function () {
$dbs = [
$this->getPdoPool()->get(),
$this->getMysqliPool()->get(),
];
Coroutine\run(function () use ($callback, $class): void {
$pool = $callback();
self::assertInstanceOf(ConnectionPool::class, $pool);
/** @var ConnectionPool $pool */
$proxy = $pool->get();
self::assertInstanceOf($class, $proxy);

foreach ($dbs as $db) {
try {
var_dump(clone $db);
} catch (\Error $e) {
if ($e->getMessage() != 'Trying to clone an uncloneable database proxy object') {
throw $e;
}
try {
clone $proxy;
} catch (\Error $e) {
if ($e->getMessage() != 'Trying to clone an uncloneable database proxy object') {
throw $e;
}
}
});
Expand Down

0 comments on commit ff2cccb

Please sign in to comment.