2015-08-10 05:16:34 +02:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Tests\Concurrent\Sync;
|
|
|
|
|
2015-08-25 06:23:42 +02:00
|
|
|
use Icicle\Concurrent\Sync\Parcel;
|
2015-08-10 05:16:34 +02:00
|
|
|
use Icicle\Tests\Concurrent\TestCase;
|
|
|
|
|
2015-08-28 22:09:07 +02:00
|
|
|
/**
|
|
|
|
* @requires extension shmop
|
|
|
|
*/
|
2015-08-25 06:23:42 +02:00
|
|
|
class ParcelTest extends TestCase
|
2015-08-10 05:16:34 +02:00
|
|
|
{
|
|
|
|
public function testConstructor()
|
|
|
|
{
|
2015-08-25 06:23:42 +02:00
|
|
|
$object = new Parcel(new \stdClass());
|
|
|
|
$this->assertInternalType('object', $object->unwrap());
|
2015-08-10 05:16:34 +02:00
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
2015-08-25 06:23:42 +02:00
|
|
|
public function testUnwrapIsOfCorrectType()
|
2015-08-10 05:16:34 +02:00
|
|
|
{
|
2015-08-25 06:23:42 +02:00
|
|
|
$object = new Parcel(new \stdClass());
|
|
|
|
$this->assertInstanceOf('stdClass', $object->unwrap());
|
2015-08-10 05:16:34 +02:00
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
2015-08-25 06:23:42 +02:00
|
|
|
public function testUnwrapIsEqual()
|
2015-08-10 05:16:34 +02:00
|
|
|
{
|
|
|
|
$object = new \stdClass();
|
2015-08-25 06:23:42 +02:00
|
|
|
$shared = new Parcel($object);
|
|
|
|
$this->assertEquals($object, $shared->unwrap());
|
2015-08-10 05:16:34 +02:00
|
|
|
$shared->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewObjectIsNotFreed()
|
|
|
|
{
|
2015-08-25 06:23:42 +02:00
|
|
|
$object = new Parcel(new \stdClass());
|
2015-08-10 05:16:34 +02:00
|
|
|
$this->assertFalse($object->isFreed());
|
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFreeReleasesObject()
|
|
|
|
{
|
2015-08-25 06:23:42 +02:00
|
|
|
$object = new Parcel(new \stdClass());
|
2015-08-10 05:16:34 +02:00
|
|
|
$object->free();
|
|
|
|
$this->assertTrue($object->isFreed());
|
|
|
|
}
|
|
|
|
|
2015-08-25 06:23:42 +02:00
|
|
|
public function testWrap()
|
2015-08-10 05:16:34 +02:00
|
|
|
{
|
2015-08-25 06:23:42 +02:00
|
|
|
$shared = new Parcel(3);
|
|
|
|
$this->assertEquals(3, $shared->unwrap());
|
2015-08-10 20:21:22 +02:00
|
|
|
|
2015-08-25 06:23:42 +02:00
|
|
|
$shared->wrap(4);
|
|
|
|
$this->assertEquals(4, $shared->unwrap());
|
2015-08-10 20:21:22 +02:00
|
|
|
|
2015-08-10 05:16:34 +02:00
|
|
|
$shared->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Icicle\Concurrent\Exception\SharedMemoryException
|
|
|
|
*/
|
2015-08-25 06:23:42 +02:00
|
|
|
public function testUnwrapThrowsErrorIfFreed()
|
2015-08-10 05:16:34 +02:00
|
|
|
{
|
2015-08-25 06:23:42 +02:00
|
|
|
$object = new Parcel(new \stdClass());
|
2015-08-10 05:16:34 +02:00
|
|
|
$object->free();
|
2015-08-25 06:23:42 +02:00
|
|
|
$object->unwrap();
|
2015-08-10 05:16:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCloneIsNewObject()
|
|
|
|
{
|
|
|
|
$object = new \stdClass();
|
2015-08-25 06:23:42 +02:00
|
|
|
$shared = new Parcel($object);
|
2015-08-10 05:16:34 +02:00
|
|
|
$clone = clone $shared;
|
2015-08-10 20:21:22 +02:00
|
|
|
|
2015-08-10 05:16:34 +02:00
|
|
|
$this->assertNotSame($shared, $clone);
|
2015-08-25 06:23:42 +02:00
|
|
|
$this->assertNotSame($object, $clone->unwrap());
|
2015-08-10 05:16:34 +02:00
|
|
|
$this->assertNotEquals($shared->__debugInfo()['id'], $clone->__debugInfo()['id']);
|
2015-08-10 20:21:22 +02:00
|
|
|
|
|
|
|
$clone->free();
|
2015-08-10 05:16:34 +02:00
|
|
|
$shared->free();
|
|
|
|
}
|
2015-08-10 21:28:47 +02:00
|
|
|
|
2015-08-10 23:36:01 +02:00
|
|
|
public function testObjectOverflowMoved()
|
|
|
|
{
|
2015-08-25 06:23:42 +02:00
|
|
|
$object = new Parcel('hi', 14);
|
|
|
|
$object->wrap('hello world');
|
2015-08-10 23:36:01 +02:00
|
|
|
|
2015-08-25 06:23:42 +02:00
|
|
|
$this->assertEquals('hello world', $object->unwrap());
|
2015-08-10 23:36:01 +02:00
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
public function testSetInSeparateProcess()
|
|
|
|
{
|
2015-08-25 06:23:42 +02:00
|
|
|
$object = new Parcel(42);
|
2015-08-10 21:28:47 +02:00
|
|
|
|
|
|
|
$this->doInFork(function () use ($object) {
|
2015-08-25 06:23:42 +02:00
|
|
|
$object->wrap(43);
|
2015-08-10 21:28:47 +02:00
|
|
|
});
|
|
|
|
|
2015-08-25 06:23:42 +02:00
|
|
|
$this->assertEquals(43, $object->unwrap());
|
2015-08-10 21:28:47 +02:00
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group posix
|
2015-08-28 22:09:07 +02:00
|
|
|
* @requires extension pcntl
|
2015-08-10 21:28:47 +02:00
|
|
|
*/
|
|
|
|
public function testFreeInSeparateProcess()
|
|
|
|
{
|
2015-08-25 06:23:42 +02:00
|
|
|
$object = new Parcel(42);
|
2015-08-10 21:28:47 +02:00
|
|
|
|
|
|
|
$this->doInFork(function () use ($object) {
|
|
|
|
$object->free();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($object->isFreed());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doInFork(callable $function)
|
|
|
|
{
|
|
|
|
switch (pcntl_fork()) {
|
|
|
|
case -1:
|
|
|
|
$this->fail('Failed to fork process.');
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
$status = (int)$function();
|
|
|
|
exit(0);
|
|
|
|
default:
|
|
|
|
pcntl_wait($status);
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
}
|
2015-08-10 05:16:34 +02:00
|
|
|
}
|