1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-30 04:39:01 +01:00

Better fix for #27

Assertions would crash the worker, this will return as a failed task.
This commit is contained in:
Aaron Piotrowski 2017-11-22 21:38:11 -06:00
parent 689de9a28b
commit e700395ac5
2 changed files with 35 additions and 4 deletions

View File

@ -2,6 +2,7 @@
namespace Amp\Parallel\Worker\Internal;
use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\Task;
/** @internal */
@ -23,10 +24,14 @@ class Job {
public function getTask(): Task {
// Classes that cannot be autoloaded will be unserialized as an instance of __PHP_Incomplete_Class.
\assert(
$this->task instanceof Task,
\sprintf("Classes implementing %s must be autoloadable by the Composer autoloader", Task::class)
);
if ($this->task instanceof \__PHP_Incomplete_Class) {
return new class implements Task {
public function run(Environment $environment) {
throw new \Error(\sprintf("Classes implementing %s must be autoloadable by the Composer autoloader", Task::class));
}
};
}
return $this->task;
}
}

View File

@ -3,8 +3,17 @@
namespace Amp\Parallel\Test\Worker;
use Amp\Loop;
use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\Task;
use Amp\Parallel\Worker\TaskError;
use Amp\PHPUnit\TestCase;
class NonAutoloadableTask implements Task {
public function run(Environment $environment) {
return 1;
}
}
abstract class AbstractWorkerTest extends TestCase {
/**
* @return \Amp\Parallel\Worker\Worker
@ -93,4 +102,21 @@ abstract class AbstractWorkerTest extends TestCase {
$this->assertRunTimeLessThan([$worker, 'kill'], 250);
$this->assertFalse($worker->isRunning());
}
public function testUnserializableTask() {
Loop::run(function () {
$worker = $this->createWorker();
$worker->start();
try {
yield $worker->enqueue(new NonAutoloadableTask);
$this->fail("Tasks that cannot be autoloaded should throw an exception");
} catch (TaskError $exception) {
$this->assertSame("Error", $exception->getName());
$this->assertGreaterThan(0, \strpos($exception->getMessage(), \sprintf("Classes implementing %s", Task::class)));
}
yield $worker->shutdown();
});
}
}