2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-08-10 05:16:34 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Test\Sync;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
use Amp\Delayed;
|
|
|
|
use Amp\Parallel\Context\Process;
|
|
|
|
use Amp\Parallel\Sync\Parcel;
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\Sync\SharedMemoryParcel;
|
2015-08-10 05:16:34 +02:00
|
|
|
|
2015-08-28 22:09:07 +02:00
|
|
|
/**
|
|
|
|
* @requires extension shmop
|
2015-08-31 19:26:11 +02:00
|
|
|
* @requires extension sysvmsg
|
2015-08-28 22:09:07 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
class SharedMemoryParcelTest extends AbstractParcelTest
|
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
const ID = __CLASS__;
|
|
|
|
|
2015-08-28 23:58:15 +02:00
|
|
|
private $parcel;
|
2015-08-10 05:16:34 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
protected function createParcel($value): Parcel
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
$this->parcel = SharedMemoryParcel::create(self::ID, $value);
|
2015-08-28 23:58:15 +02:00
|
|
|
return $this->parcel;
|
2015-08-10 05:16:34 +02:00
|
|
|
}
|
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
public function tearDown(): void
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
$this->parcel = null;
|
2015-08-10 05:16:34 +02:00
|
|
|
}
|
2015-08-10 21:28:47 +02:00
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testObjectOverflowMoved()
|
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
$object = SharedMemoryParcel::create(self::ID, 'hi', 2);
|
2019-08-27 19:17:41 +02:00
|
|
|
yield $object->synchronized(function () {
|
2015-10-18 09:12:46 +02:00
|
|
|
return 'hello world';
|
2016-08-19 00:36:58 +02:00
|
|
|
});
|
2015-08-10 23:36:01 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->assertEquals('hello world', yield $object->unwrap());
|
2015-08-10 23:36:01 +02:00
|
|
|
}
|
|
|
|
|
2015-08-10 21:28:47 +02:00
|
|
|
/**
|
|
|
|
* @group posix
|
2015-08-28 22:09:07 +02:00
|
|
|
* @requires extension pcntl
|
2015-08-10 21:28:47 +02:00
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function testSetInSeparateProcess()
|
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
$object = SharedMemoryParcel::create(self::ID, 42);
|
2015-08-10 21:28:47 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$process = new Process([__DIR__ . '/Fixture/parcel.php', self::ID]);
|
2015-08-10 21:28:47 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$promise = $object->synchronized(function (int $value): \Generator {
|
|
|
|
$this->assertSame(42, $value);
|
|
|
|
yield new Delayed(500); // Child must wait until parent finishes with parcel.
|
|
|
|
return $value + 1;
|
|
|
|
});
|
2015-08-10 21:28:47 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
yield $process->start();
|
2017-11-29 21:40:07 +01:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->assertSame(43, yield $promise);
|
2015-08-10 21:28:47 +02:00
|
|
|
|
2019-08-27 19:17:41 +02:00
|
|
|
$this->assertSame(44, yield $process->join()); // Wait for child process to finish.
|
|
|
|
$this->assertEquals(44, yield $object->unwrap());
|
2015-08-10 21:28:47 +02:00
|
|
|
}
|
2015-08-10 05:16:34 +02:00
|
|
|
}
|