Skip to content
Closed
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
5 changes: 5 additions & 0 deletions src/Communication/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ public function readLine()
*/
public function waitForData(float $maxSeconds): bool
{
// Messages already buffered by readData() are not visible to the socket select.
if ([] !== $this->receivedData) {
return true;
}

return $this->wsClient instanceof WaitForDataInterface && $this->wsClient->waitForData($maxSeconds);
}

Expand Down
16 changes: 16 additions & 0 deletions tests/Communication/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ public function testReadData(): void
self::assertFalse($connection->hasResponseForId(1));
}

public function testWaitForDataWhenMessagesAreAlreadyBuffered(): void
{
$connection = new Connection($this->mocSocket);
$connection->connect();

$this->mocSocket->addReceivedData(\json_encode(['id' => 1, 'result' => []]));
$this->mocSocket->addReceivedData(\json_encode(['id' => 2, 'result' => []]));

// readLine() pulls both messages from the socket but dispatches only the first one
self::assertTrue($connection->readLine());
self::assertTrue($connection->waitForData(0));

self::assertTrue($connection->readLine());
self::assertFalse($connection->waitForData(0));
}

public function testExceptionInvalideJson(): void
{
$this->expectException(CannotReadResponse::class);
Expand Down