2017-01-18 18:05:05 +01:00
|
|
|
<?php
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
namespace Amp\Postgres\Test;
|
|
|
|
|
2017-07-27 07:32:34 +02:00
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Delayed;
|
2017-05-26 17:47:44 +02:00
|
|
|
use Amp\Loop;
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\Postgres\CommandResult;
|
2017-11-06 01:12:12 +01:00
|
|
|
use Amp\Postgres\Link;
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\Postgres\Listener;
|
2017-07-27 07:32:34 +02:00
|
|
|
use Amp\Postgres\QueryError;
|
2017-07-28 06:20:16 +02:00
|
|
|
use Amp\Postgres\Statement;
|
2017-06-21 05:17:53 +02:00
|
|
|
use Amp\Postgres\Transaction;
|
|
|
|
use Amp\Postgres\TransactionError;
|
|
|
|
use Amp\Postgres\TupleResult;
|
2017-05-26 17:47:44 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
2017-11-06 01:12:12 +01:00
|
|
|
abstract class AbstractLinkTest extends TestCase {
|
2016-09-14 16:27:39 +02:00
|
|
|
/** @var \Amp\Postgres\Connection */
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array Start test data for database.
|
|
|
|
*/
|
|
|
|
public function getData() {
|
|
|
|
return [
|
|
|
|
['amphp', 'org'],
|
|
|
|
['github', 'com'],
|
|
|
|
['google', 'com'],
|
|
|
|
['php', 'net'],
|
|
|
|
];
|
|
|
|
}
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2017-06-15 05:46:25 +02:00
|
|
|
/**
|
|
|
|
* @param string $connectionString
|
|
|
|
*
|
2017-11-06 01:12:12 +01:00
|
|
|
* @return \Amp\Postgres\Link Connection or Link object to be tested.
|
2017-06-15 05:46:25 +02:00
|
|
|
*/
|
2017-11-06 01:12:12 +01:00
|
|
|
abstract public function createLink(string $connectionString): Link;
|
2016-09-14 16:27:39 +02:00
|
|
|
|
|
|
|
public function setUp() {
|
2017-11-06 01:12:12 +01:00
|
|
|
$this->connection = $this->createLink('host=localhost user=postgres');
|
2017-11-05 22:38:17 +01:00
|
|
|
}
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
public function testQueryWithTupleResult() {
|
2017-03-17 16:17:24 +01:00
|
|
|
Loop::run(function () {
|
2016-09-14 16:27:39 +02:00
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $this->connection->query("SELECT * FROM test");
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TupleResult::class, $result);
|
|
|
|
|
|
|
|
$this->assertSame(2, $result->numFields());
|
2017-05-16 06:28:37 +02:00
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
$data = $this->getData();
|
|
|
|
|
2016-12-30 07:10:43 +01:00
|
|
|
for ($i = 0; yield $result->advance(); ++$i) {
|
2016-09-14 16:27:39 +02:00
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[$i][0], $row['domain']);
|
|
|
|
$this->assertSame($data[$i][1], $row['tld']);
|
|
|
|
}
|
2017-03-17 16:17:24 +01:00
|
|
|
});
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
2017-11-06 01:50:52 +01:00
|
|
|
public function testQueryWithUnconsumedTupleResult() {
|
|
|
|
Loop::run(function () {
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $this->connection->query("SELECT * FROM test");
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TupleResult::class, $result);
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $this->connection->query("SELECT * FROM test");
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TupleResult::class, $result);
|
|
|
|
|
|
|
|
$data = $this->getData();
|
|
|
|
|
|
|
|
for ($i = 0; yield $result->advance(); ++$i) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[$i][0], $row['domain']);
|
|
|
|
$this->assertSame($data[$i][1], $row['tld']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
public function testQueryWithCommandResult() {
|
2017-03-17 16:17:24 +01:00
|
|
|
Loop::run(function () {
|
2016-09-14 16:27:39 +02:00
|
|
|
/** @var \Amp\Postgres\CommandResult $result */
|
|
|
|
$result = yield $this->connection->query("INSERT INTO test VALUES ('canon', 'jp')");
|
|
|
|
|
|
|
|
$this->assertInstanceOf(CommandResult::class, $result);
|
|
|
|
$this->assertSame(1, $result->affectedRows());
|
2017-03-17 16:17:24 +01:00
|
|
|
});
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Postgres\QueryError
|
|
|
|
*/
|
|
|
|
public function testQueryWithEmptyQuery() {
|
2017-03-17 16:17:24 +01:00
|
|
|
Loop::run(function () {
|
2016-09-14 16:27:39 +02:00
|
|
|
/** @var \Amp\Postgres\CommandResult $result */
|
|
|
|
$result = yield $this->connection->query('');
|
2017-03-17 16:17:24 +01:00
|
|
|
});
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Amp\Postgres\QueryError
|
|
|
|
*/
|
|
|
|
public function testQueryWithSyntaxError() {
|
2017-03-17 16:17:24 +01:00
|
|
|
Loop::run(function () {
|
2016-09-14 16:27:39 +02:00
|
|
|
/** @var \Amp\Postgres\CommandResult $result */
|
|
|
|
$result = yield $this->connection->query("SELECT & FROM test");
|
2017-03-17 16:17:24 +01:00
|
|
|
});
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrepare() {
|
2017-03-17 16:17:24 +01:00
|
|
|
Loop::run(function () {
|
2016-09-14 16:27:39 +02:00
|
|
|
$query = "SELECT * FROM test WHERE domain=\$1";
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\Statement $statement */
|
|
|
|
$statement = yield $this->connection->prepare($query);
|
|
|
|
|
|
|
|
$this->assertSame($query, $statement->getQuery());
|
|
|
|
|
|
|
|
$data = $this->getData()[0];
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $statement->execute($data[0]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TupleResult::class, $result);
|
|
|
|
|
|
|
|
$this->assertSame(2, $result->numFields());
|
2017-05-16 06:14:02 +02:00
|
|
|
|
2016-12-30 07:10:43 +01:00
|
|
|
while (yield $result->advance()) {
|
2016-09-14 16:27:39 +02:00
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[0], $row['domain']);
|
|
|
|
$this->assertSame($data[1], $row['tld']);
|
|
|
|
}
|
2017-03-17 16:17:24 +01:00
|
|
|
});
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
2017-07-31 07:34:05 +02:00
|
|
|
/**
|
|
|
|
* @depends testPrepare
|
|
|
|
* @expectedException \Amp\Postgres\QueryError
|
|
|
|
* @expectedExceptionMessage column "invalid" does not exist
|
|
|
|
*/
|
|
|
|
public function testPrepareInvalidQuery() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$query = "SELECT * FROM test WHERE invalid=\$1";
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\Statement $statement */
|
|
|
|
$statement = yield $this->connection->prepare($query);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-28 06:20:16 +02:00
|
|
|
/**
|
|
|
|
* @depends testPrepare
|
|
|
|
*/
|
|
|
|
public function testPrepareSameQuery() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$sql = "SELECT * FROM test WHERE domain=\$1";
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\Statement $statement1 */
|
|
|
|
$statement1 = yield $this->connection->prepare($sql);
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\Statement $statement2 */
|
|
|
|
$statement2 = yield $this->connection->prepare($sql);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Statement::class, $statement1);
|
|
|
|
$this->assertInstanceOf(Statement::class, $statement2);
|
|
|
|
|
|
|
|
unset($statement1);
|
|
|
|
|
|
|
|
$data = $this->getData()[0];
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $statement2->execute($data[0]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TupleResult::class, $result);
|
|
|
|
|
|
|
|
$this->assertSame(2, $result->numFields());
|
|
|
|
|
|
|
|
while (yield $result->advance()) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[0], $row['domain']);
|
|
|
|
$this->assertSame($data[1], $row['tld']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testPrepareSameQuery
|
|
|
|
*/
|
|
|
|
public function testSimultaneousPrepareSameQuery() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$sql = "SELECT * FROM test WHERE domain=\$1";
|
|
|
|
|
|
|
|
$statement1 = $this->connection->prepare($sql);
|
|
|
|
$statement2 = $this->connection->prepare($sql);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Amp\Postgres\Statement $statement1
|
|
|
|
* @var \Amp\Postgres\Statement $statement2
|
|
|
|
*/
|
|
|
|
list($statement1, $statement2) = yield [$statement1, $statement2];
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Statement::class, $statement1);
|
|
|
|
$this->assertInstanceOf(Statement::class, $statement2);
|
|
|
|
|
|
|
|
$data = $this->getData()[0];
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $statement1->execute($data[0]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TupleResult::class, $result);
|
|
|
|
|
|
|
|
$this->assertSame(2, $result->numFields());
|
|
|
|
|
|
|
|
while (yield $result->advance()) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[0], $row['domain']);
|
|
|
|
$this->assertSame($data[1], $row['tld']);
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($statement1);
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $statement2->execute($data[0]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TupleResult::class, $result);
|
|
|
|
|
|
|
|
$this->assertSame(2, $result->numFields());
|
|
|
|
|
|
|
|
while (yield $result->advance()) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[0], $row['domain']);
|
|
|
|
$this->assertSame($data[1], $row['tld']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
public function testExecute() {
|
2017-03-17 16:17:24 +01:00
|
|
|
Loop::run(function () {
|
2016-09-14 16:27:39 +02:00
|
|
|
$data = $this->getData()[0];
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $this->connection->execute("SELECT * FROM test WHERE domain=\$1", $data[0]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(TupleResult::class, $result);
|
|
|
|
|
|
|
|
$this->assertSame(2, $result->numFields());
|
|
|
|
|
2016-12-30 07:10:43 +01:00
|
|
|
while (yield $result->advance()) {
|
2016-09-14 16:27:39 +02:00
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[0], $row['domain']);
|
|
|
|
$this->assertSame($data[1], $row['tld']);
|
|
|
|
}
|
2017-03-17 16:17:24 +01:00
|
|
|
});
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
2017-11-06 06:06:17 +01:00
|
|
|
/**
|
|
|
|
* @depends testExecute
|
|
|
|
* @expectedException \Amp\Postgres\QueryError
|
|
|
|
* @expectedExceptionMessage bind message supplies 0 parameters
|
|
|
|
*/
|
|
|
|
public function testExecuteWithInvalidParams() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$result = yield $this->connection->execute("SELECT * FROM test WHERE domain=\$1");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-14 16:27:39 +02:00
|
|
|
/**
|
2017-07-27 07:32:34 +02:00
|
|
|
* @depends testQueryWithTupleResult
|
2016-09-14 16:27:39 +02:00
|
|
|
*/
|
|
|
|
public function testSimultaneousQuery() {
|
2017-07-27 07:32:34 +02:00
|
|
|
$callback = \Amp\coroutine(function ($value) {
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $this->connection->query("SELECT {$value} as value");
|
|
|
|
|
|
|
|
if ($value) {
|
|
|
|
yield new Delayed(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (yield $result->advance()) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertEquals($value, $row['value']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Loop::run(function () use ($callback) {
|
2017-07-28 06:20:16 +02:00
|
|
|
yield [$callback(0), $callback(1)];
|
2017-07-27 07:32:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testSimultaneousQuery
|
|
|
|
*/
|
2017-11-06 17:18:04 +01:00
|
|
|
public function testSimultaneousQueryWithOneFailing() {
|
2017-07-27 07:32:34 +02:00
|
|
|
$callback = \Amp\coroutine(function ($query) {
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $this->connection->query($query);
|
|
|
|
|
|
|
|
$data = $this->getData();
|
2017-05-16 06:14:02 +02:00
|
|
|
|
2017-07-27 07:32:34 +02:00
|
|
|
for ($i = 0; yield $result->advance(); ++$i) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[$i][0], $row['domain']);
|
|
|
|
$this->assertSame($data[$i][1], $row['tld']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
Loop::run(function () use (&$result, $callback) {
|
|
|
|
$successful = $callback("SELECT * FROM test");
|
2017-11-06 17:18:04 +01:00
|
|
|
$failing = $callback("SELECT & FROM test");
|
2017-07-27 07:32:34 +02:00
|
|
|
|
|
|
|
$result = yield $successful;
|
|
|
|
yield $failing;
|
|
|
|
});
|
|
|
|
} catch (QueryError $exception) {
|
|
|
|
$this->assertInstanceOf(TupleResult::class, $result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail(\sprintf("Test did not throw an instance of %s", QueryError::class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimultaneousQueryAndPrepare() {
|
|
|
|
$promises = [];
|
|
|
|
$promises[] = new Coroutine((function () {
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $this->connection->query("SELECT * FROM test");
|
|
|
|
|
|
|
|
$data = $this->getData();
|
|
|
|
|
|
|
|
for ($i = 0; yield $result->advance(); ++$i) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[$i][0], $row['domain']);
|
|
|
|
$this->assertSame($data[$i][1], $row['tld']);
|
|
|
|
}
|
|
|
|
})());
|
|
|
|
|
|
|
|
$promises[] = new Coroutine((function () {
|
|
|
|
/** @var \Amp\Postgres\Statement $statement */
|
|
|
|
$statement = (yield $this->connection->prepare("SELECT * FROM test"));
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $statement->execute();
|
|
|
|
|
|
|
|
$data = $this->getData();
|
|
|
|
|
|
|
|
for ($i = 0; yield $result->advance(); ++$i) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[$i][0], $row['domain']);
|
|
|
|
$this->assertSame($data[$i][1], $row['tld']);
|
|
|
|
}
|
|
|
|
})());
|
|
|
|
|
|
|
|
Loop::run(function () use ($promises) {
|
2017-07-28 06:20:16 +02:00
|
|
|
yield $promises;
|
2017-07-27 07:32:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSimultaneousPrepareAndExecute() {
|
|
|
|
$promises[] = new Coroutine((function () {
|
|
|
|
/** @var \Amp\Postgres\Statement $statement */
|
|
|
|
$statement = yield $this->connection->prepare("SELECT * FROM test");
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $statement->execute();
|
|
|
|
|
|
|
|
$data = $this->getData();
|
|
|
|
|
|
|
|
for ($i = 0; yield $result->advance(); ++$i) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[$i][0], $row['domain']);
|
|
|
|
$this->assertSame($data[$i][1], $row['tld']);
|
|
|
|
}
|
|
|
|
})());
|
|
|
|
|
|
|
|
$promises[] = new Coroutine((function () {
|
|
|
|
/** @var \Amp\Postgres\TupleResult $result */
|
|
|
|
$result = yield $this->connection->execute("SELECT * FROM test");
|
|
|
|
|
|
|
|
$data = $this->getData();
|
|
|
|
|
|
|
|
for ($i = 0; yield $result->advance(); ++$i) {
|
|
|
|
$row = $result->getCurrent();
|
|
|
|
$this->assertSame($data[$i][0], $row['domain']);
|
|
|
|
$this->assertSame($data[$i][1], $row['tld']);
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-07-27 07:32:34 +02:00
|
|
|
})());
|
2017-05-16 06:14:02 +02:00
|
|
|
|
2017-07-27 07:32:34 +02:00
|
|
|
Loop::run(function () use ($promises) {
|
2017-07-28 06:20:16 +02:00
|
|
|
yield $promises;
|
2017-03-17 16:17:24 +01:00
|
|
|
});
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTransaction() {
|
2017-03-17 16:17:24 +01:00
|
|
|
Loop::run(function () {
|
2016-09-14 16:27:39 +02:00
|
|
|
$isolation = Transaction::COMMITTED;
|
|
|
|
|
|
|
|
/** @var \Amp\Postgres\Transaction $transaction */
|
|
|
|
$transaction = yield $this->connection->transaction($isolation);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Transaction::class, $transaction);
|
|
|
|
|
|
|
|
$data = $this->getData()[0];
|
|
|
|
|
2017-11-05 22:38:17 +01:00
|
|
|
$this->assertTrue($transaction->isAlive());
|
2016-09-14 16:27:39 +02:00
|
|
|
$this->assertTrue($transaction->isActive());
|
|
|
|
$this->assertSame($isolation, $transaction->getIsolationLevel());
|
|
|
|
|
|
|
|
yield $transaction->savepoint('test');
|
|
|
|
|
|
|
|
$result = yield $transaction->execute("SELECT * FROM test WHERE domain=\$1 FOR UPDATE", $data[0]);
|
|
|
|
|
|
|
|
yield $transaction->rollbackTo('test');
|
|
|
|
|
|
|
|
yield $transaction->commit();
|
|
|
|
|
2017-11-05 22:38:17 +01:00
|
|
|
$this->assertFalse($transaction->isAlive());
|
2016-09-14 16:27:39 +02:00
|
|
|
$this->assertFalse($transaction->isActive());
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = yield $transaction->execute("SELECT * FROM test");
|
|
|
|
$this->fail('Query should fail after transaction commit');
|
|
|
|
} catch (TransactionError $exception) {
|
|
|
|
// Exception expected.
|
|
|
|
}
|
2017-03-17 16:17:24 +01:00
|
|
|
});
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|
2017-05-16 06:14:02 +02:00
|
|
|
|
2017-06-13 07:34:20 +02:00
|
|
|
public function testListen() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$channel = "test";
|
|
|
|
/** @var \Amp\Postgres\Listener $listener */
|
|
|
|
$listener = yield $this->connection->listen($channel);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Listener::class, $listener);
|
2017-11-06 01:12:12 +01:00
|
|
|
$this->assertSame($channel, $listener->getChannel());
|
2017-06-13 07:34:20 +02:00
|
|
|
|
2017-06-21 07:32:52 +02:00
|
|
|
Loop::delay(100, function () use ($channel) {
|
2017-06-21 06:04:54 +02:00
|
|
|
yield $this->connection->query(\sprintf("NOTIFY %s, '%s'", $channel, '0'));
|
|
|
|
yield $this->connection->query(\sprintf("NOTIFY %s, '%s'", $channel, '1'));
|
|
|
|
});
|
2017-06-13 07:34:20 +02:00
|
|
|
|
|
|
|
$count = 0;
|
2017-06-21 07:32:52 +02:00
|
|
|
Loop::delay(200, function () use ($listener) {
|
2017-06-13 07:34:20 +02:00
|
|
|
$listener->unlisten();
|
|
|
|
});
|
|
|
|
|
|
|
|
while (yield $listener->advance()) {
|
|
|
|
$this->assertSame($listener->getCurrent()->payload, (string) $count++);
|
|
|
|
}
|
2017-06-21 06:04:54 +02:00
|
|
|
|
|
|
|
$this->assertSame(2, $count);
|
2017-06-13 07:34:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testListen
|
|
|
|
*/
|
|
|
|
public function testNotify() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$channel = "test";
|
|
|
|
/** @var \Amp\Postgres\Listener $listener */
|
|
|
|
$listener = yield $this->connection->listen($channel);
|
|
|
|
|
2017-06-21 07:32:52 +02:00
|
|
|
Loop::delay(100, function () use ($channel) {
|
2017-06-21 06:04:54 +02:00
|
|
|
yield $this->connection->notify($channel, '0');
|
|
|
|
yield $this->connection->notify($channel, '1');
|
|
|
|
});
|
2017-06-13 07:34:20 +02:00
|
|
|
|
|
|
|
$count = 0;
|
2017-06-21 07:32:52 +02:00
|
|
|
Loop::delay(200, function () use ($listener) {
|
2017-06-13 07:34:20 +02:00
|
|
|
$listener->unlisten();
|
|
|
|
});
|
|
|
|
|
|
|
|
while (yield $listener->advance()) {
|
|
|
|
$this->assertSame($listener->getCurrent()->payload, (string) $count++);
|
|
|
|
}
|
2017-06-21 06:04:54 +02:00
|
|
|
|
|
|
|
$this->assertSame(2, $count);
|
2017-06-13 07:34:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testListen
|
|
|
|
* @expectedException \Amp\Postgres\QueryError
|
|
|
|
* @expectedExceptionMessage Already listening on channel
|
|
|
|
*/
|
|
|
|
public function testListenOnSameChannel() {
|
|
|
|
Loop::run(function () {
|
|
|
|
$channel = "test";
|
|
|
|
$listener = yield $this->connection->listen($channel);
|
|
|
|
$listener = yield $this->connection->listen($channel);
|
|
|
|
});
|
|
|
|
}
|
2016-09-14 16:27:39 +02:00
|
|
|
}
|