22namespace ScriptFUSIONTest \Retry ;
33
44use Amp \Delayed ;
5+ use Amp \Promise ;
6+ use Amp \Success ;
57use ScriptFUSION \Retry \FailingTooHardException ;
68
79final class RetryAsyncTest extends \PHPUnit_Framework_TestCase
@@ -94,7 +96,7 @@ public function testFailingTooHardAsync()
9496 }
9597
9698 /**
97- * These that the error callback is called before each retry.
99+ * Tests that the error callback is called before each retry.
98100 */
99101 public function testErrorCallbackAsync ()
100102 {
@@ -120,13 +122,37 @@ public function testErrorCallbackAsync()
120122 }
121123
122124 /**
123- * Tests that an error handler that returns false aborts retrying.
125+ * Tests that an error callback that returns a promise has its promise resolved.
126+ */
127+ public function testPromiseErrorCallback ()
128+ {
129+ $ delay = 250 ; // Quarter of a second.
130+ $ start = microtime (true );
131+
132+ try {
133+ \Amp \Promise \wait (
134+ \ScriptFUSION \Retry \retryAsync ($ tries = 3 , static function () {
135+ throw new \DomainException ;
136+ }, static function () use ($ delay ): Promise {
137+ return new Delayed ($ delay );
138+ })
139+ );
140+ } catch (FailingTooHardException $ outerException ) {
141+ self ::assertInstanceOf (\DomainException::class, $ outerException ->getPrevious ());
142+ }
143+
144+ self ::assertTrue (isset ($ outerException ));
145+ self ::assertGreaterThan ($ start + $ delay * ($ tries - 1 ) / 1000 , microtime (true ));
146+ }
147+
148+ /**
149+ * Tests that when error handler that returns false, it aborts retrying.
124150 */
125151 public function testErrorCallbackHaltAsync ()
126152 {
127153 $ invocations = 0 ;
128154
129- \ScriptFUSION \Retry \retryAsync ($ tries = 2 , static function () use (&$ invocations ) {
155+ \ScriptFUSION \Retry \retryAsync (2 , static function () use (&$ invocations ) {
130156 ++$ invocations ;
131157
132158 throw new \RuntimeException ;
@@ -136,4 +162,36 @@ public function testErrorCallbackHaltAsync()
136162
137163 self ::assertSame (1 , $ invocations );
138164 }
165+
166+ /**
167+ * Tests that when an error handler returns a promise that false, it aborts retrying.
168+ */
169+ public function testPromiseErrorCallbackHaltAsync ()
170+ {
171+ $ invocations = 0 ;
172+
173+ \ScriptFUSION \Retry \retryAsync (2 , static function () use (&$ invocations ) {
174+ ++$ invocations ;
175+
176+ throw new \RuntimeException ;
177+ }, static function (): Promise {
178+ return new Success (false );
179+ });
180+
181+ self ::assertSame (1 , $ invocations );
182+ }
183+
184+ /**
185+ * Tests that the exception handler can throw an exception that will not be caught.
186+ */
187+ public function testErrorCallbackCanThrow ()
188+ {
189+ $ this ->setExpectedException (\LogicException::class);
190+
191+ \ScriptFUSION \Retry \retryAsync (2 , static function () {
192+ throw new \RuntimeException ;
193+ }, static function () {
194+ throw new \LogicException ;
195+ });
196+ }
139197}
0 commit comments