-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
uncancellable() returns an awaitable that cannot be cancelled.
- Loading branch information
Showing
8 changed files
with
337 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Icicle, a library for writing asynchronous code in PHP using promises and coroutines. | ||
* | ||
* @copyright 2014-2015 Aaron Piotrowski. All rights reserved. | ||
* @license MIT See the LICENSE file that was distributed with this source code for more information. | ||
*/ | ||
|
||
namespace Icicle\Awaitable\Internal; | ||
|
||
use Icicle\Awaitable\Awaitable; | ||
|
||
class UncancellableAwaitable implements Awaitable | ||
{ | ||
use AwaitableMethods; | ||
|
||
/** | ||
* @var \Icicle\Awaitable\Awaitable | ||
*/ | ||
private $awaitable; | ||
|
||
/** | ||
* @param \Icicle\Awaitable\Awaitable $awaitable | ||
*/ | ||
public function __construct(Awaitable $awaitable) | ||
{ | ||
$this->awaitable = $awaitable; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function cancel($reason = null) {} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function timeout($timeout, $reason = null) | ||
{ | ||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function then(callable $onFulfilled = null, callable $onRejected = null) | ||
{ | ||
return $this->awaitable->then($onFulfilled, $onRejected); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function done(callable $onFulfilled = null, callable $onRejected = null) | ||
{ | ||
$this->awaitable->done($onFulfilled, $onRejected); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function delay($time) | ||
{ | ||
return $this->awaitable->delay($time); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function uncancellable() | ||
{ | ||
return $this; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function wait() | ||
{ | ||
return $this->awaitable->wait(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isPending() | ||
{ | ||
return $this->awaitable->isPending(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isFulfilled() | ||
{ | ||
return $this->awaitable->isFulfilled(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isRejected() | ||
{ | ||
return $this->awaitable->isRejected(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isCancelled() | ||
{ | ||
return $this->awaitable->isCancelled(); | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
tests/Awaitable/Internal/UncancellableAwaitableTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Icicle, a library for writing asynchronous code in PHP using promises and coroutines. | ||
* | ||
* @copyright 2014-2015 Aaron Piotrowski. All rights reserved. | ||
* @license MIT See the LICENSE file that was distributed with this source code for more information. | ||
*/ | ||
|
||
namespace Icicle\Tests\Awaitable\Internal; | ||
|
||
use Icicle\Awaitable\Awaitable; | ||
use Icicle\Awaitable\Delayed; | ||
use Icicle\Awaitable\Internal\UncancellableAwaitable; | ||
use Icicle\Loop; | ||
use Icicle\Loop\SelectLoop; | ||
use Icicle\Tests\TestCase; | ||
|
||
/** | ||
* Tests the constructor only. All other methods are covered by PromiseTest. | ||
*/ | ||
class UncancellableAwaitableTest extends TestCase | ||
{ | ||
const TIMEOUT = 0.1; | ||
|
||
/** | ||
* @var \Icicle\Awaitable\Delayed | ||
*/ | ||
private $delayed; | ||
|
||
/** | ||
* @var \Icicle\Awaitable\Internal\UncancellableAwaitable | ||
*/ | ||
private $uncancellable; | ||
|
||
public function setUp() | ||
{ | ||
Loop\loop(new SelectLoop()); | ||
|
||
$this->delayed = new Delayed(); | ||
$this->uncancellable = new UncancellableAwaitable($this->delayed); | ||
} | ||
|
||
public function testResolution() | ||
{ | ||
$this->assertTrue($this->uncancellable->isPending()); | ||
$this->assertFalse($this->uncancellable->isFulfilled()); | ||
$this->assertFalse($this->uncancellable->isRejected()); | ||
|
||
$this->delayed->resolve(); | ||
|
||
$this->assertFalse($this->uncancellable->isPending()); | ||
$this->assertTrue($this->uncancellable->isFulfilled()); | ||
$this->assertFalse($this->uncancellable->isRejected()); | ||
$this->assertFalse($this->uncancellable->isCancelled()); | ||
} | ||
|
||
public function testThen() | ||
{ | ||
$value = 1; | ||
|
||
$callback = $this->createCallback(1); | ||
$callback->method('__invoke') | ||
->with($this->identicalTo($value)) | ||
->will($this->returnValue($value)); | ||
|
||
$awaitable = $this->uncancellable->then($callback); | ||
|
||
$this->assertInstanceOf(Awaitable::class, $awaitable); | ||
|
||
$this->delayed->resolve($value); | ||
|
||
Loop\run(); | ||
} | ||
|
||
public function testDone() | ||
{ | ||
$value = 1; | ||
|
||
$callback = $this->createCallback(1); | ||
$callback->method('__invoke') | ||
->with($this->identicalTo($value)); | ||
|
||
$this->uncancellable->done($callback); | ||
|
||
$this->delayed->resolve($value); | ||
|
||
Loop\run(); | ||
} | ||
|
||
public function testTimeout() | ||
{ | ||
$awaitable = $this->uncancellable->timeout(self::TIMEOUT); | ||
|
||
$timer = Loop\timer(self::TIMEOUT * 2, $this->createCallback(1)); | ||
|
||
Loop\run(); | ||
|
||
$this->assertTrue($awaitable->isPending()); | ||
} | ||
|
||
public function testDelay() | ||
{ | ||
$awaitable = $this->uncancellable->delay(self::TIMEOUT); | ||
|
||
$this->delayed->resolve(); | ||
|
||
$this->assertRunTimeGreaterThan('Icicle\Loop\run', self::TIMEOUT - self::RUNTIME_PRECISION); | ||
|
||
$this->assertTrue($awaitable->isFulfilled()); | ||
} | ||
|
||
public function testUncancellable() | ||
{ | ||
$this->assertSame($this->uncancellable, $this->uncancellable->uncancellable()); | ||
} | ||
|
||
public function testWait() | ||
{ | ||
$value = 1; | ||
|
||
$this->delayed->resolve($value); | ||
|
||
$this->assertSame($value, $this->uncancellable->wait()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters