2020-05-13 17:15:21 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp\DisposedException;
|
2020-05-21 17:11:22 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2020-08-23 16:18:28 +02:00
|
|
|
use Amp\PipelineSource;
|
2020-05-13 17:15:21 +02:00
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
class PipelineSourceTest extends AsyncTestCase
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-08-23 16:18:28 +02:00
|
|
|
/** @var PipelineSource */
|
2020-05-13 17:15:21 +02:00
|
|
|
private $source;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2020-05-21 17:11:22 +02:00
|
|
|
parent::setUp();
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->source = new PipelineSource;
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
public function testEmit()
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-28 19:59:55 +02:00
|
|
|
$value = 'Emited Value';
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
$promise = $this->source->emit($value);
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->assertSame($value, yield $pipeline->continue());
|
2020-05-19 00:01:14 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$continue = $pipeline->continue(); // Promise will not resolve until another value is emitted or pipeline completed.
|
2020-05-19 00:01:14 +02:00
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
$this->assertNull(yield $promise);
|
|
|
|
|
2020-07-16 20:50:38 +02:00
|
|
|
$this->assertFalse($this->source->isComplete());
|
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->source->complete();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-07-16 20:50:38 +02:00
|
|
|
$this->assertTrue($this->source->isComplete());
|
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertNull(yield $continue);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 20:50:38 +02:00
|
|
|
public function testFail()
|
|
|
|
{
|
|
|
|
$this->assertFalse($this->source->isComplete());
|
|
|
|
$this->source->fail($exception = new \Exception);
|
|
|
|
$this->assertTrue($this->source->isComplete());
|
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-07-16 20:50:38 +02:00
|
|
|
|
|
|
|
try {
|
2020-08-23 16:18:28 +02:00
|
|
|
yield $pipeline->continue();
|
2020-07-16 20:50:38 +02:00
|
|
|
} catch (\Exception $caught) {
|
|
|
|
$this->assertSame($exception, $caught);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:15:21 +02:00
|
|
|
/**
|
2020-05-28 19:59:55 +02:00
|
|
|
* @depends testEmit
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
2020-05-28 19:59:55 +02:00
|
|
|
public function testEmitAfterComplete()
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
|
|
|
$this->expectException(\Error::class);
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->expectExceptionMessage('Pipelines cannot emit values after calling complete');
|
2020-05-13 17:15:21 +02:00
|
|
|
|
|
|
|
$this->source->complete();
|
2020-05-28 19:59:55 +02:00
|
|
|
$this->source->emit(1);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-28 19:59:55 +02:00
|
|
|
* @depends testEmit
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
2020-07-16 20:50:38 +02:00
|
|
|
public function testEmittingNull()
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->expectException(\TypeError::class);
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->expectExceptionMessage('Pipelines cannot emit NULL');
|
2020-05-21 17:11:22 +02:00
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
$this->source->emit(null);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-28 19:59:55 +02:00
|
|
|
* @depends testEmit
|
2020-05-13 17:15:21 +02:00
|
|
|
*/
|
2020-07-16 20:50:38 +02:00
|
|
|
public function testEmittingPromise()
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->expectException(\TypeError::class);
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->expectExceptionMessage('Pipelines cannot emit promises');
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
$this->source->emit(new Success);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoubleComplete()
|
|
|
|
{
|
|
|
|
$this->expectException(\Error::class);
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->expectExceptionMessage('Pipeline has already been completed');
|
2020-05-13 17:15:21 +02:00
|
|
|
|
|
|
|
$this->source->complete();
|
|
|
|
$this->source->complete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoubleFail()
|
|
|
|
{
|
|
|
|
$this->expectException(\Error::class);
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->expectExceptionMessage('Pipeline has already been completed');
|
2020-05-13 17:15:21 +02:00
|
|
|
|
|
|
|
$this->source->fail(new \Exception);
|
|
|
|
$this->source->fail(new \Exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoubleStart()
|
|
|
|
{
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
|
|
|
$this->expectException(\Error::class);
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->expectExceptionMessage('A pipeline may be started only once');
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
public function testEmitAfterContinue()
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-05-28 19:59:55 +02:00
|
|
|
$value = 'Emited Value';
|
2020-05-21 17:11:22 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$promise = $pipeline->continue();
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
$backPressure = $this->source->emit($value);
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertSame($value, yield $promise);
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline->continue();
|
2020-05-21 17:11:22 +02:00
|
|
|
|
|
|
|
$this->assertNull(yield $backPressure);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testContinueAfterComplete()
|
|
|
|
{
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->source->complete();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$promise = $pipeline->continue();
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertNull(yield $promise);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testContinueAfterFail()
|
|
|
|
{
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->source->fail(new \Exception('Pipeline failed'));
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$promise = $pipeline->continue();
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->expectException(\Exception::class);
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->expectExceptionMessage('Pipeline failed');
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
yield $promise;
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testCompleteAfterContinue()
|
|
|
|
{
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$promise = $pipeline->continue();
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->source->complete();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertNull(yield $promise);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
public function testDestroyingPipelineRelievesBackPressure()
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-13 17:15:21 +02:00
|
|
|
|
|
|
|
$invoked = 0;
|
|
|
|
$onResolved = function () use (&$invoked) {
|
|
|
|
$invoked++;
|
|
|
|
};
|
|
|
|
|
|
|
|
foreach (\range(1, 5) as $value) {
|
2020-05-28 19:59:55 +02:00
|
|
|
$promise = $this->source->emit($value);
|
2020-05-13 17:15:21 +02:00
|
|
|
$promise->onResolve($onResolved);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame(0, $invoked);
|
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
unset($pipeline); // Should relieve all back-pressure.
|
2020-05-13 17:15:21 +02:00
|
|
|
|
|
|
|
$this->assertSame(5, $invoked);
|
|
|
|
|
|
|
|
$this->source->complete(); // Should not throw.
|
|
|
|
|
|
|
|
$this->expectException(\Error::class);
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->expectExceptionMessage('Pipeline has already been completed');
|
2020-05-13 17:15:21 +02:00
|
|
|
|
|
|
|
$this->source->complete(); // Should throw.
|
|
|
|
}
|
|
|
|
|
2020-07-30 21:33:39 +02:00
|
|
|
public function testOnDisposal()
|
|
|
|
{
|
|
|
|
$invoked = false;
|
|
|
|
$this->source->onDisposal(function () use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertFalse($invoked);
|
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
|
|
|
$pipeline->dispose();
|
2020-07-30 21:33:39 +02:00
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
|
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testOnDisposalAfterCompletion()
|
|
|
|
{
|
|
|
|
$invoked = false;
|
|
|
|
$this->source->onDisposal(function () use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertFalse($invoked);
|
|
|
|
|
|
|
|
$this->source->complete();
|
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
|
|
|
$pipeline->dispose();
|
2020-07-30 21:33:39 +02:00
|
|
|
|
|
|
|
$this->assertFalse($invoked);
|
|
|
|
|
|
|
|
$this->source->onDisposal($this->createCallback(0));
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
public function testEmitAfterDisposal()
|
2020-05-13 17:15:21 +02:00
|
|
|
{
|
|
|
|
$this->expectException(DisposedException::class);
|
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-28 19:59:55 +02:00
|
|
|
$promise = $this->source->emit(1);
|
2020-07-29 17:29:57 +02:00
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline->dispose();
|
2020-07-29 17:29:57 +02:00
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
2020-07-17 18:22:13 +02:00
|
|
|
$this->assertTrue($this->source->isDisposed());
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertNull(yield $promise);
|
2020-05-28 19:59:55 +02:00
|
|
|
yield $this->source->emit(1);
|
2020-05-13 21:59:31 +02:00
|
|
|
}
|
|
|
|
|
2020-08-27 19:45:48 +02:00
|
|
|
public function testEmitAfterAutomaticDisposal()
|
|
|
|
{
|
|
|
|
$this->expectException(DisposedException::class);
|
|
|
|
|
|
|
|
$pipeline = $this->source->pipe();
|
|
|
|
$promise = $this->source->emit(1);
|
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
|
|
|
unset($pipeline); // Trigger automatic disposal.
|
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
|
|
|
$this->assertTrue($this->source->isDisposed());
|
|
|
|
$this->assertNull(yield $promise);
|
|
|
|
yield $this->source->emit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEmitAfterAutomaticDisposalWithPendingContinuePromise()
|
2020-08-24 23:28:08 +02:00
|
|
|
{
|
|
|
|
$pipeline = $this->source->pipe();
|
|
|
|
$promise = $pipeline->continue();
|
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
2020-08-27 19:45:48 +02:00
|
|
|
unset($pipeline); // Trigger automatic disposal.
|
2020-08-24 23:28:08 +02:00
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
|
|
|
$this->assertFalse($this->source->isDisposed());
|
|
|
|
yield $this->source->emit(1);
|
|
|
|
$this->assertSame(1, yield $promise);
|
|
|
|
|
|
|
|
$this->expectException(DisposedException::class);
|
|
|
|
|
|
|
|
$this->assertTrue($this->source->isDisposed());
|
|
|
|
yield $this->source->emit(2);
|
|
|
|
}
|
2020-05-13 21:59:31 +02:00
|
|
|
|
2020-08-27 19:45:48 +02:00
|
|
|
public function testEmitAfterExplicitDisposalWithPendingContinuePromise()
|
|
|
|
{
|
|
|
|
$pipeline = $this->source->pipe();
|
|
|
|
$promise = $pipeline->continue();
|
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
|
|
|
$pipeline->dispose();
|
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
|
|
|
$this->assertTrue($this->source->isDisposed());
|
|
|
|
|
|
|
|
$this->expectException(DisposedException::class);
|
|
|
|
|
|
|
|
$this->assertSame(1, yield $promise);
|
|
|
|
}
|
|
|
|
|
2020-05-28 19:59:55 +02:00
|
|
|
public function testEmitAfterDestruct()
|
2020-05-13 21:59:31 +02:00
|
|
|
{
|
|
|
|
$this->expectException(DisposedException::class);
|
|
|
|
|
2020-08-23 16:18:28 +02:00
|
|
|
$pipeline = $this->source->pipe();
|
2020-05-28 19:59:55 +02:00
|
|
|
$promise = $this->source->emit(1);
|
2020-07-29 17:29:57 +02:00
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
2020-08-23 16:18:28 +02:00
|
|
|
unset($pipeline);
|
2020-07-29 17:29:57 +02:00
|
|
|
$this->source->onDisposal($this->createCallback(1));
|
2020-07-17 18:22:13 +02:00
|
|
|
$this->assertTrue($this->source->isDisposed());
|
2020-05-21 17:11:22 +02:00
|
|
|
$this->assertNull(yield $promise);
|
2020-05-28 19:59:55 +02:00
|
|
|
yield $this->source->emit(1);
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|
2020-07-30 21:33:39 +02:00
|
|
|
|
|
|
|
public function testFailWithDisposedException()
|
|
|
|
{
|
|
|
|
$this->expectException(\Error::class);
|
2020-08-23 16:18:28 +02:00
|
|
|
$this->expectExceptionMessage('Cannot fail a pipeline with an instance of');
|
2020-07-30 21:33:39 +02:00
|
|
|
|
|
|
|
$this->source->fail(new DisposedException);
|
|
|
|
}
|
2020-05-13 17:15:21 +02:00
|
|
|
}
|