public function testTooManyViolentDeath() { $numberOfFailures = 12; $inAmountOfTime = 60; GracefulDeath::around(function () { $this->raiseFatalError(); })->liveForever()->avoidFutileMedicalCare($numberOfFailures, $inAmountOfTime)->reanimationPolicy($this->willBeCalled($this->exactly($numberOfFailures - 1)))->afterNaturalDeath($this->willBeCalled($this->never()))->afterViolentDeath($this->willBeCalled($this->once()))->run(); }
public function testChildStandardOutputIsCapturedAfterNaturalDeath() { GracefulDeath::around(function () { file_put_contents('php://stdout', "This is a notice"); $this->doSomethingUnharmful(); })->afterNaturalDeath(function ($status, $stdout) { $this->assertEquals("This is a notice", $stdout); })->doNotEchoOutput()->run(); }
/** * @expectedException InvalidArgumentException */ public function testBadLastAct() { GracefulDeath::around(function () { })->afterNaturalDeath(true); }
<?php require __DIR__ . "/../../vendor/autoload.php"; $options = getopt('', ['where:']); ini_set('error_log', $options['where']); ini_set('log_errors', 1); GracefulDeath::around(function () { trigger_error('ERROR'); })->run();
<?php require __DIR__ . "/../vendor/autoload.php"; // The output will be: // // Hey! You know, it's not cool to overflow the stack! GracefulDeath::around(function () { // Avoid to crash for memory exhaustion ini_set('memory_limit', -1); countToInfinite(); })->afterViolentDeath("Hey! You know, it's not cool to overflow the stack!\n")->run(); function countToInfinite($counter = 0) { return countToInfinite($counter + 1); }
<?php require __DIR__ . "/../../vendor/autoload.php"; GracefulDeath::around(function ($life) { while (!$life->askedToStop()) { usleep(10000); } })->doNotCaptureOutput()->catchSignals([SIGTERM, SIGQUIT, SIGINT])->afterNaturalDeath("Bye Bye")->run();
<?php require __DIR__ . "/../vendor/autoload.php"; // NOTE: To run this you need to have installed violent-death // see https://github.com/gabrielelana/violent-death // Sorry but I don't want to have an explicit dependency // with violent-death (wich is not so easy to install) only // to run this example // The output will be: // // Segmentation fault in 3... 2... 1... // You didn't notice but it happend! ;-) GracefulDeath::around(function () { echo "Segmentation fault in 3... 2... 1...\n"; die_violently(); })->afterViolentDeath("You didn't notice but it happend! ;-)\n")->run();
<?php require __DIR__ . "/../../vendor/autoload.php"; $options = getopt('', ['what:']); GracefulDeath::around(function () use($options) { file_put_contents('php://stderr', $options['what']); })->run();
<?php require __DIR__ . "/../vendor/autoload.php"; // The output will be: // // CTRL-C to terminate // I will live forever!!! // I will live forever!!! // I will live forever!!! // ... // Maybe not... :-( echo "CTRL-C to terminate\n"; GracefulDeath::around(function ($life) { while (!$life->askedToStop()) { echo "I will live forever!!!\n"; // Let's pretend to do something useful :-) sleep(1); } })->doNotCaptureOutput()->catchSignals([SIGTERM, SIGQUIT, SIGINT])->afterNaturalDeath("Maybe not... :-(\n")->run();
// [17497]: it's using 10.46(mb) of memory // [17497]: memory limit reached! respawn // [17554]: it's using 475.17(kb) of memory // [17554]: it's using 1.46(mb) of memory // [17554]: it's using 2.46(mb) of memory // [17554]: it's using 3.46(mb) of memory // [17554]: it's using 4.46(mb) of memory // [17554]: it's using 5.47(mb) of memory // [17554]: it's using 6.47(mb) of memory // ^C echo "CTRL-C to terminate\n"; GracefulDeath::around(function ($life) { while (!$life->askedToStop()) { $usedMemory = memory_get_usage(); printf("[%d]: it's using %s of memory\n", posix_getpid(), format($usedMemory)); if ($usedMemory > 10485760) { // 10MB printf("[%d]: memory limit reached! respawn\n", posix_getpid()); exit(5); } // Let's seize a 1MB of memory $aLotOfMemory[] = str_repeat('*', 1048576); usleep(1000 * 200); } })->doNotCaptureOutput()->catchSignals([SIGTERM, SIGQUIT, SIGINT])->reanimationPolicy(true)->run(); function format($size) { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); $power = $size > 0 ? floor(log($size, 1024)) : 0; return number_format($size / pow(1024, $power), 2, '.', ',') . '(' . $units[$power] . ')'; }
<?php require __DIR__ . "/../vendor/autoload.php"; // The output will be: // // Yes, I can ;-) GracefulDeath::around(function () { try { // Avoid to print the error in order to have clean output, don't try this at home :-) error_reporting(E_ALL ^ E_ERROR); // Creating an instance of an unknown class will cause a fatal error new UnknownClass(); } catch (Exception $e) { // A fatal error is uncatchable echo "You cannot catch this, AHAHAHAHA!!!\n"; } })->afterViolentDeath("Yes, I can ;-)\n")->run();
/** * @expectedException InvalidArgumentException */ public function testBadReanimationPolicy() { GracefulDeath::around(function () { })->reanimationPolicy('Strings are not convertible to a reanimation policy'); }
<?php require __DIR__ . "/../vendor/autoload.php"; // The output will be: // // I will live forever!!! // I will live forever!!! // I will live forever!!! // ... // I will live forever!!! // I will live forever!!! // Maybe not... :-( $startedAt = time(); GracefulDeath::around(function () { echo "I will live forever!!!\n"; // Let's pretend to do something useful :-) usleep(50000); // Avoid to print the error in order to have clean output, don't try this at home :-) error_reporting(E_ALL ^ E_ERROR); // Creating an instance of an unknown class will cause a fatal error new UnknownClass(); })->reanimationPolicy(function ($status, $attempts, $output) use($startedAt) { return time() - $startedAt < 5; })->afterViolentDeath("Maybe not... :-(\n")->run();
<?php require __DIR__ . "/../../vendor/autoload.php"; GracefulDeath::around(function () { echo 'THIS SHOULD NOT BE PRINTED'; })->doNotEchoOutput()->run();