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
13 changes: 3 additions & 10 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,12 @@

class Connection
{
protected int $timeoutSeconds;
protected int $timeoutMicroseconds;

protected function __construct(
protected Socket $socket,
protected int $bufferSize = 1024,
protected float $timeout = 0.0001,
protected int $writeTimeoutSeconds = 5,
) {
socket_set_nonblock($this->socket);

$this->timeoutSeconds = floor($this->timeout);

$this->timeoutMicroseconds = ($this->timeout * 1_000_000) - ($this->timeoutSeconds * 1_000_000);
}

/**
Expand Down Expand Up @@ -55,7 +48,7 @@ public function write(string $payload): self

$except = null;

$selectResult = @socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
$selectResult = @socket_select($read, $write, $except, $this->writeTimeoutSeconds);

if ($selectResult === false && socket_last_error() === SOCKET_EINTR) {
continue;
Expand Down Expand Up @@ -94,7 +87,7 @@ public function read(): Generator

$except = null;

$selectResult = @socket_select($read, $write, $except, $this->timeoutSeconds, $this->timeoutMicroseconds);
$selectResult = @socket_select($read, $write, $except, 0);

if ($selectResult === false && socket_last_error() === SOCKET_EINTR) {
continue;
Expand Down
14 changes: 8 additions & 6 deletions tests/ForkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,16 @@ function () {
});

test('will not hang by truncating the result when large output is returned', function () {
$value = str_repeat('#', 1048576); // 1MB
expect(
Fork::new()->run(
fn () => file_get_contents('https://stitcher.io/rss'),
fn () => file_get_contents('https://sebastiandedeyne.com/index.xml'),
fn () => file_get_contents('https://rubenvanassche.com/rss/'),
// compare md5 instead of content to avoid large text dumps in case the test fails
array_map(
'md5',
Fork::new()->run(
...array_fill(0, 16, fn () => $value)
)
)
)->toHaveCount(3);
)->toEqual(array_fill(0, 16, md5($value)));
});

test('can return objects', function () {
Expand Down Expand Up @@ -192,5 +195,4 @@ static function () {
},
)
)->toThrow(CouldNotManageTask::class);

});
Loading