2021-08-29 18:47:21 +02:00
|
|
|
<?php
|
|
|
|
|
2021-09-06 06:47:06 +02:00
|
|
|
namespace Amp\Future;
|
2021-08-29 18:47:21 +02:00
|
|
|
|
2021-08-30 06:28:25 +02:00
|
|
|
use Amp\CancelledException;
|
2021-08-29 18:47:21 +02:00
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Future;
|
2021-12-02 22:24:56 +01:00
|
|
|
use Amp\TimeoutCancellation;
|
2021-08-29 18:47:21 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-10-15 00:50:40 +02:00
|
|
|
use Revolt\EventLoop;
|
2021-08-29 18:47:21 +02:00
|
|
|
|
|
|
|
class AllTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testSingleComplete(): void
|
|
|
|
{
|
|
|
|
self::assertSame([42], all([Future::complete(42)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTwoComplete(): void
|
|
|
|
{
|
|
|
|
self::assertSame([1, 2], all([Future::complete(1), Future::complete(2)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTwoFirstPending(): void
|
|
|
|
{
|
|
|
|
$deferred = new Deferred;
|
|
|
|
|
2021-11-14 18:35:07 +01:00
|
|
|
EventLoop::delay(0.01, fn () => $deferred->complete(1));
|
2021-08-29 18:47:21 +02:00
|
|
|
|
2021-09-21 01:49:52 +02:00
|
|
|
self::assertSame([1 => 2, 0 => 1], all([$deferred->getFuture(), Future::complete(2)]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayDestructuring(): void
|
|
|
|
{
|
|
|
|
$deferred = new Deferred;
|
|
|
|
|
2021-11-14 18:35:07 +01:00
|
|
|
EventLoop::delay(0.01, fn () => $deferred->complete(1));
|
2021-09-21 01:49:52 +02:00
|
|
|
|
|
|
|
[$first, $second] = all([$deferred->getFuture(), Future::complete(2)]);
|
|
|
|
|
|
|
|
self::assertSame(1, $first);
|
|
|
|
self::assertSame(2, $second);
|
2021-08-29 18:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTwoFirstThrowing(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('foo');
|
|
|
|
|
|
|
|
all([Future::error(new \Exception('foo')), Future::complete(2)]);
|
|
|
|
}
|
|
|
|
|
2021-09-19 18:54:02 +02:00
|
|
|
public function testTwoThrowingWithOneLater(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('foo');
|
|
|
|
|
|
|
|
$deferred = new Deferred;
|
2021-10-15 00:50:40 +02:00
|
|
|
EventLoop::delay(0.1, static fn () => $deferred->error(new \Exception('bar')));
|
2021-09-19 18:54:02 +02:00
|
|
|
|
|
|
|
all([Future::error(new \Exception('foo')), $deferred->getFuture()]);
|
|
|
|
}
|
|
|
|
|
2021-08-29 18:47:21 +02:00
|
|
|
public function testTwoGeneratorThrows(): void
|
|
|
|
{
|
|
|
|
$this->expectException(\Exception::class);
|
|
|
|
$this->expectExceptionMessage('foo');
|
|
|
|
|
|
|
|
all((static function () {
|
|
|
|
yield Future::error(new \Exception('foo'));
|
|
|
|
yield Future::complete(2);
|
|
|
|
})());
|
|
|
|
}
|
2021-08-30 06:28:25 +02:00
|
|
|
|
|
|
|
public function testCancellation(): void
|
|
|
|
{
|
|
|
|
$this->expectException(CancelledException::class);
|
|
|
|
$deferreds = \array_map(function (int $value) {
|
|
|
|
$deferred = new Deferred;
|
2021-11-14 18:35:07 +01:00
|
|
|
EventLoop::delay($value / 10, fn () => $deferred->complete($value));
|
2021-08-30 06:28:25 +02:00
|
|
|
return $deferred;
|
|
|
|
}, \range(1, 3));
|
|
|
|
|
|
|
|
all(\array_map(
|
2021-11-14 18:35:07 +01:00
|
|
|
fn (Deferred $deferred) => $deferred->getFuture(),
|
2021-08-30 06:28:25 +02:00
|
|
|
$deferreds
|
2021-12-02 22:24:56 +01:00
|
|
|
), new TimeoutCancellation(0.2));
|
2021-08-30 06:28:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCompleteBeforeCancellation(): void
|
|
|
|
{
|
|
|
|
$deferreds = \array_map(function (int $value) {
|
|
|
|
$deferred = new Deferred;
|
2021-11-14 18:35:07 +01:00
|
|
|
EventLoop::delay($value / 10, fn () => $deferred->complete($value));
|
2021-08-30 06:28:25 +02:00
|
|
|
return $deferred;
|
|
|
|
}, \range(1, 3));
|
|
|
|
|
|
|
|
self::assertSame([1, 2, 3], all(\array_map(
|
2021-11-14 18:35:07 +01:00
|
|
|
fn (Deferred $deferred) => $deferred->getFuture(),
|
2021-08-30 06:28:25 +02:00
|
|
|
$deferreds
|
2021-12-02 22:24:56 +01:00
|
|
|
), new TimeoutCancellation(0.5)));
|
2021-08-30 06:28:25 +02:00
|
|
|
}
|
2021-08-29 18:47:21 +02:00
|
|
|
}
|