Skip to content

Commit be586b6

Browse files
committed
Register event handlers only when the API is busy
1 parent f87e0d5 commit be586b6

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

src/ResponseApi.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class ResponseApi
1717
public function __construct(Client $client)
1818
{
1919
$this->client = $client;
20-
21-
$client->on('message', array($this, 'handleMessage'));
22-
$client->on('close', array($this, 'handleClose'));
2320
}
2421

2522
public function __call($name, $args)
@@ -29,6 +26,11 @@ public function __call($name, $args)
2926
if ($this->ending) {
3027
$request->reject(new RuntimeException('Connection closed'));
3128
} else {
29+
if (!$this->isBusy()) {
30+
$this->client->on('message', array($this, 'handleMessage'));
31+
$this->client->on('close', array($this, 'handleClose'));
32+
}
33+
3234
$this->client->send($name, $args);
3335
$this->requests []= $request;
3436
}
@@ -38,10 +40,6 @@ public function __call($name, $args)
3840

3941
public function handleMessage(ModelInterface $message)
4042
{
41-
if (!$this->requests) {
42-
throw new UnderflowException('Unexpected reply received, no matching request found');
43-
}
44-
4543
$request = array_shift($this->requests);
4644
/* @var $request Deferred */
4745

@@ -51,8 +49,13 @@ public function handleMessage(ModelInterface $message)
5149
$request->resolve($message->getValueNative());
5250
}
5351

54-
if ($this->ending && !$this->isBusy()) {
55-
$this->client->close();
52+
if (!$this->isBusy()) {
53+
$this->client->removeListener('message', array($this, 'handleMessage'));
54+
$this->client->removeListener('close', array($this, 'handleClose'));
55+
56+
if ($this->ending) {
57+
$this->client->close();
58+
}
5659
}
5760
}
5861

tests/FunctionalTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,11 @@ public function testInvalidProtocol()
149149
$this->waitFor($api);
150150
}
151151

152-
public function testInvalidServerRepliesWithDuplicateMessages()
152+
public function testAdditionalServerRepliesAreBeingIgnored()
153153
{
154154
$client = $this->createClientResponse("+OK\r\n-ERR invalid\r\n");
155155
$api = new ResponseApi($client);
156156

157-
$client->on('error', $this->expectCallableOnce());
158-
$client->on('close', $this->expectCallableOnce());
159-
160157
$api->set('a', 0)->then($this->expectCallableOnce('OK'));
161158

162159
$this->waitFor($api);

tests/ResponseApiTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ public function testClosingClientRejectsAllRemainingRequests()
5555

5656
public function testClosedClientRejectsAllNewRequests()
5757
{
58-
$this->client->emit('close');
59-
6058
$promise = $this->responseApi->ping();
6159

60+
$this->client->emit('close');
61+
6262
$this->expectPromiseReject($promise);
6363
$this->assertFalse($this->responseApi->isBusy());
6464
}
@@ -86,10 +86,4 @@ public function testEndingBusyClosesClientWhenNotBusyAnymore()
8686
$this->client->emit('message', array(new BulkReply('PONG')));
8787
$this->assertEquals(1, $closed);
8888
}
89-
90-
public function testReceivingUnexpectedMessageThrowsException()
91-
{
92-
$this->setExpectedException('UnderflowException');
93-
$this->client->emit('message', array(new BulkReply('PONG')));
94-
}
9589
}

0 commit comments

Comments
 (0)