2015-08-10 05:16:34 +02: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 20:21:22 +02:00
|
|
|
public function testSet()
|
2015-08-10 05:16:34 +02:00
|
|
|
{
|
2015-08-10 20:21:22 +02:00
|
|
|
$shared = new SharedObject(3);
|
|
|
|
$this->assertEquals(3, $shared->deref());
|
|
|
|
|
|
|
|
$shared->set(4);
|
|
|
|
$this->assertEquals(4, $shared->deref());
|
|
|
|
|
2015-08-10 05:16:34 +02: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 20:21:22 +02:00
|
|
|
|
2015-08-10 05:16:34 +02:00
|
|
|
$this->assertNotSame($shared, $clone);
|
|
|
|
$this->assertNotSame($object, $clone->deref());
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
}
|