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

Rename close() to quit() and use promises for quit() method #65

Merged
merged 2 commits into from
Jul 5, 2018
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
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $factory->createConnection($uri)->then(function (ConnectionInterface $connection
}
);

$connection->close();
$connection->quit();
});

$loop->run();
Expand Down Expand Up @@ -259,6 +259,22 @@ $connection->ping()->then(function () {
});
```

#### quit()

The `quit(): PromiseInterface<void, Exception>` method can be used to
quit (soft-close) the connection.

This method returns a promise that will resolve (with a void value) on
success or will reject with an `Exception` on error. The MySQL protocol
is inherently sequential, so that all commands will be performed in order
and outstanding commands will be put into a queue to be executed once the
previous commands are completed.

```php
$connection->query('CREATE TABLE test ...');
$connection->quit();
```

## Install

The recommended way to install this library is [through Composer](https://getcomposer.org).
Expand Down
2 changes: 1 addition & 1 deletion examples/01-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
echo 'Error: ' . $error->getMessage() . PHP_EOL;
});

$connection->close();
$connection->quit();
}, 'printf');

$loop->run();
2 changes: 1 addition & 1 deletion examples/02-query-stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
echo 'CLOSED' . PHP_EOL;
});

$connection->close();
$connection->quit();
}, 'printf');

$loop->run();
6 changes: 2 additions & 4 deletions examples/11-interactive.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
if ($query === 'exit') {
// exit command should close the connection
echo 'bye.' . PHP_EOL;
$connection->close();
$connection->quit();
return;
}

Expand Down Expand Up @@ -74,9 +74,7 @@

// close connection when STDIN closes (EOF or CTRL+D)
$stdin->on('close', function () use ($connection) {
if ($connection->getState() === ConnectionInterface::STATE_AUTHENTICATED) {
$connection->close();
}
$connection->quit();
});

// close STDIN (stop reading) when connection closes
Expand Down
21 changes: 12 additions & 9 deletions src/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,20 @@ public function getServerOptions();
public function getState();

/**
* Close the connection.
* Quits (soft-close) the connection.
*
* @param callable|null $callback A callback which should be run after
* connection successfully closed.
*
* $callback signature:
* This method returns a promise that will resolve (with a void value) on
* success or will reject with an `Exception` on error. The MySQL protocol
* is inherently sequential, so that all commands will be performed in order
* and outstanding commands will be put into a queue to be executed once the
* previous commands are completed.
*
* function (ConnectionInterface $conn): void
* ```php
* $connection->query('CREATE TABLE test ...');
* $connection->quit();
* ```
*
* @return void
* @throws Exception if the connection is not initialized or already closed/closing
* @return PromiseInterface Returns a Promise<void,Exception>
*/
public function close($callback = null);
public function quit();
}
28 changes: 14 additions & 14 deletions src/Io/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,21 +215,21 @@ public function getState()
return $this->state;
}

/**
* {@inheritdoc}
*/
public function close($callback = null)
public function quit()
{
$this->_doCommand(new QuitCommand($this))
->on('success', function () use ($callback) {
$this->state = self::STATE_CLOSED;
$this->emit('end', [$this]);
$this->emit('close', [$this]);
if (is_callable($callback)) {
$callback($this);
}
});
$this->state = self::STATE_CLOSEING;
return new Promise(function ($resolve, $reject) {
$this->_doCommand(new QuitCommand($this))
->on('error', function ($reason) use ($reject) {
$reject($reason);
})
->on('success', function () use ($resolve) {
$this->state = self::STATE_CLOSED;
$this->emit('end', [$this]);
$this->emit('close', [$this]);
$resolve();
});
$this->state = self::STATE_CLOSEING;
});
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testConnectWithValidAuthWillRunUtilClose()
$uri = $this->getConnectionString();
$factory->createConnection($uri)->then(function (ConnectionInterface $connection) {
echo 'connected.';
$connection->close(function ($e) {
$connection->quit()->then(function () {
echo 'closed.';
});
}, 'printf')->then(null, 'printf');
Expand All @@ -127,7 +127,7 @@ public function testConnectWithValidAuthCanPingAndClose()
$connection->ping()->then(function () {
echo 'ping.';
});
$connection->close(function ($e) {
$connection->quit()->then(function () {
echo 'closed.';
});
}, 'printf')->then(null, 'printf');
Expand Down
56 changes: 39 additions & 17 deletions tests/Io/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,19 @@ public function testConnectTwiceThrowsExceptionForSecondCall()
$conn->doConnect(function () { });
}

/**
* @expectedException React\MySQL\Exception
* @expectedExceptionMessage Can't send command
*/
public function testCloseWithoutConnectThrows()
public function testQuitWithoutConnectRejects()
{
$options = $this->getConnectionOptions();
$loop = \React\EventLoop\Factory::create();
$conn = new Connection($loop, $options);

$conn->close(function () { });
$conn->quit()->done(
$this->expectCallableNever(),
function (\Exception $error) {
$this->assertInstanceOf('React\MySQL\Exception', $error);
$this->assertSame('Can\'t send command', $error->getMessage());
}
);
}

public function testQueryWithoutConnectRejects()
Expand Down Expand Up @@ -99,7 +101,7 @@ function (\Exception $error) {
);
}

public function testCloseWhileConnectingWillBeQueuedAfterConnection()
public function testQuitWhileConnectingWillBeQueuedAfterConnection()
{
$this->expectOutputString('connectedclosed');
$options = $this->getConnectionOptions();
Expand All @@ -109,14 +111,34 @@ public function testCloseWhileConnectingWillBeQueuedAfterConnection()
$conn->doConnect(function ($err) {
echo $err ? $err : 'connected';
});
$conn->close(function () {
$conn->quit()->then(function () {
echo 'closed';
});

$loop->run();
}

public function testPingAfterConnectWillEmitErrorWhenServerClosesConnection()
public function testQuitAfterQuitWhileConnectingWillBeRejected()
{
$options = $this->getConnectionOptions();
$loop = \React\EventLoop\Factory::create();
$conn = new Connection($loop, $options);

$conn->doConnect(function ($err) { });
$conn->quit();

$conn->quit()->done(
$this->expectCallableNever(),
function (\Exception $error) {
$this->assertInstanceOf('React\MySQL\Exception', $error);
$this->assertSame('Can\'t send command', $error->getMessage());
}
);

$loop->run();
}

public function testConnectWillEmitErrorWhenServerClosesConnection()
{
$this->expectOutputString('Connection lost');

Expand All @@ -142,7 +164,7 @@ public function testPingAfterConnectWillEmitErrorWhenServerClosesConnection()
$loop->run();
}

public function testConnectWillEmitErrorWhenServerClosesConnection()
public function testPingAfterConnectWillEmitErrorWhenServerClosesConnection()
{
$this->expectOutputString('Connection lost');

Expand Down Expand Up @@ -172,7 +194,7 @@ function ($err) {
$loop->run();
}

public function testPingAndCloseWhileConnectingWillBeQueuedAfterConnection()
public function testPingAndQuitWhileConnectingWillBeQueuedAfterConnection()
{
$this->expectOutputString('connectedpingclosed');
$options = $this->getConnectionOptions();
Expand All @@ -187,14 +209,14 @@ public function testPingAndCloseWhileConnectingWillBeQueuedAfterConnection()
}, function () {
echo $err;
});
$conn->close(function () {
$conn->quit()->then(function () {
echo 'closed';
});

$loop->run();
}

public function testPingAfterCloseWhileConnectingRejectsImmediately()
public function testPingAfterQuitWhileConnectingRejectsImmediately()
{
$this->expectOutputString('connectedclosed');
$options = $this->getConnectionOptions();
Expand All @@ -204,7 +226,7 @@ public function testPingAfterCloseWhileConnectingRejectsImmediately()
$conn->doConnect(function ($err) {
echo $err ? $err : 'connected';
});
$conn->close(function () {
$conn->quit()->then(function () {
echo 'closed';
});

Expand All @@ -217,7 +239,7 @@ public function testPingAfterCloseWhileConnectingRejectsImmediately()
$loop->run();
}

public function testCloseWhileConnectingWithInvalidPassWillNeverFire()
public function testQuitWhileConnectingWithInvalidPassWillNeverFire()
{
$this->expectOutputString('error');
$options = $this->getConnectionOptions();
Expand All @@ -227,7 +249,7 @@ public function testCloseWhileConnectingWithInvalidPassWillNeverFire()
$conn->doConnect(function ($err) {
echo $err ? 'error' : 'connected';
});
$conn->close(function () {
$conn->quit()->then(function () {
echo 'never';
});

Expand Down Expand Up @@ -260,7 +282,7 @@ public function testConnectWithValidPass()
});

$conn->ping()->then(function () use ($loop, $conn) {
$conn->close(function ($conn) {
$conn->quit()->done(function () use ($conn) {
$this->assertEquals($conn::STATE_CLOSED, $conn->getState());
});
});
Expand Down
8 changes: 4 additions & 4 deletions tests/NoResultQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function setUp()
$connection->query('DROP TABLE IF EXISTS book');
$connection->query($this->getDataTable());

$connection->close();
$connection->quit();
$loop->run();
}

Expand All @@ -28,7 +28,7 @@ public function testUpdateSimpleNonExistentReportsNoAffectedRows()
$this->assertEquals(0, $command->affectedRows);
});

$connection->close();
$connection->quit();
$loop->run();
}

Expand All @@ -42,7 +42,7 @@ public function testInsertSimpleReportsFirstInsertId()
$this->assertEquals(1, $command->insertId);
});

$connection->close();
$connection->quit();
$loop->run();
}

Expand All @@ -56,7 +56,7 @@ public function testUpdateSimpleReportsAffectedRow()
$this->assertEquals(1, $command->affectedRows);
});

$connection->close();
$connection->quit();
$loop->run();
}
}
Loading