Skip to content

Commit

Permalink
Fix erroneous exit code not propagating for BatchJob
Browse files Browse the repository at this point in the history
  • Loading branch information
taueres committed Sep 29, 2015
1 parent 530fcf5 commit 54e65c1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Spork/Batch/BatchJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ public function __invoke()

$results = array();
foreach ($forks as $fork) {
$results = array_merge($results, $fork->getResult());
$exitStatus = $fork->getExitStatus();
if (0 !== $exitStatus) {
// Propagate erroneous state
exit($exitStatus);
}
$results = array_merge($results, (array) $fork->getResult());
}

return $results;
Expand Down
73 changes: 73 additions & 0 deletions tests/Spork/Test/Batch/BatchJobTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Spork\Test\Batch;

class BatchJobTest extends \PHPUnit_Framework_TestCase
{
public function testErrorPropagation()
{
$manager = new \Spork\ProcessManager();
$batch = $manager->createBatchJob(range(1, 5));

$expectedExitStatus = 20;

$failingClosure = function ($data) use ($expectedExitStatus) {
// Simple condition to simulate only one point of failure
if (3 === $data) {
exit($expectedExitStatus);
}

exit(0);
};

$promise = $batch->execute($failingClosure);

$promise->wait();

$success = null;

$promise->done(function () use (& $success) {
$success = true;
});

$promise->fail(function () use (& $success) {
$success = false;
});

$this->assertFalse($success, 'Promise should fail');

$actualExitStatus = $promise->getExitStatus();

$this->assertEquals($expectedExitStatus, $actualExitStatus, 'Parent process exit status does not match the child one');
}

public function testExitStatusIsZeroOnSuccess()
{
$manager = new \Spork\ProcessManager();
$batch = $manager->createBatchJob(range(1, 5));

$simpleClosure = function () {
exit(0);
};

$promise = $batch->execute($simpleClosure);

$promise->wait();

$success = null;

$promise->done(function () use (& $success) {
$success = true;
});

$promise->fail(function () use (& $success) {
$success = false;
});

$this->assertTrue($success, 'Promise should be successful');

$actualExitStatus = $promise->getExitStatus();

$this->assertEquals(0, $actualExitStatus, 'Promise is successful but exit status is not zero');
}
}

0 comments on commit 54e65c1

Please sign in to comment.