Skip to content

Commit

Permalink
Runner: set thread number environment variable [Closes #301]
Browse files Browse the repository at this point in the history
  • Loading branch information
milo committed May 26, 2016
1 parent 1b583e9 commit 7dff568
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Framework/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ class Environment
/** Should Tester use console colors? */
const COLORS = 'NETTE_TESTER_COLORS';

/** Test is runned by Runner */
/** Test is run by Runner */
const RUNNER = 'NETTE_TESTER_RUNNER';

/** Code coverage file */
const COVERAGE = 'NETTE_TESTER_COVERAGE';

/** Thread number when run tests in multi threads */
const THREAD = 'NETTE_TESTER_THREAD';

/** @var bool used for debugging Tester itself */
public static $debugMode = TRUE;

Expand Down
21 changes: 21 additions & 0 deletions src/Runner/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ public function __construct($testFile, PhpInterpreter $interpreter, array $args
}


/**
* @param string
* @param string
* @return void
*/
public function setEnvironmentVariable($name, $value)
{
$this->envVars[$name] = $value;
}


/**
* @param string
* @return string
*/
public function getEnvironmentVariable($name)
{
return $this->envVars[$name];
}


/**
* Runs single test.
* @param int RUN_ASYNC | RUN_COLLECT_ERRORS
Expand Down
8 changes: 6 additions & 2 deletions src/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Tester\Runner;

use Tester;
use Tester\Environment;


/**
Expand Down Expand Up @@ -102,11 +102,14 @@ public function run()
}
$this->jobCount = count($this->jobs) + array_sum($this->results);

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

$this->installInterruptHandler();
while (($this->jobs || $running) && !$this->isInterrupted()) {
for ($i = count($running); $this->jobs && $i < $this->threadCount; $i++) {
while ($threads && $this->jobs) {
$running[] = $job = array_shift($this->jobs);
$async = $this->threadCount > 1 && (count($running) + count($this->jobs) > 1);
$job->setEnvironmentVariable(Environment::THREAD, array_shift($threads));
$job->run($async ? $job::RUN_ASYNC : NULL);
}

Expand All @@ -120,6 +123,7 @@ public function run()
}

if (!$job->isRunning()) {
$threads[] = $job->getEnvironmentVariable(Environment::THREAD);
$this->testHandler->assess($job);
unset($running[$key]);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Runner/Runner.environment.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ use Tester\Environment;
require __DIR__ . '/../bootstrap.php';

Assert::same('1', getenv(Environment::RUNNER));
Assert::match('%d%', getenv(Environment::THREAD));

0 comments on commit 7dff568

Please sign in to comment.