1111use Revolt \EventLoop \Driver \TracingDriver ;
1212use function Amp \async ;
1313use function Amp \Future \all ;
14+ use function Amp \now ;
1415
1516abstract class AsyncTestCase extends PHPUnitTestCase
1617{
1718 private const RUNTIME_PRECISION = 2 ;
1819
19- private DeferredFuture $ deferred ;
20+ private DeferredFuture $ deferredFuture ;
2021
2122 private string $ timeoutId ;
2223
@@ -48,15 +49,14 @@ protected function cleanup(): void
4849 protected function setUp (): void
4950 {
5051 $ this ->setUpInvoked = true ;
51-
52- $ this ->deferred = new DeferredFuture ();
52+ $ this ->deferredFuture = new DeferredFuture ();
5353
5454 EventLoop::setErrorHandler (function (\Throwable $ exception ): void {
55- if ($ this ->deferred ->isComplete ()) {
55+ if ($ this ->deferredFuture ->isComplete ()) {
5656 return ;
5757 }
5858
59- $ this ->deferred ->error (new LoopCaughtException ($ exception ));
59+ $ this ->deferredFuture ->error (new UnhandledException ($ exception ));
6060 });
6161 }
6262
@@ -73,11 +73,12 @@ final protected function runAsyncTest(mixed ...$args): mixed
7373
7474 parent ::setName ($ this ->realTestName );
7575
76- $ start = \microtime ( true );
76+ $ start = now ( );
7777
7878 try {
7979 try {
80- [$ returnValue ] = all ([
80+ [, $ returnValue ] = all ([
81+ $ this ->deferredFuture ->getFuture (),
8182 async (function () use ($ args ): mixed {
8283 try {
8384 $ result = ([$ this , $ this ->realTestName ])(...$ args );
@@ -93,12 +94,11 @@ final protected function runAsyncTest(mixed ...$args): mixed
9394
9495 return $ result ;
9596 } finally {
96- if (!$ this ->deferred ->isComplete ()) {
97- $ this ->deferred ->complete ();
97+ if (!$ this ->deferredFuture ->isComplete ()) {
98+ $ this ->deferredFuture ->complete ();
9899 }
99100 }
100101 }),
101- $ this ->deferred ->getFuture ()
102102 ]);
103103 } finally {
104104 $ this ->cleanup ();
@@ -111,7 +111,7 @@ final protected function runAsyncTest(mixed ...$args): mixed
111111 \gc_collect_cycles (); // Throw from as many destructors as possible.
112112 }
113113
114- $ end = \microtime ( true );
114+ $ end = now ( );
115115
116116 if ($ this ->minimumRuntime > 0 ) {
117117 $ actualRuntime = \round ($ end - $ start , self ::RUNTIME_PRECISION );
@@ -133,27 +133,27 @@ final protected function runTest(): mixed
133133 }
134134
135135 /**
136- * Fails the test if the loop does not run for at least the given amount of time.
136+ * Fails the test if it does not run for at least the given amount of time.
137137 *
138- * @param float $runtime Required run time in seconds.
138+ * @param float $seconds Required runtime in seconds.
139139 */
140- final protected function setMinimumRuntime (float $ runtime ): void
140+ final protected function setMinimumRuntime (float $ seconds ): void
141141 {
142- if ($ runtime < 0.001 ) {
143- throw new \Error ('Minimum runtime must be at least 0.001s ' );
142+ if ($ seconds <= 0 ) {
143+ throw new \Error ('Minimum runtime must be greater than 0, got ' . $ seconds );
144144 }
145145
146- $ this ->minimumRuntime = \round ($ runtime , self ::RUNTIME_PRECISION );
146+ $ this ->minimumRuntime = \round ($ seconds , self ::RUNTIME_PRECISION );
147147 }
148148
149149 /**
150- * Fails the test (and stops the loop) after the given timeout.
150+ * Fails the test (and stops the event loop) after the given timeout.
151151 *
152- * @param float $timeout Timeout in seconds.
152+ * @param float $seconds Timeout in seconds.
153153 */
154- final protected function setTimeout (float $ timeout ): void
154+ final protected function setTimeout (float $ seconds ): void
155155 {
156- $ this ->timeoutId = EventLoop::delay ($ timeout , function () use ($ timeout ): void {
156+ $ this ->timeoutId = EventLoop::delay ($ seconds , function () use ($ seconds ): void {
157157 EventLoop::setErrorHandler (null );
158158
159159 $ additionalInfo = '' ;
@@ -167,22 +167,26 @@ final protected function setTimeout(float $timeout): void
167167 $ additionalInfo .= "\r\n\r\nSet REVOLT_DEBUG_TRACE_WATCHERS=true as environment variable to trace watchers keeping the loop running. " ;
168168 }
169169
170- if ($ this ->deferred ->isComplete ()) {
170+ if ($ this ->deferredFuture ->isComplete ()) {
171171 return ;
172172 }
173173
174174 try {
175- $ this ->fail (\sprintf ('Expected test to complete before %0.3fs time limit%s ' , $ timeout , $ additionalInfo ));
175+ $ this ->fail (\sprintf (
176+ 'Expected test to complete before %0.3fs time limit%s ' ,
177+ $ seconds ,
178+ $ additionalInfo
179+ ));
176180 } catch (AssertionFailedError $ e ) {
177- $ this ->deferred ->error ($ e );
181+ $ this ->deferredFuture ->error ($ e );
178182 }
179183 });
180184
181185 EventLoop::unreference ($ this ->timeoutId );
182186 }
183187
184188 /**
185- * @param int $invocationCount Number of times the callback must be invoked or the test will fail.
189+ * @param int $invocationCount Number of times the callback must be invoked or the test will fail.
186190 * @param callable|null $returnCallback Callable providing a return value for the callback.
187191 *
188192 * @return \Closure&MockObject Mock object having only an __invoke method.
0 commit comments