mirror of
https://github.com/danog/parallel-functions.git
synced 2024-11-30 04:39:03 +01:00
Throw when closure is not serializable
This commit is contained in:
parent
31004c3d6f
commit
a3d89f6582
@ -3,7 +3,6 @@
|
|||||||
namespace Amp\ParallelFunctions;
|
namespace Amp\ParallelFunctions;
|
||||||
|
|
||||||
use Amp\MultiReasonException;
|
use Amp\MultiReasonException;
|
||||||
use Amp\Parallel\Sync\SerializationException;
|
|
||||||
use Amp\ParallelFunctions\Internal\ParallelTask;
|
use Amp\ParallelFunctions\Internal\ParallelTask;
|
||||||
use Amp\Promise;
|
use Amp\Promise;
|
||||||
use SuperClosure\Serializer;
|
use SuperClosure\Serializer;
|
||||||
@ -20,7 +19,7 @@ use function Amp\Promise\any;
|
|||||||
* @throws \Error If the passed callable is not safely serializable.
|
* @throws \Error If the passed callable is not safely serializable.
|
||||||
*/
|
*/
|
||||||
function parallel(callable $callable): callable {
|
function parallel(callable $callable): callable {
|
||||||
static $serializer;
|
static $serializer, $errorHandler;
|
||||||
|
|
||||||
if ($serializer === null) {
|
if ($serializer === null) {
|
||||||
$serializer = new Serializer;
|
$serializer = new Serializer;
|
||||||
@ -30,11 +29,22 @@ function parallel(callable $callable): callable {
|
|||||||
$payload = $callable;
|
$payload = $callable;
|
||||||
$type = ParallelTask::TYPE_SIMPLE;
|
$type = ParallelTask::TYPE_SIMPLE;
|
||||||
} elseif ($callable instanceof \Closure) {
|
} elseif ($callable instanceof \Closure) {
|
||||||
|
if ($errorHandler === null) {
|
||||||
|
$errorHandler = function ($errno, $errstr) {
|
||||||
|
if ($errno & \error_reporting()) {
|
||||||
|
throw new \Error($errstr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set custom error handler because Serializer only issues a notice if serialization fails.
|
||||||
|
\set_error_handler($errorHandler);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$payload = $serializer->serialize($callable);
|
$payload = $serializer->serialize($callable);
|
||||||
$type = ParallelTask::TYPE_CLOSURE;
|
$type = ParallelTask::TYPE_CLOSURE;
|
||||||
} catch (SerializationException $e) {
|
} finally {
|
||||||
throw new \Error('Unserializable closure: ' . $e->getMessage(), 0, $e);
|
\restore_error_handler();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new \Error('Unsupported callable type: ' . \gettype($callable));
|
throw new \Error('Unsupported callable type: ' . \gettype($callable));
|
||||||
|
19
test/ParallelTest.php
Normal file
19
test/ParallelTest.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Amp\ParallelFunctions\Test;
|
||||||
|
|
||||||
|
use Amp\PHPUnit\TestCase;
|
||||||
|
use function Amp\ParallelFunctions\parallel;
|
||||||
|
|
||||||
|
class ParallelTest extends TestCase {
|
||||||
|
/**
|
||||||
|
* @expectedException \Error
|
||||||
|
* @expectedExceptionMessage Serialization of closure failed
|
||||||
|
*/
|
||||||
|
public function testUnserializableClosure() {
|
||||||
|
$unserializable = new class {};
|
||||||
|
$callable = parallel(function () use ($unserializable) {
|
||||||
|
return 1;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user