Skip to content
Merged
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 CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# CHANGELOG


## 1.16.2 (Unreleased)

* Fix buffered events delaying responses by 50ms each


## 1.16.1 (2026-07-06)

* Pass explicit trim characters ahead of the PHP 8.6 trim default change
Expand Down
18 changes: 16 additions & 2 deletions src/Communication/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ public function readData()

while ($this->readLine()) {
$hasData = true;

if (!$this->hasBufferedData()) {
break;
}
}

return $hasData;
Expand All @@ -344,10 +348,20 @@ public function readLine()
// dispatch first line of buffer
$datum = \array_shift($this->receivedData);
if ($datum) {
return $this->dispatchMessage($datum);
$this->dispatchMessage($datum);
}

return false;
return null !== $datum;
}

/**
* True if received data is waiting to be read.
*
* @internal
*/
public function hasBufferedData(): bool
{
return !empty($this->receivedData);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Communication/ResponseReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function checkForResponse()

// read data
while (!$this->connection->hasResponseForId($id)) {
if (!$this->connection->readLine()) {
if (!$this->connection->readLine() || !$this->connection->hasBufferedData()) {
break;
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Communication/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use HeadlessChromium\Exception\CommunicationException\InvalidResponse;
use HeadlessChromium\Exception\OperationTimedOut;
use PHPUnit\Framework\TestCase;
use stdClass;

/**
* @covers \HeadlessChromium\Communication\Connection
Expand Down Expand Up @@ -206,6 +207,43 @@ public function testReadData(): void
self::assertFalse($connection->hasResponseForId(1));
}

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

$emitWatcher = new stdClass();
$emitWatcher->emittedCount = 0;

$connection->on('method:qux.quux', function () use ($emitWatcher): void {
++$emitWatcher->emittedCount;
});

$this->mocSocket->addReceivedData(\json_encode(['method' => 'qux.quux', 'params' => []]));
$this->mocSocket->addReceivedData(\json_encode(['method' => 'qux.quux', 'params' => []]));

self::assertTrue($connection->readData());
self::assertSame(2, $emitWatcher->emittedCount);
}

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

$connection->once('method:qux.quux', function (): void {
$this->mocSocket->addReceivedData(\json_encode(['id' => 1, 'foo' => 'bar']));
});

$this->mocSocket->addReceivedData(\json_encode(['method' => 'qux.quux', 'params' => []]));

self::assertTrue($connection->readData());
self::assertFalse($connection->hasResponseForId(1));

self::assertTrue($connection->readData());
self::assertTrue($connection->hasResponseForId(1));
}

public function testExceptionInvalideJson(): void
{
$this->expectException(CannotReadResponse::class);
Expand Down
41 changes: 41 additions & 0 deletions tests/Communication/ResponseReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,47 @@ public function testWaitForResponseIsAtomic(): void
self::assertSame(1, $emitWatcher->emittedCount);
}

public function testCheckForResponseReadsPastEvents(): void
{
$message = new Message('foo', ['bar' => 'baz']);
$mockSocket = new MockSocket();
$connection = new Connection($mockSocket);

$emitWatcher = new stdClass();
$emitWatcher->emittedCount = 0;

$connection->on('method:qux.quux', function () use ($emitWatcher): void {
++$emitWatcher->emittedCount;
});

$responseReader = new ResponseReader($message, $connection);

$mockSocket->addReceivedData(\json_encode(['method' => 'qux.quux', 'params' => []]));
$mockSocket->addReceivedData(\json_encode(['method' => 'qux.quux', 'params' => []]));
$mockSocket->addReceivedData(\json_encode(['id' => $message->getId(), 'foo' => 'qux']));

self::assertTrue($responseReader->checkForResponse());
self::assertSame(2, $emitWatcher->emittedCount);
}

public function testCheckForResponseReceivesOnlyOnce(): void
{
$message = new Message('foo', ['bar' => 'baz']);
$mockSocket = new MockSocket();
$connection = new Connection($mockSocket);

$connection->once('method:qux.quux', function () use ($mockSocket, $message): void {
$mockSocket->addReceivedData(\json_encode(['id' => $message->getId(), 'foo' => 'qux']));
});

$responseReader = new ResponseReader($message, $connection);

$mockSocket->addReceivedData(\json_encode(['method' => 'qux.quux', 'params' => []]));

self::assertFalse($responseReader->checkForResponse());
self::assertTrue($responseReader->checkForResponse());
}

public function testExceptionNoResponse(): void
{
$message = new Message('foo', ['bar' => 'baz']);
Expand Down