From 0ddf9a656e96ae247d123a7f72cfbfd43a58b651 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Thu, 16 Jul 2020 13:50:38 -0500 Subject: [PATCH] Add Deferred::isResolved() and StreamSource::isComplete() --- lib/Deferred.php | 9 ++++++ lib/Internal/EmitSource.php | 8 +++++ lib/Internal/Placeholder.php | 5 +++ lib/Stream.php | 2 +- lib/StreamSource.php | 8 +++++ test/DeferredTest.php | 3 ++ ...mitSourceTest.php => StreamSourceTest.php} | 31 +++++++++++++++---- 7 files changed, 59 insertions(+), 7 deletions(-) rename test/{EmitSourceTest.php => StreamSourceTest.php} (87%) diff --git a/lib/Deferred.php b/lib/Deferred.php index 39e47ac..8f2ed0f 100644 --- a/lib/Deferred.php +++ b/lib/Deferred.php @@ -23,6 +23,7 @@ final class Deferred use Internal\Placeholder { resolve as public; fail as public; + isResolved as public; } }; @@ -37,6 +38,14 @@ final class Deferred return $this->promise; } + /** + * @return bool True if the contained promise has been resolved. + */ + public function isResolved(): bool + { + return $this->resolver->isResolved(); + } + /** * Fulfill the promise with the given value. * diff --git a/lib/Internal/EmitSource.php b/lib/Internal/EmitSource.php index 65ca222..0029d97 100644 --- a/lib/Internal/EmitSource.php +++ b/lib/Internal/EmitSource.php @@ -218,6 +218,14 @@ final class EmitSource return $deferred->promise(); } + /** + * @return bool True if the stream has been completed or failed. + */ + public function isComplete(): bool + { + return $this->completed; + } + /** * Completes the stream. ** diff --git a/lib/Internal/Placeholder.php b/lib/Internal/Placeholder.php index f84c651..fdb6ec6 100644 --- a/lib/Internal/Placeholder.php +++ b/lib/Internal/Placeholder.php @@ -89,6 +89,11 @@ trait Placeholder } } + private function isResolved(): bool + { + return $this->resolved; + } + /** * @param mixed $value * diff --git a/lib/Stream.php b/lib/Stream.php index 471e3fd..3717862 100644 --- a/lib/Stream.php +++ b/lib/Stream.php @@ -10,7 +10,7 @@ namespace Amp; interface Stream { /** - * Succeeds with the yielded value if the stream has yielded a value or null if the stream has completed. + * Succeeds with the emitted value if the stream has emitted a value or null if the stream has completed. * If the stream fails, the returned promise will fail with the same exception. * * @return Promise Resolves with null if the stream has completed. diff --git a/lib/StreamSource.php b/lib/StreamSource.php index b16ddd1..e2ca679 100644 --- a/lib/StreamSource.php +++ b/lib/StreamSource.php @@ -48,6 +48,14 @@ final class StreamSource return $this->source->emit($value); } + /** + * @return bool True if the stream has been completed or failed. + */ + public function isComplete(): bool + { + return $this->source->isComplete(); + } + /** * Completes the stream. * diff --git a/test/DeferredTest.php b/test/DeferredTest.php index 5d10edf..97e6bde 100644 --- a/test/DeferredTest.php +++ b/test/DeferredTest.php @@ -29,6 +29,8 @@ class DeferredTest extends BaseTest $value = "Resolution value"; $promise = $this->deferred->promise(); + $this->assertFalse($this->deferred->isResolved()); + $invoked = false; $promise->onResolve(function ($exception, $value) use (&$invoked, &$result) { $invoked = true; @@ -37,6 +39,7 @@ class DeferredTest extends BaseTest $this->deferred->resolve($value); + $this->assertTrue($this->deferred->isResolved()); $this->assertTrue($invoked); $this->assertSame($value, $result); } diff --git a/test/EmitSourceTest.php b/test/StreamSourceTest.php similarity index 87% rename from test/EmitSourceTest.php rename to test/StreamSourceTest.php index 6b89772..7e72635 100644 --- a/test/EmitSourceTest.php +++ b/test/StreamSourceTest.php @@ -3,20 +3,20 @@ namespace Amp\Test; use Amp\DisposedException; -use Amp\Internal\EmitSource; use Amp\PHPUnit\AsyncTestCase; use Amp\Promise; +use Amp\StreamSource; use Amp\Success; -class EmitSourceTest extends AsyncTestCase +class StreamSourceTest extends AsyncTestCase { - /** @var EmitSource */ + /** @var StreamSource */ private $source; public function setUp() { parent::setUp(); - $this->source = new EmitSource; + $this->source = new StreamSource; } public function testEmit() @@ -33,11 +33,30 @@ class EmitSourceTest extends AsyncTestCase $this->assertInstanceOf(Promise::class, $promise); $this->assertNull(yield $promise); + $this->assertFalse($this->source->isComplete()); + $this->source->complete(); + $this->assertTrue($this->source->isComplete()); + $this->assertNull(yield $continue); } + public function testFail() + { + $this->assertFalse($this->source->isComplete()); + $this->source->fail($exception = new \Exception); + $this->assertTrue($this->source->isComplete()); + + $stream = $this->source->stream(); + + try { + yield $stream->continue(); + } catch (\Exception $caught) { + $this->assertSame($exception, $caught); + } + } + /** * @depends testEmit */ @@ -53,7 +72,7 @@ class EmitSourceTest extends AsyncTestCase /** * @depends testEmit */ - public function testEmitingNull() + public function testEmittingNull() { $this->expectException(\TypeError::class); $this->expectExceptionMessage('Streams cannot emit NULL'); @@ -64,7 +83,7 @@ class EmitSourceTest extends AsyncTestCase /** * @depends testEmit */ - public function testEmitingPromise() + public function testEmittingPromise() { $this->expectException(\TypeError::class); $this->expectExceptionMessage('Streams cannot emit promises');