2015-08-09 22:16:34 -05:00
|
|
|
<?php
|
|
|
|
namespace Icicle\Tests\Concurrent\Sync;
|
|
|
|
|
|
|
|
use Icicle\Concurrent\Sync\SharedObject;
|
|
|
|
use Icicle\Tests\Concurrent\TestCase;
|
|
|
|
|
|
|
|
class SharedObjectTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testConstructor()
|
|
|
|
{
|
|
|
|
$object = new SharedObject(new \stdClass());
|
|
|
|
$this->assertInternalType('object', $object->deref());
|
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDerefIsOfCorrectType()
|
|
|
|
{
|
|
|
|
$object = new SharedObject(new \stdClass());
|
|
|
|
$this->assertInstanceOf('stdClass', $object->deref());
|
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDerefIsEqual()
|
|
|
|
{
|
|
|
|
$object = new \stdClass();
|
|
|
|
$shared = new SharedObject($object);
|
|
|
|
$this->assertEquals($object, $shared->deref());
|
|
|
|
$shared->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNewObjectIsNotFreed()
|
|
|
|
{
|
|
|
|
$object = new SharedObject(new \stdClass());
|
|
|
|
$this->assertFalse($object->isFreed());
|
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFreeReleasesObject()
|
|
|
|
{
|
|
|
|
$object = new SharedObject(new \stdClass());
|
|
|
|
$object->free();
|
|
|
|
$this->assertTrue($object->isFreed());
|
|
|
|
}
|
|
|
|
|
2015-08-10 13:21:22 -05:00
|
|
|
public function testSet()
|
2015-08-09 22:16:34 -05:00
|
|
|
{
|
2015-08-10 13:21:22 -05:00
|
|
|
$shared = new SharedObject(3);
|
|
|
|
$this->assertEquals(3, $shared->deref());
|
|
|
|
|
|
|
|
$shared->set(4);
|
|
|
|
$this->assertEquals(4, $shared->deref());
|
|
|
|
|
2015-08-09 22:16:34 -05:00
|
|
|
$shared->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Icicle\Concurrent\Exception\SharedMemoryException
|
|
|
|
*/
|
|
|
|
public function testDerefThrowsErrorIfFreed()
|
|
|
|
{
|
|
|
|
$object = new SharedObject(new \stdClass());
|
|
|
|
$object->free();
|
|
|
|
$object->deref();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCloneIsNewObject()
|
|
|
|
{
|
|
|
|
$object = new \stdClass();
|
|
|
|
$shared = new SharedObject($object);
|
|
|
|
$clone = clone $shared;
|
2015-08-10 13:21:22 -05:00
|
|
|
|
2015-08-09 22:16:34 -05:00
|
|
|
$this->assertNotSame($shared, $clone);
|
|
|
|
$this->assertNotSame($object, $clone->deref());
|
|
|
|
$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
|
|
|
|
|
|
|
/**
|
|
|
|
* @group posix
|
|
|
|
*/
|
|
|
|
public function testSetInSeparateProcess()
|
|
|
|
{
|
|
|
|
$object = new SharedObject(42);
|
|
|
|
|
|
|
|
$this->doInFork(function () use ($object) {
|
|
|
|
$object->set(43);
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertEquals(43, $object->deref());
|
|
|
|
$object->free();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group posix
|
|
|
|
*/
|
|
|
|
public function testFreeInSeparateProcess()
|
|
|
|
{
|
|
|
|
$object = new SharedObject(42);
|
|
|
|
|
|
|
|
$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-09 22:16:34 -05:00
|
|
|
}
|