Skip to content

Commit d6c299a

Browse files
committed
Update according to latest spec changes + better signal tests
1 parent d50de88 commit d6c299a

File tree

1 file changed

+85
-40
lines changed

1 file changed

+85
-40
lines changed

src/Test.php

Lines changed: 85 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ function testNoMemoryLeak($type, $args)
464464

465465
function testExecutionOrderGuarantees()
466466
{
467-
$this->expectOutputString("01 02 03 04 05 05 10 11 12 13 13 20 21 22 23 ");
467+
$this->expectOutputString("01 02 03 04 ".str_repeat("05 ", 8)."10 11 12 ".str_repeat("13 ", 4)."20 21 22 23 24 25 30 31");
468468
$this->start(function(Driver $loop) use (&$ticks) {
469469
$f = function() use ($loop) {
470470
$args = func_get_args();
@@ -476,31 +476,20 @@ function testExecutionOrderGuarantees()
476476
echo array_shift($args) . array_shift($args), " ";
477477
};
478478
};
479-
$dep = function($i, $fn) use ($loop, &$max, &$cur) {
480-
$max || $max = new \SplObjectStorage;
481-
$cur || $cur = new \SplObjectStorage;
482-
$max[$fn] = max(isset($max[$fn]) ? $max[$fn] : 0, $i);
483-
$cur[$fn] = 0;
484-
return function($watcherId) use ($loop, $i, $fn, &$max, &$cur) {
485-
$this->assertSame($i, $cur[$fn]);
486-
if ($cur[$fn] == $max[$fn]) {
487-
$fn($watcherId);
488-
} else {
489-
$loop->cancel($watcherId);
490-
}
491-
$cur[$fn] = $cur[$fn] + 1;
492-
};
493-
};
494479

495-
$loop->onWritable(STDIN, $dep(0, $writ = $f(0, 5)));
496-
$writ1 = $loop->onWritable(STDIN, $dep(1, $writ));
497-
$writ2 = $loop->onWritable(STDIN, $dep(3, $writ));
480+
$loop->onWritable(STDIN, $f(0, 5));
481+
$writ1 = $loop->onWritable(STDIN, $f(0, 5));
482+
$writ2 = $loop->onWritable(STDIN, $f(0, 5));
498483

499-
$loop->delay($msDelay = 0, $dep(0, $del = $f(0, 5)));
500-
$del1 = $loop->delay($msDelay = 0, $dep(1, $del));
501-
$del2 = $loop->delay($msDelay = 0, $dep(3, $del));
484+
$loop->delay($msDelay = 0, $f(0, 5));
485+
$del1 = $loop->delay($msDelay = 0, $f(0, 5));
486+
$del2 = $loop->delay($msDelay = 0, $f(0, 5));
502487
$del3 = $loop->delay($msDelay = 0, $f());
503-
$del4 = $loop->delay($msDelay = 0, $dep(1, $defdel = $f(1, 3)));
488+
$del4 = $loop->delay($msDelay = 0, $f(1, 3));
489+
$del5 = $loop->delay($msDelay = 0, $f(2, 0));
490+
$loop->defer(function() use ($loop, $del5) {
491+
$loop->disable($del5);
492+
});
504493
$loop->cancel($del3);
505494
$loop->disable($del1);
506495
$loop->disable($del2);
@@ -510,29 +499,31 @@ function testExecutionOrderGuarantees()
510499
$loop->disable($writ1);
511500
$loop->disable($writ2);
512501
$loop->enable($writ1);
513-
$writ4 = $loop->onWritable(STDIN, $dep(1, $defwrit = $f(1, 3)));
514-
$loop->onWritable(STDIN, $dep(2, $writ));
502+
$writ4 = $loop->onWritable(STDIN, $f(1, 3));
503+
$loop->onWritable(STDIN, $f(0, 5));
515504
$loop->enable($writ2);
516505
$loop->disable($writ4);
517-
$loop->defer(function() use ($loop, $writ4, $dep, $defwrit) {
518-
$loop->onWritable(STDIN, $dep(0, $defwrit));
506+
$loop->defer(function() use ($loop, $writ4, $f) {
519507
$loop->enable($writ4);
508+
$loop->onWritable(STDIN, $f(1, 3));
520509
});
521510

522511
$loop->enable($del1);
523-
$loop->delay($msDelay = 0, $dep(2, $del));
512+
$loop->delay($msDelay = 0, $f(0, 5));
524513
$loop->enable($del2);
525514
$loop->disable($del4);
526-
$loop->defer(function() use ($loop, $del4, $dep, $defdel) {
527-
$loop->onWritable(STDIN, $dep(0, $defdel));
515+
$loop->defer(function() use ($loop, $del4, $f) {
528516
$loop->enable($del4);
517+
$loop->onWritable(STDIN, $f(1, 3));
529518
});
530519

531-
$loop->delay($msDelay = 5, $f(2, 0));
532-
$loop->repeat($msDelay = 5, $f(2, 1));
533-
$rep1 = $loop->repeat($msDelay = 5, $f(2, 3));
520+
$loop->delay($msDelay = 15, $f(3, 1));
521+
$loop->delay($msDelay = 6, $f(2, 5));
522+
$loop->delay($msDelay = 5, $f(2, 1));
523+
$loop->repeat($msDelay = 5, $f(2, 2));
524+
$rep1 = $loop->repeat($msDelay = 5, $f(2, 4));
534525
$loop->disable($rep1);
535-
$loop->delay($msDelay = 5, $f(2, 2));
526+
$loop->delay($msDelay = 5, $f(2, 3));
536527
$loop->enable($rep1);
537528

538529
$loop->defer($f(0, 1));
@@ -543,12 +534,21 @@ function testExecutionOrderGuarantees()
543534
$loop->disable($def1);
544535
$loop->cancel($def3);
545536
$loop->enable($def1);
546-
$loop->defer(function() use ($loop, $def2, $del4, $f) {
537+
$loop->defer(function() use ($loop, $def2, $del4, $del5, $f) {
547538
$tick = $f(0, 4);
548539
$tick("invalid");
549540
$loop->defer($f(1, 0));
550541
$loop->enable($def2);
551542
$loop->defer($f(1, 2));
543+
$loop->defer(function() use ($loop, $del5, $f) {
544+
$loop->enable($del5);
545+
$loop->defer(function() use ($loop, $f) {
546+
usleep(7000); // to have $msDelay == 5 and $msDelay == 6 run at the same tick (but not $msDelay == 15)
547+
$loop->defer(function () use ($loop, $f) {
548+
$loop->defer($f(3, 0));
549+
});
550+
});
551+
});
552552
});
553553
$loop->disable($def2);
554554
});
@@ -920,16 +920,61 @@ function testInitiallyDisabledOnSignalWatcher()
920920

921921
$this->expectOutputString("caught SIGUSR1");
922922
$this->start(function(Driver $loop) {
923-
$sigWatcherId = $loop->onSignal(SIGUSR1, function() use ($loop) {
923+
$stop = $loop->delay($msDelay = 10, function() use ($loop) {
924+
echo "ERROR: manual stop";
925+
$loop->stop();
926+
});
927+
$watcherId = $loop->onSignal(SIGUSR1, function($watcherId) use ($loop, $stop) {
924928
echo "caught SIGUSR1";
929+
$loop->disable($stop);
930+
$loop->disable($watcherId);
931+
});
932+
$loop->disable($watcherId);
933+
934+
$loop->delay($msDelay = 1, function() use ($loop, $watcherId) {
935+
$loop->enable($watcherId);
936+
$loop->delay($msDelay = 1, function() {
937+
\posix_kill(\getmypid(), \SIGUSR1);
938+
});
939+
});
940+
});
941+
}
942+
943+
/** @depends testSignalCapability */
944+
function testNestedLoopSignalDispatch()
945+
{
946+
if (!\extension_loaded("posix")) {
947+
$this->markTestSkipped("ext/posix required to test signal handlers");
948+
}
949+
950+
$this->expectOutputString("inner SIGUSR1\nouter SIGUSR1\n");
951+
$this->start(function(Driver $loop) {
952+
$loop->delay($msDelay = 30, function() use ($loop) {
953+
$loop->stop();
954+
});
955+
$loop->onSignal(SIGUSR1, function () use ($loop) {
956+
echo "outer SIGUSR1\n";
925957
$loop->stop();
926-
}, $options = ["enable" => false]);
958+
});
927959

928-
$loop->delay($msDelay = 10, function() use ($loop, $sigWatcherId) {
929-
$loop->enable($sigWatcherId);
930-
$loop->delay($msDelay = 10, function() use ($sigWatcherId) {
960+
$loop->delay($msDelay = 1, function () {
961+
$loop = $this->getFactory()->create();
962+
$stop = $loop->delay($msDelay = 10, function() use ($loop) {
963+
echo "ERROR: manual stop";
964+
$loop->stop();
965+
});
966+
$loop->onSignal(SIGUSR1, function ($watcherId) use ($loop, $stop) {
967+
echo "inner SIGUSR1\n";
968+
$loop->cancel($stop);
969+
$loop->cancel($watcherId);
970+
});
971+
$loop->delay($msDelay = 1, function () {
931972
\posix_kill(\getmypid(), \SIGUSR1);
932973
});
974+
$loop->run();
975+
});
976+
$loop->delay($msDelay = 20, function () {
977+
\posix_kill(\getmypid(), \SIGUSR1);
933978
});
934979
});
935980
}

0 commit comments

Comments
 (0)