Skip to content

Commit ae230b7

Browse files
Fix waitForData crash when the socket is disconnected (#33)
1 parent 1308260 commit ae230b7

4 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/Client.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ protected function configure(array $options): void
302302
*/
303303
public function waitForData(float $maxSeconds): ?bool
304304
{
305+
if (!$this->isConnected()) {
306+
return null;
307+
}
308+
305309
return $this->socket->waitForData($maxSeconds);
306310
}
307311
}

src/Socket/AbstractSocket.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ public function send(string $data): ?int
233233
*/
234234
public function waitForData(float $maxSeconds): ?bool
235235
{
236+
if (null === $this->socket) {
237+
return null;
238+
}
239+
236240
$read = [$this->socket];
237241
$write = null;
238242
$except = null;

tests/ClientTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function testSend(): void
8989
$instance->addRequestHeader('X-Test', 'Custom Request Header');
9090

9191
self::assertNull($instance->receive(), 'Receive before connect');
92+
self::assertNull($instance->waitForData(0), 'Wait for data before connect');
9293

9394
$success = $instance->connect();
9495
self::assertTrue($success, 'Client can connect to test server');
@@ -123,6 +124,7 @@ public function testSend(): void
123124
$instance->disconnect();
124125

125126
self::assertFalse($instance->isConnected());
127+
self::assertNull($instance->waitForData(0), 'Wait for data after disconnect');
126128
} finally {
127129
$helper->tearDown();
128130
}

tests/Socket/ClientSocketTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ public function testSendTooEarly(): void
110110
$instance->send('foo');
111111
}
112112

113+
public function testWaitForDataTooEarly(): void
114+
{
115+
$instance = self::getInstance('ws://localhost:8000');
116+
117+
self::assertNull($instance->waitForData(0));
118+
}
119+
113120
/**
114121
* Test the connect, send, receive method.
115122
*/

0 commit comments

Comments
 (0)