1
0
mirror of https://github.com/danog/amp.git synced 2025-01-21 21:01:16 +01:00

Update to promise 0.4.0 and event-loop 0.5.0, add humbug config

This commit is contained in:
Niklas Keller 2017-01-07 13:47:45 +01:00
parent 559c64fdb8
commit ae69e92266
52 changed files with 446 additions and 432 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@ build
composer.lock
phpunit.xml
vendor
humbuglog.txt
humbug.json

View File

@ -27,14 +27,15 @@
],
"require": {
"php": ">=7",
"async-interop/promise": "^0.3",
"async-interop/event-loop": "^0.4",
"async-interop/event-loop-implementation": "^0.4"
"async-interop/promise": "^0.4",
"async-interop/event-loop": "^0.5",
"async-interop/event-loop-implementation": "^0.5"
},
"require-dev": {
"amphp/loop": "dev-master",
"async-interop/promise-test": "dev-master",
"friendsofphp/php-cs-fixer": "~1.9"
"async-interop/promise-test": "^0.4",
"friendsofphp/php-cs-fixer": "~1.9",
"humbug/humbug": "^1@dev"
},
"autoload": {
"psr-4": {

View File

@ -4,7 +4,7 @@
require dirname(__DIR__) . '/vendor/autoload.php';
use Amp\{ Coroutine, Emitter, Pause, Loop\NativeLoop };
use Interop\Async\Loop;
use AsyncInterop\Loop;
Loop::execute(Amp\wrap(function () {
try {

View File

@ -4,7 +4,7 @@
require dirname(__DIR__) . '/vendor/autoload.php';
use Amp\{ Coroutine, Emitter, Listener, Pause, Stream, Loop\NativeLoop };
use Interop\Async\Loop;
use AsyncInterop\Loop;
Loop::execute(Amp\wrap(function () {
try {

View File

@ -4,7 +4,7 @@
require dirname(__DIR__) . '/vendor/autoload.php';
use Amp\{ Coroutine, Listener, Pause, Producer, Stream };
use Interop\Async\Loop;
use AsyncInterop\Loop;
Loop::execute(Amp\wrap(function () {
try {

11
humbug.json.dist Normal file
View File

@ -0,0 +1,11 @@
{
"source": {
"directories": [
"lib"
]
},
"timeout": 10,
"logs": {
"text": "humbuglog.txt"
}
}

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\{ Loop, Promise };
use AsyncInterop\{ Loop, Promise };
/**
* Creates a promise from a generator function yielding promises.

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\Promise;
use AsyncInterop\Promise;
// @codeCoverageIgnoreStart
try {
@ -15,7 +15,7 @@ try {
*/
final class Deferred {
/**
* @var \Interop\Async\Promise
* @var \AsyncInterop\Promise
*/
private $promise;
@ -37,7 +37,7 @@ try {
}
/**
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*/
public function promise(): Promise {
return $this->promise;
@ -73,7 +73,7 @@ try {
}
/**
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*/
public function promise(): Promise {
return $this;

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\Promise;
use AsyncInterop\Promise;
// @codeCoverageIgnoreStart
try {
@ -56,7 +56,7 @@ try {
*
* @param mixed $value
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*/
public function emit($value): Promise {
return ($this->emit)($value);

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\Promise\ErrorHandler;
use AsyncInterop\Promise\ErrorHandler;
/**
* Creates a failed stream (which is also a promise) using the given exception.
@ -28,7 +28,7 @@ final class Failure implements Stream {
ErrorHandler::notify($exception);
}
}
/**
* {@inheritdoc}
*/

View File

@ -3,11 +3,11 @@
namespace Amp\Internal;
use Amp\Failure;
use Interop\Async\{ Promise, Promise\ErrorHandler };
use AsyncInterop\{ Promise, Promise\ErrorHandler };
/**
* Trait used by Promise implementations. Do not use this trait in your code, instead compose your class from one of
* the available classes implementing \Interop\Async\Promise.
* the available classes implementing \AsyncInterop\Promise.
*
* @internal
*/

View File

@ -2,7 +2,7 @@
namespace Amp\Internal;
use Interop\Async\Promise;
use AsyncInterop\Promise;
/**
* An promise that cannot be externally resolved. Used by Deferred in development mode.
@ -24,7 +24,7 @@ final class PrivatePromise implements Promise {
$resolve = function ($value = null) {
$this->resolve($value);
};
/**
* Fails the promise with the given exception.
*

View File

@ -3,7 +3,7 @@
namespace Amp\Internal;
use Amp\Stream;
use Interop\Async\Promise;
use AsyncInterop\Promise;
/**
* An stream that cannot externally emit values. Used by Emitter in development mode.
@ -22,7 +22,7 @@ final class PrivateStream implements Stream {
*
* @param mixed $value
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*/
$emit = function ($value = null): Promise {
return $this->emit($value);

View File

@ -3,7 +3,7 @@
namespace Amp\Internal;
use Amp\{ Deferred, Success };
use Interop\Async\{ Promise, Promise\ErrorHandler };
use AsyncInterop\{ Promise, Promise\ErrorHandler };
/**
* Trait used by Stream implementations. Do not use this trait in your code, instead compose your class from one of
@ -20,7 +20,7 @@ trait Producer {
/** @var callable[] */
private $listeners = [];
/**
* @param callable $onNext
*/
@ -38,7 +38,7 @@ trait Producer {
*
* @param mixed $value
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*
* @throws \Error If the stream has resolved.
*/
@ -56,16 +56,16 @@ trait Producer {
);
return;
}
if ($e) {
$this->fail($e);
$deferred->fail($e);
return;
}
$deferred->resolve($this->emit($v));
});
return $deferred->promise();
}

View File

@ -2,7 +2,7 @@
namespace Amp\Internal;
use Interop\Async\Promise\ErrorHandler;
use AsyncInterop\Promise\ErrorHandler;
/**
* Stores a set of functions to be invoked when a promise is resolved.

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\Promise;
use AsyncInterop\Promise;
/**
* Creates a promise that calls $promisor only when the result of the promise is requested (i.e. when() is called on
@ -13,7 +13,7 @@ class Lazy implements Promise {
/** @var callable|null */
private $promisor;
/** @var \Interop\Async\Promise|null */
/** @var \AsyncInterop\Promise|null */
private $promise;
/**

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\Promise;
use AsyncInterop\Promise;
/**
* Asynchronous iterator that can be used within a coroutine to iterate over the emitted values from an Stream.
@ -111,7 +111,7 @@ class Listener {
* Succeeds with true if an emitted value is available by calling getCurrent() or false if the stream has
* resolved. If the stream fails, the returned promise will fail with the same exception.
*
* @return \Interop\Async\Promise<bool>
* @return \AsyncInterop\Promise<bool>
*/
public function advance(): Promise {
if (isset($this->deferreds[$this->position])) {

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\{ Loop, Promise };
use AsyncInterop\{ Loop, Promise };
/**
* Creates a promise that resolves itself with a given value after a number of milliseconds.

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\Loop;
use AsyncInterop\Loop;
final class Producer implements Stream {
use CallableMaker, Internal\Producer;
@ -18,19 +18,19 @@ final class Producer implements Stream {
if (!$result instanceof \Generator) {
throw new \Error("The callable did not return a Generator");
}
Loop::defer(function () use ($result) {
$coroutine = new Coroutine($result);
$coroutine->when(function ($exception, $value) {
if ($this->resolved) {
return;
}
if ($exception) {
$this->fail($exception);
return;
}
$this->resolve($value);
});
});

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\Promise;
use AsyncInterop\Promise;
/**
* Represents a set of asynchronous values. A stream is analogous to an asynchronous generator, yielding (emitting)

View File

@ -2,11 +2,11 @@
namespace Amp;
use Interop\Async\{ Promise, Promise\ErrorHandler };
use AsyncInterop\{ Promise, Promise\ErrorHandler };
/**
* Creates a successful stream (which is also a promise) using the given value (which can be any value except another
* object implementing \Interop\Async\Promise).
* object implementing \AsyncInterop\Promise).
*/
final class Success implements Stream {
/** @var mixed */
@ -36,7 +36,7 @@ final class Success implements Stream {
ErrorHandler::notify($exception);
}
}
/**
* {@inheritdoc}
*/

View File

@ -2,7 +2,7 @@
namespace Amp;
use Interop\Async\{ Loop, Promise\ErrorHandler };
use AsyncInterop\{ Loop, Promise\ErrorHandler };
ErrorHandler::set(function (\Throwable $exception) {
Loop::defer(function () use ($exception) {

View File

@ -2,24 +2,24 @@
namespace Amp;
use Interop\Async\{ Loop, Promise };
use AsyncInterop\{ Loop, Promise };
/**
* Wraps the callback in a promise/coroutine-aware function that automatically upgrades Generators to coroutines and
* calls rethrow() on the returned promises (or the coroutine created).
*
* @param callable(...$args): \Generator|\Interop\Async\Promise|mixed $callback
* @param callable(...$args): \Generator|\AsyncInterop\Promise|mixed $callback
*
* @return callable(...$args): void
*/
function wrap(callable $callback): callable {
return function (...$args) use ($callback) {
$result = $callback(...$args);
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise) {
rethrow($result);
}
@ -33,7 +33,7 @@ function wrap(callable $callback): callable {
*
* @param callable(mixed ...$args): mixed $worker
*
* @return callable(mixed ...$args): \Interop\Async\Promise
* @return callable(mixed ...$args): \AsyncInterop\Promise
*/
function coroutine(callable $worker): callable {
return function (...$args) use ($worker): Promise {
@ -58,7 +58,7 @@ function coroutine(callable $worker): callable {
/**
* Registers a callback that will forward the failure reason to the Loop error handler if the promise fails.
*
* @param \Interop\Async\Promise $promise
* @param \AsyncInterop\Promise $promise
*/
function rethrow(Promise $promise) {
$promise->when(function ($exception) {
@ -71,7 +71,7 @@ function rethrow(Promise $promise) {
/**
* Runs the event loop until the promise is resolved. Should not be called within a running event loop.
*
* @param \Interop\Async\Promise $promise
* @param \AsyncInterop\Promise $promise
*
* @return mixed Promise success value.
*
@ -102,10 +102,10 @@ function wait(Promise $promise) {
/**
* Pipe the promised value through the specified functor once it resolves.
*
* @param \Interop\Async\Promise $promise
* @param \AsyncInterop\Promise $promise
* @param callable(mixed $value): mixed $functor
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*/
function pipe(Promise $promise, callable $functor): Promise {
$deferred = new Deferred;
@ -127,12 +127,12 @@ function pipe(Promise $promise, callable $functor): Promise {
}
/**
* @param \Interop\Async\Promise $promise
* @param \AsyncInterop\Promise $promise
* @param string $className Exception class name to capture. Given callback will only be invoked if the failure reason
* is an instance of the given exception class name.
* @param callable(\Throwable $exception): mixed $functor
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*/
function capture(Promise $promise, string $className, callable $functor): Promise {
$deferred = new Deferred;
@ -164,10 +164,10 @@ function capture(Promise $promise, string $className, callable $functor): Promis
* If the timeout expires before the promise is resolved, the returned promise fails with an instance of
* \Amp\TimeoutException.
*
* @param \Interop\Async\Promise $promise
* @param \AsyncInterop\Promise $promise
* @param int $timeout Timeout in milliseconds.
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*/
function timeout(Promise $promise, int $timeout): Promise {
$deferred = new Deferred;
@ -201,7 +201,7 @@ function timeout(Promise $promise, int $timeout): Promise {
*
* @param object $thenable Object with a then() method.
*
* @return \Interop\Async\Promise Promise resolved by the $thenable object.
* @return \AsyncInterop\Promise Promise resolved by the $thenable object.
*
* @throws \Error If the provided object does not have a then() method.
*/
@ -229,7 +229,7 @@ function lift(callable $worker): callable {
/**
* @param mixed ...$args Promises or values.
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*/
return function (...$args) use ($worker): Promise {
foreach ($args as $key => $arg) {
@ -258,7 +258,7 @@ function lift(callable $worker): callable {
*
* @param Promise[] $promises
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*
* @throws \Error If a non-Promise is in the array.
*/
@ -300,7 +300,7 @@ function any(array $promises): Promise {
*
* @param Promise[] $promises
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*
* @throws \Error If a non-Promise is in the array.
*/
@ -346,7 +346,7 @@ function all(array $promises): Promise {
*
* @param Promise[] $promises
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*
* @throws \Error If the array is empty or a non-Promise is in the array.
*/
@ -394,7 +394,7 @@ function first(array $promises): Promise {
* @param Promise[] $promises
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*/
function some(array $promises): Promise {
if (empty($promises)) {
@ -438,7 +438,7 @@ function some(array $promises): Promise {
*
* @param Promise[] $promises
*
* @return \Interop\Async\Promise
* @return \AsyncInterop\Promise
*
* @throws \Error If the array is empty or a non-Promise is in the array.
*/
@ -484,7 +484,7 @@ function choose(array $promises): Promise {
* @param callable(mixed $value): mixed $callback
* @param Promise[] ...$promises
*
* @return \Interop\Async\Promise[] Array of promises resolved with the result of the mapped function.
* @return \AsyncInterop\Promise[] Array of promises resolved with the result of the mapped function.
*/
function map(callable $callback, array ...$promises): array {
$callback = lift($callback);
@ -514,7 +514,7 @@ function stream(/* iterable */ $iterable): Stream {
if (!$iterable instanceof \Traversable && !\is_array($iterable)) {
throw new \TypeError("Must provide an array or instance of Traversable");
}
return new Producer(function (callable $emit) use ($iterable) {
foreach ($iterable as $value) {
yield $emit($value);
@ -638,7 +638,7 @@ function merge(array $streams): Stream {
all($streams)->when(function ($exception, array $values = null) use (&$pending, $emitter) {
$pending = false;
if ($exception) {
$emitter->fail($exception);
return;
@ -674,11 +674,11 @@ function concat(array $streams): Stream {
foreach ($streams as $stream) {
$generator = function ($value) use ($emitter, $promise) {
static $pending = true, $failed = false;
if ($failed) {
return;
}
if ($pending) {
try {
yield $promise;
@ -688,7 +688,7 @@ function concat(array $streams): Stream {
return; // Prior stream failed.
}
}
yield $emitter->emit($value);
};
$subscriptions[] = $stream->listen(function ($value) use ($generator) {

View File

@ -4,10 +4,10 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, Success };
use Interop\Async\Promise;
use AsyncInterop\Promise;
class PromiseMock {
/** @var \Interop\Async\Promise */
/** @var \AsyncInterop\Promise */
private $promise;
public function __construct(Promise $promise) {
@ -35,7 +35,7 @@ class AdaptTest extends \PHPUnit_Framework_TestCase {
$mock = $this->getMockBuilder(PromiseMock::class)
->disableOriginalConstructor()
->getMock();
$mock->expects($this->once())
->method("then")
->with(
@ -46,12 +46,12 @@ class AdaptTest extends \PHPUnit_Framework_TestCase {
return is_callable($reject);
})
);
$promise = Amp\adapt($mock);
$this->assertInstanceOf(Promise::class, $promise);
}
/**
* @depends testThenCalled
*/
@ -68,7 +68,7 @@ class AdaptTest extends \PHPUnit_Framework_TestCase {
$this->assertSame($value, $result);
}
/**
* @depends testThenCalled
*/

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Pause, Success };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class AllTest extends \PHPUnit_Framework_TestCase {
public function testEmptyArray() {

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, Pause, Success };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class AnyTest extends \PHPUnit_Framework_TestCase {
public function testEmptyArray() {
@ -28,30 +28,30 @@ class AnyTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals([[], [1, 2, 3]], $result);
}
public function testFailedPromisesArray() {
$exception = new \Exception;
$promises = [new Failure($exception), new Failure($exception), new Failure($exception)];
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
Amp\any($promises)->when($callback);
$this->assertEquals([[$exception, $exception, $exception], []], $result);
}
public function testMixedPromisesArray() {
$exception = new \Exception;
$promises = [new Success(1), new Failure($exception), new Success(3)];
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
Amp\any($promises)->when($callback);
$this->assertEquals([[1 => $exception], [0 => 1, 2 => 3]], $result);
}
@ -72,7 +72,7 @@ class AnyTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals([[], [1, 2, 3]], $result);
}
/**
* @depends testMixedPromisesArray
*/

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, Success };
use Interop\Async\Promise;
use AsyncInterop\Promise;
class CaptureTest extends \PHPUnit_Framework_TestCase {
public function testSuccessfulPromise() {
@ -56,7 +56,7 @@ class CaptureTest extends \PHPUnit_Framework_TestCase {
$this->assertSame($exception, $reason);
$this->assertSame(-1, $result);
}
/**
* @depends testFailedPromise
*/

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, Pause, Success };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class ChooseTest extends \PHPUnit_Framework_TestCase {
/**
@ -26,43 +26,43 @@ class ChooseTest extends \PHPUnit_Framework_TestCase {
$this->assertSame(1, $result);
}
public function testFailedPromisesArray() {
$exception = new \Exception;
$promises = [new Failure($exception), new Failure($exception), new Failure($exception)];
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
Amp\choose($promises)->when($callback);
$this->assertSame($exception, $reason);
}
public function testFirstPromiseSuccessfulArray() {
$exception = new \Exception;
$promises = [new Success(1), new Failure($exception), new Success(3)];
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
Amp\choose($promises)->when($callback);
$this->assertSame(1, $result);
}
public function testFirstPromiseFailedArray() {
$exception = new \Exception;
$promises = [new Failure($exception), new Success(2), new Success(3)];
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
Amp\choose($promises)->when($callback);
$this->assertSame($exception, $reason);
}
@ -80,10 +80,10 @@ class ChooseTest extends \PHPUnit_Framework_TestCase {
Amp\choose($promises)->when($callback);
});
$this->assertSame(3, $result);
}
/**
* @expectedException \Error
* @expectedExceptionMessage Non-promise provided

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\Producer;
use Interop\Async\Loop;
use AsyncInterop\Loop;
class ConcatTest extends \PHPUnit_Framework_TestCase {
public function getStreams() {
@ -14,7 +14,7 @@ class ConcatTest extends \PHPUnit_Framework_TestCase {
[[Amp\stream(\range(1, 4)), Amp\stream(\range(5, 10))], \range(1, 10)],
];
}
/**
* @dataProvider getStreams
*
@ -24,14 +24,14 @@ class ConcatTest extends \PHPUnit_Framework_TestCase {
public function testConcat(array $streams, array $expected) {
Loop::execute(function () use ($streams, $expected) {
$stream = Amp\concat($streams);
Amp\each($stream, function ($value) use ($expected) {
static $i = 0;
$this->assertSame($expected[$i++], $value);
});
});
}
/**
* @depends testConcat
*/
@ -43,24 +43,24 @@ class ConcatTest extends \PHPUnit_Framework_TestCase {
yield $emit(6); // Emit once before failing.
throw $exception;
});
$stream = Amp\concat([Amp\stream(\range(1, 5)), $producer, Amp\stream(\range(7, 10))]);
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$stream->when($callback);
});
$this->assertSame(\range(1, 6), $results);
$this->assertSame($exception, $reason);
}
/**
* @expectedException \Error
* @expectedExceptionMessage Non-stream provided

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Coroutine, Failure, InvalidYieldError, Pause, Success };
use Interop\Async\{ Loop, Promise };
use AsyncInterop\{ Loop, Promise };
class CoroutineTest extends \PHPUnit_Framework_TestCase {
const TIMEOUT = 100;
@ -81,7 +81,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
}
}
public function testInvalidYield() {
$generator = function () {
yield 1;
@ -113,7 +113,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$this->assertInstanceOf(InvalidYieldError::class, $reason);
}
/**
* @depends testInvalidYield
*/
@ -125,16 +125,16 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
// No further yields.
}
};
$coroutine = new Coroutine($generator());
$coroutine->when(function ($exception) use (&$reason) {
$reason = $exception;
});
$this->assertInstanceOf(InvalidYieldError::class, $reason);
}
/**
* @depends testInvalidYieldCatchingThrownException
*/
@ -146,16 +146,16 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
yield new Success;
}
};
$coroutine = new Coroutine($generator());
$coroutine->when(function ($exception) use (&$reason) {
$reason = $exception;
});
$this->assertInstanceOf(InvalidYieldError::class, $reason);
}
/**
* @depends testInvalidYieldCatchingThrownException
*/
@ -168,13 +168,13 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
throw $exception;
}
};
$coroutine = new Coroutine($generator());
$coroutine->when(function ($exception) use (&$reason) {
$reason = $exception;
});
$this->assertInstanceOf(InvalidYieldError::class, $reason);
$this->assertSame($exception, $reason->getPrevious());
}
@ -199,7 +199,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$this->assertInstanceOf(InvalidYieldError::class, $reason);
}
/**
* @depends testInvalidYield
*/
@ -481,7 +481,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$this->assertInstanceOf(Coroutine::class, $callable());
}
/**
* @depends testCoroutineFunction
*/
@ -491,18 +491,18 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$callable = Amp\coroutine(function ($value) {
return $value;
});
$promise = $callable($promise);
$this->assertInstanceOf(Promise::class, $promise);
$promise->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertSame($value, $result);
}
/**
* @depends testCoroutineFunction
*/
@ -511,18 +511,18 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$callable = Amp\coroutine(function ($value) {
return $value;
});
$promise = $callable($value);
$this->assertInstanceOf(Promise::class, $promise);
$promise->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertSame($value, $result);
}
/**
* @depends testCoroutineFunction
*/
@ -531,82 +531,82 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$callable = Amp\coroutine(function () use ($exception) {
throw $exception;
});
$promise = $callable();
$this->assertInstanceOf(Promise::class, $promise);
$promise->when(function ($exception, $value) use (&$reason) {
$reason = $exception;
});
$this->assertSame($exception, $reason);
}
public function testCoroutineResolvedWithReturn() {
$value = 1;
$generator = function () use ($value) {
return $value;
yield; // Unreachable, but makes function a coroutine.
};
$coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertSame($value, $result);
}
/**
* @depends testCoroutineResolvedWithReturn
*/
public function testYieldFromGenerator() {
$value = 1;
$generator = function () use ($value) {
$generator = function () use ($value) {
return yield new Success($value);
};
return yield from $generator();
};
$coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertSame($value, $result);
}
/**
* @depends testCoroutineResolvedWithReturn
*/
public function testFastReturningGenerator()
{
$value = 1;
$generator = function () use ($value) {
if (true) {
return $value;
}
yield;
return -$value;
};
$coroutine = new Coroutine($generator());
$coroutine->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertSame($value, $result);
}
}

View File

@ -3,7 +3,7 @@
namespace Amp\Test;
use Amp\Deferred;
use Interop\Async\Promise;
use AsyncInterop\Promise;
class DeferredTest extends \PHPUnit_Framework_TestCase {
/** @var \Amp\Deferred */
@ -17,7 +17,7 @@ class DeferredTest extends \PHPUnit_Framework_TestCase {
$promise = $this->deferred->promise();
$this->assertInstanceOf(Promise::class, $promise);
}
/**
* @depends testGetPromise
*/
@ -30,7 +30,7 @@ class DeferredTest extends \PHPUnit_Framework_TestCase {
$invoked = true;
$result = $value;
});
$this->deferred->resolve($value);
$this->assertTrue($invoked);

View File

@ -4,26 +4,26 @@ namespace Amp\Test;
use Amp;
use Amp\{ Producer, Stream, Emitter };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class EachTest extends \PHPUnit_Framework_TestCase {
public function testNoValuesEmitted() {
$invoked = false;
Loop::execute(function () use (&$invoked){
$emitter = new Emitter;
$stream = Amp\each($emitter->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
$this->assertInstanceOf(Stream::class, $stream);
$emitter->resolve();
});
$this->assertFalse($invoked);
}
public function testValuesEmitted() {
$count = 0;
$values = [1, 2, 3];
@ -36,28 +36,28 @@ class EachTest extends \PHPUnit_Framework_TestCase {
}
return $final;
});
$stream = Amp\each($producer, function ($value) use (&$count) {
++$count;
return $value + 1;
}, function ($value) use (&$invoked) {
return $value + 1;
});
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$stream->when(function ($exception, $value) use (&$result) {
$result = $value;
});
});
$this->assertSame(\count($values), $count);
$this->assertSame(\array_map(function ($value) { return $value + 1; }, $values), $results);
$this->assertSame($final + 1, $result);
}
/**
* @depends testValuesEmitted
*/
@ -70,25 +70,25 @@ class EachTest extends \PHPUnit_Framework_TestCase {
yield $emit($value);
}
});
$stream = Amp\each($producer, function () use ($exception) {
throw $exception;
});
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$stream->when($callback);
});
$this->assertSame($exception, $reason);
}
/**
* @depends testValuesEmitted
*/
@ -103,49 +103,49 @@ class EachTest extends \PHPUnit_Framework_TestCase {
yield $emit($value);
}
});
$stream = Amp\each($producer, function ($value) use (&$count) {
++$count;
return $value + 1;
}, function ($value) use ($exception) {
throw $exception;
});
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$stream->when($callback);
});
$this->assertSame(\count($values), $count);
$this->assertSame(\array_map(function ($value) { return $value + 1; }, $values), $results);
$this->assertSame($exception, $reason);
}
public function testStreamFails() {
$invoked = false;
$exception = new \Exception;
Loop::execute(function () use (&$invoked, &$reason, &$exception){
$emitter = new Emitter;
$stream = Amp\each($emitter->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
$emitter->fail($exception);
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$stream->when($callback);
});
$this->assertFalse($invoked);
$this->assertSame($exception, $reason);
}

View File

@ -3,7 +3,7 @@
namespace Amp\Test;
use Amp\Failure;
use Interop\Async\Loop;
use AsyncInterop\Loop;
class FailureTest extends \PHPUnit_Framework_TestCase {
/**

View File

@ -4,26 +4,26 @@ namespace Amp\Test;
use Amp;
use Amp\{ Producer, Stream, Emitter };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class FilterTest extends \PHPUnit_Framework_TestCase {
public function testNoValuesEmitted() {
$invoked = false;
Loop::execute(function () use (&$invoked){
$emitter = new Emitter;
$stream = Amp\filter($emitter->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
$this->assertInstanceOf(Stream::class, $stream);
$emitter->resolve();
});
$this->assertFalse($invoked);
}
public function testValuesEmitted() {
$count = 0;
$values = [1, 2, 3];
@ -35,25 +35,25 @@ class FilterTest extends \PHPUnit_Framework_TestCase {
yield $emit($value);
}
});
$stream = Amp\filter($producer, function ($value) use (&$count) {
++$count;
return $value & 1;
});
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$stream->when(function ($exception, $value) use (&$result) {
$result = $value;
});
});
$this->assertSame(\count($values), $count);
$this->assertSame($expected, $results);
}
/**
* @depends testValuesEmitted
*/
@ -66,44 +66,44 @@ class FilterTest extends \PHPUnit_Framework_TestCase {
yield $emit($value);
}
});
$stream = Amp\filter($producer, function () use ($exception) {
throw $exception;
});
$stream->listen(function ($value) use (&$results) {
$results[] = $value;
});
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$stream->when($callback);
});
$this->assertSame($exception, $reason);
}
public function testStreamFails() {
$invoked = false;
$exception = new \Exception;
Loop::execute(function () use (&$invoked, &$reason, &$exception){
$emitter = new Emitter;
$stream = Amp\filter($emitter->stream(), function ($value) use (&$invoked) {
$invoked = true;
});
$emitter->fail($exception);
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$stream->when($callback);
});
$this->assertFalse($invoked);
$this->assertSame($exception, $reason);
}

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, MultiReasonException, Pause, Success };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class FirstTest extends \PHPUnit_Framework_TestCase {
/**
@ -26,31 +26,31 @@ class FirstTest extends \PHPUnit_Framework_TestCase {
$this->assertSame(1, $result);
}
public function testFailedPromisesArray() {
$exception = new \Exception;
$promises = [new Failure($exception), new Failure($exception), new Failure($exception)];
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
Amp\first($promises)->when($callback);
$this->assertInstanceOf(MultiReasonException::class, $reason);
$this->assertEquals([$exception, $exception, $exception], $reason->getReasons());
}
public function testMixedPromisesArray() {
$exception = new \Exception;
$promises = [new Failure($exception), new Failure($exception), new Success(3)];
$callback = function ($exception, $value) use (&$result) {
$result = $value;
};
Amp\first($promises)->when($callback);
$this->assertSame(3, $result);
}
@ -68,10 +68,10 @@ class FirstTest extends \PHPUnit_Framework_TestCase {
Amp\first($promises)->when($callback);
});
$this->assertSame(3, $result);
}
/**
* @expectedException \Error
* @expectedExceptionMessage Non-promise provided

View File

@ -4,25 +4,25 @@ namespace Amp\Test;
use Amp;
use Amp\Pause;
use Interop\Async\Loop;
use AsyncInterop\Loop;
class IntervalTest extends \PHPUnit_Framework_TestCase {
const TIMEOUT = 10;
public function testInterval() {
$count = 3;
$stream = Amp\interval(self::TIMEOUT, $count);
$i = 0;
$stream = Amp\each($stream, function ($value) use (&$i) {
$this->assertSame(++$i, $value);
});
Amp\wait($stream);
$this->assertSame($count, $i);
}
/**
* @depends testInterval
*/
@ -31,16 +31,16 @@ class IntervalTest extends \PHPUnit_Framework_TestCase {
$count = 5;
Loop::execute(function () use (&$invoked, $count) {
$stream = Amp\interval(self::TIMEOUT, $count);
$stream->listen(function () use (&$invoked) {
++$invoked;
return new Pause(self::TIMEOUT * 2);
});
});
$this->assertSame($count, $invoked);
}
/**
* @expectedException \Error
* @expectedExceptionMessage The number of times to emit must be a positive value

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, Lazy, Success };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class LazyTest extends \PHPUnit_Framework_TestCase {
public function testPromisorNotCalledOnConstruct() {
@ -14,7 +14,7 @@ class LazyTest extends \PHPUnit_Framework_TestCase {
});
$this->assertFalse($invoked);
}
public function testPromisorReturningScalar() {
$invoked = false;
$value = 1;
@ -22,15 +22,15 @@ class LazyTest extends \PHPUnit_Framework_TestCase {
$invoked = true;
return $value;
});
$lazy->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertTrue($invoked);
$this->assertSame($value, $result);
}
public function testPromisorReturningSuccessfulPromise() {
$invoked = false;
$value = 1;
@ -39,15 +39,15 @@ class LazyTest extends \PHPUnit_Framework_TestCase {
$invoked = true;
return $promise;
});
$lazy->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertTrue($invoked);
$this->assertSame($value, $result);
}
public function testPromisorReturningFailedPromise() {
$invoked = false;
$exception = new \Exception;
@ -56,15 +56,15 @@ class LazyTest extends \PHPUnit_Framework_TestCase {
$invoked = true;
return $promise;
});
$lazy->when(function ($exception, $value) use (&$reason) {
$reason = $exception;
});
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}
public function testPromisorThrowingException() {
$invoked = false;
$exception = new \Exception;
@ -72,11 +72,11 @@ class LazyTest extends \PHPUnit_Framework_TestCase {
$invoked = true;
throw $exception;
});
$lazy->when(function ($exception, $value) use (&$reason) {
$reason = $exception;
});
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}

View File

@ -4,100 +4,100 @@ namespace Amp\Test;
use Amp;
use Amp\{ Deferred, Failure, Success };
use Interop\Async\{ Loop, Promise };
use AsyncInterop\{ Loop, Promise };
class MapTest extends \PHPUnit_Framework_TestCase {
public function testEmptyArray() {
$values = [];
$invoked = false;
$result = Amp\map(function () use (&$invoked) {
$invoked = true;
}, $values);
$this->assertSame($result, $values);
$this->assertFalse($invoked);
}
public function testSuccessfulPromisesArray() {
Loop::execute(Amp\wrap(function () {
$promises = [new Success(1), new Success(2), new Success(3)];;
$count = 0;
$callback = function ($value) use (&$count) {
++$count;
return $value - 1;
};
$result = Amp\map($callback, $promises);
$this->assertTrue(\is_array($result));
foreach ($result as $key => $promise) {
$this->assertInstanceOf(Promise::class, $promise);
$this->assertSame($key, Amp\wait($promise));
}
$this->assertSame(\count($promises), $count);
}));
}
public function testPendingPromisesArray() {
$deferreds = [
new Deferred,
new Deferred,
new Deferred,
];
$promises = \array_map(function (Deferred $deferred) {
return $deferred->promise();
}, $deferreds);
$count = 0;
$callback = function ($value) use (&$count) {
++$count;
return $value - 1;
};
$result = Amp\map($callback, $promises);
$this->assertTrue(\is_array($result));
foreach ($deferreds as $key => $deferred) {
$deferred->resolve($key + 1);
}
foreach ($result as $key => $promise) {
$this->assertInstanceOf(Promise::class, $promise);
$this->assertSame($key, Amp\wait($promise));
}
$this->assertSame(\count($promises), $count);
}
public function testFailedPromisesArray() {
Loop::execute(Amp\wrap(function () {
$exception = new \Exception;
$promises = [new Failure($exception), new Failure($exception), new Failure($exception)];;
$count = 0;
$callback = function ($value) use (&$count) {
++$count;
return $value - 1;
};
$result = Amp\map($callback, $promises);
$this->assertTrue(\is_array($result));
foreach ($result as $key => $promise) {
$this->assertInstanceOf(Promise::class, $promise);
}
$this->assertSame(0, $count);
}));
}
/**
* @depends testFailedPromisesArray
*/
@ -106,17 +106,17 @@ class MapTest extends \PHPUnit_Framework_TestCase {
Loop::execute(Amp\wrap(function () {
$promises = [new Success(1), new Success(2), new Success(3)];;
$exception = new \Exception;
$callback = function () use ($exception) {
throw $exception;
};
$result = Amp\map($callback, $promises);
foreach ($result as $key => $promise) {
$this->assertInstanceOf(Promise::class, $promise);
}
foreach ($result as $key => $promise) {
try {
Amp\wait($promise);
@ -126,14 +126,14 @@ class MapTest extends \PHPUnit_Framework_TestCase {
}
}));
}
/**
* @depends testPendingPromisesArray
*/
public function testMultipleArrays() {
$promises1 = [new Success(1), new Success(2), new Success(3)];;
$promises2 = [new Success(3), new Success(2), new Success(1)];;
$count = 0;
$callback = function ($value1, $value2) use (&$count) {
++$count;

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\Producer;
use Interop\Async\Loop;
use AsyncInterop\Loop;
class MergeTest extends \PHPUnit_Framework_TestCase {
public function getStreams() {
@ -14,7 +14,7 @@ class MergeTest extends \PHPUnit_Framework_TestCase {
[[Amp\stream(\range(1, 4)), Amp\stream(\range(5, 10))], [1, 5, 2, 6, 3, 7, 4, 8, 9, 10]],
];
}
/**
* @dataProvider getStreams
*
@ -24,14 +24,14 @@ class MergeTest extends \PHPUnit_Framework_TestCase {
public function testMerge(array $streams, array $expected) {
Loop::execute(function () use ($streams, $expected) {
$stream = Amp\merge($streams);
Amp\each($stream, function ($value) use ($expected) {
static $i = 0;
$this->assertSame($expected[$i++], $value);
});
});
}
/**
* @depends testMerge
*/
@ -42,19 +42,19 @@ class MergeTest extends \PHPUnit_Framework_TestCase {
yield $emit(1); // Emit once before failing.
throw $exception;
});
$stream = Amp\merge([$producer, Amp\stream(\range(1, 5))]);
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$stream->when($callback);
});
$this->assertSame($exception, $reason);
}
/**
* @expectedException \Error
* @expectedExceptionMessage Non-stream provided

View File

@ -4,11 +4,11 @@ namespace Amp\Test;
use Amp;
use Amp\{ Producer, Listener, Pause, Emitter };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class ListenerTest extends \PHPUnit_Framework_TestCase {
const TIMEOUT = 10;
public function testSingleEmittingStream() {
Loop::execute(Amp\wrap(function () {
$value = 1;
@ -16,43 +16,43 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
yield $emit($value);
return $value;
});
$listener = new Listener($stream);
while (yield $listener->advance()) {
$this->assertSame($listener->getCurrent(), $value);
}
$this->assertSame($listener->getResult(), $value);
}));
}
/**
* @depends testSingleEmittingStream
*/
public function testFastEmittingStream() {
Loop::execute(Amp\wrap(function () {
$count = 10;
$emitter = new Emitter;
$listener = new Listener($emitter->stream());
for ($i = 0; $i < $count; ++$i) {
$promises[] = $emitter->emit($i);
}
$emitter->resolve($i);
for ($i = 0; yield $listener->advance(); ++$i) {
$this->assertSame($listener->getCurrent(), $i);
}
$this->assertSame($count, $i);
$this->assertSame($listener->getResult(), $i);
}));
}
/**
* @depends testSingleEmittingStream
*/
@ -66,70 +66,70 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
}
return $i;
});
$listener = new Listener($stream);
for ($i = 0; yield $listener->advance(); ++$i) {
$this->assertSame($listener->getCurrent(), $i);
}
$this->assertSame($count, $i);
$this->assertSame($listener->getResult(), $i);
}));
}
/**
* @depends testFastEmittingStream
*/
public function testDrain() {
Loop::execute(Amp\wrap(function () {
$count = 10;
$emitter = new Emitter;
$listener = new Listener($emitter->stream());
for ($i = 0; $i < $count; ++$i) {
$promises[] = $emitter->emit($i);
}
$emitter->resolve($i);
$values = $listener->drain();
$this->assertSame(\range(0, $count - 1), $values);
}));
}
/**
* @expectedException \Error
* @expectedExceptionMessage The stream has not resolved
*/
public function testDrainBeforeResolution() {
$emitter = new Emitter;
$listener = new Listener($emitter->stream());
$listener->drain();
}
public function testFailingStream() {
Loop::execute(Amp\wrap(function () {
$exception = new \Exception;
$emitter = new Emitter;
$listener = new Listener($emitter->stream());
$emitter->fail($exception);
try {
while (yield $listener->advance());
$this->fail("Listener::advance() should throw stream failure reason");
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
}
try {
$result = $listener->getResult();
$this->fail("Listener::getResult() should throw stream failure reason");
@ -138,35 +138,35 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
}
}));
}
/**
* @expectedException \Error
* @expectedExceptionMessage Promise returned from advance() must resolve before calling this method
*/
public function testGetCurrentBeforeAdvanceResolves() {
$emitter = new Emitter;
$listener = new Listener($emitter->stream());
$promise = $listener->advance();
$listener->getCurrent();
}
/**
* @expectedException \Error
* @expectedExceptionMessage The stream has resolved
*/
public function testGetCurrentAfterResolution() {
$emitter = new Emitter;
$listener = new Listener($emitter->stream());
$emitter->resolve();
$listener->getCurrent();
}
/**
* @expectedException \Error
* @expectedExceptionMessage The stream has not resolved
@ -174,9 +174,9 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
public function testGetResultBeforeResolution() {
Loop::execute(Amp\wrap(function () {
$emitter = new Emitter;
$listener = new Listener($emitter->stream());
$listener->getResult();
}));
}

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\Pause;
use Interop\Async\Loop;
use AsyncInterop\Loop;
class PauseTest extends \PHPUnit_Framework_TestCase {
public function testPause() {

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, Success };
use Interop\Async\Promise;
use AsyncInterop\Promise;
class PipeTest extends \PHPUnit_Framework_TestCase {
public function testSuccessfulPromise() {
@ -54,7 +54,7 @@ class PipeTest extends \PHPUnit_Framework_TestCase {
$this->assertFalse($invoked);
$this->assertSame($exception, $reason);
}
/**
* @depends testSuccessfulPromise
*/
@ -64,19 +64,19 @@ class PipeTest extends \PHPUnit_Framework_TestCase {
$invoked = true;
throw $exception;
};
$value = 1;
$promise = new Success($value);
$promise = Amp\pipe($promise, $callback);
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
$promise->when($callback);
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}

View File

@ -2,7 +2,7 @@
namespace Amp\Test;
use Interop\Async\{ Loop, Promise };
use AsyncInterop\{ Loop, Promise };
class Placeholder {
use \Amp\Internal\Placeholder {
@ -323,7 +323,7 @@ class PlaceholderTraitTest extends \PHPUnit_Framework_TestCase {
$this->placeholder->when(function () {
$this->placeholder->resolve();
});
$this->placeholder->resolve();
});
}

View File

@ -4,11 +4,11 @@ namespace Amp\Test;
use Amp;
use Amp\{ Deferred, Producer, Pause };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class ProducerTest extends \PHPUnit_Framework_TestCase {
const TIMEOUT = 100;
/**
* @expectedException \Error
* @expectedExceptionMessage The callable did not return a Generator
@ -16,33 +16,33 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
public function testNonGeneratorCallable() {
$producer = new Producer(function () {});
}
public function testEmit() {
$invoked = false;
Loop::execute(Amp\wrap(function () use (&$invoked) {
$value = 1;
$producer = new Producer(function (callable $emit) use ($value) {
yield $emit($value);
return $value;
});
$invoked = false;
$callback = function ($emitted) use (&$invoked, $value) {
$invoked = true;
$this->assertSame($emitted, $value);
};
$producer->listen($callback);
$producer->when(function ($exception, $result) use ($value) {
$this->assertSame($result, $value);
});
}));
$this->assertTrue($invoked);
}
/**
* @depends testEmit
*/
@ -50,26 +50,26 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
$invoked = false;
Loop::execute(Amp\wrap(function () use (&$invoked) {
$deferred = new Deferred();
$producer = new Producer(function (callable $emit) use ($deferred) {
return yield $emit($deferred->promise());
});
$value = 1;
$invoked = false;
$callback = function ($emitted) use (&$invoked, $value) {
$invoked = true;
$this->assertSame($emitted, $value);
};
$producer->listen($callback);
$deferred->resolve($value);
}));
$this->assertTrue($invoked);
}
/**
* @depends testEmitSuccessfulPromise
*/
@ -77,19 +77,19 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
$exception = new \Exception;
Loop::execute(Amp\wrap(function () use ($exception) {
$deferred = new Deferred();
$producer = new Producer(function (callable $emit) use ($deferred) {
return yield $emit($deferred->promise());
});
$deferred->fail($exception);
$producer->when(function ($reason) use ($exception) {
$this->assertSame($reason, $exception);
});
}));
}
/**
* @depends testEmit
*/
@ -103,28 +103,28 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
}
$time = microtime(true) - $time;
});
$producer->listen(function () {
return new Pause(self::TIMEOUT);
});
}));
$this->assertGreaterThan(self::TIMEOUT * $emits, $time * 1000);
}
/**
* @depends testEmit
*/
public function testSubscriberThrows() {
$exception = new \Exception;
try {
Loop::execute(Amp\wrap(function () use ($exception) {
$producer = new Producer(function (callable $emit) {
yield $emit(1);
yield $emit(2);
});
$producer->listen(function () use ($exception) {
throw $exception;
});
@ -133,20 +133,20 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
$this->assertSame($exception, $caught);
}
}
/**
* @depends testEmit
*/
public function testProducerCoroutineThrows() {
$exception = new \Exception;
try {
Loop::execute(Amp\wrap(function () use ($exception) {
$producer = new Producer(function (callable $emit) use ($exception) {
yield $emit(1);
throw $exception;
});
Amp\wait($producer);
}));
} catch (\Exception $caught) {

View File

@ -3,7 +3,7 @@
namespace Amp\Test;
use Amp\{ Deferred, Failure, Success };
use Interop\Async\{ Loop, Promise };
use AsyncInterop\{ Loop, Promise };
class Producer {
use \Amp\Internal\Producer {
@ -24,19 +24,19 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
public function testEmit() {
$invoked = false;
$value = 1;
$callback = function ($emitted) use (&$invoked, $value) {
$invoked = true;
$this->assertSame($emitted, $value);
};
$this->producer->listen($callback);
$promise = $this->producer->emit($value);
$this->assertInstanceOf(Promise::class, $promise);
$this->assertTrue($invoked);
}
/**
* @depends testEmit
*/
@ -44,18 +44,18 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
$invoked = false;
$value = 1;
$promise = new Success($value);
$callback = function ($emitted) use (&$invoked, $value) {
$invoked = true;
$this->assertSame($emitted, $value);
};
$this->producer->listen($callback);
$this->producer->emit($promise);
$this->assertTrue($invoked);
}
/**
* @depends testEmit
*/
@ -63,25 +63,25 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
$invoked = false;
$exception = new \Exception;
$promise = new Failure($exception);
$callback = function ($emitted) use (&$invoked) {
$invoked = true;
};
$this->producer->listen($callback);
$this->producer->emit($promise);
$this->assertFalse($invoked);
$this->producer->when(function ($exception) use (&$invoked, &$reason) {
$invoked = true;
$reason = $exception;
});
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}
/**
* @depends testEmit
*/
@ -89,51 +89,51 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
$invoked = false;
$value = 1;
$deferred = new Deferred;
$callback = function ($emitted) use (&$invoked) {
$invoked = true;
};
$callback = function ($emitted) use (&$invoked, $value) {
$invoked = true;
$this->assertSame($emitted, $value);
};
$this->producer->listen($callback);
$this->producer->emit($deferred->promise());
$this->assertFalse($invoked);
$deferred->resolve($value);
$this->assertTrue($invoked);
}
/**
* @depends testEmit
*/
public function testEmitPendingPromiseThenNonPromise() {
$invoked = false;
$deferred = new Deferred;
$callback = function ($emitted) use (&$invoked, &$result) {
$invoked = true;
$result = $emitted;
};
$this->producer->listen($callback);
$this->producer->emit($deferred->promise());
$this->assertFalse($invoked);
$this->producer->emit(2);
$this->assertTrue($invoked);
$this->assertSame(2, $result);
$deferred->resolve(1);
$this->assertSame(1, $result);
}
/**
* @depends testEmit
* @expectedException \Error
@ -143,7 +143,7 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
$this->producer->resolve();
$this->producer->emit(1);
}
/**
* @depends testEmit
* @expectedException \Error
@ -152,21 +152,21 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
public function testEmitPendingPromiseThenResolve() {
$invoked = false;
$deferred = new Deferred;
$promise = $this->producer->emit($deferred->promise());
$this->producer->resolve();
$deferred->resolve();
$promise->when(function ($exception) use (&$invoked, &$reason) {
$invoked = true;
$reason = $exception;
});
$this->assertTrue($invoked);
throw $reason;
}
/**
* @depends testEmit
* @expectedException \Error
@ -175,69 +175,69 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
public function testEmitPendingPromiseThenFail() {
$invoked = false;
$deferred = new Deferred;
$promise = $this->producer->emit($deferred->promise());
$this->producer->resolve();
$deferred->fail(new \Exception);
$promise->when(function ($exception) use (&$invoked, &$reason) {
$invoked = true;
$reason = $exception;
});
$this->assertTrue($invoked);
throw $reason;
}
public function testSubscriberThrows() {
$exception = new \Exception;
try {
Loop::execute(function () use ($exception) {
$this->producer->listen(function () use ($exception) {
throw $exception;
});
$this->producer->emit(1);
});
} catch (\Exception $caught) {
$this->assertSame($exception, $caught);
}
}
public function testSubscriberReturnsSuccessfulPromise() {
$invoked = true;
$value = 1;
$promise = new Success($value);
$this->producer->listen(function () use ($promise) {
return $promise;
});
$promise = $this->producer->emit(1);
$promise->when(function () use (&$invoked) {
$invoked = true;
});
$this->assertTrue($invoked);
}
public function testSubscriberReturnsFailedPromise() {
$exception = new \Exception;
$promise = new Failure($exception);
try {
Loop::execute(function () use ($exception, $promise) {
$this->producer->listen(function () use ($promise) {
return $promise;
});
$promise = $this->producer->emit(1);
$promise->when(function () use (&$invoked) {
$invoked = true;
});
$this->assertTrue($invoked);
});
} catch (\Exception $caught) {

View File

@ -9,7 +9,7 @@ class Promise {
}
}
class PromiseTest extends \Interop\Async\Promise\Test {
class PromiseTest extends \AsyncInterop\Promise\Test {
public function promise() {
$promise = new Promise;
return [

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\Failure;
use Interop\Async\Loop;
use AsyncInterop\Loop;
class RethrowTest extends \PHPUnit_Framework_TestCase {
public function testWaitOnPendingPromise() {

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, MultiReasonException, Pause, Success };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class SomeTest extends \PHPUnit_Framework_TestCase {
/**
@ -26,17 +26,17 @@ class SomeTest extends \PHPUnit_Framework_TestCase {
$this->assertSame([[], [1, 2, 3]], $result);
}
public function testFailedPromisesArray() {
$exception = new \Exception;
$promises = [new Failure($exception), new Failure($exception), new Failure($exception)];
$callback = function ($exception, $value) use (&$reason) {
$reason = $exception;
};
Amp\some($promises)->when($callback);
$this->assertInstanceOf(MultiReasonException::class, $reason);
$this->assertEquals([$exception, $exception, $exception], $reason->getReasons());
}

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, Pause, Success };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class StreamTest extends \PHPUnit_Framework_TestCase {
public function testSuccessfulPromises() {

View File

@ -3,7 +3,7 @@
namespace Amp\Test;
use Amp\Success;
use Interop\Async\{ Loop, Promise };
use AsyncInterop\{ Loop, Promise };
class SuccessTest extends \PHPUnit_Framework_TestCase {
/**

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, Pause, Success };
use Interop\Async\{ Loop, Promise };
use AsyncInterop\{ Loop, Promise };
class TimeoutTest extends \PHPUnit_Framework_TestCase {
public function testSuccessfulPromise() {
@ -44,7 +44,7 @@ class TimeoutTest extends \PHPUnit_Framework_TestCase {
$this->assertSame($exception, $reason);
});
}
/**
* @depends testSuccessfulPromise
*/

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Deferred, Failure, Pause, Success };
use Interop\Async\Loop;
use AsyncInterop\Loop;
class WaitTest extends \PHPUnit_Framework_TestCase {
public function testWaitOnSuccessfulPromise()