diff --git a/src/Arara/Process/Child.php b/src/Arara/Process/Child.php index 784985b..3bb783d 100644 --- a/src/Arara/Process/Child.php +++ b/src/Arara/Process/Child.php @@ -106,18 +106,11 @@ public function kill() * * @return void */ - protected function setTimeoutHandler(array $context) + protected function setHandlerAlarm(array $context) { - $action = $this->action; - $control = $this->control; - $control->signal()->handle('alarm', function () use ($action, $control, $context) { - // @codeCoverageIgnoreStart - $context['finishTime'] = time(); - $action->trigger(Action::EVENT_TIMEOUT, $control, $context); - $control->quit(3); - // @codeCoverageIgnoreEnd - }); - $control->signal()->alarm($this->timeout); + $handler = new Handler\SignalAlarm($this->control, $this->action, $context); + $this->control->signal()->handle('alarm', $handler); + $this->control->signal()->alarm($this->timeout); } /** @@ -125,14 +118,9 @@ protected function setTimeoutHandler(array $context) * * @return void */ - protected function setPhpErrorHandler() + protected function setHandlerErrorException() { - set_error_handler( - function ($severity, $message, $filename, $line) { - throw new ErrorException($message, 0, $severity, $filename, $line); - }, - E_ALL & ~E_NOTICE - ); + set_error_handler(new Handler\ErrorException(), E_ALL & ~E_NOTICE); } /** @@ -196,12 +184,12 @@ public function start() $context = array( 'processId' => $this->processId, - 'startTime' => time(), 'timeout' => $this->timeout, + 'startTime' => time(), ); - $this->setTimeoutHandler($context); - $this->setPhpErrorHandler(); + $this->setHandlerAlarm($context); + $this->setHandlerErrorException(); $this->run($context); restore_error_handler(); } diff --git a/src/Arara/Process/Control.php b/src/Arara/Process/Control.php index 26c65a5..0f659c4 100644 --- a/src/Arara/Process/Control.php +++ b/src/Arara/Process/Control.php @@ -13,10 +13,10 @@ public function __construct() { $this->info = new Control\Info(); $this->signal = new Control\Signal(); - $this->signal->handle('child', new Control\Signal\ChildHandler($this)); - $this->signal->handle('interrupt', new Control\Signal\InterruptHandler($this)); - $this->signal->handle('quit', new Control\Signal\QuitHandler($this)); - $this->signal->handle('terminate', new Control\Signal\TerminateHandler($this)); + $this->signal->handle('child', new Handler\SignalChild($this)); + $this->signal->handle('interrupt', new Handler\SignalInterrupt($this)); + $this->signal->handle('quit', new Handler\SignalQuit($this)); + $this->signal->handle('terminate', new Handler\SignalTerminate($this)); } /** diff --git a/src/Arara/Process/Handler/ErrorException.php b/src/Arara/Process/Handler/ErrorException.php new file mode 100644 index 0000000..1c5b623 --- /dev/null +++ b/src/Arara/Process/Handler/ErrorException.php @@ -0,0 +1,11 @@ +action = $action; + $this->context = $context; + + parent::__construct($control); + } + + public function __invoke($signal) + { + $this->context['finishTime'] = time(); + $this->action->trigger(Action::EVENT_TIMEOUT, $this->control, $this->context); + $this->control->quit(3); + } +} diff --git a/src/Arara/Process/Control/Signal/ChildHandler.php b/src/Arara/Process/Handler/SignalChild.php similarity index 70% rename from src/Arara/Process/Control/Signal/ChildHandler.php rename to src/Arara/Process/Handler/SignalChild.php index 03100a5..c9c2a2b 100644 --- a/src/Arara/Process/Control/Signal/ChildHandler.php +++ b/src/Arara/Process/Handler/SignalChild.php @@ -1,8 +1,8 @@ getMockForAbstractClass('Arara\Process\Control\Signal\AbstractHandler', array($control)); + $handler = $this->getMockForAbstractClass('Arara\Process\Handler\SignalAbstract', array($control)); $this->assertAttributeSame($control, 'control', $handler); } diff --git a/tests/Arara/Process/Handler/SignalAlarmTest.php b/tests/Arara/Process/Handler/SignalAlarmTest.php new file mode 100644 index 0000000..026893f --- /dev/null +++ b/tests/Arara/Process/Handler/SignalAlarmTest.php @@ -0,0 +1,59 @@ +getMockBuilder('Arara\Process\Control') + ->setMethods(array('quit')) + ->getMock(); + + $control + ->expects($this->once()) + ->method('quit') + ->with(3); + + $action = $this + ->getMockBuilder('Arara\Process\Action\Action') + ->setMethods(array('execute', 'trigger')) + ->getMock(); + + $action + ->expects($this->once()) + ->method('trigger') + ->with(Action::EVENT_TIMEOUT, $control, array('finishTime' => self::TIMESTAMP)); + + $handler = new SignalAlarm($control, $action, array()); + $handler(SIGALRM); + } +} diff --git a/tests/Arara/Process/Control/Signal/ChildHandlerTest.php b/tests/Arara/Process/Handler/SignalChildTest.php similarity index 79% rename from tests/Arara/Process/Control/Signal/ChildHandlerTest.php rename to tests/Arara/Process/Handler/SignalChildTest.php index bfc47d5..d43a7ed 100644 --- a/tests/Arara/Process/Control/Signal/ChildHandlerTest.php +++ b/tests/Arara/Process/Handler/SignalChildTest.php @@ -1,11 +1,11 @@ with(0, (WNOHANG | WUNTRACED)) ->will($this->returnValue(0)); - $handler = new ChildHandler($control); + $handler = new SignalChild($control); $handler(SIGCHLD); } @@ -37,7 +37,7 @@ public function testShouldTryToWaitUntilChildrenFinish() ->with(0, (WNOHANG | WUNTRACED)) ->will($this->onConsecutiveCalls(999, 0)); - $handler = new ChildHandler($control); + $handler = new SignalChild($control); $handler(SIGCHLD); } } diff --git a/tests/Arara/Process/Control/Signal/InterruptHandlerTest.php b/tests/Arara/Process/Handler/SignalInterruptTest.php similarity index 64% rename from tests/Arara/Process/Control/Signal/InterruptHandlerTest.php rename to tests/Arara/Process/Handler/SignalInterruptTest.php index f7271d0..fb9ed29 100644 --- a/tests/Arara/Process/Control/Signal/InterruptHandlerTest.php +++ b/tests/Arara/Process/Handler/SignalInterruptTest.php @@ -1,11 +1,11 @@ method('quit') ->with(3); - $handler = new InterruptHandler($control); + $handler = new SignalInterrupt($control); $handler(SIGINT); } } diff --git a/tests/Arara/Process/Control/Signal/QuitHandlerTest.php b/tests/Arara/Process/Handler/SignalQuitTest.php similarity index 66% rename from tests/Arara/Process/Control/Signal/QuitHandlerTest.php rename to tests/Arara/Process/Handler/SignalQuitTest.php index 361eba7..35b2d63 100644 --- a/tests/Arara/Process/Control/Signal/QuitHandlerTest.php +++ b/tests/Arara/Process/Handler/SignalQuitTest.php @@ -1,11 +1,11 @@ method('quit') ->with(4); - $handler = new QuitHandler($control); + $handler = new SignalQuit($control); $handler(SIGQUIT); } } diff --git a/tests/Arara/Process/Control/Signal/TerminateHandlerTest.php b/tests/Arara/Process/Handler/SignalTerminateTest.php similarity index 64% rename from tests/Arara/Process/Control/Signal/TerminateHandlerTest.php rename to tests/Arara/Process/Handler/SignalTerminateTest.php index 0693ec8..a02aeb1 100644 --- a/tests/Arara/Process/Control/Signal/TerminateHandlerTest.php +++ b/tests/Arara/Process/Handler/SignalTerminateTest.php @@ -1,11 +1,11 @@ method('quit') ->with(0); - $handler = new TerminateHandler($control); + $handler = new SignalTerminate($control); $handler(SIGTERM); } }