mirror of
https://github.com/danog/amp.git
synced 2024-12-12 09:29:45 +01:00
3af013d418
Amp Future instances double both as Promisor and Promise implementations when AMP_DEBUG is defined and set to false. This switch allows private Promise resolution safety by default at the expense of performance. Amp applications should set AMP_DEBUG to false in production environments to maximize performance.
53 lines
1.6 KiB
PHP
53 lines
1.6 KiB
PHP
<?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) {
|
|
$promisor = new PromisorPrivateImpl;
|
|
$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() {
|
|
$promisor = new PromisorPrivateImpl;
|
|
$stream = (new PromiseStream($promisor->promise()))->stream();
|
|
$stream->next();
|
|
}
|
|
|
|
/**
|
|
* @expectedException \LogicException
|
|
* @expectedExceptionMessage Cannot advance stream: subject Promise failed
|
|
*/
|
|
public function testStreamThrowsIfIteratedAfterFailure() {
|
|
$promisor = new PromisorPrivateImpl;
|
|
$promisor->fail(new \Exception("test"));
|
|
$stream = (new PromiseStream($promisor->promise()))->stream();
|
|
$stream->next();
|
|
}
|
|
}
|