1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00

Add Deferred::isResolved() and StreamSource::isComplete()

This commit is contained in:
Aaron Piotrowski 2020-07-16 13:50:38 -05:00
parent 1577906fc1
commit 0ddf9a656e
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
7 changed files with 59 additions and 7 deletions

View File

@ -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.
*

View File

@ -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.
**

View File

@ -89,6 +89,11 @@ trait Placeholder
}
}
private function isResolved(): bool
{
return $this->resolved;
}
/**
* @param mixed $value
*

View File

@ -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<mixed|null> Resolves with null if the stream has completed.

View File

@ -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.
*

View File

@ -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);
}

View File

@ -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');