From 3f2bde0b62f6e83b6f9f44c781cc1dea5ad439d5 Mon Sep 17 00:00:00 2001 From: Saif Eddin Gmati <29315886+azjezz@users.noreply.github.com> Date: Wed, 10 Nov 2021 22:33:40 +0100 Subject: [PATCH] fix(io): fix readAll() blocks (#270) Signed-off-by: azjezz --- examples/io/benchmark.php | 6 ++++++ examples/shell/concurrent.php | 9 ++++++--- examples/unix/concurrent.php | 7 +++++++ src/Psl/IO/ReadHandleConvenienceMethodsTrait.php | 8 +++++--- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/examples/io/benchmark.php b/examples/io/benchmark.php index fe98cd0f..0d7f65d8 100644 --- a/examples/io/benchmark.php +++ b/examples/io/benchmark.php @@ -13,10 +13,16 @@ use function microtime; use function round; use function strlen; +use const PHP_OS_FAMILY; require __DIR__ . '/../../vendor/autoload.php'; Async\main(static function (): int { + if (PHP_OS_FAMILY === 'Windows') { + IO\output_handle()->writeAll('This example requires does not support Windows.'); + + return 0; + } $args = getopt('i:o:t:'); $input_file = $args['i'] ?? '/dev/zero'; diff --git a/examples/shell/concurrent.php b/examples/shell/concurrent.php index a0ca7d09..2eb487c9 100644 --- a/examples/shell/concurrent.php +++ b/examples/shell/concurrent.php @@ -14,9 +14,12 @@ $start = time(); Async\concurrent([ - static fn() => Shell\execute('sleep', ['2']), - static fn() => Shell\execute('sleep', ['2']), - static fn() => Shell\execute('sleep', ['1']), + static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']), + static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']), + static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']), + static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']), + static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']), + static fn() => Shell\execute(PHP_BINARY, ['-r', '$t = time(); while(time() < ($t+1)) { echo "."; }']), ]); $duration = time() - $start; diff --git a/examples/unix/concurrent.php b/examples/unix/concurrent.php index 36c9c81d..64dc96e8 100644 --- a/examples/unix/concurrent.php +++ b/examples/unix/concurrent.php @@ -9,10 +9,17 @@ use Psl\IO; use Psl\Str; use Psl\Unix; +use const PHP_OS_FAMILY; require __DIR__ . '/../../vendor/autoload.php'; Async\main(static function (): int { + if (PHP_OS_FAMILY === 'Windows') { + IO\output_handle()->writeAll('This example requires does not support Windows.'); + + return 0; + } + $file = Filesystem\create_temporary_file(prefix: 'psl-examples') . ".sock"; $output = IO\output_handle(); diff --git a/src/Psl/IO/ReadHandleConvenienceMethodsTrait.php b/src/Psl/IO/ReadHandleConvenienceMethodsTrait.php index d9365f16..d4a5e556 100644 --- a/src/Psl/IO/ReadHandleConvenienceMethodsTrait.php +++ b/src/Psl/IO/ReadHandleConvenienceMethodsTrait.php @@ -30,7 +30,7 @@ trait ReadHandleConvenienceMethodsTrait */ public function readAll(?int $max_bytes = null, ?float $timeout = null): string { - $to_read = $max_bytes ?? Internal\ResourceHandle::DEFAULT_READ_BUFFER_SIZE; + $to_read = $max_bytes; /** @var Psl\Ref $data */ $data = new Psl\Ref(''); @@ -51,8 +51,10 @@ static function () use ($data): void { /** @psalm-suppress MissingThrowsDocblock */ $chunk = $this->read($chunk_size, $timer->getRemaining()); $data->value .= $chunk; - $to_read -= strlen($chunk); - } while ($to_read > 0 && $chunk !== ''); + if ($to_read !== null) { + $to_read -= strlen($chunk); + } + } while (($to_read === null || $to_read > 0) && $chunk !== ''); return $data->value; }