1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-04 02:27:55 +01:00
parallel/test/Sync/SharedMemoryParcelTest.php

76 lines
1.9 KiB
PHP
Raw Normal View History

2016-12-30 02:16:04 +01:00
<?php
2016-08-23 23:47:40 +02:00
namespace Amp\Parallel\Test\Sync;
2016-08-18 18:04:48 +02:00
2017-11-29 21:40:07 +01:00
use Amp\Loop;
2016-08-23 23:47:40 +02:00
use Amp\Parallel\Sync\SharedMemoryParcel;
use Amp\Promise;
/**
* @requires extension shmop
2015-08-31 19:26:11 +02:00
* @requires extension sysvmsg
*/
2016-08-19 00:36:58 +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;
2016-08-19 00:36:58 +02:00
protected function createParcel($value) {
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;
}
2016-08-19 00:36:58 +02:00
public function tearDown() {
2017-11-29 21:40:07 +01:00
$this->parcel = null;
}
2015-08-10 21:28:47 +02:00
2016-08-19 00:36:58 +02:00
public function testObjectOverflowMoved() {
2017-11-29 21:40:07 +01:00
$object = SharedMemoryParcel::create(self::ID, 'hi', 2);
2016-08-19 00:36:58 +02:00
$awaitable = $object->synchronized(function () {
return 'hello world';
2016-08-19 00:36:58 +02:00
});
Promise\wait($awaitable);
$this->assertEquals('hello world', Promise\wait($object->unwrap()));
}
2015-08-10 21:28:47 +02:00
/**
* @group posix
* @requires extension pcntl
2015-08-10 21:28:47 +02:00
*/
2016-08-19 00:36:58 +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
$this->doInFork(function () use ($object) {
2017-11-29 21:40:07 +01:00
$awaitable = $object->synchronized(function ($value) {
return $value + 1;
2016-08-19 00:36:58 +02:00
});
Promise\wait($awaitable);
2015-08-10 21:28:47 +02:00
});
$this->assertEquals(43, Promise\wait($object->unwrap()));
2015-08-10 21:28:47 +02:00
}
/**
* @group posix
* @requires extension pcntl
2015-08-10 21:28:47 +02:00
*/
2017-11-29 21:40:07 +01:00
public function testInSeparateProcess() {
$parcel = SharedMemoryParcel::create(self::ID, 42);
$this->doInFork(function () {
Loop::run(function () {
$parcel = SharedMemoryParcel::use(self::ID);
$this->assertSame(43, yield $parcel->synchronized(function ($value) {
$this->assertSame(42, $value);
return $value + 1;
}));
});
2015-08-10 21:28:47 +02:00
});
2017-11-29 21:40:07 +01:00
Loop::run(function () use ($parcel) {
$this->assertSame(43, yield $parcel->unwrap());
});
2015-08-10 21:28:47 +02:00
}
}