Skip to content

Commit

Permalink
chore(async): remove timeout support for run()
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Dec 26, 2021
1 parent 3198be9 commit 6f28f4f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 74 deletions.
2 changes: 1 addition & 1 deletion docs/component/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- [later](./../../src/Psl/Async/later.php#L14)
- [main](./../../src/Psl/Async/main.php#L18)
- [reflect](./../../src/Psl/Async/reflect.php#L25)
- [run](./../../src/Psl/Async/run.php#L21)
- [run](./../../src/Psl/Async/run.php#L19)
- [series](./../../src/Psl/Async/series.php#L22)
- [sleep](./../../src/Psl/Async/sleep.php#L10)

Expand Down
44 changes: 4 additions & 40 deletions src/Psl/Async/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

use Closure;
use Exception as RootException;
use Psl;
use Psl\Async\Exception\TimeoutException;

/**
* Create a new fiber asynchronously using the given closure.
Expand All @@ -18,51 +16,17 @@
*
* @return Awaitable<T>
*/
function run(Closure $closure, ?float $timeout = null): Awaitable
function run(Closure $closure): Awaitable
{
$state = new Internal\State();

/** @var Psl\Ref<string|null> $timeout_watcher */
$timeout_watcher = new Psl\Ref(null);
/** @var Psl\Ref<string|null> $delay_watcher */
$delay_watcher = new Psl\Ref(null);

if (null !== $timeout) {
$timeout_watcher->value = Scheduler::delay($timeout, static function () use ($state, $delay_watcher, $timeout_watcher): void {
if (null !== $delay_watcher->value) {
$delay_watcher_value = $delay_watcher->value;
$delay_watcher->value = null;
Scheduler::cancel($delay_watcher_value);
}

$timeout_watcher->value = null;
$state->error(new TimeoutException());
});

Scheduler::unreference($timeout_watcher->value);
}

$delay_watcher->value = Scheduler::defer(static function () use ($closure, $state, $timeout_watcher): void {
$exception = null;
$result = null;
Scheduler::defer(static function () use ($closure, $state): void {
try {
$result = $closure();
} catch (RootException $exception) {
}

if (null !== $timeout_watcher->value) {
$timeout_watcher_value = $timeout_watcher->value;
$timeout_watcher->value = null;
Scheduler::cancel($timeout_watcher_value);
} elseif ($state->isComplete()) {
// timeout has been reached.
return;
}

if (null !== $exception) {
$state->error($exception);
} else {
$state->complete($result);
} catch (RootException $exception) {
$state->error($exception);
}
});

Expand Down
33 changes: 0 additions & 33 deletions tests/unit/Async/RunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,4 @@ public function testRun(): void

static::assertSame('hello', Async\await($awaitable));
}

public function testRunWithTimeout(): void
{
$awaitable = Async\run(static function (): string {
Async\concurrently([
static fn() => Async\sleep(0.01),
static fn() => Async\sleep(0.01),
static fn() => Async\sleep(0.01),
]);

return 'hello';
}, timeout: 0.02);

static::assertSame('hello', $awaitable->await());
}

public function testRunTimedOut(): void
{
$awaitable = Async\run(static function (): string {
Async\concurrently([
static fn() => Async\sleep(1),
static fn() => Async\sleep(1),
static fn() => Async\sleep(1),
]);

return 'hello';
}, timeout: 0.0001);

$this->expectException(Async\Exception\TimeoutException::class);
$this->expectExceptionMessage('operation timed out.');

$awaitable->await();
}
}

0 comments on commit 6f28f4f

Please sign in to comment.