2021-09-17 02:16:51 +02:00
< ? php
namespace Amp\Future ;
use Amp\CancelledException ;
use Amp\CompositeException ;
2022-01-16 17:39:04 +01:00
use Amp\CompositeLengthException ;
2021-12-02 22:29:45 +01:00
use Amp\DeferredFuture ;
2021-09-17 02:16:51 +02:00
use Amp\Future ;
2021-12-02 22:24:56 +01:00
use Amp\TimeoutCancellation ;
2021-09-17 02:16:51 +02:00
use PHPUnit\Framework\TestCase ;
2021-10-15 00:50:40 +02:00
use Revolt\EventLoop ;
2021-09-17 02:16:51 +02:00
2022-01-16 17:39:04 +01:00
class AwaitAnyNTest extends TestCase
2021-09-17 02:16:51 +02:00
{
public function testSingleComplete () : void
{
2022-01-16 17:39:04 +01:00
self :: assertSame ([ 0 => 42 ], awaitAnyN ( 1 , [ Future :: complete ( 42 )]));
2021-09-17 02:16:51 +02:00
}
public function testTwoComplete () : void
{
2022-01-16 17:39:04 +01:00
self :: assertSame ([ 1 , 2 ], awaitAnyN ( 2 , [ Future :: complete ( 1 ), Future :: complete ( 2 )]));
2021-09-17 02:16:51 +02:00
}
public function testTwoFirstPending () : void
{
2021-12-02 22:29:45 +01:00
$deferred = new DeferredFuture ();
2021-09-17 02:16:51 +02:00
2022-01-16 17:39:04 +01:00
self :: assertSame ([ 1 => 2 ], awaitAnyN ( 1 , [ $deferred -> getFuture (), Future :: complete ( 2 )]));
2021-09-17 02:16:51 +02:00
}
public function testTwoFirstThrowing () : void
{
2021-12-02 23:00:12 +01:00
self :: assertSame (
[ 'two' => 2 ],
2022-01-16 17:39:04 +01:00
awaitAnyN ( 1 , [ 'one' => Future :: error ( new \Exception ( 'foo' )), 'two' => Future :: complete ( 2 )])
2021-12-02 23:00:12 +01:00
);
2021-09-17 02:16:51 +02:00
}
public function testTwoBothThrowing () : void
{
$this -> expectException ( CompositeException :: class );
2022-01-16 17:39:04 +01:00
$this -> expectExceptionMessage ( 'Multiple exceptions encountered (2); use "Amp\CompositeException::getReasons()" to retrieve the array of exceptions thrown:' );
2021-09-17 02:16:51 +02:00
2022-01-16 17:39:04 +01:00
Future\awaitAnyN ( 2 , [ Future :: error ( new \Exception ( 'foo' )), Future :: error ( new \RuntimeException ( 'bar' ))]);
2021-09-17 02:16:51 +02:00
}
public function testTwoGeneratorThrows () : void
{
2022-01-16 17:39:04 +01:00
self :: assertSame ([ 1 => 2 ], awaitAnyN ( 1 , ( static function () {
2021-09-17 02:16:51 +02:00
yield Future :: error ( new \Exception ( 'foo' ));
yield Future :: complete ( 2 );
2022-01-16 17:39:04 +01:00
})()));
2021-09-17 02:16:51 +02:00
}
public function testCancellation () : void
{
$this -> expectException ( CancelledException :: class );
$deferreds = \array_map ( function ( int $value ) {
2021-12-02 22:29:45 +01:00
$deferred = new DeferredFuture ;
2021-11-14 18:35:07 +01:00
EventLoop :: delay ( $value / 10 , fn () => $deferred -> complete ( $value ));
2021-09-17 02:16:51 +02:00
return $deferred ;
}, \range ( 1 , 3 ));
2022-01-16 17:39:04 +01:00
awaitAnyN ( 3 , \array_map (
2021-12-02 22:29:45 +01:00
fn ( DeferredFuture $deferred ) => $deferred -> getFuture (),
2021-09-17 02:16:51 +02:00
$deferreds
2022-01-16 17:39:04 +01:00
), new TimeoutCancellation ( 0.05 ));
2021-09-17 02:16:51 +02:00
}
public function testCompleteBeforeCancellation () : void
{
$deferreds = \array_map ( function ( int $value ) {
2021-12-02 22:29:45 +01:00
$deferred = new DeferredFuture ;
2021-11-14 18:35:07 +01:00
EventLoop :: delay ( $value / 10 , fn () => $deferred -> complete ( $value ));
2021-09-17 02:16:51 +02:00
return $deferred ;
}, \range ( 1 , 3 ));
2022-01-16 17:39:04 +01:00
self :: assertSame ( \range ( 1 , 3 ), awaitAnyN ( 3 , \array_map (
2021-12-02 22:29:45 +01:00
fn ( DeferredFuture $deferred ) => $deferred -> getFuture (),
2021-09-17 02:16:51 +02:00
$deferreds
2022-01-16 17:39:04 +01:00
), new TimeoutCancellation ( 0.5 )));
2021-09-17 02:16:51 +02:00
}
public function testZero () : void
{
$this -> expectException ( \ValueError :: class );
$this -> expectExceptionMessage ( 'greater than 0' );
2022-01-16 17:39:04 +01:00
awaitAnyN ( 0 , []);
2021-09-17 02:16:51 +02:00
}
public function testTooFew () : void
{
2022-01-16 17:39:04 +01:00
$this -> expectException ( CompositeLengthException :: class );
$this -> expectExceptionMessage ( 'Argument #2 ($futures) contains too few futures to satisfy the required count of 3' );
awaitAnyN ( 3 , [ Future :: complete ( 1 ), Future :: complete ( 2 )]);
2021-09-17 02:16:51 +02:00
}
}