Skip to content

Commit

Permalink
interrupt signal is converted to InterruptException and handled by Cl…
Browse files Browse the repository at this point in the history
…iTester

allows to interrupt watch mode
  • Loading branch information
dg committed Nov 24, 2021
1 parent 6b9b34f commit 8bac4c2
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 58 deletions.
23 changes: 22 additions & 1 deletion src/Runner/CliTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function run(): ?int
$runner->setEnvironmentVariable(Environment::RUNNER, '1');
$runner->setEnvironmentVariable(Environment::COLORS, (string) (int) Environment::$useColors);

$this->installInterruptHandler();

if ($this->options['--coverage']) {
$coverageFile = $this->prepareCodeCoverage($runner);
}
Expand Down Expand Up @@ -360,7 +362,9 @@ private function setupErrors(): void
});

set_exception_handler(function (\Throwable $e) {
$this->displayException($e);
if (!$e instanceof InterruptException) {
$this->displayException($e);
}
exit(2);
});
}
Expand All @@ -374,4 +378,21 @@ private function displayException(\Throwable $e): void
: Dumper::color('white/red', 'Error: ' . $e->getMessage());
echo "\n";
}


private function installInterruptHandler(): void
{
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGINT, function (): void {
pcntl_signal(SIGINT, SIG_DFL);
throw new InterruptException;
});
pcntl_async_signals(true);

} elseif (function_exists('sapi_windows_set_ctrl_handler') && PHP_SAPI === 'cli') {
sapi_windows_set_ctrl_handler(function (): void {
throw new InterruptException;
});
}
}
}
80 changes: 23 additions & 57 deletions src/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,36 +125,37 @@ public function run(): bool

$threads = range(1, $this->threadCount);

$this->installInterruptHandler();
$async = $this->threadCount > 1 && count($this->jobs) > 1;

while (($this->jobs || $running) && !$this->isInterrupted()) {
while ($threads && $this->jobs) {
$running[] = $job = array_shift($this->jobs);
$job->setEnvironmentVariable(Environment::THREAD, (string) array_shift($threads));
$job->run($async ? $job::RUN_ASYNC : 0);
}

if ($async) {
usleep(Job::RUN_USLEEP); // stream_select() doesn't work with proc_open()
}
try {
while (($this->jobs || $running) && !$this->interrupted) {
while ($threads && $this->jobs) {
$running[] = $job = array_shift($this->jobs);
$job->setEnvironmentVariable(Environment::THREAD, (string) array_shift($threads));
$job->run($async ? $job::RUN_ASYNC : 0);
}

foreach ($running as $key => $job) {
if ($this->isInterrupted()) {
break 2;
if ($async) {
usleep(Job::RUN_USLEEP); // stream_select() doesn't work with proc_open()
}

if (!$job->isRunning()) {
$threads[] = $job->getEnvironmentVariable(Environment::THREAD);
$this->testHandler->assess($job);
unset($running[$key]);
foreach ($running as $key => $job) {
if ($this->interrupted) {
break 2;
}

if (!$job->isRunning()) {
$threads[] = $job->getEnvironmentVariable(Environment::THREAD);
$this->testHandler->assess($job);
unset($running[$key]);
}
}
}
}
$this->removeInterruptHandler();

foreach ($this->outputHandlers as $handler) {
$handler->end();
} finally {
foreach ($this->outputHandlers as $handler) {
$handler->end();
}
}

return $this->result;
Expand Down Expand Up @@ -235,41 +236,6 @@ public function getInterpreter(): PhpInterpreter
}


private function installInterruptHandler(): void
{
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGINT, function (): void {
pcntl_signal(SIGINT, SIG_DFL);
$this->interrupted = true;
});
} elseif (function_exists('sapi_windows_set_ctrl_handler') && PHP_SAPI === 'cli') {
sapi_windows_set_ctrl_handler(function () {
$this->interrupted = true;
});
}
}


private function removeInterruptHandler(): void
{
if (function_exists('pcntl_signal')) {
pcntl_signal(SIGINT, SIG_DFL);
} elseif (function_exists('sapi_windows_set_ctrl_handler') && PHP_SAPI === 'cli') {
sapi_windows_set_ctrl_handler(null);
}
}


private function isInterrupted(): bool
{
if (function_exists('pcntl_signal_dispatch')) {
pcntl_signal_dispatch();
}

return $this->interrupted;
}


private function getLastResult(Test $test): int
{
$signature = $test->getSignature();
Expand Down
15 changes: 15 additions & 0 deletions src/Runner/exceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* This file is part of the Nette Tester.
* Copyright (c) 2009 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Tester\Runner;


class InterruptException extends \Exception
{
}
1 change: 1 addition & 0 deletions src/tester.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

declare(strict_types=1);

require __DIR__ . '/Runner/exceptions.php';
require __DIR__ . '/Runner/Test.php';
require __DIR__ . '/Runner/PhpInterpreter.php';
require __DIR__ . '/Runner/Runner.php';
Expand Down

0 comments on commit 8bac4c2

Please sign in to comment.