1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00

Add Loop::set, auto-wrapping, fixup things

This commit is contained in:
Niklas Keller 2017-03-10 21:31:57 +01:00
commit e64bbfb9f5
51 changed files with 1574 additions and 361 deletions

View File

@ -46,7 +46,6 @@
"Amp\\": "lib/"
},
"files": [
"lib/bootstrap.php",
"lib/functions.php"
]
},

View File

@ -1,12 +1,14 @@
#!/usr/bin/env php
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';
use Amp\{ Coroutine, Emitter, Pause, Loop\NativeLoop };
use AsyncInterop\Loop;
use Amp\Coroutine;
use Amp\Emitter;
use Amp\Pause;
use Amp\Loop;
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
try {
$emitter = new Emitter;
@ -17,7 +19,7 @@ Loop::execute(Amp\wrap(function () {
return new Pause(500); // Artificial back-pressure on stream.
});
$stream->when(function ($exception, $value) {
$stream->when(function (Throwable $exception = null, $value) {
if ($exception) {
printf("Stream failed: %s\n", $exception->getMessage());
return;
@ -45,4 +47,4 @@ Loop::execute(Amp\wrap(function () {
} catch (\Exception $exception) {
printf("Exception: %s\n", $exception);
}
}), $loop = new NativeLoop());
});

View File

@ -3,10 +3,14 @@
require dirname(__DIR__) . '/vendor/autoload.php';
use Amp\{ Coroutine, Emitter, Listener, Pause, Stream, Loop\NativeLoop };
use AsyncInterop\Loop;
use Amp\Coroutine;
use Amp\Emitter;
use Amp\Listener;
use Amp\Pause;
use Amp\Stream;
use Amp\Loop;
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
try {
$emitter = new Emitter;
@ -40,7 +44,7 @@ Loop::execute(Amp\wrap(function () {
yield new Coroutine($generator($stream));
} catch (\Exception $exception) {
} catch (\Throwable $exception) {
printf("Exception: %s\n", $exception);
}
}), $loop = new NativeLoop());
});

View File

@ -3,10 +3,14 @@
require dirname(__DIR__) . '/vendor/autoload.php';
use Amp\{ Coroutine, Listener, Pause, Producer, Stream };
use AsyncInterop\Loop;
use Amp\Coroutine;
use Amp\Listener;
use Amp\Pause;
use Amp\Producer;
use Amp\Stream;
use Amp\Loop;
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
try {
$producer = new Producer(function (callable $emit) {
yield $emit(1);
@ -38,4 +42,4 @@ Loop::execute(Amp\wrap(function () {
} catch (\Exception $exception) {
printf("Exception: %s\n", $exception);
}
}));
});

View File

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

View File

@ -1,6 +1,6 @@
<?php
namespace Amp\Loop\Internal;
namespace Amp\Internal;
class Watcher {
const DEFER = 0b00000001;

View File

@ -3,13 +3,14 @@
namespace Amp;
use Amp\Loop\Driver;
use Amp\Loop\Factory;
use Amp\Loop\InvalidWatcherException;
use Amp\Loop\UnsupportedFeatureException;
/**
* Accessor to allow global access to the event loop.
*
* @see \AsyncInterop\Loop\Driver
* @see \Amp\Loop\Driver
*/
final class Loop
{
@ -18,6 +19,15 @@ final class Loop
*/
private static $driver = null;
/**
* Sets the driver to be used for `Loop::run()`.
*
* @param Driver|null $driver
*/
public static function set(Driver $driver = null) {
self::$driver = $driver;
}
/**
* Run the event loop and optionally execute a callback within the scope of it.
*
@ -32,7 +42,7 @@ final class Loop
public static function run(callable $callback = null)
{
if ($callback) {
self::$driver->defer($callback);
self::$driver->defer(wrap($callback));
}
self::$driver->run();
@ -78,7 +88,7 @@ final class Loop
*/
public static function defer(callable $callback, $data = null)
{
return self::$driver->defer($callback, $data);
return self::$driver->defer(wrap($callback), $data);
}
/**
@ -99,7 +109,7 @@ final class Loop
*/
public static function delay(int $delay, callable $callback, $data = null)
{
return self::$driver->delay($delay, $callback, $data);
return self::$driver->delay($delay, wrap($callback), $data);
}
/**
@ -120,7 +130,7 @@ final class Loop
*/
public static function repeat(int $interval, callable $callback, $data = null)
{
return self::$driver->repeat($interval, $callback, $data);
return self::$driver->repeat($interval, wrap($callback), $data);
}
/**
@ -144,7 +154,7 @@ final class Loop
*/
public static function onReadable($stream, callable $callback, $data = null)
{
return self::$driver->onReadable($stream, $callback, $data);
return self::$driver->onReadable($stream, wrap($callback), $data);
}
/**
@ -168,7 +178,7 @@ final class Loop
*/
public static function onWritable($stream, callable $callback, $data = null)
{
return self::$driver->onWritable($stream, $callback, $data);
return self::$driver->onWritable($stream, wrap($callback), $data);
}
/**
@ -193,7 +203,7 @@ final class Loop
*/
public static function onSignal(int $signo, callable $callback, $data = null)
{
return self::$driver->onSignal($signo, $callback, $data);
return self::$driver->onSignal($signo, wrap($callback), $data);
}
/**
@ -364,4 +374,9 @@ final class Loop
{
// intentionally left blank
}
}
}
// Default factory, don't move this a file loaded by the composer "files" autoload mechanism, otherwise custom
// implementations might have issues setting a default loop, because it's overridden by us then.
Loop::set((new Factory)->create());

View File

@ -2,7 +2,7 @@
namespace Amp\Loop;
use Amp\Loop\Internal\Watcher;
use Amp\Internal\Watcher;
/**
* Event loop driver which implements all basic operations to allow interoperability.
@ -431,7 +431,7 @@ abstract class Driver
*
* @return void
*/
final public function setState($key, $value)
final public function setState(string $key, $value)
{
if ($value === null) {
unset($this->registry[$key]);
@ -450,7 +450,7 @@ abstract class Driver
*
* @return mixed The previously stored value or `null` if it doesn't exist.
*/
final public function getState($key)
final public function getState(string $key)
{
return isset($this->registry[$key]) ? $this->registry[$key] : null;
}
@ -598,13 +598,6 @@ abstract class Driver
throw $exception;
}
$errorHandler = $this->errorHandler;
$errorHandler($exception);
} catch (\Exception $exception) { // @todo Remove when PHP 5.x support is no longer needed.
if (null === $this->errorHandler) {
throw $exception;
}
$errorHandler = $this->errorHandler;
$errorHandler($exception);
}

View File

@ -2,7 +2,7 @@
namespace Amp\Loop;
use Amp\Loop\Internal\Watcher;
use Amp\Internal\Watcher;
class EvLoop extends Driver {
/** @var \EvLoop */
@ -38,7 +38,7 @@ class EvLoop extends Driver {
}
$this->ioCallback = function (\EvIO $event) {
/** @var \Amp\Loop\Internal\Watcher $watcher */
/** @var \Amp\Internal\Watcher $watcher */
$watcher = $event->data;
$callback = $watcher->callback;
@ -46,7 +46,7 @@ class EvLoop extends Driver {
};
$this->timerCallback = function (\EvTimer $event) {
/** @var \Amp\Loop\Internal\Watcher $watcher */
/** @var \Amp\Internal\Watcher $watcher */
$watcher = $event->data;
if ($watcher->type & Watcher::DELAY) {
@ -58,7 +58,7 @@ class EvLoop extends Driver {
};
$this->signalCallback = function (\EvSignal $event) {
/** @var \Amp\Loop\Internal\Watcher $watcher */
/** @var \Amp\Internal\Watcher $watcher */
$watcher = $event->data;
$callback = $watcher->callback;
@ -176,9 +176,9 @@ class EvLoop extends Driver {
/**
* {@inheritdoc}
*/
public function cancel($watcherIdentifier) {
parent::cancel($watcherIdentifier);
unset($this->events[$watcherIdentifier]);
public function cancel(string $watcherId) {
parent::cancel($watcherId);
unset($this->events[$watcherId]);
}
/**

View File

@ -2,7 +2,7 @@
namespace Amp\Loop;
use Amp\Loop\Internal\Watcher;
use Amp\Internal\Watcher;
class EventLoop extends Driver {
/** @var \EventBase */
@ -195,12 +195,12 @@ class EventLoop extends Driver {
/**
* {@inheritdoc}
*/
public function cancel($watcherIdentifier) {
parent::cancel($watcherIdentifier);
public function cancel(string $watcherId) {
parent::cancel($watcherId);
if (isset($this->events[$watcherIdentifier])) {
$this->events[$watcherIdentifier]->free();
unset($this->events[$watcherIdentifier]);
if (isset($this->events[$watcherId])) {
$this->events[$watcherId]->free();
unset($this->events[$watcherId]);
}
}

26
lib/Loop/Factory.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace Amp\Loop;
class Factory {
/**
* Creates a new loop instance and chooses the best available driver.
*
* @return Driver
*/
public function create(): Driver {
if (UvLoop::supported()) {
return new UvLoop;
}
if (EvLoop::supported()) {
return new EvLoop;
}
if (EventLoop::supported()) {
return new EventLoop;
}
return new NativeLoop;
}
}

View File

@ -2,19 +2,19 @@
namespace Amp\Loop;
use Amp\Loop\Internal\Watcher;
use Amp\Internal\Watcher;
class NativeLoop extends Driver {
/** @var resource[] */
private $readStreams = [];
/** @var \Amp\Loop\Internal\Watcher[][] */
/** @var \Amp\Internal\Watcher[][] */
private $readWatchers = [];
/** @var resource[] */
private $writeStreams = [];
/** @var \Amp\Loop\Internal\Watcher[][] */
/** @var \Amp\Internal\Watcher[][] */
private $writeWatchers = [];
/** @var int[] */
@ -23,7 +23,7 @@ class NativeLoop extends Driver {
/** @var \SplPriorityQueue */
private $timerQueue;
/** @var \Amp\Loop\Internal\Watcher[][] */
/** @var \Amp\Internal\Watcher[][] */
private $signalWatchers = [];
/** @var bool */
@ -166,10 +166,10 @@ class NativeLoop extends Driver {
/**
* {@inheritdoc}
*
* @throws \AsyncInterop\Loop\UnsupportedFeatureException If the pcntl extension is not available.
* @throws \Amp\Loop\UnsupportedFeatureException If the pcntl extension is not available.
* @throws \RuntimeException If creating the backend signal handler fails.
*/
public function onSignal($signo, callable $callback, $data = null) {
public function onSignal(int $signo, callable $callback, $data = null) {
if (!$this->signalHandling) {
throw new UnsupportedFeatureException("Signal handling requires the pcntl extension");
}

View File

@ -2,7 +2,7 @@
namespace Amp\Loop;
use Amp\Loop\Internal\Watcher;
use Amp\Internal\Watcher;
class UvLoop extends Driver {
/** @var resource A uv_loop resource created with uv_loop_new() */
@ -11,7 +11,7 @@ class UvLoop extends Driver {
/** @var resource[] */
private $events = [];
/** @var \Amp\Loop\Internal\Watcher[]|\Amp\Loop\Internal\Watcher[][] */
/** @var \Amp\Internal\Watcher[]|\Amp\Internal\Watcher[][] */
private $watchers = [];
/** @var resource[] */
@ -230,20 +230,20 @@ class UvLoop extends Driver {
/**
* {@inheritdoc}
*/
public function cancel($watcherIdentifier) {
parent::cancel($watcherIdentifier);
public function cancel(string $watcherId) {
parent::cancel($watcherId);
if (!isset($this->events[$watcherIdentifier])) {
if (!isset($this->events[$watcherId])) {
return;
}
$event = $this->events[$watcherIdentifier];
$event = $this->events[$watcherId];
if (empty($this->watchers[(int) $event])) {
\uv_close($event);
}
unset($this->events[$watcherIdentifier]);
unset($this->events[$watcherId]);
}
/**

View File

@ -2,7 +2,7 @@
namespace Amp;
use AsyncInterop\{ Loop, Promise };
use AsyncInterop\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 AsyncInterop\Loop;
use Amp\Loop;
final class Producer implements Stream {
use CallableMaker, Internal\Producer;

View File

@ -106,7 +106,7 @@ function rethrow(Promise $promise) {
*/
function wait(Promise $promise) {
$resolved = false;
Loop::execute(function () use (&$resolved, &$value, &$exception, $promise) {
Loop::run(function () use (&$resolved, &$value, &$exception, $promise) {
$promise->when(function ($e, $v) use (&$resolved, &$value, &$exception) {
Loop::stop();
$resolved = true;

View File

@ -22,6 +22,9 @@
<directory suffix=".php">lib</directory>
</whitelist>
</filter>
<listeners>
<listener class="Amp\Test\LoopReset"></listener>
</listeners>
<logging>
<log type="coverage-html" target="build/coverage" title="Amp" highlight="true"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Pause, Success };
use AsyncInterop\Loop;
use Amp\Loop;
class AllTest extends \PHPUnit_Framework_TestCase {
public function testEmptyArray() {
@ -30,7 +30,7 @@ class AllTest extends \PHPUnit_Framework_TestCase {
}
public function testPendingAwatiablesArray() {
Loop::execute(function () use (&$result) {
Loop::run(function () use (&$result) {
$promises = [
new Pause(20, 1),
new Pause(30, 2),
@ -50,7 +50,7 @@ class AllTest extends \PHPUnit_Framework_TestCase {
public function testArrayKeysPreserved() {
$expected = ['one' => 1, 'two' => 2, 'three' => 3];
Loop::execute(function () use (&$result) {
Loop::run(function () use (&$result) {
$promises = [
'one' => new Pause(20, 1),
'two' => new Pause(30, 2),

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Failure, Pause, Success };
use AsyncInterop\Loop;
use Amp\Loop;
class AnyTest extends \PHPUnit_Framework_TestCase {
public function testEmptyArray() {
@ -56,7 +56,7 @@ class AnyTest extends \PHPUnit_Framework_TestCase {
}
public function testPendingAwatiablesArray() {
Loop::execute(function () use (&$result) {
Loop::run(function () use (&$result) {
$promises = [
new Pause(20, 1),
new Pause(30, 2),
@ -80,7 +80,7 @@ class AnyTest extends \PHPUnit_Framework_TestCase {
$exception = new \Exception;
$expected = [['two' => $exception], ['one' => 1, 'three' => 3]];
Loop::execute(function () use (&$result, $exception) {
Loop::run(function () use (&$result, $exception) {
$promises = [
'one' => new Pause(20, 1),
'two' => new Failure($exception),

View File

@ -3,7 +3,8 @@
namespace Amp\Test;
use Amp;
use Amp\{ Coroutine, Success };
use Amp\Coroutine;
use Amp\Success;
use AsyncInterop\Promise;
class CallTest extends \PHPUnit_Framework_TestCase {

View File

@ -3,7 +3,8 @@
namespace Amp\Test;
use Amp;
use Amp\{ Failure, Success };
use Amp\Failure;
use Amp\Success;
use AsyncInterop\Promise;
class CaptureTest extends \PHPUnit_Framework_TestCase {

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\Producer;
use AsyncInterop\Loop;
use Amp\Loop;
class ConcatTest extends \PHPUnit_Framework_TestCase {
public function getStreams() {
@ -22,7 +22,7 @@ class ConcatTest extends \PHPUnit_Framework_TestCase {
* @param array $expected
*/
public function testConcat(array $streams, array $expected) {
Loop::execute(function () use ($streams, $expected) {
Loop::run(function () use ($streams, $expected) {
$stream = Amp\concat($streams);
Amp\each($stream, function ($value) use ($expected) {
@ -38,7 +38,7 @@ class ConcatTest extends \PHPUnit_Framework_TestCase {
public function testConcatWithFailedStream() {
$exception = new \Exception;
$results = [];
Loop::execute(function () use (&$results, &$reason, $exception) {
Loop::run(function () use (&$results, &$reason, $exception) {
$producer = new Producer(function (callable $emit) use ($exception) {
yield $emit(6); // Emit once before failing.
throw $exception;

View File

@ -3,8 +3,13 @@
namespace Amp\Test;
use Amp;
use Amp\{ Coroutine, Failure, InvalidYieldError, Pause, Success };
use AsyncInterop\{ Loop, Promise };
use Amp\Coroutine;
use Amp\Failure;
use Amp\InvalidYieldError;
use Amp\Loop;
use Amp\Pause;
use Amp\Success;
use AsyncInterop\Promise;
class CoroutineTest extends \PHPUnit_Framework_TestCase {
const TIMEOUT = 100;
@ -45,7 +50,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
public function testYieldPendingPromise() {
$value = 1;
Loop::execute(function () use (&$yielded, $value) {
Loop::run(function () use (&$yielded, $value) {
$generator = function () use (&$yielded, $value) {
$yielded = yield new Pause(self::TIMEOUT, $value);
};
@ -280,7 +285,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$exception = new \Exception;
$value = 1;
Loop::execute(function () use (&$yielded, &$invoked, &$reason, $exception, $value) {
Loop::run(function () use (&$yielded, &$invoked, &$reason, $exception, $value) {
$invoked = false;
$generator = function () use (&$yielded, &$invoked, $exception, $value) {
try {
@ -313,7 +318,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$exception = new \Exception;
$value = 1;
Loop::execute(function () use (&$yielded, &$reason, $exception, $value) {
Loop::run(function () use (&$yielded, &$reason, $exception, $value) {
$generator = function () use (&$yielded, $exception, $value) {
try {
throw $exception;
@ -364,7 +369,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
*/
public function testYieldConsecutiveSucceeded() {
$invoked = false;
Loop::execute(function () use (&$invoked) {
Loop::run(function () use (&$invoked) {
$count = 1000;
$promise = new Success;
@ -389,7 +394,7 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
*/
public function testYieldConsecutiveFailed() {
$invoked = false;
Loop::execute(function () use (&$invoked) {
Loop::run(function () use (&$invoked) {
$count = 1000;
$promise = new Failure(new \Exception);

View File

@ -1,48 +0,0 @@
<?php
namespace AsyncInterop\Loop\Test;
use AsyncInterop\Loop\Driver;
class DummyDriver extends Driver
{
public $defers;
public $handler;
public static $id = "a";
public function run() {
while (list($defer, $data) = array_shift($this->defers)) {
try {
$defer(self::$id++, $data);
} catch (\Exception $e) {
if ($handler = $this->handler) {
$handler($e);
} else {
throw $e;
}
}
}
}
public function defer(callable $callback, $data = null) {
$this->defers[] = [$callback, $data];
}
public function setErrorHandler(callable $callback = null) {
$this->handler = $callback;
}
public function stop() {}
public function delay($delay, callable $callback, $data = null) { return self::$id++; }
public function repeat($interval, callable $callback, $data = null) { return self::$id++; }
public function onReadable($stream, callable $callback, $data = null) { return self::$id++; }
public function onWritable($stream, callable $callback, $data = null) { return self::$id++; }
public function onSignal($signo, callable $callback, $data = null) { return self::$id++; }
public function enable($watcherId) {}
public function disable($watcherId) {}
public function cancel($watcherId) {}
public function reference($watcherId) {}
public function unreference($watcherId) {}
public function getInfo() {}
public function getHandle() {}
}

View File

@ -3,13 +3,15 @@
namespace Amp\Test;
use Amp;
use Amp\{ Producer, Stream, Emitter };
use AsyncInterop\Loop;
use Amp\Producer;
use Amp\Stream;
use Amp\Emitter;
use Amp\Loop;
class EachTest extends \PHPUnit_Framework_TestCase {
public function testNoValuesEmitted() {
$invoked = false;
Loop::execute(function () use (&$invoked){
Loop::run(function () use (&$invoked){
$emitter = new Emitter;
$stream = Amp\each($emitter->stream(), function ($value) use (&$invoked) {
@ -29,7 +31,7 @@ class EachTest extends \PHPUnit_Framework_TestCase {
$values = [1, 2, 3];
$final = 4;
$results = [];
Loop::execute(function () use (&$results, &$result, &$count, $values, $final) {
Loop::run(function () use (&$results, &$result, &$count, $values, $final) {
$producer = new Producer(function (callable $emit) use ($values, $final) {
foreach ($values as $value) {
yield $emit($value);
@ -64,7 +66,7 @@ class EachTest extends \PHPUnit_Framework_TestCase {
public function testOnNextCallbackThrows() {
$values = [1, 2, 3];
$exception = new \Exception;
Loop::execute(function () use (&$reason, $values, $exception) {
Loop::run(function () use (&$reason, $values, $exception) {
$producer = new Producer(function (callable $emit) use ($values) {
foreach ($values as $value) {
yield $emit($value);
@ -97,7 +99,7 @@ class EachTest extends \PHPUnit_Framework_TestCase {
$values = [1, 2, 3];
$results = [];
$exception = new \Exception;
Loop::execute(function () use (&$reason, &$results, &$count, $values, $exception) {
Loop::run(function () use (&$reason, &$results, &$count, $values, $exception) {
$producer = new Producer(function (callable $emit) use ($values) {
foreach ($values as $value) {
yield $emit($value);
@ -130,7 +132,7 @@ class EachTest extends \PHPUnit_Framework_TestCase {
public function testStreamFails() {
$invoked = false;
$exception = new \Exception;
Loop::execute(function () use (&$invoked, &$reason, &$exception){
Loop::run(function () use (&$invoked, &$reason, &$exception){
$emitter = new Emitter;
$stream = Amp\each($emitter->stream(), function ($value) use (&$invoked) {

View File

@ -3,19 +3,15 @@
namespace Amp\Test\Loop;
use Amp\Loop\EvLoop;
use AsyncInterop\Loop\DriverFactory;
use AsyncInterop\Loop\Test;
use Amp\Test\LoopTest;
/**
* @requires extension ev
*/
class EvLoopTest extends Test {
class EvLoopLoopTest extends LoopTest {
public function getFactory() {
$factory = $this->getMockBuilder(DriverFactory::class)->getMock();
$factory->method('create')
->willReturn(new EvLoop);
return $factory;
return function () {
return new EvLoop;
};
}
}

View File

@ -3,19 +3,15 @@
namespace Amp\Test\Loop;
use Amp\Loop\EventLoop;
use AsyncInterop\Loop\DriverFactory;
use AsyncInterop\Loop\Test;
use Amp\Test\LoopTest;
/**
* @requires extension event
*/
class EventLoopTest extends Test {
class EventLoopLoopTest extends LoopTest {
public function getFactory() {
$factory = $this->getMockBuilder(DriverFactory::class)->getMock();
$factory->method('create')
->willReturn(new EventLoop);
return $factory;
return function () {
return new EventLoop;
};
}
}

View File

@ -3,7 +3,6 @@
namespace Amp\Test;
use Amp\Failure;
use AsyncInterop\Loop;
class FailureTest extends \PHPUnit_Framework_TestCase {
/**
@ -29,29 +28,4 @@ class FailureTest extends \PHPUnit_Framework_TestCase {
$this->assertSame(1, $invoked);
$this->assertSame($exception, $reason);
}
/**
* @depends testWhen
*/
public function testWhenThrowingForwardsToLoopHandlerOnSuccess() {
Loop::execute(function () use (&$invoked) {
$invoked = 0;
$expected = new \Exception;
Loop::setErrorHandler(function ($exception) use (&$invoked, $expected) {
++$invoked;
$this->assertSame($expected, $exception);
});
$callback = function () use ($expected) {
throw $expected;
};
$success = new Failure(new \Exception);
$success->when($callback);
});
$this->assertSame(1, $invoked);
}
}

View File

@ -3,13 +3,15 @@
namespace Amp\Test;
use Amp;
use Amp\{ Producer, Stream, Emitter };
use AsyncInterop\Loop;
use Amp\Producer;
use Amp\Stream;
use Amp\Emitter;
use Amp\Loop;
class FilterTest extends \PHPUnit_Framework_TestCase {
public function testNoValuesEmitted() {
$invoked = false;
Loop::execute(function () use (&$invoked){
Loop::run(function () use (&$invoked){
$emitter = new Emitter;
$stream = Amp\filter($emitter->stream(), function ($value) use (&$invoked) {
@ -29,7 +31,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase {
$values = [1, 2, 3];
$results = [];
$expected = [1, 3];
Loop::execute(function () use (&$results, &$result, &$count, $values) {
Loop::run(function () use (&$results, &$result, &$count, $values) {
$producer = new Producer(function (callable $emit) use ($values) {
foreach ($values as $value) {
yield $emit($value);
@ -60,7 +62,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase {
public function testCallbackThrows() {
$values = [1, 2, 3];
$exception = new \Exception;
Loop::execute(function () use (&$reason, $values, $exception) {
Loop::run(function () use (&$reason, $values, $exception) {
$producer = new Producer(function (callable $emit) use ($values) {
foreach ($values as $value) {
yield $emit($value);
@ -88,7 +90,7 @@ class FilterTest extends \PHPUnit_Framework_TestCase {
public function testStreamFails() {
$invoked = false;
$exception = new \Exception;
Loop::execute(function () use (&$invoked, &$reason, &$exception){
Loop::run(function () use (&$invoked, &$reason, &$exception){
$emitter = new Emitter;
$stream = Amp\filter($emitter->stream(), function ($value) use (&$invoked) {

View File

@ -3,8 +3,11 @@
namespace Amp\Test;
use Amp;
use Amp\{ Failure, MultiReasonException, Pause, Success };
use AsyncInterop\Loop;
use Amp\Failure;
use Amp\MultiReasonException;
use Amp\Pause;
use Amp\Success;
use Amp\Loop;
class FirstTest extends \PHPUnit_Framework_TestCase {
/**
@ -55,7 +58,7 @@ class FirstTest extends \PHPUnit_Framework_TestCase {
}
public function testPendingAwatiablesArray() {
Loop::execute(function () use (&$result) {
Loop::run(function () use (&$result) {
$promises = [
new Pause(20, 1),
new Pause(30, 2),

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\Pause;
use AsyncInterop\Loop;
use Amp\Loop;
class IntervalTest extends \PHPUnit_Framework_TestCase {
const TIMEOUT = 10;
@ -29,7 +29,7 @@ class IntervalTest extends \PHPUnit_Framework_TestCase {
public function testSlowConsumer() {
$invoked = 0;
$count = 5;
Loop::execute(function () use (&$invoked, $count) {
Loop::run(function () use (&$invoked, $count) {
$stream = Amp\interval(self::TIMEOUT, $count);
$stream->listen(function () use (&$invoked) {
@ -46,6 +46,6 @@ class IntervalTest extends \PHPUnit_Framework_TestCase {
* @expectedExceptionMessage The number of times to emit must be a positive value
*/
public function testInvalidCount() {
$stream = Amp\interval(self::TIMEOUT, -1);
Amp\interval(self::TIMEOUT, -1);
}
}

View File

@ -3,7 +3,9 @@
namespace Amp\Test;
use Amp;
use Amp\{ Failure, LazyPromise, Success };
use Amp\Failure;
use Amp\LazyPromise;
use Amp\Success;
class LazyPromiseTest extends \PHPUnit_Framework_TestCase {
public function testPromisorNotCalledOnConstruct() {

View File

@ -3,14 +3,17 @@
namespace Amp\Test;
use Amp;
use Amp\{ Producer, Listener, Pause, Emitter };
use AsyncInterop\Loop;
use Amp\Producer;
use Amp\Listener;
use Amp\Pause;
use Amp\Emitter;
use Amp\Loop;
class ListenerTest extends \PHPUnit_Framework_TestCase {
const TIMEOUT = 10;
public function testSingleEmittingStream() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$value = 1;
$stream = new Producer(function (callable $emit) use ($value) {
yield $emit($value);
@ -24,14 +27,14 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
}
$this->assertSame($listener->getResult(), $value);
}));
});
}
/**
* @depends testSingleEmittingStream
*/
public function testFastEmittingStream() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$count = 10;
$emitter = new Emitter;
@ -50,14 +53,14 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
$this->assertSame($count, $i);
$this->assertSame($listener->getResult(), $i);
}));
});
}
/**
* @depends testSingleEmittingStream
*/
public function testSlowEmittingStream() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$count = 10;
$stream = new Producer(function (callable $emit) use ($count) {
for ($i = 0; $i < $count; ++$i) {
@ -75,14 +78,14 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
$this->assertSame($count, $i);
$this->assertSame($listener->getResult(), $i);
}));
});
}
/**
* @depends testFastEmittingStream
*/
public function testDrain() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$count = 10;
$expected = \range(0, $count - 1);
@ -107,7 +110,7 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
$values = $listener->drain();
$this->assertSame($expected, $values);
}));
});
}
/**
@ -123,7 +126,7 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
}
public function testFailingStream() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$exception = new \Exception;
$emitter = new Emitter;
@ -145,7 +148,7 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
}
}));
});
}
/**
@ -181,7 +184,7 @@ class ListenerTest extends \PHPUnit_Framework_TestCase {
* @expectedExceptionMessage The stream has not resolved
*/
public function testGetResultBeforeResolution() {
Loop::execute(Amp\wrap(function () {
Loop::run(Amp\wrap(function () {
$emitter = new Emitter;
$listener = new Listener($emitter->stream());

12
test/LoopReset.php Normal file
View File

@ -0,0 +1,12 @@
<?php
namespace Amp\Test;
use Amp\Loop;
use PHPUnit_Framework_Test;
class LoopReset extends \PHPUnit_Framework_BaseTestListener {
public function endTest(PHPUnit_Framework_Test $test, $time) {
Loop::set((new Loop\Factory)->create());
}
}

View File

@ -1,19 +1,19 @@
<?php
namespace AsyncInterop\Loop;
namespace Amp\Test;
class LoopStateTest extends \PHPUnit_Framework_TestCase
{
use Amp\Loop\Driver;
class LoopStateTest extends \PHPUnit_Framework_TestCase {
/** @var Driver */
private $loop;
protected function setUp()
{
protected function setUp() {
$this->loop = $this->getMockForAbstractClass(Driver::class);
}
/** @test */
public function defaultsToNull()
{
public function defaultsToNull() {
$this->assertNull($this->loop->getState("foobar"));
}
@ -21,14 +21,12 @@ class LoopStateTest extends \PHPUnit_Framework_TestCase
* @test
* @dataProvider provideValues
*/
public function getsPreviouslySetValue($value)
{
public function getsPreviouslySetValue($value) {
$this->loop->setState("foobar", $value);
$this->assertSame($value, $this->loop->getState("foobar"));
}
public function provideValues()
{
public function provideValues() {
return [
["string"],
[42],

File diff suppressed because it is too large Load Diff

View File

@ -3,8 +3,12 @@
namespace Amp\Test;
use Amp;
use Amp\{ Deferred, Failure, Pause, Success };
use AsyncInterop\{ Loop, Promise };
use Amp\Deferred;
use Amp\Failure;
use Amp\Loop;
use Amp\Pause;
use Amp\Success;
use AsyncInterop\Promise;
class MapTest extends \PHPUnit_Framework_TestCase {
public function testEmptyArray() {
@ -20,7 +24,7 @@ class MapTest extends \PHPUnit_Framework_TestCase {
}
public function testSuccessfulPromisesArray() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$promises = [new Success(1), new Success(2), new Success(3)];;
$count = 0;
@ -39,7 +43,7 @@ class MapTest extends \PHPUnit_Framework_TestCase {
}
$this->assertSame(\count($promises), $count);
}));
});
}
public function testPendingPromisesArray() {
@ -76,7 +80,7 @@ class MapTest extends \PHPUnit_Framework_TestCase {
}
public function testFailedPromisesArray() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$exception = new \Exception;
$promises = [new Failure($exception), new Failure($exception), new Failure($exception)];;
@ -95,7 +99,7 @@ class MapTest extends \PHPUnit_Framework_TestCase {
}
$this->assertSame(0, $count);
}));
});
}
/**
@ -103,7 +107,7 @@ class MapTest extends \PHPUnit_Framework_TestCase {
*/
public function testCallbackThrowingExceptionRejectsPromises()
{
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$promises = [new Success(1), new Success(2), new Success(3)];;
$exception = new \Exception;
@ -124,7 +128,7 @@ class MapTest extends \PHPUnit_Framework_TestCase {
$this->assertSame($exception, $reason);
}
}
}));
});
}
/**

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\Producer;
use AsyncInterop\Loop;
use Amp\Loop;
class MergeTest extends \PHPUnit_Framework_TestCase {
public function getStreams() {
@ -22,7 +22,7 @@ class MergeTest extends \PHPUnit_Framework_TestCase {
* @param array $expected
*/
public function testMerge(array $streams, array $expected) {
Loop::execute(function () use ($streams, $expected) {
Loop::run(function () use ($streams, $expected) {
$stream = Amp\merge($streams);
Amp\each($stream, function ($value) use ($expected) {
@ -37,7 +37,7 @@ class MergeTest extends \PHPUnit_Framework_TestCase {
*/
public function testMergeWithFailedStream() {
$exception = new \Exception;
Loop::execute(function () use (&$reason, $exception) {
Loop::run(function () use (&$reason, $exception) {
$producer = new Producer(function (callable $emit) use ($exception) {
yield $emit(1); // Emit once before failing.
throw $exception;

View File

@ -3,12 +3,14 @@
namespace Amp\Test;
use Amp;
use Amp\{ Emitter, Message, Success };
use AsyncInterop\Loop;
use Amp\Emitter;
use Amp\Message;
use Amp\Success;
use Amp\Loop;
class MessageTest extends \PHPUnit_Framework_TestCase {
public function testBufferingAll() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
@ -23,11 +25,11 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
$result = yield $message;
$this->assertSame(\implode($values), $result);
}));
});
}
public function testFullStreamConsumption() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$result = 1;
@ -48,11 +50,11 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
$this->assertSame(\implode($values), $buffer);
$this->assertSame("", yield $message);
$this->assertSame($result, $message->getResult());
}));
});
}
public function testFastResolvingStream() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$result = 1;
@ -73,10 +75,10 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
$this->assertSame([\implode($values)], $emitted);
$this->assertSame(\implode($values), yield $message);
$this->assertSame($result, $message->getResult());
}));
});
}
public function testPartialStreamConsumption() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$values = ["abc", "def", "ghi"];
$emitter = new Emitter;
@ -96,11 +98,11 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
$emitter->resolve();
$this->assertSame(\implode($values), yield $message);
}));
});
}
public function testFailingStream() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$exception = new \Exception;
$value = "abc";
@ -117,17 +119,17 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
}
}));
});
}
public function testEmptyStream() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$value = 1;
$message = new Message(new Success($value));
$this->assertFalse(yield $message->advance());
$this->assertSame($value, $message->getResult());
}));
});
}
/**
@ -135,7 +137,7 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
* @expectedExceptionMessage The stream has resolved
*/
public function testAdvanceAfterCompletion() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$value = "abc";
$emitter = new Emitter;
@ -147,7 +149,7 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
for ($i = 0; $i < 3; ++$i) {
yield $message->advance();
}
}));
});
}
/**
@ -155,7 +157,7 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
* @expectedExceptionMessage The stream has resolved
*/
public function testGetCurrentAfterCompletion() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$value = "abc";
$emitter = new Emitter;
@ -167,7 +169,7 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
while (yield $message->advance());
$message->getCurrent();
}));
});
}
/**
@ -175,10 +177,10 @@ class MessageTest extends \PHPUnit_Framework_TestCase {
* @expectedExceptionMessage The stream has not resolved
*/
public function testGetResultBeforeCompletion() {
Loop::execute(Amp\wrap(function () {
Loop::run(function () {
$emitter = new Emitter;
$message = new Message($emitter->stream());
$message->getResult();
}));
});
}
}

View File

@ -3,16 +3,12 @@
namespace Amp\Test\Loop;
use Amp\Loop\NativeLoop;
use AsyncInterop\Loop\DriverFactory;
use AsyncInterop\Loop\Test;
use Amp\Test\LoopTest;
class NativeLoopTest extends Test {
class NativeLoopLoopTest extends LoopTest {
public function getFactory() {
$factory = $this->getMockBuilder(DriverFactory::class)->getMock();
$factory->method('create')
->willReturn(new NativeLoop());
return $factory;
return function () {
return new NativeLoop;
};
}
}

View File

@ -3,7 +3,7 @@
namespace Amp\Test;
use Amp\Pause;
use AsyncInterop\Loop;
use Amp\Loop;
class PauseTest extends \PHPUnit_Framework_TestCase {
public function testPause() {
@ -11,7 +11,7 @@ class PauseTest extends \PHPUnit_Framework_TestCase {
$value = "test";
$start = microtime(true);
Loop::execute(function () use (&$result, $time, $value) {
Loop::run(function () use (&$result, $time, $value) {
$promise = new Pause($time, $value);
$callback = function ($exception, $value) use (&$result) {
@ -31,7 +31,7 @@ class PauseTest extends \PHPUnit_Framework_TestCase {
$start = microtime(true);
$invoked = false;
Loop::execute(function () use (&$invoked, $time, $value) {
Loop::run(function () use (&$invoked, $time, $value) {
$promise = new Pause($time, $value);
$promise->unreference();
@ -55,7 +55,7 @@ class PauseTest extends \PHPUnit_Framework_TestCase {
$start = microtime(true);
$invoked = false;
Loop::execute(function () use (&$invoked, $time, $value) {
Loop::run(function () use (&$invoked, $time, $value) {
$promise = new Pause($time, $value);
$promise->unreference();
$promise->reference();

View File

@ -2,7 +2,8 @@
namespace Amp\Test;
use AsyncInterop\{ Loop, Promise };
use Amp\Loop;
use AsyncInterop\Promise;
class Placeholder {
use \Amp\Internal\Placeholder {
@ -104,7 +105,7 @@ class PlaceholderTraitTest extends \PHPUnit_Framework_TestCase {
* @depends testWhenOnSuccess
*/
public function testWhenThrowingForwardsToLoopHandlerOnSuccess() {
Loop::execute(function () use (&$invoked) {
Loop::run(function () use (&$invoked) {
$invoked = 0;
$expected = new \Exception;
@ -129,7 +130,7 @@ class PlaceholderTraitTest extends \PHPUnit_Framework_TestCase {
* @depends testWhenAfterSuccess
*/
public function testWhenThrowingForwardsToLoopHandlerAfterSuccess() {
Loop::execute(function () use (&$invoked) {
Loop::run(function () use (&$invoked) {
$invoked = 0;
$expected = new \Exception;
@ -235,7 +236,7 @@ class PlaceholderTraitTest extends \PHPUnit_Framework_TestCase {
* @depends testWhenOnSuccess
*/
public function testWhenThrowingForwardsToLoopHandlerOnFail() {
Loop::execute(function () use (&$invoked) {
Loop::run(function () use (&$invoked) {
$invoked = 0;
$expected = new \Exception;
@ -260,7 +261,7 @@ class PlaceholderTraitTest extends \PHPUnit_Framework_TestCase {
* @depends testWhenOnSuccess
*/
public function testWhenThrowingForwardsToLoopHandlerAfterFail() {
Loop::execute(function () use (&$invoked) {
Loop::run(function () use (&$invoked) {
$invoked = 0;
$expected = new \Exception;
@ -319,7 +320,7 @@ class PlaceholderTraitTest extends \PHPUnit_Framework_TestCase {
* @expectedExceptionMessage Promise has already been resolved
*/
public function testResolveAgainWithinWhenCallback() {
Loop::execute(function () {
Loop::run(function () {
$this->placeholder->when(function () {
$this->placeholder->resolve();
});

View File

@ -4,7 +4,7 @@ namespace Amp\Test;
use Amp;
use Amp\{ Deferred, Producer, Pause };
use AsyncInterop\Loop;
use Amp\Loop;
class ProducerTest extends \PHPUnit_Framework_TestCase {
const TIMEOUT = 100;
@ -14,12 +14,12 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
* @expectedExceptionMessage The callable did not return a Generator
*/
public function testNonGeneratorCallable() {
$producer = new Producer(function () {});
new Producer(function () {});
}
public function testEmit() {
$invoked = false;
Loop::execute(Amp\wrap(function () use (&$invoked) {
Loop::run(Amp\wrap(function () use (&$invoked) {
$value = 1;
$producer = new Producer(function (callable $emit) use ($value) {
@ -48,7 +48,7 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
*/
public function testEmitSuccessfulPromise() {
$invoked = false;
Loop::execute(Amp\wrap(function () use (&$invoked) {
Loop::run(function () use (&$invoked) {
$deferred = new Deferred();
$producer = new Producer(function (callable $emit) use ($deferred) {
@ -65,7 +65,7 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
$producer->listen($callback);
$deferred->resolve($value);
}));
});
$this->assertTrue($invoked);
}
@ -75,7 +75,7 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
*/
public function testEmitFailedPromise() {
$exception = new \Exception;
Loop::execute(Amp\wrap(function () use ($exception) {
Loop::run(function () use ($exception) {
$deferred = new Deferred();
$producer = new Producer(function (callable $emit) use ($deferred) {
@ -87,7 +87,7 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
$producer->when(function ($reason) use ($exception) {
$this->assertSame($reason, $exception);
});
}));
});
}
/**
@ -95,7 +95,7 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
*/
public function testEmitBackPressure() {
$emits = 3;
Loop::execute(Amp\wrap(function () use (&$time, $emits) {
Loop::run(function () use (&$time, $emits) {
$producer = new Producer(function (callable $emit) use (&$time, $emits) {
$time = microtime(true);
for ($i = 0; $i < $emits; ++$i) {
@ -107,7 +107,7 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
$producer->listen(function () {
return new Pause(self::TIMEOUT);
});
}));
});
$this->assertGreaterThan(self::TIMEOUT * $emits, $time * 1000);
}
@ -119,7 +119,7 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
$exception = new \Exception;
try {
Loop::execute(Amp\wrap(function () use ($exception) {
Loop::run(function () use ($exception) {
$producer = new Producer(function (callable $emit) {
yield $emit(1);
yield $emit(2);
@ -128,7 +128,7 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
$producer->listen(function () use ($exception) {
throw $exception;
});
}));
});
} catch (\Exception $caught) {
$this->assertSame($exception, $caught);
}
@ -141,14 +141,14 @@ class ProducerTest extends \PHPUnit_Framework_TestCase {
$exception = new \Exception;
try {
Loop::execute(Amp\wrap(function () use ($exception) {
Loop::run(function () use ($exception) {
$producer = new Producer(function (callable $emit) use ($exception) {
yield $emit(1);
throw $exception;
});
Amp\wait($producer);
}));
});
} catch (\Exception $caught) {
$this->assertSame($exception, $caught);
}

View File

@ -2,8 +2,10 @@
namespace Amp\Test;
use Amp\{ Deferred, Failure, Success };
use AsyncInterop\{ Loop, Promise };
use Amp\{
Deferred, Failure, Loop, Success
};
use AsyncInterop\Promise;
class Producer {
use \Amp\Internal\Producer {
@ -90,10 +92,6 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
$value = 1;
$deferred = new Deferred;
$callback = function ($emitted) use (&$invoked) {
$invoked = true;
};
$callback = function ($emitted) use (&$invoked, $value) {
$invoked = true;
$this->assertSame($emitted, $value);
@ -194,7 +192,7 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
$exception = new \Exception;
try {
Loop::execute(function () use ($exception) {
Loop::run(function () use ($exception) {
$this->producer->listen(function () use ($exception) {
throw $exception;
});
@ -228,7 +226,7 @@ class ProducerTraitTest extends \PHPUnit_Framework_TestCase {
$promise = new Failure($exception);
try {
Loop::execute(function () use ($exception, $promise) {
Loop::run(function () use ($exception, $promise) {
$this->producer->listen(function () use ($promise) {
return $promise;
});

View File

@ -4,14 +4,14 @@ namespace Amp\Test;
use Amp;
use Amp\Failure;
use AsyncInterop\Loop;
use Amp\Loop;
class RethrowTest extends \PHPUnit_Framework_TestCase {
public function testWaitOnPendingPromise() {
$exception = new \Exception;
try {
Loop::execute(function () use ($exception) {
Loop::run(function () use ($exception) {
$promise = new Failure($exception);
Amp\rethrow($promise);

View File

@ -3,8 +3,11 @@
namespace Amp\Test;
use Amp;
use Amp\{ Failure, MultiReasonException, Pause, Success };
use AsyncInterop\Loop;
use Amp\Failure;
use Amp\MultiReasonException;
use Amp\Pause;
use Amp\Success;
use Amp\Loop;
class SomeTest extends \PHPUnit_Framework_TestCase {
/**
@ -55,7 +58,7 @@ class SomeTest extends \PHPUnit_Framework_TestCase {
}
public function testPendingAwatiablesArray() {
Loop::execute(function () use (&$result) {
Loop::run(function () use (&$result) {
$promises = [
new Pause(20, 1),
new Pause(30, 2),
@ -75,7 +78,7 @@ class SomeTest extends \PHPUnit_Framework_TestCase {
public function testArrayKeysPreserved() {
$expected = [[], ['one' => 1, 'two' => 2, 'three' => 3]];
Loop::execute(function () use (&$result) {
Loop::run(function () use (&$result) {
$promises = [
'one' => new Pause(20, 1),
'two' => new Pause(30, 2),

View File

@ -3,13 +3,15 @@
namespace Amp\Test;
use Amp;
use Amp\{ Failure, Pause, Success };
use AsyncInterop\Loop;
use Amp\Failure;
use Amp\Pause;
use Amp\Success;
use Amp\Loop;
class StreamTest extends \PHPUnit_Framework_TestCase {
public function testSuccessfulPromises() {
$results = [];
Loop::execute(function () use (&$results) {
Loop::run(function () use (&$results) {
$stream = Amp\stream([new Success(1), new Success(2), new Success(3)]);
$stream->listen(function ($value) use (&$results) {
@ -22,7 +24,7 @@ class StreamTest extends \PHPUnit_Framework_TestCase {
public function testFailedPromises() {
$exception = new \Exception;
Loop::execute(function () use (&$reason, $exception) {
Loop::run(function () use (&$reason, $exception) {
$stream = Amp\stream([new Failure($exception), new Failure($exception)]);
$callback = function ($exception, $value) use (&$reason) {
@ -38,7 +40,7 @@ class StreamTest extends \PHPUnit_Framework_TestCase {
public function testMixedPromises() {
$exception = new \Exception;
$results = [];
Loop::execute(function () use (&$results, &$reason, $exception) {
Loop::run(function () use (&$results, &$reason, $exception) {
$stream = Amp\stream([new Success(1), new Success(2), new Failure($exception), new Success(4)]);
$stream->listen(function ($value) use (&$results) {
@ -58,7 +60,7 @@ class StreamTest extends \PHPUnit_Framework_TestCase {
public function testPendingPromises() {
$results = [];
Loop::execute(function () use (&$results) {
Loop::run(function () use (&$results) {
$stream = Amp\stream([new Pause(30, 1), new Pause(10, 2), new Pause(20, 3), new Success(4)]);
$stream->listen(function ($value) use (&$results) {
@ -71,7 +73,7 @@ class StreamTest extends \PHPUnit_Framework_TestCase {
public function testTraversable() {
$results = [];
Loop::execute(function () use (&$results) {
Loop::run(function () use (&$results) {
$generator = (function () {
foreach (\range(1, 4) as $value) {
yield $value;

View File

@ -2,8 +2,9 @@
namespace Amp\Test;
use Amp\Loop;
use Amp\Success;
use AsyncInterop\{ Loop, Promise };
use AsyncInterop\Promise;
class SuccessTest extends \PHPUnit_Framework_TestCase {
/**
@ -34,7 +35,7 @@ class SuccessTest extends \PHPUnit_Framework_TestCase {
* @depends testWhen
*/
public function testWhenThrowingForwardsToLoopHandlerOnSuccess() {
Loop::execute(function () use (&$invoked) {
Loop::run(function () use (&$invoked) {
$invoked = 0;
$expected = new \Exception;

View File

@ -3,12 +3,15 @@
namespace Amp\Test;
use Amp;
use Amp\{ Failure, Pause, Success };
use AsyncInterop\{ Loop, Promise };
use Amp\Failure;
use Amp\Loop;
use Amp\Pause;
use Amp\Success;
use AsyncInterop\Promise;
class TimeoutTest extends \PHPUnit_Framework_TestCase {
public function testSuccessfulPromise() {
Loop::execute(function () {
Loop::run(function () {
$value = 1;
$promise = new Success($value);
@ -27,7 +30,7 @@ class TimeoutTest extends \PHPUnit_Framework_TestCase {
}
public function testFailedPromise() {
Loop::execute(function () {
Loop::run(function () {
$exception = new \Exception;
$promise = new Failure($exception);
@ -51,7 +54,7 @@ class TimeoutTest extends \PHPUnit_Framework_TestCase {
public function testFastPending() {
$value = 1;
Loop::execute(function () use (&$result, $value) {
Loop::run(function () use (&$result, $value) {
$promise = new Pause(50, $value);
$promise = Amp\timeout($promise, 100);
@ -71,7 +74,7 @@ class TimeoutTest extends \PHPUnit_Framework_TestCase {
* @depends testSuccessfulPromise
*/
public function testSlowPending() {
Loop::execute(function () use (&$reason) {
Loop::run(function () use (&$reason) {
$promise = new Pause(200);
$promise = Amp\timeout($promise, 100);

View File

@ -3,19 +3,15 @@
namespace Amp\Test\Loop;
use Amp\Loop\UvLoop;
use AsyncInterop\Loop\DriverFactory;
use AsyncInterop\Loop\Test;
use Amp\Test\LoopTest;
/**
* @requires extension uv
*/
class UvLoopTest extends Test {
class UvLoopLoopTest extends LoopTest {
public function getFactory() {
$factory = $this->getMockBuilder(DriverFactory::class)->getMock();
$factory->method('create')
->willReturn(new UvLoop);
return $factory;
return function () {
return new UvLoop;
};
}
}

View File

@ -3,12 +3,14 @@
namespace Amp\Test;
use Amp;
use Amp\{ Deferred, Failure, Pause, Success };
use AsyncInterop\Loop;
use Amp\Deferred;
use Amp\Failure;
use Amp\Pause;
use Amp\Success;
use Amp\Loop;
class WaitTest extends \PHPUnit_Framework_TestCase {
public function testWaitOnSuccessfulPromise()
{
public function testWaitOnSuccessfulPromise() {
$value = 1;
$promise = new Success($value);
@ -18,8 +20,7 @@ class WaitTest extends \PHPUnit_Framework_TestCase {
$this->assertSame($value, $result);
}
public function testWaitOnFailedPromise()
{
public function testWaitOnFailedPromise() {
$exception = new \Exception();
$promise = new Failure($exception);
@ -37,9 +38,8 @@ class WaitTest extends \PHPUnit_Framework_TestCase {
/**
* @depends testWaitOnSuccessfulPromise
*/
public function testWaitOnPendingPromise()
{
Loop::execute(function () {
public function testWaitOnPendingPromise() {
Loop::run(function () {
$value = 1;
$promise = new Pause(100, $value);
@ -54,8 +54,7 @@ class WaitTest extends \PHPUnit_Framework_TestCase {
* @expectedException \Error
* @expectedExceptionMessage Loop stopped without resolving promise
*/
public function testPromiseWithNoResolutionPathThrowsException()
{
public function testPromiseWithNoResolutionPathThrowsException() {
$promise = new Deferred;
$result = Amp\wait($promise->promise());