diff --git a/src/Spork/Batch/BatchJob.php b/src/Spork/Batch/BatchJob.php index f783a96..2202b52 100644 --- a/src/Spork/Batch/BatchJob.php +++ b/src/Spork/Batch/BatchJob.php @@ -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; diff --git a/tests/Spork/Test/Batch/BatchJobTest.php b/tests/Spork/Test/Batch/BatchJobTest.php new file mode 100644 index 0000000..8c88994 --- /dev/null +++ b/tests/Spork/Test/Batch/BatchJobTest.php @@ -0,0 +1,73 @@ +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 should 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, 'When the promise is successful the exit status should be zero'); + } +}