1
0
mirror of https://github.com/danog/amp.git synced 2024-12-04 18:38:17 +01:00
amp/test/InternalProducerTest.php

192 lines
4.9 KiB
PHP
Raw Normal View History

2020-09-28 05:19:52 +02:00
<?php
namespace Amp\Test;
use Amp\Deferred;
use Amp\Failure;
use Amp\Internal\Producer;
use Amp\PHPUnit\AsyncTestCase;
use Amp\PHPUnit\TestException;
use Amp\Promise;
use Amp\Success;
2021-03-26 22:34:32 +01:00
use function Amp\async;
use function Amp\await;
2020-09-28 05:19:52 +02:00
class InternalProducerTest extends AsyncTestCase
{
private Producer $producer;
public function setUp(): void
{
parent::setUp();
$this->producer = new Producer;
}
2021-03-26 22:34:32 +01:00
public function testEmit(): void
2020-09-28 05:19:52 +02:00
{
$value = 1;
2021-03-26 22:34:32 +01:00
$promise = async(fn () => $this->producer->emit($value));
2020-09-28 05:19:52 +02:00
2021-03-26 22:34:32 +01:00
self::assertTrue(await($this->producer->advance()));
self::assertSame($value, $this->producer->getCurrent());
2020-09-28 05:19:52 +02:00
2021-03-26 22:34:32 +01:00
self::assertInstanceOf(Promise::class, $promise);
2020-09-28 05:19:52 +02:00
}
/**
* @depends testEmit
*/
public function testEmitSuccessfulPromise(): \Generator
{
$value = 1;
$promise = new Success($value);
$promise = $this->producer->emit($promise);
2021-03-26 22:34:32 +01:00
self::assertTrue(yield $this->producer->advance());
self::assertSame($value, $this->producer->getCurrent());
2020-09-28 05:19:52 +02:00
2021-03-26 22:34:32 +01:00
self::assertInstanceOf(Promise::class, $promise);
2020-09-28 05:19:52 +02:00
}
/**
* @depends testEmit
*/
public function testEmitFailedPromise(): \Generator
{
$exception = new TestException;
$promise = new Failure($exception);
$promise = $this->producer->emit($promise);
try {
2021-03-26 22:34:32 +01:00
self::assertTrue(yield $this->producer->advance());
self::fail("The exception used to fail the iterator should be thrown from advance()");
2020-09-28 05:19:52 +02:00
} catch (TestException $reason) {
2021-03-26 22:34:32 +01:00
self::assertSame($reason, $exception);
2020-09-28 05:19:52 +02:00
}
2021-03-26 22:34:32 +01:00
self::assertInstanceOf(Promise::class, $promise);
2020-09-28 05:19:52 +02:00
}
/**
* @depends testEmit
*/
2021-03-26 22:34:32 +01:00
public function testEmitPendingPromise(): void
2020-09-28 05:19:52 +02:00
{
$value = 1;
$deferred = new Deferred;
$this->producer->emit($deferred->promise());
$deferred->resolve($value);
2021-03-26 22:34:32 +01:00
self::assertTrue(await($this->producer->advance()));
self::assertSame($value, $this->producer->getCurrent());
2020-09-28 05:19:52 +02:00
}
/**
* @depends testEmit
*/
2021-03-26 22:34:32 +01:00
public function testEmitPendingPromiseThenNonPromise(): void
2020-09-28 05:19:52 +02:00
{
$deferred = new Deferred;
$this->producer->emit($deferred->promise());
$this->producer->emit(2);
2021-03-26 22:34:32 +01:00
self::assertTrue(await($this->producer->advance()));
self::assertSame(2, $this->producer->getCurrent());
2020-09-28 05:19:52 +02:00
$deferred->resolve(1);
2021-03-26 22:34:32 +01:00
self::assertTrue(await($this->producer->advance()));
self::assertSame(1, $this->producer->getCurrent());
2020-09-28 05:19:52 +02:00
}
/**
* @depends testEmit
*/
public function testEmitAfterComplete(): void
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("Iterators cannot emit values after calling complete");
$this->producer->complete();
$this->producer->emit(1);
}
public function testGetCurrentAfterComplete(): void
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("The iterator has completed");
$this->producer->complete();
$this->producer->getCurrent();
}
/**
* @depends testEmit
*/
2021-03-26 22:34:32 +01:00
public function testEmitPendingPromiseThenComplete(): void
2020-09-28 05:19:52 +02:00
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("The iterator was completed before the promise result could be emitted");
$deferred = new Deferred;
$promise = $this->producer->emit($deferred->promise());
$this->producer->complete();
$deferred->resolve();
2021-03-26 22:34:32 +01:00
await($promise);
2020-09-28 05:19:52 +02:00
}
/**
* @depends testEmit
*/
2021-03-26 22:34:32 +01:00
public function testEmitPendingPromiseThenFail(): void
2020-09-28 05:19:52 +02:00
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("The iterator was completed before the promise result could be emitted");
$deferred = new Deferred;
$promise = $this->producer->emit($deferred->promise());
$this->producer->complete();
$deferred->fail(new \Exception);
2021-03-26 22:34:32 +01:00
await($promise);
2020-09-28 05:19:52 +02:00
}
public function testDoubleAdvance(): void
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("The prior promise returned must resolve before invoking this method again");
$this->producer->advance();
$this->producer->advance();
}
public function testGetCurrentBeforeAdvance(): void
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("Promise returned from advance() must resolve before calling this method");
$this->producer->getCurrent();
}
public function testDoubleComplete(): void
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("Iterator has already been completed");
$this->producer->complete();
$this->producer->complete();
}
}