Skip to content

Commit

Permalink
Merge pull request #3 from alchemy-fr/improvement/remove-ticks
Browse files Browse the repository at this point in the history
Remove tick declaration
  • Loading branch information
bburnichon committed Mar 11, 2016
2 parents ec7c28d + 5e4d6e6 commit 69fdd4a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 71 deletions.
6 changes: 5 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ language: php

sudo: false

cache:
directories:
- $HOME/.composer

php:
- '5.3'
- '5.4'
Expand All @@ -18,7 +22,7 @@ before_script:
- phpenv config-rm xdebug.ini
- echo "extension=zmq.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
- composer self-update
- composer install --prefer-source
- composer install

script:
- vendor/bin/phpunit -c phpunit.xml.dist
Expand Down
2 changes: 0 additions & 2 deletions src/Alchemy/TaskManager/Job/AbstractJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public function getStatus()
*/
final public function run(JobDataInterface $data = null, $callback = null)
{
declare(ticks=1);
$data = $data ?: new NullJobData();
$this->dispatcher->dispatch(JobEvents::START, new JobEvent($this, $data));
$this->setup($data);
Expand All @@ -150,7 +149,6 @@ final public function run(JobDataInterface $data = null, $callback = null)
*/
final public function singleRun(JobDataInterface $data = null, $callback = null)
{
declare(ticks=1);
$data = $data ?: new NullJobData();
$this->dispatcher->dispatch(JobEvents::START, new JobEvent($this, $data));
$this->setup($data);
Expand Down
137 changes: 69 additions & 68 deletions tests/Alchemy/Test/TaskManager/Job/AbstractJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,7 @@ public function setUp()

private function getPauseScript()
{
return '<?php
require "'.__DIR__.'/../../../../../vendor/autoload.php";
use Alchemy\TaskManager\Job\JobDataInterface;
class Job extends Alchemy\TaskManager\Job\AbstractJob
{
protected function doRun(JobDataInterface $data)
{
}
protected function getPauseDuration()
{
return 2;
}
}
$job = new Job();
$job->run();
';
return $this->createScript('', array(), '', '2');
}

public function testCustomEventsAreWelcomed()
Expand Down Expand Up @@ -91,58 +72,28 @@ public function testStopDispatchAnEventOnFirstCall()

private function getPauseAndLoopScript()
{
return '<?php
require "'.__DIR__.'/../../../../../vendor/autoload.php";
use Alchemy\TaskManager\Job\JobDataInterface;
class Job extends Alchemy\TaskManager\Job\AbstractJob
{
protected function doRun(JobDataInterface $data)
{
echo "loop\n";
}
protected function getPauseDuration()
{
return 0.1;
}
}
$job = new Job();
$job->run();
';
return $this->createScript('echo "loop\\n";' . PHP_EOL);
}

private function getEventsScript($throwException)
{
return '<?php
require "'.__DIR__.'/../../../../../vendor/autoload.php";
use Alchemy\TaskManager\Job\JobDataInterface;
use Alchemy\TaskManager\Event\JobEvents;
class Job extends Alchemy\TaskManager\Job\AbstractJob
{
protected function doRun(JobDataInterface $data)
{
'.($throwException ? 'throw new \Exception("failure");' : '').'
}
protected function getPauseDuration()
{
return 0.1;
}
}
$job = new Job();
$job->addSubscriber(new Alchemy\TaskManager\Event\JobSubscriber\StopSignalSubscriber(Neutron\SignalHandler\SignalHandler::getInstance()));
$job->addListener(JobEvents::START, function () { echo "job-start\n"; });
$job->addListener(JobEvents::TICK, function () { echo "job-tick\n"; });
$job->addListener(JobEvents::STOP, function () { echo "job-stop\n"; });
$job->addListener(JobEvents::EXCEPTION, function () { echo "job-exception\n"; });
$job->run();
';
$body = $throwException ? 'throw new \Exception("failure");' . PHP_EOL : '';
$additionalUses = array(
'Alchemy\TaskManager\Event\JobSubscriber\StopSignalSubscriber',
'Alchemy\TaskManager\Event\JobEvents',
'Neutron\SignalHandler\SignalHandler',
);

$jobInitializer = <<<EOS
\$job->addSubscriber(new StopSignalSubscriber(SignalHandler::getInstance()));
\$job->addListener(JobEvents::START, function () { echo "job-start\\n"; });
\$job->addListener(JobEvents::TICK, function () { echo "job-tick\\n"; });
\$job->addListener(JobEvents::STOP, function () { echo "job-stop\\n"; });
\$job->addListener(JobEvents::EXCEPTION, function () { echo "job-exception\\n"; });
EOS;

return $this->createScript($body, $additionalUses, $jobInitializer);
}

public function testPauseDoesAPause()
Expand All @@ -169,6 +120,7 @@ public function testEvents()
$process->stop();
$this->assertFalse($process->isRunning());
$data = array_filter(explode("\n", $process->getOutput()));
$this->assertGreaterThanOrEqual(3, count($data), 'Expected more events');
$this->assertSame(JobEvents::START, $data[0]);
$this->assertSame(JobEvents::TICK, $data[1]);
$this->assertContains(JobEvents::STOP, $data);
Expand Down Expand Up @@ -321,6 +273,55 @@ public function testJobCanBeRestartedAfterAFailure()
$this->setExpectedException('Alchemy\Test\TaskManager\Job\JobFailureException', 'Total failure.');
$job->run();
}

/**
* @param string $doRunBody
* @param string[] $additionalUses
* @param string $jobInitializer
* @param string $pauseDuration
* @return string
*/
private function createScript($doRunBody = '', array $additionalUses = array(), $jobInitializer = '', $pauseDuration = '0.1')
{
$path = realpath(__DIR__ . '/../../../../../vendor/autoload.php');

$createUseClosure = function ($useClause) {
return sprintf("use %s;\n", $useClause);
};

$additionalUses[] = 'Alchemy\TaskManager\Job\JobDataInterface';
$additionalUses = array_unique($additionalUses);
sort($additionalUses);

$useClauses = implode('', array_map($createUseClosure, $additionalUses));

return <<<EOS
<?php
declare(ticks=1);
require "$path";
$useClauses
class Job extends Alchemy\TaskManager\Job\AbstractJob
{
protected function doRun(JobDataInterface \$data)
{
$doRunBody
}
protected function getPauseDuration()
{
return $pauseDuration;
}
}
\$job = new Job();
$jobInitializer
\$job->run();
EOS;
}
}

class JobTest extends AbstractJob
Expand Down
2 changes: 2 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(ticks=1);

$loader = require __DIR__.'/../vendor/autoload.php';
$loader->add('Alchemy\Test', __DIR__);
$loader->add('Alchemy\Functional', __DIR__);

0 comments on commit 69fdd4a

Please sign in to comment.