From a508ee3103e209d1e67a30fe3b0f3bd7613e035b Mon Sep 17 00:00:00 2001 From: chbiel Date: Sun, 15 Feb 2015 01:13:57 +0100 Subject: [PATCH 1/5] add backward compatibility for TEST_TOKEN, so added a UNIQUE_TEST_TOKEN variable that will be passed in the runner --- src/ParaTest/Runners/PHPUnit/Runner.php | 30 ++++++++++------- .../paratest-only-tests/EnvironmentTest.php | 4 ++- .../ParaTest/Runners/PHPUnit/RunnerTest.php | 33 ++++++++++++++----- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/ParaTest/Runners/PHPUnit/Runner.php b/src/ParaTest/Runners/PHPUnit/Runner.php index e871d3bc..58ebdb8e 100644 --- a/src/ParaTest/Runners/PHPUnit/Runner.php +++ b/src/ParaTest/Runners/PHPUnit/Runner.php @@ -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); } } } @@ -134,7 +134,11 @@ protected function initTokens() { $this->tokens = array(); for ($i=0; $i< $this->options->processes; $i++) { - $this->tokens[uniqid()] = true; + $this->tokens[$i] = array( + 'token' => $i, + 'unique' => uniqid(), + 'available' => true + ); } } @@ -142,13 +146,13 @@ protected function initTokens() * 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; @@ -161,7 +165,8 @@ protected function getNextAvailableToken() */ protected function releaseToken($tokenIdentifier) { - $this->tokens[$tokenIdentifier] = true; + $filtered = array_filter($this->tokens, function($val) use ($tokenIdentifier) { return ($val['token'] === $tokenIdentifier);}); + $this->tokens[array_keys($filtered)[0]]['available'] = true; } /** @@ -171,7 +176,8 @@ 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);}); + $this->tokens[array_keys($filtered)[0]]['available'] = false; } /** diff --git a/test/fixtures/paratest-only-tests/EnvironmentTest.php b/test/fixtures/paratest-only-tests/EnvironmentTest.php index ec576305..b43205cf 100644 --- a/test/fixtures/paratest-only-tests/EnvironmentTest.php +++ b/test/fixtures/paratest-only-tests/EnvironmentTest.php @@ -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)); } } diff --git a/test/unit/ParaTest/Runners/PHPUnit/RunnerTest.php b/test/unit/ParaTest/Runners/PHPUnit/RunnerTest.php index 3e25109b..cb7c35d8 100644 --- a/test/unit/ParaTest/Runners/PHPUnit/RunnerTest.php +++ b/test/unit/ParaTest/Runners/PHPUnit/RunnerTest.php @@ -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']); } } \ No newline at end of file From ef016079166a5a2216704704445a1e9e9eddfc3b Mon Sep 17 00:00:00 2001 From: chbiel Date: Sun, 15 Feb 2015 01:18:35 +0100 Subject: [PATCH 2/5] add UNIQUE_TEST_TOKEN to the wrapper runner --- src/ParaTest/Runners/PHPUnit/Worker.php | 5 ++++- src/ParaTest/Runners/PHPUnit/WrapperRunner.php | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ParaTest/Runners/PHPUnit/Worker.php b/src/ParaTest/Runners/PHPUnit/Worker.php index a100333a..62a8e6b3 100644 --- a/src/ParaTest/Runners/PHPUnit/Worker.php +++ b/src/ParaTest/Runners/PHPUnit/Worker.php @@ -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); diff --git a/src/ParaTest/Runners/PHPUnit/WrapperRunner.php b/src/ParaTest/Runners/PHPUnit/WrapperRunner.php index cefff33a..883f1009 100644 --- a/src/ParaTest/Runners/PHPUnit/WrapperRunner.php +++ b/src/ParaTest/Runners/PHPUnit/WrapperRunner.php @@ -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; } From 32b01c3f7001e69af049d7056a81baf66001d6ad Mon Sep 17 00:00:00 2001 From: chbiel Date: Sun, 15 Feb 2015 01:20:18 +0100 Subject: [PATCH 3/5] style fixes --- src/ParaTest/Runners/PHPUnit/Runner.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/ParaTest/Runners/PHPUnit/Runner.php b/src/ParaTest/Runners/PHPUnit/Runner.php index 58ebdb8e..b689220d 100644 --- a/src/ParaTest/Runners/PHPUnit/Runner.php +++ b/src/ParaTest/Runners/PHPUnit/Runner.php @@ -133,12 +133,8 @@ private function setExitCode(ExecutableTest $test) protected function initTokens() { $this->tokens = array(); - for ($i=0; $i< $this->options->processes; $i++) { - $this->tokens[$i] = array( - 'token' => $i, - 'unique' => uniqid(), - 'available' => true - ); + for ($i = 0; $i < $this->options->processes; $i++) { + $this->tokens[$i] = array('token' => $i, 'unique' => uniqid(), 'available' => true); } } @@ -165,7 +161,7 @@ protected function getNextAvailableToken() */ protected function releaseToken($tokenIdentifier) { - $filtered = array_filter($this->tokens, function($val) use ($tokenIdentifier) { return ($val['token'] === $tokenIdentifier);}); + $filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { return ($val['token'] === $tokenIdentifier); }); $this->tokens[array_keys($filtered)[0]]['available'] = true; } @@ -176,7 +172,7 @@ protected function releaseToken($tokenIdentifier) */ protected function acquireToken($tokenIdentifier) { - $filtered = array_filter($this->tokens, function($val) use ($tokenIdentifier) { return ($val['token'] === $tokenIdentifier);}); + $filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { return ($val['token'] === $tokenIdentifier); }); $this->tokens[array_keys($filtered)[0]]['available'] = false; } From d342805cf893762926721f58f2c7710da33fad8d Mon Sep 17 00:00:00 2001 From: chbiel Date: Sun, 15 Feb 2015 01:27:37 +0100 Subject: [PATCH 4/5] style fixes --- src/ParaTest/Runners/PHPUnit/Runner.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ParaTest/Runners/PHPUnit/Runner.php b/src/ParaTest/Runners/PHPUnit/Runner.php index b689220d..2984385b 100644 --- a/src/ParaTest/Runners/PHPUnit/Runner.php +++ b/src/ParaTest/Runners/PHPUnit/Runner.php @@ -161,7 +161,9 @@ protected function getNextAvailableToken() */ protected function releaseToken($tokenIdentifier) { - $filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { return ($val['token'] === $tokenIdentifier); }); + $filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { + return ($val['token'] === $tokenIdentifier); + }); $this->tokens[array_keys($filtered)[0]]['available'] = true; } @@ -172,7 +174,9 @@ protected function releaseToken($tokenIdentifier) */ protected function acquireToken($tokenIdentifier) { - $filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { return ($val['token'] === $tokenIdentifier); }); + $filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { + return ($val['token'] === $tokenIdentifier); + }); $this->tokens[array_keys($filtered)[0]]['available'] = false; } From 7a1be0a0e8c1a0f0ddfc595a52e60865d311bdce Mon Sep 17 00:00:00 2001 From: chbiel Date: Sun, 15 Feb 2015 01:29:27 +0100 Subject: [PATCH 5/5] fix syntax error in php 5.3 --- src/ParaTest/Runners/PHPUnit/Runner.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ParaTest/Runners/PHPUnit/Runner.php b/src/ParaTest/Runners/PHPUnit/Runner.php index 2984385b..04b5e983 100644 --- a/src/ParaTest/Runners/PHPUnit/Runner.php +++ b/src/ParaTest/Runners/PHPUnit/Runner.php @@ -164,7 +164,8 @@ protected function releaseToken($tokenIdentifier) $filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { return ($val['token'] === $tokenIdentifier); }); - $this->tokens[array_keys($filtered)[0]]['available'] = true; + $keys = array_keys($filtered); + $this->tokens[$keys[0]]['available'] = true; } /** @@ -177,7 +178,8 @@ protected function acquireToken($tokenIdentifier) $filtered = array_filter($this->tokens, function ($val) use ($tokenIdentifier) { return ($val['token'] === $tokenIdentifier); }); - $this->tokens[array_keys($filtered)[0]]['available'] = false; + $keys = array_keys($filtered); + $this->tokens[$keys[0]]['available'] = false; } /**