1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/test/Future/AllTest.php

83 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2021-09-06 06:47:06 +02:00
namespace Amp\Future;
2021-08-30 06:28:25 +02:00
use Amp\CancelledException;
use Amp\Deferred;
use Amp\Future;
2021-08-30 06:28:25 +02:00
use Amp\TimeoutCancellationToken;
use PHPUnit\Framework\TestCase;
use Revolt\EventLoop\Loop;
use function Amp\Future\all;
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-08-30 06:28:25 +02:00
Loop::delay(0.01, fn() => $deferred->complete(1));
self::assertSame([1, 2], all([$deferred->getFuture(), Future::complete(2)]));
}
public function testTwoFirstThrowing(): void
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('foo');
all([Future::error(new \Exception('foo')), Future::complete(2)]);
}
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;
Loop::delay($value / 10, fn() => $deferred->complete($value));
return $deferred;
}, \range(1, 3));
all(\array_map(
fn(Deferred $deferred) => $deferred->getFuture(),
$deferreds
), new TimeoutCancellationToken(0.2));
}
public function testCompleteBeforeCancellation(): void
{
$deferreds = \array_map(function (int $value) {
$deferred = new Deferred;
Loop::delay($value / 10, fn() => $deferred->complete($value));
return $deferred;
}, \range(1, 3));
self::assertSame([1, 2, 3], all(\array_map(
fn(Deferred $deferred) => $deferred->getFuture(),
$deferreds
), new TimeoutCancellationToken(0.5)));
}
}