Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify usage by supporting new Socket API and use new default loop in test suite #136

Merged
merged 2 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ proxy servers etc.), you can explicitly pass a custom instance of the
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):

```php
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'dns' => '127.0.0.1',
'tcp' => array(
'bindto' => '192.168.10.1:0'
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"react/promise": "^2.7",
"react/promise-stream": "^1.1",
"react/promise-timer": "^1.5",
"react/socket": "^1.8"
"react/socket": "^1.9"
},
"require-dev": {
"clue/block-react": "^1.2",
Expand Down
6 changes: 3 additions & 3 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Factory
* [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
*
* ```php
* $connector = new React\Socket\Connector(null, array(
* $connector = new React\Socket\Connector(array(
* 'dns' => '127.0.0.1',
* 'tcp' => array(
* 'bindto' => '192.168.10.1:0'
Expand All @@ -62,7 +62,7 @@ class Factory
public function __construct(LoopInterface $loop = null, ConnectorInterface $connector = null)
{
$this->loop = $loop ?: Loop::get();
$this->connector = $connector ?: new Connector($this->loop);
$this->connector = $connector ?: new Connector(array(), $this->loop);
}

/**
Expand Down Expand Up @@ -95,7 +95,7 @@ public function __construct(LoopInterface $loop = null, ConnectorInterface $conn
* ```php
* $promise = $factory->createConnection($url);
*
* $loop->addTimer(3.0, function () use ($promise) {
* Loop::addTimer(3.0, function () use ($promise) {
* $promise->cancel();
* });
* ```
Expand Down
99 changes: 41 additions & 58 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace React\Tests\MySQL;

use React\EventLoop\Loop;
use React\MySQL\ConnectionInterface;
use React\MySQL\Factory;
use React\Socket\Server;
use React\Socket\SocketServer;
use React\Promise\Promise;

class FactoryTest extends BaseTestCase
Expand Down Expand Up @@ -104,21 +105,19 @@ public function testConnectWithInvalidCharsetWillRejectWithoutConnecting()

public function testConnectWithInvalidHostRejectsWithConnectionError()
{
$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString(array('host' => 'example.invalid'));
$promise = $factory->createConnection($uri);

$promise->then(null, $this->expectCallableOnce());

$loop->run();
Loop::run();
}

public function testConnectWithInvalidPassRejectsWithAuthenticationError()
{
$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString(array('passwd' => 'invalidpass'));
$promise = $factory->createConnection($uri);
Expand All @@ -132,33 +131,31 @@ public function testConnectWithInvalidPassRejectsWithAuthenticationError()
)
));

$loop->run();
Loop::run();
}

public function testConnectWillRejectWhenServerClosesConnection()
{
$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$server = new Server(0, $loop);
$server->on('connection', function ($connection) use ($server) {
$server->close();
$socket = new SocketServer('127.0.0.1:0', array());
$socket->on('connection', function ($connection) use ($socket) {
$socket->close();
$connection->close();
});

$parts = parse_url($server->getAddress());
$parts = parse_url($socket->getAddress());
$uri = $this->getConnectionString(array('host' => $parts['host'], 'port' => $parts['port']));

$promise = $factory->createConnection($uri);
$promise->then(null, $this->expectCallableOnce());

$loop->run();
Loop::run();
}

public function testConnectWillRejectOnExplicitTimeoutDespiteValidAuth()
{
$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString() . '?timeout=0';

Expand All @@ -173,13 +170,12 @@ public function testConnectWillRejectOnExplicitTimeoutDespiteValidAuth()
)
));

$loop->run();
Loop::run();
}

public function testConnectWillRejectOnDefaultTimeoutFromIniDespiteValidAuth()
{
$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString();

Expand All @@ -197,15 +193,14 @@ public function testConnectWillRejectOnDefaultTimeoutFromIniDespiteValidAuth()
)
));

$loop->run();
Loop::run();
}

public function testConnectWithValidAuthWillRunUntilQuit()
{
$this->expectOutputString('connected.closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString();
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) {
Expand All @@ -215,15 +210,14 @@ public function testConnectWithValidAuthWillRunUntilQuit()
});
}, 'printf')->then(null, 'printf');

$loop->run();
Loop::run();
}

public function testConnectWithValidAuthAndWithoutDbNameWillRunUntilQuit()
{
$this->expectOutputString('connected.closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString(array('dbname' => ''));
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) {
Expand All @@ -233,15 +227,14 @@ public function testConnectWithValidAuthAndWithoutDbNameWillRunUntilQuit()
});
}, 'printf')->then(null, 'printf');

$loop->run();
Loop::run();
}

public function testConnectWithValidAuthWillIgnoreNegativeTimeoutAndRunUntilQuit()
{
$this->expectOutputString('connected.closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString() . '?timeout=-1';
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) {
Expand All @@ -251,15 +244,14 @@ public function testConnectWithValidAuthWillIgnoreNegativeTimeoutAndRunUntilQuit
});
}, 'printf')->then(null, 'printf');

$loop->run();
Loop::run();
}

public function testConnectWithValidAuthCanPingAndThenQuit()
{
$this->expectOutputString('connected.ping.closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString();
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) {
Expand All @@ -273,15 +265,14 @@ public function testConnectWithValidAuthCanPingAndThenQuit()

}, 'printf')->then(null, 'printf');

$loop->run();
Loop::run();
}

public function testConnectWithValidAuthCanQueuePingAndQuit()
{
$this->expectOutputString('connected.ping.closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString();
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) {
Expand All @@ -294,15 +285,14 @@ public function testConnectWithValidAuthCanQueuePingAndQuit()
});
}, 'printf')->then(null, 'printf');

$loop->run();
Loop::run();
}

public function testConnectWithValidAuthQuitOnlyOnce()
{
$this->expectOutputString('connected.closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString();
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) {
Expand All @@ -315,15 +305,14 @@ public function testConnectWithValidAuthQuitOnlyOnce()
});
}, 'printf')->then(null, 'printf');

$loop->run();
Loop::run();
}

public function testConnectWithValidAuthCanCloseOnlyOnce()
{
$this->expectOutputString('connected.closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString();
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) {
Expand All @@ -339,15 +328,14 @@ public function testConnectWithValidAuthCanCloseOnlyOnce()
$connection->close();
}, 'printf')->then(null, 'printf');

$loop->run();
Loop::run();
}

public function testConnectWithValidAuthCanCloseAndAbortPing()
{
$this->expectOutputString('connected.aborted pending (Connection lost).aborted queued (Connection lost).closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString();
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) {
Expand All @@ -368,7 +356,7 @@ public function testConnectWithValidAuthCanCloseAndAbortPing()
$connection->close();
}, 'printf')->then(null, 'printf');

$loop->run();
Loop::run();
}

public function testCancelConnectWillCancelPendingConnection()
Expand Down Expand Up @@ -433,8 +421,7 @@ public function testConnectLazyWithAnyAuthWillQuitWithoutRunning()
{
$this->expectOutputString('closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = 'mysql://random:pass@host';
$connection = $factory->createLazyConnection($uri);
Expand All @@ -448,8 +435,7 @@ public function testConnectLazyWithValidAuthWillRunUntilQuitAfterPing()
{
$this->expectOutputString('closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString();
$connection = $factory->createLazyConnection($uri);
Expand All @@ -460,29 +446,27 @@ public function testConnectLazyWithValidAuthWillRunUntilQuitAfterPing()
echo 'closed.';
});

$loop->run();
Loop::run();
}

/**
* @doesNotPerformAssertions
*/
public function testConnectLazyWithValidAuthWillRunUntilIdleTimerAfterPingEvenWithoutQuit()
{
$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString() . '?idle=0';
$connection = $factory->createLazyConnection($uri);

$connection->ping();

$loop->run();
Loop::run();
}

public function testConnectLazyWithInvalidAuthWillRejectPingButWillNotEmitErrorOrClose()
{
$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString(array('passwd' => 'invalidpass'));
$connection = $factory->createLazyConnection($uri);
Expand All @@ -492,15 +476,14 @@ public function testConnectLazyWithInvalidAuthWillRejectPingButWillNotEmitErrorO

$connection->ping()->then(null, $this->expectCallableOnce());

$loop->run();
Loop::run();
}

public function testConnectLazyWithValidAuthWillPingBeforeQuitButNotAfter()
{
$this->expectOutputString('ping.closed.');

$loop = \React\EventLoop\Factory::create();
$factory = new Factory($loop);
$factory = new Factory();

$uri = $this->getConnectionString();
$connection = $factory->createLazyConnection($uri);
Expand All @@ -517,6 +500,6 @@ public function testConnectLazyWithValidAuthWillPingBeforeQuitButNotAfter()
echo 'never reached';
});

$loop->run();
Loop::run();
}
}
Loading