mirror of
https://github.com/danog/parallel-functions.git
synced 2024-12-02 17:52:05 +01:00
6d033dbd67
This largely reverts d304ce7a54
. Too bad, I was hoping to reuse CallableTask.
120 lines
3.5 KiB
PHP
120 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Amp\ParallelFunctions\Test;
|
|
|
|
use Amp\Parallel\Sync\SerializationException;
|
|
use Amp\Parallel\Worker\Pool;
|
|
use Amp\ParallelFunctions\Test\Fixture\TestCallables;
|
|
use Amp\PHPUnit\TestCase;
|
|
use Amp\Promise;
|
|
use Amp\Success;
|
|
use function Amp\ParallelFunctions\parallel;
|
|
|
|
class UnserializableClass {
|
|
public function __invoke() {
|
|
}
|
|
|
|
public function instanceMethod() {
|
|
}
|
|
|
|
public static function staticMethod() {
|
|
}
|
|
}
|
|
|
|
class ParallelTest extends TestCase {
|
|
public function testUnserializableClosure() {
|
|
$this->expectException(SerializationException::class);
|
|
$this->expectExceptionMessage("Unsupported callable: Serialization of 'class@anonymous' is not allowed");
|
|
|
|
$unserializable = new class {
|
|
};
|
|
Promise\wait(parallel(function () use ($unserializable) {
|
|
return 1;
|
|
})());
|
|
}
|
|
|
|
public function testCustomPool() {
|
|
$mock = $this->createMock(Pool::class);
|
|
$mock->expects($this->once())
|
|
->method("enqueue")
|
|
->willReturn(new Success(1));
|
|
|
|
$callable = parallel(function () {
|
|
return 0;
|
|
}, $mock);
|
|
|
|
$this->assertSame(1, Promise\wait($callable()));
|
|
}
|
|
|
|
public function testClassStaticMethod() {
|
|
$callable = [TestCallables::class, 'staticMethod'];
|
|
$result = $callable(1);
|
|
$callable = parallel($callable);
|
|
|
|
$this->assertSame($result, Promise\wait($callable(1)));
|
|
}
|
|
|
|
public function testClassInstanceMethod() {
|
|
$instance = new TestCallables;
|
|
|
|
$callable = [$instance, 'instanceMethod'];
|
|
$result = $callable(1);
|
|
$callable = parallel($callable);
|
|
|
|
$this->assertSame($result, Promise\wait($callable(1)));
|
|
}
|
|
|
|
public function testCallableClass() {
|
|
$callable = new TestCallables;
|
|
$result = $callable(1);
|
|
$callable = parallel($callable);
|
|
|
|
$this->assertSame($result, Promise\wait($callable(1)));
|
|
}
|
|
|
|
public function testUnserializableCallable() {
|
|
$this->expectException(SerializationException::class);
|
|
$this->expectExceptionMessage("Unsupported callable: Serialization of 'class@anonymous' is not allowed");
|
|
|
|
$callable = new class {
|
|
public function __invoke() {
|
|
}
|
|
};
|
|
|
|
Promise\wait(parallel($callable)());
|
|
}
|
|
|
|
public function testUnserializableClassInstance() {
|
|
$this->expectException(\Error::class);
|
|
$this->expectExceptionMessage('Uncaught Error in worker with message "When using a class instance as a callable, the class must be autoloadable"');
|
|
|
|
$callable = new UnserializableClass;
|
|
|
|
$callable = parallel($callable);
|
|
|
|
Promise\wait($callable());
|
|
}
|
|
|
|
public function testUnserializableClassInstanceMethod() {
|
|
$this->expectException(\Error::class);
|
|
$this->expectExceptionMessage('Uncaught Error in worker with message "When using a class instance method as a callable, the class must be autoloadable"');
|
|
|
|
$callable = [new UnserializableClass, 'instanceMethod'];
|
|
|
|
$callable = parallel($callable);
|
|
|
|
Promise\wait($callable());
|
|
}
|
|
|
|
public function testUnserializableClassStaticMethod() {
|
|
$this->expectException(\Error::class);
|
|
$this->expectExceptionMessage('Uncaught Error in worker with message "Class \'Amp\\ParallelFunctions\\Test\\UnserializableClass\' not found"');
|
|
|
|
$callable = [UnserializableClass::class, 'staticMethod'];
|
|
|
|
$callable = parallel($callable);
|
|
|
|
Promise\wait($callable());
|
|
}
|
|
}
|