From 2bae664b03b1c319935e969b3dbfaafde5ec2622 Mon Sep 17 00:00:00 2001 From: Saif Eddin Gmati <29315886+azjezz@users.noreply.github.com> Date: Wed, 8 Dec 2021 21:39:34 +0100 Subject: [PATCH] chore(async): remove `await_readable`, `await_writable`, `await_signal`, and `wrap` functions (#305) Signed-off-by: azjezz --- composer.lock | 125 ++++++++++++++++++++----- docs/component/async.md | 8 +- src/Psl/Async/all.php | 10 +- src/Psl/Async/await_readable.php | 62 ------------ src/Psl/Async/await_signal.php | 47 ---------- src/Psl/Async/await_writable.php | 60 ------------ src/Psl/Async/parallel.php | 51 +--------- src/Psl/Async/wrap.php | 24 ----- src/Psl/Internal/Loader.php | 4 - src/Psl/Shell/execute.php | 11 +++ tests/unit/Async/AwaitReadableTest.php | 46 --------- tests/unit/Async/WrapTest.php | 32 ------- 12 files changed, 119 insertions(+), 361 deletions(-) delete mode 100644 src/Psl/Async/await_readable.php delete mode 100644 src/Psl/Async/await_signal.php delete mode 100644 src/Psl/Async/await_writable.php delete mode 100644 src/Psl/Async/wrap.php delete mode 100644 tests/unit/Async/AwaitReadableTest.php delete mode 100644 tests/unit/Async/WrapTest.php diff --git a/composer.lock b/composer.lock index 69c3dff0..4f2f64a7 100644 --- a/composer.lock +++ b/composer.lock @@ -319,6 +319,77 @@ ], "time": "2021-09-13T08:41:34+00:00" }, + { + "name": "composer/pcre", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/composer/pcre.git", + "reference": "3d322d715c43a1ac36c7fe215fa59336265500f2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/composer/pcre/zipball/3d322d715c43a1ac36c7fe215fa59336265500f2", + "reference": "3d322d715c43a1ac36c7fe215fa59336265500f2", + "shasum": "" + }, + "require": { + "php": "^5.3.2 || ^7.0 || ^8.0" + }, + "require-dev": { + "phpstan/phpstan": "^1", + "phpstan/phpstan-strict-rules": "^1.1", + "symfony/phpunit-bridge": "^4.2 || ^5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Composer\\Pcre\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "http://seld.be" + } + ], + "description": "PCRE wrapping library that offers type-safe preg_* replacements.", + "keywords": [ + "PCRE", + "preg", + "regex", + "regular expression" + ], + "support": { + "issues": "https://github.com/composer/pcre/issues", + "source": "https://github.com/composer/pcre/tree/1.0.0" + }, + "funding": [ + { + "url": "https://packagist.com", + "type": "custom" + }, + { + "url": "https://github.com/composer", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/composer/composer", + "type": "tidelift" + } + ], + "time": "2021-12-06T15:17:27+00:00" + }, { "name": "composer/semver", "version": "3.2.6", @@ -402,25 +473,27 @@ }, { "name": "composer/xdebug-handler", - "version": "2.0.2", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/composer/xdebug-handler.git", - "reference": "84674dd3a7575ba617f5a76d7e9e29a7d3891339" + "reference": "6555461e76962fd0379c444c46fd558a0fcfb65e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/xdebug-handler/zipball/84674dd3a7575ba617f5a76d7e9e29a7d3891339", - "reference": "84674dd3a7575ba617f5a76d7e9e29a7d3891339", + "url": "https://api.github.com/repos/composer/xdebug-handler/zipball/6555461e76962fd0379c444c46fd558a0fcfb65e", + "reference": "6555461e76962fd0379c444c46fd558a0fcfb65e", "shasum": "" }, "require": { + "composer/pcre": "^1", "php": "^5.3.2 || ^7.0 || ^8.0", "psr/log": "^1 || ^2 || ^3" }, "require-dev": { - "phpstan/phpstan": "^0.12.55", - "symfony/phpunit-bridge": "^4.2 || ^5" + "phpstan/phpstan": "^1.0", + "phpstan/phpstan-strict-rules": "^1.1", + "symfony/phpunit-bridge": "^4.2 || ^5.0 || ^6.0" }, "type": "library", "autoload": { @@ -446,7 +519,7 @@ "support": { "irc": "irc://irc.freenode.org/composer", "issues": "https://github.com/composer/xdebug-handler/issues", - "source": "https://github.com/composer/xdebug-handler/tree/2.0.2" + "source": "https://github.com/composer/xdebug-handler/tree/2.0.3" }, "funding": [ { @@ -462,7 +535,7 @@ "type": "tidelift" } ], - "time": "2021-07-31T17:03:58+00:00" + "time": "2021-12-08T13:07:32+00:00" }, { "name": "dnoegel/php-xdg-base-dir", @@ -2104,16 +2177,16 @@ }, { "name": "phpspec/prophecy", - "version": "1.14.0", + "version": "v1.15.0", "source": { "type": "git", "url": "https://github.com/phpspec/prophecy.git", - "reference": "d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e" + "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpspec/prophecy/zipball/d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e", - "reference": "d86dfc2e2a3cd366cee475e52c6bb3bbc371aa0e", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/bbcd7380b0ebf3961ee21409db7b38bc31d69a13", + "reference": "bbcd7380b0ebf3961ee21409db7b38bc31d69a13", "shasum": "" }, "require": { @@ -2165,22 +2238,22 @@ ], "support": { "issues": "https://github.com/phpspec/prophecy/issues", - "source": "https://github.com/phpspec/prophecy/tree/1.14.0" + "source": "https://github.com/phpspec/prophecy/tree/v1.15.0" }, - "time": "2021-09-10T09:02:12+00:00" + "time": "2021-12-08T12:19:24+00:00" }, { "name": "phpunit/php-code-coverage", - "version": "9.2.9", + "version": "9.2.10", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "f301eb1453c9e7a1bc912ee8b0ea9db22c60223b" + "reference": "d5850aaf931743067f4bfc1ae4cbd06468400687" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/f301eb1453c9e7a1bc912ee8b0ea9db22c60223b", - "reference": "f301eb1453c9e7a1bc912ee8b0ea9db22c60223b", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/d5850aaf931743067f4bfc1ae4cbd06468400687", + "reference": "d5850aaf931743067f4bfc1ae4cbd06468400687", "shasum": "" }, "require": { @@ -2236,7 +2309,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.9" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.10" }, "funding": [ { @@ -2244,20 +2317,20 @@ "type": "github" } ], - "time": "2021-11-19T15:21:02+00:00" + "time": "2021-12-05T09:12:13+00:00" }, { "name": "phpunit/php-file-iterator", - "version": "3.0.5", + "version": "3.0.6", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-file-iterator.git", - "reference": "aa4be8575f26070b100fccb67faabb28f21f66f8" + "reference": "cf1c2e7c203ac650e352f4cc675a7021e7d1b3cf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/aa4be8575f26070b100fccb67faabb28f21f66f8", - "reference": "aa4be8575f26070b100fccb67faabb28f21f66f8", + "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/cf1c2e7c203ac650e352f4cc675a7021e7d1b3cf", + "reference": "cf1c2e7c203ac650e352f4cc675a7021e7d1b3cf", "shasum": "" }, "require": { @@ -2296,7 +2369,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-file-iterator/issues", - "source": "https://github.com/sebastianbergmann/php-file-iterator/tree/3.0.5" + "source": "https://github.com/sebastianbergmann/php-file-iterator/tree/3.0.6" }, "funding": [ { @@ -2304,7 +2377,7 @@ "type": "github" } ], - "time": "2020-09-28T05:57:25+00:00" + "time": "2021-12-02T12:48:52+00:00" }, { "name": "phpunit/php-invoker", diff --git a/docs/component/async.md b/docs/component/async.md index affd7cf8..e2ed8add 100644 --- a/docs/component/async.md +++ b/docs/component/async.md @@ -12,21 +12,17 @@ #### `Functions` -- [all](./../../src/Psl/Async/all.php#L26) +- [all](./../../src/Psl/Async/all.php#L22) - [any](./../../src/Psl/Async/any.php#L25) - [await](./../../src/Psl/Async/await.php#L18) -- [await_readable](./../../src/Psl/Async/await_readable.php#L23) -- [await_signal](./../../src/Psl/Async/await_signal.php#L18) -- [await_writable](./../../src/Psl/Async/await_writable.php#L21) - [first](./../../src/Psl/Async/first.php#L22) - [later](./../../src/Psl/Async/later.php#L14) - [main](./../../src/Psl/Async/main.php#L16) -- [parallel](./../../src/Psl/Async/parallel.php#L64) +- [parallel](./../../src/Psl/Async/parallel.php#L21) - [reflect](./../../src/Psl/Async/reflect.php#L25) - [run](./../../src/Psl/Async/run.php#L20) - [series](./../../src/Psl/Async/series.php#L21) - [sleep](./../../src/Psl/Async/sleep.php#L10) -- [wrap](./../../src/Psl/Async/wrap.php#L21) #### `Classes` diff --git a/src/Psl/Async/all.php b/src/Psl/Async/all.php index 62b53a6f..723d6b7f 100644 --- a/src/Psl/Async/all.php +++ b/src/Psl/Async/all.php @@ -10,18 +10,14 @@ /** * Awaits all awaitables to complete concurrently. * - * If one awaitable fails, the exception will be thrown immediately, and the result of the callables will be ignored. - * - * If multiple awaitables failed at once, a {@see Exception\CompositeException} will be thrown. - * - * Once the awaitables have completed, an array containing the results will be returned preserving the original awaitables order. - * * @template Tk of array-key * @template Tv * * @param iterable> $awaitables * - * @return array Unwrapped values with the order preserved. + * @throws Exception\CompositeException If multiple awaitables failed at once. + * + * @return array an array containing the results, preserving the original awaitables order. */ function all(iterable $awaitables): array { diff --git a/src/Psl/Async/await_readable.php b/src/Psl/Async/await_readable.php deleted file mode 100644 index 4f59da3a..00000000 --- a/src/Psl/Async/await_readable.php +++ /dev/null @@ -1,62 +0,0 @@ - $suspension->throw(new Exception\TimeoutException())); - Scheduler::unreference($timeout_watcher); - } - - $watcher = EventLoop::onReadable( - $resource, - static fn() => $suspension->resume($resource), - ); - - if (!$reference) { - Scheduler::unreference($watcher); - } - - try { - $suspension->suspend(); - } catch (Throwable $e) { - if (!is_resource($resource)) { - throw new ResourceClosedException('Resource was closed before it became readable.'); - } - - /** - * @psalm-suppress MissingThrowsDocblock - * @psalm-suppress PossiblyUndefinedVariable - */ - throw $e; - } finally { - Scheduler::cancel($watcher); - - // cancel timeout watcher - if (null !== $timeout_watcher) { - Scheduler::cancel($timeout_watcher); - } - } -} diff --git a/src/Psl/Async/await_signal.php b/src/Psl/Async/await_signal.php deleted file mode 100644 index 88b8a355..00000000 --- a/src/Psl/Async/await_signal.php +++ /dev/null @@ -1,47 +0,0 @@ - $suspension->throw(new Exception\TimeoutException())); - Scheduler::unreference($timeout_watcher); - } - - $watcher = EventLoop::onSignal( - $signal, - static fn() => $suspension->resume($signal), - ); - - if (!$reference) { - Scheduler::unreference($watcher); - } - - try { - $suspension->suspend(); - } finally { - Scheduler::cancel($watcher); - - // cancel timeout watcher - if (null !== $timeout_watcher) { - Scheduler::cancel($timeout_watcher); - } - } -} diff --git a/src/Psl/Async/await_writable.php b/src/Psl/Async/await_writable.php deleted file mode 100644 index 112e1bdf..00000000 --- a/src/Psl/Async/await_writable.php +++ /dev/null @@ -1,60 +0,0 @@ - $suspension->throw(new Exception\TimeoutException())); - Scheduler::unreference($timeout_watcher); - } - - $watcher = EventLoop::onWritable( - $resource, - static fn() => $suspension->resume($resource), - ); - - if (!$reference) { - Scheduler::unreference($watcher); - } - - try { - $suspension->suspend(); - } catch (Throwable $e) { - if (!is_resource($resource)) { - throw new ResourceClosedException('Resource was closed before it became writable.'); - } - - /** - * @psalm-suppress MissingThrowsDocblock - * @psalm-suppress PossiblyUndefinedVariable - */ - throw $e; - } finally { - Scheduler::cancel($watcher); - - // cancel timeout watcher - if (null !== $timeout_watcher) { - Scheduler::cancel($timeout_watcher); - } - } -} diff --git a/src/Psl/Async/parallel.php b/src/Psl/Async/parallel.php index abeb8a02..adbcfaff 100644 --- a/src/Psl/Async/parallel.php +++ b/src/Psl/Async/parallel.php @@ -7,59 +7,16 @@ use Psl\Dict; /** - * Run the tasks iterable of functions in parallel, without waiting until the previous function has completed. - * - * If one tasks fails, the exception will be thrown immediately, and the result of the callables will be ignored. - * - * If multiple tasks failed at once, a {@see Exception\CompositeException} will be thrown. - * - * Once the tasks have completed, an array containing the results will be returned preserving the original awaitables order. - * - * - * use Psl\Async; - * - * // execute `getOne()` and `getTwo()` functions in parallel. - * - * [$one, $two] = Async\parallel([ - * getOne(...), - * getTwo(...), - * ]); - * - * - * @note {@see parallel()} is about kicking-off I/O tasks in parallel, not about parallel execution of code. - * If your tasks do not use any timers or perform any I/O, they will actually be executed in series. - * - * - * use Psl\Async; - * - * // the following runs in series. - * - * [$one, $two] = Async\parallel([ - * fn() => file_get_contents('path/to/file1.txt'), - * fn() => file_get_contents('path/to/file2.txt'), - * ]); - * - * - * @note Use {@see reflect()} to continue the execution of other tasks when a task fails. - * - * - * use Psl\Async; - * - * // execute `getOne()` and `getTwo()` functions in parallel. - * // if either one of the given tasks fails, the other will continue execution. - * - * [$one, $two] = Async\parallel([ - * Async\reflect(getOne(...)), - * Async\reflect(getTwo(...)), - * ]); - * + * Run the iterable of functions in parallel, without waiting until the previous function has completed. * * @template Tk of array-key * @template Tv * * @param iterable $tasks * - * @return array + * @throws Exception\CompositeException If multiple functions failed at once. + * + * @return array an array containing the results, preserving the original functions order. */ function parallel(iterable $tasks): array { diff --git a/src/Psl/Async/wrap.php b/src/Psl/Async/wrap.php deleted file mode 100644 index 412909d0..00000000 --- a/src/Psl/Async/wrap.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * @see reflect() - */ -function wrap(callable $task): Result\ResultInterface -{ - return run(reflect($task))->await(); -} diff --git a/src/Psl/Internal/Loader.php b/src/Psl/Internal/Loader.php index c0b571df..cfca7311 100644 --- a/src/Psl/Internal/Loader.php +++ b/src/Psl/Internal/Loader.php @@ -449,14 +449,10 @@ final class Loader 'Psl\Async\run', 'Psl\Async\parallel', 'Psl\Async\reflect', - 'Psl\Async\wrap', 'Psl\Async\series', 'Psl\Async\await', 'Psl\Async\any', 'Psl\Async\all', - 'Psl\Async\await_readable', - 'Psl\Async\await_writable', - 'Psl\Async\await_signal', 'Psl\Async\first', 'Psl\Async\later', 'Psl\Async\sleep', diff --git a/src/Psl/Shell/execute.php b/src/Psl/Shell/execute.php index d2e4ea7d..16790a25 100644 --- a/src/Psl/Shell/execute.php +++ b/src/Psl/Shell/execute.php @@ -176,6 +176,17 @@ static function (array $m) use ( static fn(): string => $stderr->readAll(timeout: $timeout), ]); // @codeCoverageIgnoreStart + } catch (Async\Exception\CompositeException $exception) { + $reasons = $exception->getReasons(); + if ($reasons[0] instanceof IO\Exception\TimeoutException) { + throw new Exception\TimeoutException('reached timeout while the process output is still not readable.', 0, $reasons[0]); + } + + if ($reasons[1] instanceof IO\Exception\TimeoutException) { + throw new Exception\TimeoutException('reached timeout while the process output is still not readable.', 0, $reasons[1]); + } + + throw new Exception\RuntimeException('Failed to reach process output.', 0, $exception ?? null); } catch (IO\Exception\TimeoutException $previous) { throw new Exception\TimeoutException('reached timeout while the process output is still not readable.', 0, $previous); // @codeCoverageIgnoreEnd diff --git a/tests/unit/Async/AwaitReadableTest.php b/tests/unit/Async/AwaitReadableTest.php deleted file mode 100644 index 47e588d3..00000000 --- a/tests/unit/Async/AwaitReadableTest.php +++ /dev/null @@ -1,46 +0,0 @@ -value .= '[read:waiting]'; - - $read_socket = $read->getStream(); - Async\await_readable($read_socket); - - $ref->value .= '[read:ready]'; - }), - Async\run(static function () use ($ref, $write) { - $ref->value .= '[write:sleep]'; - - Async\sleep(0.001); - - $write->writeImmediately("hello"); - - $ref->value .= '[write:done]'; - }), - ]; - - Async\all($handles); - - static::assertSame('[read:waiting][write:sleep][write:done][read:ready]', $ref->value); - - $write->close(); - $read->close(); - } -} diff --git a/tests/unit/Async/WrapTest.php b/tests/unit/Async/WrapTest.php deleted file mode 100644 index 59a5e4b8..00000000 --- a/tests/unit/Async/WrapTest.php +++ /dev/null @@ -1,32 +0,0 @@ -getException()->getMessage()); - static::assertSame('success', $two->getResult()); - } -}