11<?php
2+ declare (strict_types=1 );
3+
24namespace ScriptFUSIONTest \Retry ;
35
4- use Amp \Delayed ;
5- use Amp \Promise ;
6- use Amp \Success ;
6+ use Amp \Future ;
77use PHPUnit \Framework \TestCase ;
88use ScriptFUSION \Retry \FailingTooHardException ;
9- use function Amp \Promise \wait ;
10- use function ScriptFUSION \Retry \retryAsync ;
9+ use function Amp \async ;
10+ use function Amp \delay ;
11+ use function ScriptFUSION \Retry \retry ;
1112
1213final class RetryAsyncTest extends TestCase
1314{
1415 /**
15- * Tests that a successful promise is returned without retrying.
16+ * Tests that a successful Future is returned without retrying.
1617 */
17- public function testWithoutFailingAsync ()
18+ public function testWithoutFailingAsync (): void
1819 {
1920 $ invocations = 0 ;
2021
21- $ value = wait (
22- retryAsync ($ tries = 1 , static function () use (&$ invocations ) {
22+ $ value =
23+ retry ($ tries = 1 , static function () use (&$ invocations ) {
2324 ++$ invocations ;
2425
25- return new Delayed (0 , 'foo ' );
26+ return self :: delayAndReturn (0 , 'foo ' );
2627 })
27- ) ;
28+ ;
2829
2930 self ::assertSame ($ tries , $ invocations );
3031 self ::assertSame ('foo ' , $ value );
3132 }
3233
3334 /**
34- * Tests that a failed promise is retried.
35+ * Tests that a failed Future is retried.
3536 */
36- public function testFailingOnceAsync ()
37+ public function testFailingOnceAsync (): void
3738 {
3839 $ invocations = 0 ;
3940 $ failed = false ;
4041
41- $ value = wait (
42- retryAsync ($ tries = 2 , static function () use (&$ invocations , &$ failed ) {
42+ $ value =
43+ retry ($ tries = 2 , static function () use (&$ invocations , &$ failed ) {
4344 ++$ invocations ;
4445
4546 if (!$ failed ) {
@@ -48,9 +49,9 @@ public function testFailingOnceAsync()
4849 throw new \RuntimeException ;
4950 }
5051
51- return new Delayed (0 , 'foo ' );
52+ return self :: delayAndReturn (0 , 'foo ' );
5253 })
53- ) ;
54+ ;
5455
5556 self ::assertTrue ($ failed );
5657 self ::assertSame ($ tries , $ invocations );
@@ -60,17 +61,17 @@ public function testFailingOnceAsync()
6061 /**
6162 * Tests that trying zero times yields null.
6263 */
63- public function testZeroTriesAsync ()
64+ public function testZeroTriesAsync (): void
6465 {
6566 $ invocations = 0 ;
6667
67- $ value = wait (
68- retryAsync ($ tries = 0 , static function () use (&$ invocations ) {
68+ $ value =
69+ retry ($ tries = 0 , static function () use (&$ invocations ) {
6970 ++$ invocations ;
7071
71- return new Delayed (0 , 'foo ' );
72+ return self :: delayAndReturn (0 , 'foo ' );
7273 })
73- ) ;
74+ ;
7475
7576 self ::assertSame ($ tries , $ invocations );
7677 self ::assertNull ($ value );
@@ -79,13 +80,13 @@ public function testZeroTriesAsync()
7980 /**
8081 * Tests that reaching maximum tries throws FailingTooHardException.
8182 */
82- public function testFailingTooHardAsync ()
83+ public function testFailingTooHardAsync (): void
8384 {
8485 $ invocations = 0 ;
8586 $ outerException = $ innerException = null ;
8687
8788 try {
88- retryAsync ($ tries = 3 , static function () use (&$ invocations , &$ innerException ) {
89+ retry ($ tries = 3 , static function () use (&$ invocations , &$ innerException ) {
8990 ++$ invocations ;
9091
9192 throw $ innerException = new \RuntimeException ;
@@ -101,13 +102,13 @@ public function testFailingTooHardAsync()
101102 /**
102103 * Tests that the error callback is called before each retry.
103104 */
104- public function testErrorCallbackAsync ()
105+ public function testErrorCallbackAsync (): void
105106 {
106107 $ invocations = $ errors = 0 ;
107108 $ outerException = $ innerException = null ;
108109
109110 try {
110- retryAsync ($ tries = 2 , static function () use (&$ invocations , &$ innerException ) {
111+ retry ($ tries = 2 , static function () use (&$ invocations , &$ innerException ) {
111112 ++$ invocations ;
112113
113114 throw $ innerException = new \RuntimeException ;
@@ -127,76 +128,77 @@ public function testErrorCallbackAsync()
127128 }
128129
129130 /**
130- * Tests that an error callback that returns a promise has its promise resolved.
131+ * Tests that an error callback that returns a Future has its Future resolved.
131132 */
132- public function testPromiseErrorCallback ()
133+ public function testFutureErrorCallback (): void
133134 {
134- $ delay = 250 ; // Quarter of a second.
135+ $ delay = .25 ; // Quarter of a second.
135136 $ start = microtime (true );
136137
137138 try {
138- wait (
139- retryAsync ($ tries = 3 , static function () {
140- throw new \DomainException ;
141- }, static function () use ($ delay ): Promise {
142- return new Delayed ($ delay );
143- })
144- );
139+ retry ($ tries = 3 , static function () {
140+ throw new \DomainException ;
141+ }, fn () => self ::delayAndReturn ($ delay ));
145142 } catch (FailingTooHardException $ outerException ) {
146143 self ::assertInstanceOf (\DomainException::class, $ outerException ->getPrevious ());
147144 }
148145
149146 self ::assertTrue (isset ($ outerException ));
150- self ::assertGreaterThan ($ start + $ delay * ($ tries - 1 ) / 1000 , microtime (true ));
147+ self ::assertGreaterThan ($ start + $ delay * ($ tries - 1 ), microtime (true ));
151148 }
152149
153150 /**
154151 * Tests that when error handler that returns false, it aborts retrying.
155152 */
156- public function testErrorCallbackHaltAsync ()
153+ public function testErrorCallbackHaltAsync (): void
157154 {
158155 $ invocations = 0 ;
159156
160- retryAsync (2 , static function () use (&$ invocations ) {
157+ retry (2 , static function () use (&$ invocations ): never {
161158 ++$ invocations ;
162159
163160 throw new \RuntimeException ;
164- }, static function () {
165- return false ;
166- });
161+ }, fn () => false );
167162
168163 self ::assertSame (1 , $ invocations );
169164 }
170165
171166 /**
172- * Tests that when an error handler returns a promise that false, it aborts retrying.
167+ * Tests that when an error handler returns a Future that false, it aborts retrying.
173168 */
174- public function testPromiseErrorCallbackHaltAsync ()
169+ public function testFutureErrorCallbackHaltAsync (): void
175170 {
176171 $ invocations = 0 ;
177172
178- retryAsync (2 , static function () use (&$ invocations ) {
173+ retry (2 , static function () use (&$ invocations ): never {
179174 ++$ invocations ;
180175
181176 throw new \RuntimeException ;
182- }, static function (): Promise {
183- return new Success (false );
184- });
177+ }, fn () => Future::complete (false ));
185178
186179 self ::assertSame (1 , $ invocations );
187180 }
188181
189182 /**
190183 * Tests that the exception handler can throw an exception that will not be caught.
191184 */
192- public function testErrorCallbackCanThrow ()
185+ public function testErrorCallbackCanThrow (): void
193186 {
194187 $ this ->expectException (\LogicException::class);
195188
196- retryAsync (2 , static function () {
189+ retry (2 , static function (): never {
197190 throw new \RuntimeException ;
198- }, static function () {
191+ }, static function (): never {
199192 throw new \LogicException ;
200193 });
201194 }
195+
196+ private static function delayAndReturn (float $ delay , string $ return = null ): Future
197+ {
198+ return async (static function () use ($ delay , $ return ): ?string {
199+ delay ($ delay );
200+
201+ return $ return ;
202+ });
203+ }
202204}
0 commit comments