2016-12-29 19:16:04 -06:00
|
|
|
<?php
|
2015-08-09 22:16:34 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
namespace Amp\Parallel\Test\Sync;
|
2016-08-18 11:04:48 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
use Amp\Parallel\Sync\SharedMemoryParcel;
|
2015-08-09 22:16:34 -05:00
|
|
|
|
2015-08-28 15:09:07 -05:00
|
|
|
/**
|
|
|
|
* @requires extension shmop
|
2015-08-31 12:26:11 -05:00
|
|
|
* @requires extension sysvmsg
|
2015-08-28 15:09:07 -05:00
|
|
|
*/
|
2016-08-18 17:36:58 -05:00
|
|
|
class SharedMemoryParcelTest extends AbstractParcelTest {
|
2015-08-28 16:58:15 -05:00
|
|
|
private $parcel;
|
2015-08-09 22:16:34 -05:00
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
protected function createParcel($value) {
|
2015-12-04 23:50:32 -06:00
|
|
|
$this->parcel = new SharedMemoryParcel($value);
|
2015-08-28 16:58:15 -05:00
|
|
|
return $this->parcel;
|
2015-08-09 22:16:34 -05:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function tearDown() {
|
2015-08-28 16:58:15 -05:00
|
|
|
if ($this->parcel !== null) {
|
|
|
|
$this->parcel->free();
|
|
|
|
}
|
2015-08-09 22:16:34 -05:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testNewObjectIsNotFreed() {
|
2015-12-04 23:50:32 -06:00
|
|
|
$object = new SharedMemoryParcel(new \stdClass());
|
2015-08-09 22:16:34 -05:00
|
|
|
$this->assertFalse($object->isFreed());
|
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testFreeReleasesObject() {
|
2015-12-04 23:50:32 -06:00
|
|
|
$object = new SharedMemoryParcel(new \stdClass());
|
2015-08-09 22:16:34 -05:00
|
|
|
$object->free();
|
|
|
|
$this->assertTrue($object->isFreed());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-23 16:47:40 -05:00
|
|
|
* @expectedException \Amp\Parallel\SharedMemoryException
|
2015-08-09 22:16:34 -05:00
|
|
|
*/
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testUnwrapThrowsErrorIfFreed() {
|
2015-12-04 23:50:32 -06:00
|
|
|
$object = new SharedMemoryParcel(new \stdClass());
|
2015-08-09 22:16:34 -05:00
|
|
|
$object->free();
|
2015-08-24 23:23:42 -05:00
|
|
|
$object->unwrap();
|
2015-08-09 22:16:34 -05:00
|
|
|
}
|
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testCloneIsNewObject() {
|
2016-08-22 18:25:19 -05:00
|
|
|
$object = new \stdClass;
|
2015-12-04 23:50:32 -06:00
|
|
|
$shared = new SharedMemoryParcel($object);
|
2015-08-09 22:16:34 -05:00
|
|
|
$clone = clone $shared;
|
2015-08-10 13:21:22 -05:00
|
|
|
|
2015-08-09 22:16:34 -05:00
|
|
|
$this->assertNotSame($shared, $clone);
|
2015-08-24 23:23:42 -05:00
|
|
|
$this->assertNotSame($object, $clone->unwrap());
|
2015-08-09 22:16:34 -05:00
|
|
|
$this->assertNotEquals($shared->__debugInfo()['id'], $clone->__debugInfo()['id']);
|
2015-08-10 13:21:22 -05:00
|
|
|
|
|
|
|
$clone->free();
|
2015-08-09 22:16:34 -05:00
|
|
|
$shared->free();
|
|
|
|
}
|
2015-08-10 14:28:47 -05:00
|
|
|
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testObjectOverflowMoved() {
|
2015-12-04 23:50:32 -06:00
|
|
|
$object = new SharedMemoryParcel('hi', 14);
|
2016-08-18 17:36:58 -05:00
|
|
|
$awaitable = $object->synchronized(function () {
|
2015-10-18 02:12:46 -05:00
|
|
|
return 'hello world';
|
2016-08-18 17:36:58 -05:00
|
|
|
});
|
2017-03-16 17:03:59 -05:00
|
|
|
\Amp\Promise\wait($awaitable);
|
2015-08-10 16:36:01 -05:00
|
|
|
|
2015-08-24 23:23:42 -05:00
|
|
|
$this->assertEquals('hello world', $object->unwrap());
|
2015-08-10 16:36:01 -05:00
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
2015-08-10 14:28:47 -05:00
|
|
|
/**
|
|
|
|
* @group posix
|
2015-08-28 15:09:07 -05:00
|
|
|
* @requires extension pcntl
|
2015-08-10 14:28:47 -05:00
|
|
|
*/
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testSetInSeparateProcess() {
|
2015-12-04 23:50:32 -06:00
|
|
|
$object = new SharedMemoryParcel(42);
|
2015-08-10 14:28:47 -05:00
|
|
|
|
|
|
|
$this->doInFork(function () use ($object) {
|
2016-08-18 17:36:58 -05:00
|
|
|
$awaitable = $object->synchronized(function () {
|
2015-10-18 02:12:46 -05:00
|
|
|
return 43;
|
2016-08-18 17:36:58 -05:00
|
|
|
});
|
2017-03-16 17:03:59 -05:00
|
|
|
\Amp\Promise\wait($awaitable);
|
2015-08-10 14:28:47 -05:00
|
|
|
});
|
|
|
|
|
2015-08-24 23:23:42 -05:00
|
|
|
$this->assertEquals(43, $object->unwrap());
|
2015-08-10 14:28:47 -05:00
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group posix
|
2015-08-28 15:09:07 -05:00
|
|
|
* @requires extension pcntl
|
2015-08-10 14:28:47 -05:00
|
|
|
*/
|
2016-08-18 17:36:58 -05:00
|
|
|
public function testFreeInSeparateProcess() {
|
2015-12-04 23:50:32 -06:00
|
|
|
$object = new SharedMemoryParcel(42);
|
2015-08-10 14:28:47 -05:00
|
|
|
|
|
|
|
$this->doInFork(function () use ($object) {
|
|
|
|
$object->free();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($object->isFreed());
|
|
|
|
}
|
2015-08-09 22:16:34 -05:00
|
|
|
}
|