mirror of
https://github.com/danog/amp.git
synced 2024-11-27 12:35:02 +01:00
Fix some tests; PSR-2 function definitions
This commit is contained in:
parent
ff1e57a7cd
commit
ffe714f1ef
256
src/Test.php
256
src/Test.php
@ -17,28 +17,33 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
/** @var Driver */
|
/** @var Driver */
|
||||||
public $loop;
|
public $loop;
|
||||||
|
|
||||||
function setUp() {
|
function setUp()
|
||||||
|
{
|
||||||
$this->loop = $this->getFactory()->create();
|
$this->loop = $this->getFactory()->create();
|
||||||
if (!$this->loop instanceof Driver) {
|
if (!$this->loop instanceof Driver) {
|
||||||
$this->fail("Factory did not return a loop Driver");
|
$this->fail("Factory did not return a loop Driver");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function start($cb) {
|
function start($cb)
|
||||||
|
{
|
||||||
$cb($this->loop);
|
$cb($this->loop);
|
||||||
$this->loop->run();
|
$this->loop->run();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testEmptyLoop() {
|
function testEmptyLoop()
|
||||||
|
{
|
||||||
$this->loop->run();
|
$this->loop->run();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testStopWorksEvenIfNotCurrentlyRunning() {
|
function testStopWorksEvenIfNotCurrentlyRunning()
|
||||||
|
{
|
||||||
$this->loop->stop();
|
$this->loop->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: The running nesting is important for being able to continue actually still running loops (i.e. running flag set, if the driver has one) inside register_shutdown_function() for example
|
// Note: The running nesting is important for being able to continue actually still running loops (i.e. running flag set, if the driver has one) inside register_shutdown_function() for example
|
||||||
function testLoopRunsCanBeConsecutiveAndNested() {
|
function testLoopRunsCanBeConsecutiveAndNested()
|
||||||
|
{
|
||||||
$this->expectOutputString("123456");
|
$this->expectOutputString("123456");
|
||||||
$this->start(function (Driver $loop) {
|
$this->start(function (Driver $loop) {
|
||||||
$loop->defer(function() {
|
$loop->defer(function() {
|
||||||
@ -69,16 +74,18 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSignalCapability() {
|
function testSignalCapability()
|
||||||
|
{
|
||||||
try {
|
try {
|
||||||
$watcher = $this->loop->onSignal(SIGUSR1, function() {});
|
$watcher = $this->loop->onSignal(SIGUSR1, function() {});
|
||||||
$this->loop->cancel($watcher);
|
$this->loop->cancel($watcher);
|
||||||
} catch (\UnsupportedFeatureException $e) {
|
} catch (UnsupportedFeatureException $e) {
|
||||||
$this->markTestSkipped("The loop is not capable of handling signals properly. Skipping.");
|
$this->markTestSkipped("The loop is not capable of handling signals properly. Skipping.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function testWatcherUnrefRerefRunResult() {
|
function testWatcherUnrefRerefRunResult()
|
||||||
|
{
|
||||||
$invoked = false;
|
$invoked = false;
|
||||||
$this->start(function(Driver $loop) use (&$invoked) {
|
$this->start(function(Driver $loop) use (&$invoked) {
|
||||||
$watcher = $loop->defer(function() use (&$invoked) {
|
$watcher = $loop->defer(function() use (&$invoked) {
|
||||||
@ -91,18 +98,18 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertTrue($invoked);
|
$this->assertTrue($invoked);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testImmediatelyWatcherUnrefRunResult() {
|
function testDeferWatcherUnrefRunResult()
|
||||||
$invoked = false;
|
{
|
||||||
$this->start(function(Driver $loop) use (&$invoked) {
|
$this->start(function(Driver $loop) {
|
||||||
$watcher = $loop->defer(function() use (&$invoked) {
|
$watcher = $loop->defer(function() {
|
||||||
$invoked = true;
|
$this->fail("Unreferenced defer watcher should not keep loop running");
|
||||||
});
|
});
|
||||||
$loop->unreference($watcher);
|
$loop->unreference($watcher);
|
||||||
});
|
});
|
||||||
$this->assertFalse($invoked);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function testOnceWatcherUnrefRunResult() {
|
function testOnceWatcherUnrefRunResult()
|
||||||
|
{
|
||||||
$invoked = false;
|
$invoked = false;
|
||||||
$this->start(function(Driver $loop) use (&$invoked) {
|
$this->start(function(Driver $loop) use (&$invoked) {
|
||||||
$watcher = $loop->delay(2000, function() use (&$invoked) {
|
$watcher = $loop->delay(2000, function() use (&$invoked) {
|
||||||
@ -114,7 +121,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertFalse($invoked);
|
$this->assertFalse($invoked);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testRepeatWatcherUnrefRunResult() {
|
function testRepeatWatcherUnrefRunResult()
|
||||||
|
{
|
||||||
$invoked = false;
|
$invoked = false;
|
||||||
$this->start(function(Driver $loop) use (&$invoked) {
|
$this->start(function(Driver $loop) use (&$invoked) {
|
||||||
$watcher = $loop->repeat(2000, function() use (&$invoked) {
|
$watcher = $loop->repeat(2000, function() use (&$invoked) {
|
||||||
@ -125,7 +133,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertFalse($invoked);
|
$this->assertFalse($invoked);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testOnReadableWatcherUnrefRunResult() {
|
function testOnReadableWatcherUnrefRunResult()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$watcher = $loop->onReadable(STDIN, function() {
|
$watcher = $loop->onReadable(STDIN, function() {
|
||||||
// empty
|
// empty
|
||||||
@ -134,7 +143,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testOnWritableWatcherKeepAliveRunResult() {
|
function testOnWritableWatcherKeepAliveRunResult()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$watcher = $loop->onWritable(STDOUT, function() {
|
$watcher = $loop->onWritable(STDOUT, function() {
|
||||||
// empty
|
// empty
|
||||||
@ -144,7 +154,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @depends testSignalCapability */
|
/** @depends testSignalCapability */
|
||||||
function testOnSignalWatcherKeepAliveRunResult() {
|
function testOnSignalWatcherKeepAliveRunResult()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$watcher = $loop->onSignal(SIGUSR1, function() {
|
$watcher = $loop->onSignal(SIGUSR1, function() {
|
||||||
// empty
|
// empty
|
||||||
@ -153,55 +164,29 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testDisabledDeferReenableInSubsequentTick() {
|
function testDisabledDeferReenableInSubsequentTick()
|
||||||
$this->expectOutputString("12345");
|
{
|
||||||
|
$this->expectOutputString("123");
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$loop->defer(function() use ($loop, &$watcherId) {
|
$watcherId = $loop->defer(function ($watcherId) {
|
||||||
$loop->defer(function($watcherId) use ($loop) {
|
echo 3;
|
||||||
$loop->disable($watcherId);
|
|
||||||
echo 3;
|
|
||||||
});
|
|
||||||
$loop->enable($watcherId);
|
|
||||||
$loop->defer(function() {
|
|
||||||
echo 5;
|
|
||||||
});
|
|
||||||
$loop->disable($watcherId);
|
|
||||||
$loop->cancel($watcherId);
|
|
||||||
echo 1;
|
|
||||||
});
|
|
||||||
$watcherId = $loop->defer(function($watcherId) use ($loop) {
|
|
||||||
$loop->cancel($watcherId);
|
|
||||||
echo 4;
|
|
||||||
});
|
});
|
||||||
$loop->disable($watcherId);
|
$loop->disable($watcherId);
|
||||||
$loop->defer(function ($watcherId) use ($loop) {
|
$loop->defer(function () use ($loop, $watcherId) {
|
||||||
$loop->disable($watcherId);
|
|
||||||
$loop->enable($watcherId);
|
$loop->enable($watcherId);
|
||||||
echo 2;
|
echo 2;
|
||||||
});
|
});
|
||||||
|
echo 1;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSuccessOnDeferWatcherIdReuse() {
|
|
||||||
$this->start(function(Driver $loop) {
|
function provideRegistrationArgs()
|
||||||
$watcherId = $loop->defer(function($watcherId) use ($loop) {
|
{
|
||||||
$loop->disable($watcherId);
|
|
||||||
$loop->disable($watcherId);
|
|
||||||
$loop->enable($watcherId);
|
|
||||||
$loop->enable($watcherId);
|
|
||||||
$loop->enable($watcherId);
|
|
||||||
});
|
|
||||||
$loop->defer(function() use ($loop, $watcherId) {
|
|
||||||
$loop->cancel($watcherId);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function provideRegistrationArgs() {
|
|
||||||
$args = [
|
$args = [
|
||||||
["defer", [function() {}]],
|
["defer", [function() {}]],
|
||||||
["delay", [5, function() {}]],
|
["delay", [5, function() {}]],
|
||||||
["repeat", [5, function() {}]],
|
["repeat", [5, function() {}]],
|
||||||
["onWritable", [\STDOUT, function() {}]],
|
["onWritable", [\STDOUT, function() {}]],
|
||||||
["onReadable", [\STDIN, function() {}]],
|
["onReadable", [\STDIN, function() {}]],
|
||||||
["onSignal", [\SIGUSR1, function() {}]],
|
["onSignal", [\SIGUSR1, function() {}]],
|
||||||
@ -213,7 +198,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
/**
|
/**
|
||||||
* @dataProvider provideRegistrationArgs
|
* @dataProvider provideRegistrationArgs
|
||||||
*/
|
*/
|
||||||
function testDisableWithConsecutiveCancel($type, $args) {
|
function testDisableWithConsecutiveCancel($type, $args)
|
||||||
|
{
|
||||||
if ($type === "onSignal") {
|
if ($type === "onSignal") {
|
||||||
$this->testSignalCapability();
|
$this->testSignalCapability();
|
||||||
}
|
}
|
||||||
@ -221,7 +207,7 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->start(function(Driver $loop) use ($type, $args) {
|
$this->start(function(Driver $loop) use ($type, $args) {
|
||||||
$func = [$loop, $type];
|
$func = [$loop, $type];
|
||||||
$watcherId = \call_user_func_array($func, $args);
|
$watcherId = \call_user_func_array($func, $args);
|
||||||
$watcherId->disable($func);
|
$loop->disable($watcherId);
|
||||||
$loop->defer(function() use ($loop, $watcherId) {
|
$loop->defer(function() use ($loop, $watcherId) {
|
||||||
$loop->cancel($watcherId);
|
$loop->cancel($watcherId);
|
||||||
});
|
});
|
||||||
@ -231,7 +217,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
/**
|
/**
|
||||||
* @dataProvider provideRegistrationArgs
|
* @dataProvider provideRegistrationArgs
|
||||||
*/
|
*/
|
||||||
function testWatcherReferenceInfo($type, $args) {
|
function testWatcherReferenceInfo($type, $args)
|
||||||
|
{
|
||||||
if ($type === "onSignal") {
|
if ($type === "onSignal") {
|
||||||
$this->testSignalCapability();
|
$this->testSignalCapability();
|
||||||
}
|
}
|
||||||
@ -301,7 +288,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
/**
|
/**
|
||||||
* @dataProvider provideRegistrationArgs
|
* @dataProvider provideRegistrationArgs
|
||||||
*/
|
*/
|
||||||
function testWatcherRegistrationAndCancellationInfo($type, $args) {
|
function testWatcherRegistrationAndCancellationInfo($type, $args)
|
||||||
|
{
|
||||||
if ($type === "onSignal") {
|
if ($type === "onSignal") {
|
||||||
$this->testSignalCapability();
|
$this->testSignalCapability();
|
||||||
}
|
}
|
||||||
@ -357,24 +345,17 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$info = $loop->info();
|
$info = $loop->info();
|
||||||
$expected = ["enabled" => 0, "disabled" => 0];
|
$expected = ["enabled" => 0, "disabled" => 0];
|
||||||
$this->assertSame($expected, $info[$type]);
|
$this->assertSame($expected, $info[$type]);
|
||||||
|
|
||||||
// invoke watcher control ops again to ensure they succeed
|
|
||||||
$loop->cancel($watcherId);
|
|
||||||
$loop->disable($watcherId);
|
|
||||||
$loop->enable($watcherId);
|
|
||||||
$info = $loop->info();
|
|
||||||
$expected = ["enabled" => 0, "disabled" => 0];
|
|
||||||
$this->assertSame($expected, $info[$type]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider provideRegistrationArgs
|
* @dataProvider provideRegistrationArgs
|
||||||
*/
|
*/
|
||||||
function testNoMemoryLeak($type, $args) {
|
function testNoMemoryLeak($type, $args)
|
||||||
|
{
|
||||||
if ($type === "onSignal") {
|
if ($type === "onSignal") {
|
||||||
$this->testSignalCapability();
|
$this->testSignalCapability();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->start(function(Driver $loop) use ($type, $args) {
|
$this->start(function(Driver $loop) use ($type, $args) {
|
||||||
$initialMem = memory_get_usage();
|
$initialMem = memory_get_usage();
|
||||||
$cb = function ($runs) use ($loop, $type, $args) {
|
$cb = function ($runs) use ($loop, $type, $args) {
|
||||||
@ -395,13 +376,13 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
for ($watchers = [], $i = 0; $i < $runs; $i++) {
|
for ($watchers = [], $i = 0; $i < $runs; $i++) {
|
||||||
$watchers[] = \call_user_func_array($func, $args);
|
$watchers[] = \call_user_func_array($func, $args);
|
||||||
}
|
}
|
||||||
if ($type == "repeat") {
|
if ($type === "repeat") {
|
||||||
$loop->delay($msInterval = 7, function () use ($loop, $watchers) {
|
$loop->delay($msInterval = 7, function () use ($loop, $watchers) {
|
||||||
foreach ($watchers as $watcher) {
|
foreach ($watchers as $watcher) {
|
||||||
$loop->cancel($watcher);
|
$loop->cancel($watcher);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} elseif ($type != "defer" && $type != "delay") {
|
} elseif ($type !== "defer" && $type !== "delay") {
|
||||||
$loop->defer(function () use ($loop, $watchers) {
|
$loop->defer(function () use ($loop, $watchers) {
|
||||||
foreach ($watchers as $watcher) {
|
foreach ($watchers as $watcher) {
|
||||||
$loop->cancel($watcher);
|
$loop->cancel($watcher);
|
||||||
@ -409,7 +390,7 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
$loop->run();
|
$loop->run();
|
||||||
if ($type == "defer") {
|
if ($type === "defer") {
|
||||||
$loop->defer($fn = function ($watcherId, $i) use (&$fn, $loop) {
|
$loop->defer($fn = function ($watcherId, $i) use (&$fn, $loop) {
|
||||||
if ($i) {
|
if ($i) {
|
||||||
$loop->defer($fn, --$i);
|
$loop->defer($fn, --$i);
|
||||||
@ -417,7 +398,7 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
}, $runs);
|
}, $runs);
|
||||||
$loop->run();
|
$loop->run();
|
||||||
}
|
}
|
||||||
if ($type == "delay") {
|
if ($type === "delay") {
|
||||||
$loop->delay($msDelay = 0, $fn = function ($watcherId, $i) use (&$fn, $loop) {
|
$loop->delay($msDelay = 0, $fn = function ($watcherId, $i) use (&$fn, $loop) {
|
||||||
if ($i) {
|
if ($i) {
|
||||||
$loop->delay($msDelay = 0, $fn, --$i);
|
$loop->delay($msDelay = 0, $fn, --$i);
|
||||||
@ -425,7 +406,7 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
}, $runs);
|
}, $runs);
|
||||||
$loop->run();
|
$loop->run();
|
||||||
}
|
}
|
||||||
if ($type == "repeat") {
|
if ($type === "repeat") {
|
||||||
$loop->repeat($msDelay = 0, $fn = function ($watcherId, $i) use (&$fn, $loop) {
|
$loop->repeat($msDelay = 0, $fn = function ($watcherId, $i) use (&$fn, $loop) {
|
||||||
$loop->cancel($watcherId);
|
$loop->cancel($watcherId);
|
||||||
if ($i) {
|
if ($i) {
|
||||||
@ -434,7 +415,7 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
}, $runs);
|
}, $runs);
|
||||||
$loop->run();
|
$loop->run();
|
||||||
}
|
}
|
||||||
if ($type == "onWritable") {
|
if ($type === "onWritable") {
|
||||||
$loop->defer(function($watcherId, $runs) use ($loop) {
|
$loop->defer(function($watcherId, $runs) use ($loop) {
|
||||||
$fn = function ($watcherId, $socket, $i) use (&$fn, $loop) {
|
$fn = function ($watcherId, $socket, $i) use (&$fn, $loop) {
|
||||||
$loop->cancel($watcherId);
|
$loop->cancel($watcherId);
|
||||||
@ -454,7 +435,7 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
}, $runs + 1);
|
}, $runs + 1);
|
||||||
$loop->run();
|
$loop->run();
|
||||||
}
|
}
|
||||||
if ($type == "onSignal") {
|
if ($type === "onSignal") {
|
||||||
$watchers = [$loop->repeat(\SIGUSR1, $fn = function ($watcherId, $i) use (&$fn, $loop, &$watchers) {
|
$watchers = [$loop->repeat(\SIGUSR1, $fn = function ($watcherId, $i) use (&$fn, $loop, &$watchers) {
|
||||||
if ($i) {
|
if ($i) {
|
||||||
$watchers[] = $loop->onSignal(\SIGUSR1, $fn, --$i);
|
$watchers[] = $loop->onSignal(\SIGUSR1, $fn, --$i);
|
||||||
@ -473,27 +454,35 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$initialMem = memory_get_usage() - $closureMem;
|
$initialMem = memory_get_usage() - $closureMem;
|
||||||
$cb(10000);
|
$cb(10000);
|
||||||
unset($cb);
|
unset($cb);
|
||||||
|
|
||||||
gc_collect_cycles();
|
gc_collect_cycles();
|
||||||
$endMem = memory_get_usage();
|
$endMem = memory_get_usage();
|
||||||
|
|
||||||
/* this is allowing some memory usage due to runtime caches etc., but nothing actually leaking */
|
/* this is allowing some memory usage due to runtime caches etc., but nothing actually leaking */
|
||||||
$this->assertTrue($endMem - $initialMem < 40000); // 4 * 10000, as 4 is minimal sizeof(void *)
|
$this->assertLessThan(40000, $endMem - $initialMem); // 4 * 10000, as 4 is minimal sizeof(void *)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSuccessOnEnableNonexistentWatcher() {
|
/**
|
||||||
|
* @expectedException \LogicException
|
||||||
|
*/
|
||||||
|
function testSuccessOnEnableNonexistentWatcher()
|
||||||
|
{
|
||||||
$this->loop->enable("nonexistentWatcher");
|
$this->loop->enable("nonexistentWatcher");
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSuccessOnDisableNonexistentWatcher() {
|
function testSuccessOnDisableNonexistentWatcher()
|
||||||
|
{
|
||||||
$this->loop->disable("nonexistentWatcher");
|
$this->loop->disable("nonexistentWatcher");
|
||||||
}
|
}
|
||||||
|
|
||||||
function testSuccessOnCancelNonexistentWatcher() {
|
function testSuccessOnCancelNonexistentWatcher()
|
||||||
|
{
|
||||||
$this->loop->cancel("nonexistentWatcher");
|
$this->loop->cancel("nonexistentWatcher");
|
||||||
}
|
}
|
||||||
|
|
||||||
function testEnablingWatcherAllowsSubsequentInvocation() {
|
function testEnablingWatcherAllowsSubsequentInvocation()
|
||||||
|
{
|
||||||
$loop = $this->loop;
|
$loop = $this->loop;
|
||||||
$increment = 0;
|
$increment = 0;
|
||||||
$watcherId = $loop->defer(function() use (&$increment) {
|
$watcherId = $loop->defer(function() use (&$increment) {
|
||||||
@ -509,24 +498,28 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertEquals(1, $increment);
|
$this->assertEquals(1, $increment);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testUnresolvedEventsAreReenabledOnRunFollowingPreviousStop() {
|
function testUnresolvedEventsAreReenabledOnRunFollowingPreviousStop()
|
||||||
|
{
|
||||||
$increment = 0;
|
$increment = 0;
|
||||||
$this->start(function(Driver $loop) use (&$increment) {
|
$this->start(function(Driver $loop) use (&$increment) {
|
||||||
$loop->delay($msDelay = 5, function () use ($loop, &$increment) {
|
|
||||||
$increment++;
|
|
||||||
$loop->stop();
|
|
||||||
});
|
|
||||||
|
|
||||||
$loop->defer([$loop, "stop"]);
|
$loop->defer([$loop, "stop"]);
|
||||||
$loop->run();
|
$loop->run();
|
||||||
|
|
||||||
|
$loop->defer(function () use (&$increment, $loop) {
|
||||||
|
$loop->delay($msDelay = 100, function () use ($loop, &$increment) {
|
||||||
|
$increment++;
|
||||||
|
$loop->stop();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
$this->assertEquals(0, $increment);
|
$this->assertEquals(0, $increment);
|
||||||
\usleep(5000);
|
\usleep(5000);
|
||||||
});
|
});
|
||||||
$this->assertEquals(1, $increment);
|
$this->assertEquals(1, $increment);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testTimerWatcherParameterOrder() {
|
function testTimerWatcherParameterOrder()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$counter = 0;
|
$counter = 0;
|
||||||
$loop->defer(function ($watcherId) use ($loop, &$counter) {
|
$loop->defer(function ($watcherId) use ($loop, &$counter) {
|
||||||
@ -551,8 +544,9 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testStreamWatcherParameterOrder() {
|
function testStreamWatcherParameterOrder()
|
||||||
$this->start(function(Driver $loop) {
|
{
|
||||||
|
$this->start(function(Driver $loop) use (&$invoked) {
|
||||||
$invoked = 0;
|
$invoked = 0;
|
||||||
$loop->onWritable(STDOUT, function ($watcherId, $stream) use ($loop, &$invoked) {
|
$loop->onWritable(STDOUT, function ($watcherId, $stream) use ($loop, &$invoked) {
|
||||||
$this->assertInternalType("string", $watcherId);
|
$this->assertInternalType("string", $watcherId);
|
||||||
@ -560,11 +554,12 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$invoked++;
|
$invoked++;
|
||||||
$loop->cancel($watcherId);
|
$loop->cancel($watcherId);
|
||||||
});
|
});
|
||||||
$this->assertSame(1, $invoked);
|
|
||||||
});
|
});
|
||||||
|
$this->assertSame(1, $invoked);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testDisablingWatcherPreventsSubsequentInvocation() {
|
function testDisablingWatcherPreventsSubsequentInvocation()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$increment = 0;
|
$increment = 0;
|
||||||
$watcherId = $loop->defer(function () use (&$increment) {
|
$watcherId = $loop->defer(function () use (&$increment) {
|
||||||
@ -578,7 +573,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testImmediateExecution() {
|
function testImmediateExecution()
|
||||||
|
{
|
||||||
$loop = $this->loop;
|
$loop = $this->loop;
|
||||||
$increment = 0;
|
$increment = 0;
|
||||||
$this->start(function(Driver $loop) use (&$increment) {
|
$this->start(function(Driver $loop) use (&$increment) {
|
||||||
@ -590,7 +586,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertEquals(1, $increment);
|
$this->assertEquals(1, $increment);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testImmediatelyCallbacksDoNotRecurseInSameTick() {
|
function testImmediatelyCallbacksDoNotRecurseInSameTick()
|
||||||
|
{
|
||||||
$increment = 0;
|
$increment = 0;
|
||||||
$this->start(function(Driver $loop) use (&$increment) {
|
$this->start(function(Driver $loop) use (&$increment) {
|
||||||
$loop->defer(function () use ($loop, &$increment) {
|
$loop->defer(function () use ($loop, &$increment) {
|
||||||
@ -604,7 +601,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertEquals(1, $increment);
|
$this->assertEquals(1, $increment);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testRunExecutesEventsUntilExplicitlyStopped() {
|
function testRunExecutesEventsUntilExplicitlyStopped()
|
||||||
|
{
|
||||||
$increment = 0;
|
$increment = 0;
|
||||||
$this->start(function(Driver $loop) use (&$increment) {
|
$this->start(function(Driver $loop) use (&$increment) {
|
||||||
$loop->repeat($msInterval = 5, function ($watcherId) use ($loop, &$increment) {
|
$loop->repeat($msInterval = 5, function ($watcherId) use ($loop, &$increment) {
|
||||||
@ -621,7 +619,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
* @expectedException \Exception
|
* @expectedException \Exception
|
||||||
* @expectedExceptionMessage loop error
|
* @expectedExceptionMessage loop error
|
||||||
*/
|
*/
|
||||||
function testLoopAllowsExceptionToBubbleUpDuringStart() {
|
function testLoopAllowsExceptionToBubbleUpDuringStart()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$loop->defer(function() {
|
$loop->defer(function() {
|
||||||
throw new \Exception("loop error");
|
throw new \Exception("loop error");
|
||||||
@ -633,15 +632,17 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
* @expectedException \RuntimeException
|
* @expectedException \RuntimeException
|
||||||
* @expectedExceptionMessage test
|
* @expectedExceptionMessage test
|
||||||
*/
|
*/
|
||||||
function testLoopAllowsExceptionToBubbleUpFromRepeatingAlarmDuringStart() {
|
function testLoopAllowsExceptionToBubbleUpFromRepeatingAlarmDuringStart()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$loop->repeat($msInterval = 0, function () {
|
$loop->repeat($msInterval = 1, function () {
|
||||||
throw new \RuntimeException("test");
|
throw new \RuntimeException("test");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testErrorHandlerCapturesUncaughtException() {
|
function testErrorHandlerCapturesUncaughtException()
|
||||||
|
{
|
||||||
$msg = "";
|
$msg = "";
|
||||||
$this->loop->setErrorHandler(function(\Exception $error) use (&$msg) {
|
$this->loop->setErrorHandler(function(\Exception $error) use (&$msg) {
|
||||||
$msg = $error->getMessage();
|
$msg = $error->getMessage();
|
||||||
@ -650,9 +651,6 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$loop->defer(function() {
|
$loop->defer(function() {
|
||||||
throw new \Exception("loop error");
|
throw new \Exception("loop error");
|
||||||
});
|
});
|
||||||
$loop->defer(function() {
|
|
||||||
$this->fail("No other handlers must be run after a loop error");
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
$this->assertSame("loop error", $msg);
|
$this->assertSame("loop error", $msg);
|
||||||
}
|
}
|
||||||
@ -661,7 +659,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
* @expectedException \Exception
|
* @expectedException \Exception
|
||||||
* @expectedExceptionMessage errorception
|
* @expectedExceptionMessage errorception
|
||||||
*/
|
*/
|
||||||
function testOnErrorFailure() {
|
function testOnErrorFailure()
|
||||||
|
{
|
||||||
$this->loop->setErrorHandler(function() {
|
$this->loop->setErrorHandler(function() {
|
||||||
throw new \Exception("errorception");
|
throw new \Exception("errorception");
|
||||||
});
|
});
|
||||||
@ -676,7 +675,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
* @expectedException \RuntimeException
|
* @expectedException \RuntimeException
|
||||||
* @expectedExceptionMessage test
|
* @expectedExceptionMessage test
|
||||||
*/
|
*/
|
||||||
function testLoopException() {
|
function testLoopException()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$loop->defer(function() use ($loop) {
|
$loop->defer(function() use ($loop) {
|
||||||
// force next tick, outside of primary startup tick
|
// force next tick, outside of primary startup tick
|
||||||
@ -690,7 +690,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
/**
|
/**
|
||||||
* @depends testSignalCapability
|
* @depends testSignalCapability
|
||||||
*/
|
*/
|
||||||
function testOnSignalWatcher() {
|
function testOnSignalWatcher()
|
||||||
|
{
|
||||||
if (!\extension_loaded("posix")) {
|
if (!\extension_loaded("posix")) {
|
||||||
$this->markTestSkipped("ext/posix required to test signal handlers");
|
$this->markTestSkipped("ext/posix required to test signal handlers");
|
||||||
}
|
}
|
||||||
@ -711,7 +712,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testInitiallyDisabledOnSignalWatcher() {
|
function testInitiallyDisabledOnSignalWatcher()
|
||||||
|
{
|
||||||
if (!\extension_loaded("posix")) {
|
if (!\extension_loaded("posix")) {
|
||||||
$this->markTestSkipped("ext/posix required to test signal handlers");
|
$this->markTestSkipped("ext/posix required to test signal handlers");
|
||||||
}
|
}
|
||||||
@ -732,7 +734,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testCancelRemovesWatcher() {
|
function testCancelRemovesWatcher()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$watcherId = $loop->delay($msDelay = 10, function () {
|
$watcherId = $loop->delay($msDelay = 10, function () {
|
||||||
$this->fail('Watcher was not cancelled as expected');
|
$this->fail('Watcher was not cancelled as expected');
|
||||||
@ -745,9 +748,10 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testOnWritableWatcher() {
|
function testOnWritableWatcher()
|
||||||
|
{
|
||||||
$flag = false;
|
$flag = false;
|
||||||
$this->start(function(Driver $loop) use ($flag) {
|
$this->start(function(Driver $loop) use (&$flag) {
|
||||||
$loop->onWritable(STDOUT, function () use ($loop, &$flag) {
|
$loop->onWritable(STDOUT, function () use ($loop, &$flag) {
|
||||||
$flag = true;
|
$flag = true;
|
||||||
$loop->stop();
|
$loop->stop();
|
||||||
@ -757,7 +761,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertTrue($flag);
|
$this->assertTrue($flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testInitiallyDisabledWriteWatcher() {
|
function testInitiallyDisabledWriteWatcher()
|
||||||
|
{
|
||||||
$increment = 0;
|
$increment = 0;
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$watcherId = $loop->onWritable(STDOUT, function () use (&$increment) {
|
$watcherId = $loop->onWritable(STDOUT, function () use (&$increment) {
|
||||||
@ -769,7 +774,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertSame(0, $increment);
|
$this->assertSame(0, $increment);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testInitiallyDisabledWriteWatcherIsTriggeredOnceEnabled() {
|
function testInitiallyDisabledWriteWatcherIsTriggeredOnceEnabled()
|
||||||
|
{
|
||||||
$this->expectOutputString("12");
|
$this->expectOutputString("12");
|
||||||
$this->start(function (Driver $loop) {
|
$this->start(function (Driver $loop) {
|
||||||
$watcherId = $loop->onWritable(STDOUT, function () use ($loop) {
|
$watcherId = $loop->onWritable(STDOUT, function () use ($loop) {
|
||||||
@ -787,7 +793,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
/**
|
/**
|
||||||
* @expectedException \RuntimeException
|
* @expectedException \RuntimeException
|
||||||
*/
|
*/
|
||||||
function testStreamWatcherDoesntSwallowExceptions() {
|
function testStreamWatcherDoesntSwallowExceptions()
|
||||||
|
{
|
||||||
$this->start(function(Driver $loop) {
|
$this->start(function(Driver $loop) {
|
||||||
$loop->onWritable(STDOUT, function () {
|
$loop->onWritable(STDOUT, function () {
|
||||||
throw new \RuntimeException;
|
throw new \RuntimeException;
|
||||||
@ -796,10 +803,11 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function testReactorRunsUntilNoWatchersRemain() {
|
function testReactorRunsUntilNoWatchersRemain()
|
||||||
|
{
|
||||||
$var1 = $var2 = 0;
|
$var1 = $var2 = 0;
|
||||||
$this->start(function(Driver $loop) use (&$var1, &$var2) {
|
$this->start(function(Driver $loop) use (&$var1, &$var2) {
|
||||||
$loop->repeat($msDelay = 0, function ($watcherId) use ($loop, &$var1) {
|
$loop->repeat($msDelay = 1, function ($watcherId) use ($loop, &$var1) {
|
||||||
if (++$var1 === 3) {
|
if (++$var1 === 3) {
|
||||||
$loop->cancel($watcherId);
|
$loop->cancel($watcherId);
|
||||||
}
|
}
|
||||||
@ -815,11 +823,12 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertSame(4, $var2);
|
$this->assertSame(4, $var2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testReactorRunsUntilNoWatchersRemainWhenStartedDeferred() {
|
function testReactorRunsUntilNoWatchersRemainWhenStartedDeferred()
|
||||||
|
{
|
||||||
$var1 = $var2 = 0;
|
$var1 = $var2 = 0;
|
||||||
$this->start(function(Driver $loop) use (&$var1, &$var2) {
|
$this->start(function(Driver $loop) use (&$var1, &$var2) {
|
||||||
$loop->defer(function() use ($loop, &$var1, &$var2) {
|
$loop->defer(function() use ($loop, &$var1, &$var2) {
|
||||||
$loop->repeat($msDelay = 0, function ($watcherId) use ($loop, &$var1) {
|
$loop->repeat($msDelay = 1, function ($watcherId) use ($loop, &$var1) {
|
||||||
if (++$var1 === 3) {
|
if (++$var1 === 3) {
|
||||||
$loop->cancel($watcherId);
|
$loop->cancel($watcherId);
|
||||||
}
|
}
|
||||||
@ -836,7 +845,8 @@ abstract class Test extends \PHPUnit_Framework_TestCase {
|
|||||||
$this->assertSame(4, $var2);
|
$this->assertSame(4, $var2);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testOptionalCallbackDataPassedOnInvocation() {
|
function testOptionalCallbackDataPassedOnInvocation()
|
||||||
|
{
|
||||||
$callbackData = new \StdClass;
|
$callbackData = new \StdClass;
|
||||||
|
|
||||||
$this->start(function(Driver $loop) use ($callbackData) {
|
$this->start(function(Driver $loop) use ($callbackData) {
|
||||||
|
Loading…
Reference in New Issue
Block a user