Skip to content

Commit 5fc7784

Browse files
authored
Merge pull request #9 from WyriHaximus/rework-internals-to-be-more-reliable
Rework internals to be more reliable
2 parents bfa65c9 + cd87264 commit 5fc7784

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/RunTestsInFibersTrait.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44

55
namespace WyriHaximus\React\PHPUnit;
66

7-
use React\Promise\PromiseInterface;
7+
use React\EventLoop\Loop;
8+
use React\Promise\Deferred;
89
use ReflectionClass;
910

1011
use function assert;
1112
use function is_string;
1213
use function React\Async\async;
1314
use function React\Async\await;
1415
use function React\Promise\race;
15-
use function React\Promise\reject;
16-
use function React\Promise\Timer\sleep;
1716

1817
trait RunTestsInFibersTrait
1918
{
@@ -69,15 +68,18 @@ final protected function runAsyncTest(mixed ...$args): mixed
6968
$timeout = $methodTimeout->timeout();
7069
}
7170

71+
$sleepingDeferred = new Deferred();
72+
$sleepTimer = Loop::addTimer($timeout, static fn () => $sleepingDeferred->reject(new TimedOut('Test timed out after ' . $timeout . ' second(s)')));
73+
7274
/**
7375
* @psalm-suppress MixedArgument
7476
* @psalm-suppress UndefinedInterfaceMethod
7577
*/
7678
return await(race([
7779
async(
7880
fn (): mixed => ([$this, $this->realTestName])(...$args), /** @phpstan-ignore-line */
79-
)(),
80-
sleep($timeout)->then(static fn (): PromiseInterface => reject(new TimedOut('Test timed out after ' . $timeout . ' second(s)'))),
81+
)()->always(static fn () => Loop::cancelTimer($sleepTimer)),
82+
$sleepingDeferred->promise(),
8183
]));
8284
}
8385

0 commit comments

Comments
 (0)