Skip to content

Commit bf38153

Browse files
committed
Allow a timeout to be passed into the await* methods
1 parent 5525f78 commit bf38153

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/TestCase.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
abstract class TestCase extends PHPUnitTestCase
1717
{
18+
const DEFAULT_AWAIT_TIMEOUT = 60;
19+
1820
/**
1921
* @var string
2022
*/
@@ -146,40 +148,40 @@ protected function getFilesInDirectory(string $path): array
146148
* @param LoopInterface|null $loop
147149
* @return mixed
148150
*/
149-
protected function await(PromiseInterface $promise, LoopInterface $loop = null)
151+
protected function await(PromiseInterface $promise, LoopInterface $loop = null, float $timeout = self::DEFAULT_AWAIT_TIMEOUT)
150152
{
151153
if (!($loop instanceof LoopInterface)) {
152154
$loop = Factory::create();
153155
}
154156

155-
return await($promise, $loop);
157+
return await($promise, $loop, $timeout);
156158
}
157159

158160
/**
159161
* @param array $promises
160162
* @param LoopInterface|null $loop
161163
* @return array
162164
*/
163-
protected function awaitAll(array $promises, LoopInterface $loop = null)
165+
protected function awaitAll(array $promises, LoopInterface $loop = null, float $timeout = self::DEFAULT_AWAIT_TIMEOUT)
164166
{
165167
if (!($loop instanceof LoopInterface)) {
166168
$loop = Factory::create();
167169
}
168170

169-
return awaitAll($promises, $loop);
171+
return awaitAll($promises, $loop, $timeout);
170172
}
171173

172174
/**
173175
* @param array $promises
174176
* @param LoopInterface|null $loop
175177
* @return mixed
176178
*/
177-
protected function awaitAny(array $promises, LoopInterface $loop = null)
179+
protected function awaitAny(array $promises, LoopInterface $loop = null, float $timeout = self::DEFAULT_AWAIT_TIMEOUT)
178180
{
179181
if (!($loop instanceof LoopInterface)) {
180182
$loop = Factory::create();
181183
}
182184

183-
return awaitAny($promises, $loop);
185+
return awaitAny($promises, $loop, $timeout);
184186
}
185187
}

tests/TestCaseTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
use React\EventLoop\Factory;
77
use React\EventLoop\LoopInterface;
88
use React\EventLoop\StreamSelectLoop;
9+
use React\Promise\Deferred;
910
use function React\Promise\resolve;
11+
use function React\Promise\Timer\timeout;
12+
use React\Promise\Timer\TimeoutException;
1013

1114
final class TestCaseTest extends TestCase
1215
{
@@ -94,4 +97,14 @@ public function testTrueFalse(bool $bool)
9497
{
9598
static::assertInternalType('bool', $bool);
9699
}
100+
101+
/**
102+
* @dataProvider provideEventLoop
103+
*/
104+
public function testAwaitTimeout(LoopInterface $loop = null)
105+
{
106+
self::expectException(TimeoutException::class);
107+
108+
$this->await((new Deferred())->promise(), $loop, 0.1);
109+
}
97110
}

0 commit comments

Comments
 (0)