Skip to content

Commit

Permalink
Merge pull request #156 from chbiel/add-seperat-env-for-unique-token
Browse files Browse the repository at this point in the history
Add seperat env for unique token
  • Loading branch information
julianseeger committed Feb 15, 2015
2 parents 07b9f2d + 7a1be0a commit 25e2ac9
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 25 deletions.
34 changes: 21 additions & 13 deletions src/ParaTest/Runners/PHPUnit/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ private function fillRunQueue()
{
$opts = $this->options;
while (sizeof($this->pending) && sizeof($this->running) < $opts->processes) {
$token = $this->getNextAvailableToken();
if ($token !== false) {
$this->acquireToken($token);
$env = array('TEST_TOKEN' => $token) + Habitat::getAll();
$this->running[$token] = array_shift($this->pending)->run($opts->phpunit, $opts->filtered, $env);
$tokenData = $this->getNextAvailableToken();
if ($tokenData !== false) {
$this->acquireToken($tokenData['token']);
$env = array('TEST_TOKEN' => $tokenData['token'], 'UNIQUE_TEST_TOKEN' => $tokenData['unique']) + Habitat::getAll();
$this->running[$tokenData['token']] = array_shift($this->pending)->run($opts->phpunit, $opts->filtered, $env);
}
}
}
Expand Down Expand Up @@ -133,22 +133,22 @@ private function setExitCode(ExecutableTest $test)
protected function initTokens()
{
$this->tokens = array();
for ($i=0; $i< $this->options->processes; $i++) {
$this->tokens[uniqid()] = true;
for ($i = 0; $i < $this->options->processes; $i++) {
$this->tokens[$i] = array('token' => $i, 'unique' => uniqid(), 'available' => true);
}
}

/**
* Gets the next token that is available to be acquired
* from a finished process
*
* @return bool|int
* @return bool|array
*/
protected function getNextAvailableToken()
{
foreach ($this->tokens as $token => $free) {
if ($free) {
return $token;
foreach ($this->tokens as $data) {
if ($data['available']) {
return $data;
}
}
return false;
Expand All @@ -161,7 +161,11 @@ protected function getNextAvailableToken()
*/
protected function releaseToken($tokenIdentifier)
{
$this->tokens[$tokenIdentifier] = true;
$filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) {
return ($val['token'] === $tokenIdentifier);
});
$keys = array_keys($filtered);
$this->tokens[$keys[0]]['available'] = true;
}

/**
Expand All @@ -171,7 +175,11 @@ protected function releaseToken($tokenIdentifier)
*/
protected function acquireToken($tokenIdentifier)
{
$this->tokens[$tokenIdentifier] = false;
$filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) {
return ($val['token'] === $tokenIdentifier);
});
$keys = array_keys($filtered);
$this->tokens[$keys[0]]['available'] = false;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/ParaTest/Runners/PHPUnit/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ class Worker
*/
private $currentlyExecuting;

public function start($wrapperBinary, $token = 1)
public function start($wrapperBinary, $token = 1, $uniqueToken = null)
{
$bin = 'PARATEST=1 ';
if (is_numeric($token)) {
$bin .= "TEST_TOKEN=$token ";
}
if ($uniqueToken) {
$bin .= "UNIQUE_TEST_TOKEN=$uniqueToken ";
}
$bin .= "exec $wrapperBinary";
$pipes = array();
$this->proc = proc_open($bin, self::$descriptorspec, $pipes);
Expand Down
4 changes: 3 additions & 1 deletion src/ParaTest/Runners/PHPUnit/WrapperRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ private function startWorkers()
$worker = new Worker();
if ($this->options->noTestTokens) {
$token = null;
$uniqueToken = null;
} else {
$token = $i;
$uniqueToken = uniqid();
}
$worker->start($wrapper, $token);
$worker->start($wrapper, $token, $uniqueToken);
$this->streams[] = $worker->stdout();
$this->workers[] = $worker;
}
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/paratest-only-tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public function testParatestVariableIsDefined()
public function testTestTokenVariableIsDefinedCorrectly()
{
$token = getenv('TEST_TOKEN');
$this->assertTrue(!empty($token));
$unqiueToken = getenv('UNIQUE_TEST_TOKEN');
$this->assertTrue(is_numeric($token));
$this->assertTrue(!empty($unqiueToken));
}
}
33 changes: 24 additions & 9 deletions test/unit/ParaTest/Runners/PHPUnit/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,36 +45,51 @@ public function testConstructorAssignsTokens()

public function testGetsNextAvailableTokenReturnsTokenIdentifier()
{
$tokens = array(0 => false, 1 => false, 2 => true, 3 => false);
$tokens = array(
0 => array('token' => 0, 'unique' => uniqid(), 'available' => false),
1 => array('token' => 1, 'unique' => uniqid(), 'available' => false),
2 => array('token' => 2, 'unique' => uniqid(), 'available' => true),
3 => array('token' => 3, 'unique' => uniqid(), 'available' => false)
);
$opts = array('processes' => 4, 'path' => FIXTURES . DS . 'tests', 'bootstrap' => 'hello', 'functional' => true);
$runner = new Runner($opts);
$this->setObjectValue($runner, 'tokens', $tokens);

$token = $this->call($runner, 'getNextAvailableToken');
$this->assertEquals(2, $token);
$tokenData = $this->call($runner, 'getNextAvailableToken');
$this->assertEquals(2, $tokenData['token']);
}

public function testGetNextAvailableTokenReturnsFalseWhenNoTokensAreAvailable()
{
$tokens = array(0 => false, 1 => false, 2 => false, 3 => false);
$tokens = array(
0 => array('token' => 0, 'unique' => uniqid(), 'available' => false),
1 => array('token' => 1, 'unique' => uniqid(), 'available' => false),
2 => array('token' => 2, 'unique' => uniqid(), 'available' => false),
3 => array('token' => 3, 'unique' => uniqid(), 'available' => false)
);
$opts = array('processes' => 4, 'path' => FIXTURES . DS . 'tests', 'bootstrap' => 'hello', 'functional' => true);
$runner = new Runner($opts);
$this->setObjectValue($runner, 'tokens', $tokens);

$token = $this->call($runner, 'getNextAvailableToken');
$this->assertTrue($token === false);
$tokenData = $this->call($runner, 'getNextAvailableToken');
$this->assertTrue($tokenData === false);
}

public function testReleaseTokenMakesTokenAvailable()
{
$tokens = array(0 => false, 1 => false, 2 => false, 3 => false);
$tokens = array(
0 => array('token' => 0, 'unique' => uniqid(), 'available' => false),
1 => array('token' => 1, 'unique' => uniqid(), 'available' => false),
2 => array('token' => 2, 'unique' => uniqid(), 'available' => false),
3 => array('token' => 3, 'unique' => uniqid(), 'available' => false)
);
$opts = array('processes' => 4, 'path' => FIXTURES . DS . 'tests', 'bootstrap' => 'hello', 'functional' => true);
$runner = new Runner($opts);
$this->setObjectValue($runner, 'tokens', $tokens);

$this->assertFalse($tokens[1]);
$this->assertFalse($tokens[1]['available']);
$this->call($runner, 'releaseToken', 1);
$tokens = $this->getObjectValue($runner, 'tokens');
$this->assertTrue($tokens[1]);
$this->assertTrue($tokens[1]['available']);
}
}

0 comments on commit 25e2ac9

Please # to comment.