1
0
mirror of https://github.com/danog/amp.git synced 2025-01-08 22:18:16 +01:00
amp/test/PrivateFutureTest.php

98 lines
3.0 KiB
PHP
Raw Normal View History

2014-09-22 22:47:48 +02:00
<?php
2014-09-23 04:38:32 +02:00
namespace Amp\Test;
2014-09-22 22:47:48 +02:00
2014-09-23 04:38:32 +02:00
use Amp\PrivateFuture;
use Amp\NativeReactor;
2014-09-22 22:47:48 +02:00
class PrivateFutureTest extends \PHPUnit_Framework_TestCase {
public function testPromiseReturnsUnresolvedInstance() {
$promisor = new PrivateFuture;
$this->assertInstanceOf('Amp\Unresolved', $promisor->promise());
2014-09-22 22:47:48 +02:00
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Promise already resolved
*/
public function testSucceedThrowsIfAlreadyResolved() {
$promisor = new PrivateFuture;
2014-09-22 22:47:48 +02:00
$promisor->succeed(42);
$promisor->succeed('zanzibar');
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage A Promise cannot act as its own resolution result
*/
public function testSucceedThrowsIfPromiseIsTheResolutionValue() {
$promisor = new PrivateFuture;
2014-09-22 22:47:48 +02:00
$promise = $promisor->promise();
$promisor->succeed($promise);
}
/**
* @expectedException \LogicException
* @expectedExceptionMessage Promise already resolved
*/
public function testFailThrowsIfAlreadyResolved() {
$promisor = new PrivateFuture;
2014-09-22 22:47:48 +02:00
$promisor->succeed(42);
$promisor->fail(new \Exception);
}
public function testSucceedingWithPromisePipelinesResult() {
(new NativeReactor)->run(function($reactor) {
$promisor = new PrivateFuture;
$next = new PrivateFuture;
$reactor->once(function() use ($next) {
$next->succeed(42);
}, $msDelay = 1);
$promisor->succeed($next->promise());
$result = (yield $promisor->promise());
$this->assertSame(42, $result);
});
2014-09-22 22:47:48 +02:00
}
/**
* @expectedException \RuntimeException
* @expectedExceptionMessage fugazi
*/
public function testFailingWithPromisePipelinesResult() {
(new NativeReactor)->run(function($reactor) {
$promisor = new PrivateFuture;
$next = new PrivateFuture;
$reactor->once(function() use ($next) {
$next->fail(new \RuntimeException('fugazi'));
}, $msDelay = 10);
2014-09-22 22:47:48 +02:00
$promisor->succeed($next->promise());
2014-09-22 22:47:48 +02:00
yield $promisor->promise();
});
2014-09-22 22:47:48 +02:00
}
2015-02-06 05:44:11 +01:00
public function testUpdate() {
$updatable = 0;
(new NativeReactor)->run(function() use (&$updatable) {
$i = 0;
$promisor = new PrivateFuture;
$updater = function($reactor, $watcherId) use ($promisor, &$i) {
$promisor->update(++$i);
if ($i === 3) {
$reactor->cancel($watcherId);
// reactor run loop should now be able to exit
}
};
$promise = $promisor->promise();
$promise->watch(function($updateData) use (&$updatable) {
$updatable += $updateData;
});
yield 'repeat' => [$updater, $msDelay = 10];
});
$this->assertSame(6, $updatable);
}
2014-09-22 22:47:48 +02:00
}