Skip to content

Commit 24fe594

Browse files
committed
Minor executor changes and test updates
1 parent f9423d0 commit 24fe594

File tree

3 files changed

+28
-22
lines changed

3 files changed

+28
-22
lines changed

lib/PgSqlExecutor.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ public function __construct($handle, $socket) {
5050

5151
while ($result = \pg_get_notify($handle, \PGSQL_ASSOC)) {
5252
$channel = $result["message"];
53-
if (isset($listeners[$channel])) {
54-
$notification = new Notification;
55-
$notification->channel = $channel;
56-
$notification->pid = $result["pid"];
57-
$notification->payload = $result["payload"];
58-
$listeners[$channel]->emit($notification);
53+
54+
if (!isset($listeners[$channel])) {
55+
return;
5956
}
57+
58+
$notification = new Notification;
59+
$notification->channel = $channel;
60+
$notification->pid = $result["pid"];
61+
$notification->payload = $result["payload"];
62+
$listeners[$channel]->emit($notification);
6063
}
6164

6265
if ($deferred === null) {
@@ -76,7 +79,7 @@ public function __construct($handle, $socket) {
7679

7780
$this->await = Loop::onWritable($socket, static function ($watcher) use (&$deferred, $handle) {
7881
$flush = \pg_flush($handle);
79-
if (0 === $flush) {
82+
if ($flush === 0) {
8083
return; // Not finished sending data, listen again.
8184
}
8285

@@ -231,7 +234,7 @@ public function listen(string $channel): Promise {
231234
throw new QueryError(\sprintf("Already listening on channel '%s'", $channel));
232235
}
233236

234-
$this->listeners[$channel] = $emitter = new Emitter;
237+
$this->listeners[$channel] = $emitter = new Emitter;
235238

236239
try {
237240
yield $this->query(\sprintf("LISTEN %s", $channel));
@@ -265,9 +268,7 @@ private function unlisten(string $channel): Promise {
265268
}
266269

267270
$promise = $this->query(\sprintf("UNLISTEN %s", $channel));
268-
$promise->onResolve(function () use ($emitter) {
269-
$emitter->complete();
270-
});
271+
$promise->onResolve([$emitter, "complete"]);
271272
return $promise;
272273
}
273274
}

lib/PqExecutor.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,7 @@ private function unlisten(string $channel): Promise {
284284
}
285285

286286
$promise = new Coroutine($this->send([$this->handle, "unlistenAsync"], $channel));
287-
$promise->onResolve(function () use ($emitter) {
288-
$emitter->complete();
289-
});
287+
$promise->onResolve([$emitter, "complete"]);
290288
return $promise;
291289
}
292290
}

test/AbstractConnectionTest.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Amp\Postgres\TransactionError;
1111
use Amp\Postgres\TupleResult;
1212
use PHPUnit\Framework\TestCase;
13+
use function Amp\asyncCall;
1314

1415
abstract class AbstractConnectionTest extends TestCase {
1516
/** @var \Amp\Postgres\Connection */
@@ -191,18 +192,21 @@ public function testListen() {
191192

192193
$this->assertInstanceOf(Listener::class, $listener);
193194

194-
yield $this->connection->query(\sprintf("NOTIFY %s, '%s'", $channel, '0'));
195-
yield $this->connection->query(\sprintf("NOTIFY %s, '%s'", $channel, '1'));
195+
asyncCall(function () use ($channel) {
196+
yield $this->connection->query(\sprintf("NOTIFY %s, '%s'", $channel, '0'));
197+
yield $this->connection->query(\sprintf("NOTIFY %s, '%s'", $channel, '1'));
198+
});
196199

197200
$count = 0;
198-
Loop::defer(function () use (&$count, $listener) {
201+
Loop::delay(100, function () use ($listener) {
199202
$listener->unlisten();
200-
$this->assertSame(2, $count);
201203
});
202204

203205
while (yield $listener->advance()) {
204206
$this->assertSame($listener->getCurrent()->payload, (string) $count++);
205207
}
208+
209+
$this->assertSame(2, $count);
206210
});
207211
}
208212

@@ -215,18 +219,21 @@ public function testNotify() {
215219
/** @var \Amp\Postgres\Listener $listener */
216220
$listener = yield $this->connection->listen($channel);
217221

218-
yield $this->connection->notify($channel, '0');
219-
yield $this->connection->notify($channel, '1');
222+
asyncCall(function () use ($channel) {
223+
yield $this->connection->notify($channel, '0');
224+
yield $this->connection->notify($channel, '1');
225+
});
220226

221227
$count = 0;
222-
Loop::defer(function () use (&$count, $listener) {
228+
Loop::delay(100, function () use ($listener) {
223229
$listener->unlisten();
224-
$this->assertSame(2, $count);
225230
});
226231

227232
while (yield $listener->advance()) {
228233
$this->assertSame($listener->getCurrent()->payload, (string) $count++);
229234
}
235+
236+
$this->assertSame(2, $count);
230237
});
231238
}
232239

0 commit comments

Comments
 (0)