2015-05-18 20:25:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp\PromiseStream;
|
|
|
|
use Amp\NativeReactor;
|
|
|
|
|
|
|
|
class PromiseStreamTest extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Exception
|
|
|
|
* @expectedExceptionMessage test
|
|
|
|
*/
|
|
|
|
public function testStreamThrowsIfPromiseFails() {
|
|
|
|
(new NativeReactor)->run(function($reactor) {
|
2015-05-19 05:57:34 +02:00
|
|
|
$promisor = new PromisorPrivateImpl;
|
2015-05-18 20:25:33 +02:00
|
|
|
$reactor->repeat(function($reactor, $watcherId) use (&$i, $promisor) {
|
|
|
|
$i++;
|
|
|
|
$promisor->update($i);
|
|
|
|
if ($i === 3) {
|
|
|
|
$reactor->cancel($watcherId);
|
|
|
|
$promisor->fail(new \Exception(
|
|
|
|
"test"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
|
|
|
|
$result = (yield new PromiseStream($promisor->promise()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
* @expectedExceptionMessage Cannot advance stream: previous Promise not yet resolved
|
|
|
|
*/
|
|
|
|
public function testStreamThrowsIfPrematurelyIterated() {
|
2015-05-19 05:57:34 +02:00
|
|
|
$promisor = new PromisorPrivateImpl;
|
2015-05-18 20:25:33 +02:00
|
|
|
$stream = (new PromiseStream($promisor->promise()))->stream();
|
|
|
|
$stream->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
* @expectedExceptionMessage Cannot advance stream: subject Promise failed
|
|
|
|
*/
|
|
|
|
public function testStreamThrowsIfIteratedAfterFailure() {
|
2015-05-19 05:57:34 +02:00
|
|
|
$promisor = new PromisorPrivateImpl;
|
2015-05-18 20:25:33 +02:00
|
|
|
$promisor->fail(new \Exception("test"));
|
|
|
|
$stream = (new PromiseStream($promisor->promise()))->stream();
|
|
|
|
$stream->next();
|
|
|
|
}
|
|
|
|
}
|