2017-12-14 02:29:19 +01:00
< ? php
namespace Amp\ParallelFunctions\Test ;
2019-01-08 01:14:31 +01:00
use Amp\Parallel\Sync\SerializationException ;
2017-12-15 03:26:03 +01:00
use Amp\Parallel\Worker\Pool ;
2018-10-28 16:18:24 +01:00
use Amp\ParallelFunctions\Test\Fixture\TestCallables ;
2022-02-01 13:33:52 +01:00
use Amp\PHPUnit\AsyncTestCase ;
2017-12-15 03:26:03 +01:00
use Amp\Promise ;
use Amp\Success ;
2022-02-02 23:04:43 +01:00
use function Amp\ParallelFunctions\parallel ;
2017-12-14 02:29:19 +01:00
2022-02-02 23:04:43 +01:00
class UnserializableClass
{
public function __invoke ()
{
2018-10-28 16:18:24 +01:00
}
2022-02-02 23:04:43 +01:00
public function instanceMethod ()
{
2018-10-28 16:18:24 +01:00
}
2022-02-02 23:04:43 +01:00
public static function staticMethod ()
{
2018-10-28 16:18:24 +01:00
}
}
2022-02-02 23:04:43 +01:00
class ParallelTest extends AsyncTestCase
{
public function testUnserializableClosure ()
{
2019-01-08 01:14:31 +01:00
$this -> expectException ( SerializationException :: class );
2019-01-09 23:37:17 +01:00
$this -> expectExceptionMessage ( " Unsupported callable: Serialization of 'class@anonymous' is not allowed " );
2019-01-08 01:14:31 +01:00
2017-12-14 08:31:15 +01:00
$unserializable = new class {
};
2019-01-08 01:14:31 +01:00
Promise\wait ( parallel ( function () use ( $unserializable ) {
2017-12-14 02:29:19 +01:00
return 1 ;
2019-01-08 01:14:31 +01:00
})());
2017-12-14 02:29:19 +01:00
}
2017-12-15 03:26:03 +01:00
2022-02-02 23:04:43 +01:00
public function testCustomPool ()
{
2017-12-15 03:26:03 +01:00
$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 ()));
}
2018-10-28 16:18:24 +01:00
2022-02-02 23:04:43 +01:00
public function testClassStaticMethod ()
{
2018-10-28 16:18:24 +01:00
$callable = [ TestCallables :: class , 'staticMethod' ];
$result = $callable ( 1 );
$callable = parallel ( $callable );
$this -> assertSame ( $result , Promise\wait ( $callable ( 1 )));
}
2022-02-02 23:04:43 +01:00
public function testClassInstanceMethod ()
{
2018-10-28 16:18:24 +01:00
$instance = new TestCallables ;
$callable = [ $instance , 'instanceMethod' ];
$result = $callable ( 1 );
$callable = parallel ( $callable );
$this -> assertSame ( $result , Promise\wait ( $callable ( 1 )));
}
2022-02-02 23:04:43 +01:00
public function testCallableClass ()
{
2018-10-28 16:18:24 +01:00
$callable = new TestCallables ;
$result = $callable ( 1 );
$callable = parallel ( $callable );
$this -> assertSame ( $result , Promise\wait ( $callable ( 1 )));
}
2022-02-02 23:04:43 +01:00
public function testUnserializableCallable ()
{
2019-01-08 01:14:31 +01:00
$this -> expectException ( SerializationException :: class );
2019-01-09 23:37:17 +01:00
$this -> expectExceptionMessage ( " Unsupported callable: Serialization of 'class@anonymous' is not allowed " );
2018-10-28 16:18:24 +01:00
$callable = new class {
2022-02-02 23:04:43 +01:00
public function __invoke ()
{
2018-10-28 16:18:24 +01:00
}
};
2019-01-08 01:14:31 +01:00
Promise\wait ( parallel ( $callable )());
2018-10-28 16:18:24 +01:00
}
2022-02-02 23:04:43 +01:00
public function testUnserializableClassInstance ()
{
2018-10-28 16:18:24 +01:00
$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 ());
}
2022-02-02 23:04:43 +01:00
public function testUnserializableClassInstanceMethod ()
{
2018-10-28 16:18:24 +01:00
$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 ());
}
2022-02-02 23:04:43 +01:00
public function testUnserializableClassStaticMethod ()
{
2018-10-28 16:18:24 +01:00
$this -> expectException ( \Error :: class );
2022-02-01 13:33:52 +01:00
$this -> expectExceptionMessage (
PHP_VERSION_ID >= 80000 ?
'Uncaught Error in worker with message "Class "Amp\\ParallelFunctions\\Test\\UnserializableClass" not found"' :
'Uncaught Error in worker with message "Class \'Amp\\ParallelFunctions\\Test\\UnserializableClass\' not found"'
);
2018-10-28 16:18:24 +01:00
$callable = [ UnserializableClass :: class , 'staticMethod' ];
$callable = parallel ( $callable );
Promise\wait ( $callable ());
}
2017-12-14 02:29:19 +01:00
}