mirror of
https://github.com/amphp/parallel.git
synced 2025-02-24 06:42:26 +01:00
Fix parcel method name errors and tests
This commit is contained in:
parent
0e9b8cca4a
commit
d727d6a0d4
@ -210,7 +210,7 @@ class Parcel implements ParcelInterface, \Serializable
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
'id' => $this->key,
|
'id' => $this->key,
|
||||||
'object' => $this->deref(),
|
'object' => $this->unwrap(),
|
||||||
'freed' => false,
|
'freed' => false,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
|
|||||||
throw new SemaphoreException('Failed to create the semaphore.');
|
throw new SemaphoreException('Failed to create the semaphore.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->data = new SharedObject([
|
$this->data = new Parcel([
|
||||||
'locks' => (int)$maxLocks,
|
'locks' => (int)$maxLocks,
|
||||||
'waitQueue' => [],
|
'waitQueue' => [],
|
||||||
]);
|
]);
|
||||||
@ -62,7 +62,7 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
|
|||||||
$this->lock();
|
$this->lock();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$data = $this->data->deref();
|
$data = $this->data->unwrap();
|
||||||
|
|
||||||
// Attempt to acquire a lock from the semaphore. If no locks are
|
// Attempt to acquire a lock from the semaphore. If no locks are
|
||||||
// available immediately, we have a lot of work to do...
|
// available immediately, we have a lot of work to do...
|
||||||
@ -73,7 +73,7 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
|
|||||||
// thread that has been waiting longer than us.
|
// thread that has been waiting longer than us.
|
||||||
$id = mt_rand();
|
$id = mt_rand();
|
||||||
$data['waitQueue'][] = $id;
|
$data['waitQueue'][] = $id;
|
||||||
$this->data->set($data);
|
$this->data->wrap($data);
|
||||||
|
|
||||||
// Sleep for a while, giving a chance for other threads to release
|
// Sleep for a while, giving a chance for other threads to release
|
||||||
// their locks. After we finish sleeping, we can check again to see
|
// their locks. After we finish sleeping, we can check again to see
|
||||||
@ -82,7 +82,7 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
|
|||||||
$this->unlock();
|
$this->unlock();
|
||||||
yield Coroutine\sleep(self::LATENCY_TIMEOUT);
|
yield Coroutine\sleep(self::LATENCY_TIMEOUT);
|
||||||
$this->lock();
|
$this->lock();
|
||||||
$data = $this->data->deref();
|
$data = $this->data->unwrap();
|
||||||
} while ($data['locks'] <= 0 || $data['waitQueue'][0] !== $id);
|
} while ($data['locks'] <= 0 || $data['waitQueue'][0] !== $id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,10 +90,10 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
|
|||||||
// semaphore is available for us to use, so decrement the lock count
|
// semaphore is available for us to use, so decrement the lock count
|
||||||
// to mark it as taken, and return a lock object that represents the
|
// to mark it as taken, and return a lock object that represents the
|
||||||
// lock just acquired.
|
// lock just acquired.
|
||||||
$data = $this->data->deref();
|
$data = $this->data->unwrap();
|
||||||
--$data['locks'];
|
--$data['locks'];
|
||||||
$data['waitQueue'] = array_slice($data['waitQueue'], 1);
|
$data['waitQueue'] = array_slice($data['waitQueue'], 1);
|
||||||
$this->data->set($data);
|
$this->data->wrap($data);
|
||||||
|
|
||||||
yield new Lock(function (Lock $lock) {
|
yield new Lock(function (Lock $lock) {
|
||||||
$this->release();
|
$this->release();
|
||||||
@ -149,9 +149,9 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
|
|||||||
{
|
{
|
||||||
$this->lock();
|
$this->lock();
|
||||||
|
|
||||||
$data = $this->data->deref();
|
$data = $this->data->unwrap();
|
||||||
++$data['locks'];
|
++$data['locks'];
|
||||||
$this->data->set($data);
|
$this->data->wrap($data);
|
||||||
|
|
||||||
$this->unlock();
|
$this->unlock();
|
||||||
}
|
}
|
||||||
|
@ -1,54 +1,54 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Icicle\Tests\Concurrent\Sync;
|
namespace Icicle\Tests\Concurrent\Sync;
|
||||||
|
|
||||||
use Icicle\Concurrent\Sync\SharedObject;
|
use Icicle\Concurrent\Sync\Parcel;
|
||||||
use Icicle\Tests\Concurrent\TestCase;
|
use Icicle\Tests\Concurrent\TestCase;
|
||||||
|
|
||||||
class SharedObjectTest extends TestCase
|
class ParcelTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testConstructor()
|
public function testConstructor()
|
||||||
{
|
{
|
||||||
$object = new SharedObject(new \stdClass());
|
$object = new Parcel(new \stdClass());
|
||||||
$this->assertInternalType('object', $object->deref());
|
$this->assertInternalType('object', $object->unwrap());
|
||||||
$object->free();
|
$object->free();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDerefIsOfCorrectType()
|
public function testUnwrapIsOfCorrectType()
|
||||||
{
|
{
|
||||||
$object = new SharedObject(new \stdClass());
|
$object = new Parcel(new \stdClass());
|
||||||
$this->assertInstanceOf('stdClass', $object->deref());
|
$this->assertInstanceOf('stdClass', $object->unwrap());
|
||||||
$object->free();
|
$object->free();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDerefIsEqual()
|
public function testUnwrapIsEqual()
|
||||||
{
|
{
|
||||||
$object = new \stdClass();
|
$object = new \stdClass();
|
||||||
$shared = new SharedObject($object);
|
$shared = new Parcel($object);
|
||||||
$this->assertEquals($object, $shared->deref());
|
$this->assertEquals($object, $shared->unwrap());
|
||||||
$shared->free();
|
$shared->free();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNewObjectIsNotFreed()
|
public function testNewObjectIsNotFreed()
|
||||||
{
|
{
|
||||||
$object = new SharedObject(new \stdClass());
|
$object = new Parcel(new \stdClass());
|
||||||
$this->assertFalse($object->isFreed());
|
$this->assertFalse($object->isFreed());
|
||||||
$object->free();
|
$object->free();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFreeReleasesObject()
|
public function testFreeReleasesObject()
|
||||||
{
|
{
|
||||||
$object = new SharedObject(new \stdClass());
|
$object = new Parcel(new \stdClass());
|
||||||
$object->free();
|
$object->free();
|
||||||
$this->assertTrue($object->isFreed());
|
$this->assertTrue($object->isFreed());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSet()
|
public function testWrap()
|
||||||
{
|
{
|
||||||
$shared = new SharedObject(3);
|
$shared = new Parcel(3);
|
||||||
$this->assertEquals(3, $shared->deref());
|
$this->assertEquals(3, $shared->unwrap());
|
||||||
|
|
||||||
$shared->set(4);
|
$shared->wrap(4);
|
||||||
$this->assertEquals(4, $shared->deref());
|
$this->assertEquals(4, $shared->unwrap());
|
||||||
|
|
||||||
$shared->free();
|
$shared->free();
|
||||||
}
|
}
|
||||||
@ -56,21 +56,21 @@ class SharedObjectTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @expectedException \Icicle\Concurrent\Exception\SharedMemoryException
|
* @expectedException \Icicle\Concurrent\Exception\SharedMemoryException
|
||||||
*/
|
*/
|
||||||
public function testDerefThrowsErrorIfFreed()
|
public function testUnwrapThrowsErrorIfFreed()
|
||||||
{
|
{
|
||||||
$object = new SharedObject(new \stdClass());
|
$object = new Parcel(new \stdClass());
|
||||||
$object->free();
|
$object->free();
|
||||||
$object->deref();
|
$object->unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCloneIsNewObject()
|
public function testCloneIsNewObject()
|
||||||
{
|
{
|
||||||
$object = new \stdClass();
|
$object = new \stdClass();
|
||||||
$shared = new SharedObject($object);
|
$shared = new Parcel($object);
|
||||||
$clone = clone $shared;
|
$clone = clone $shared;
|
||||||
|
|
||||||
$this->assertNotSame($shared, $clone);
|
$this->assertNotSame($shared, $clone);
|
||||||
$this->assertNotSame($object, $clone->deref());
|
$this->assertNotSame($object, $clone->unwrap());
|
||||||
$this->assertNotEquals($shared->__debugInfo()['id'], $clone->__debugInfo()['id']);
|
$this->assertNotEquals($shared->__debugInfo()['id'], $clone->__debugInfo()['id']);
|
||||||
|
|
||||||
$clone->free();
|
$clone->free();
|
||||||
@ -79,10 +79,10 @@ class SharedObjectTest extends TestCase
|
|||||||
|
|
||||||
public function testObjectOverflowMoved()
|
public function testObjectOverflowMoved()
|
||||||
{
|
{
|
||||||
$object = new SharedObject('hi', 14);
|
$object = new Parcel('hi', 14);
|
||||||
$object->set('hello world');
|
$object->wrap('hello world');
|
||||||
|
|
||||||
$this->assertEquals('hello world', $object->deref());
|
$this->assertEquals('hello world', $object->unwrap());
|
||||||
$object->free();
|
$object->free();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,13 +91,13 @@ class SharedObjectTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testSetInSeparateProcess()
|
public function testSetInSeparateProcess()
|
||||||
{
|
{
|
||||||
$object = new SharedObject(42);
|
$object = new Parcel(42);
|
||||||
|
|
||||||
$this->doInFork(function () use ($object) {
|
$this->doInFork(function () use ($object) {
|
||||||
$object->set(43);
|
$object->wrap(43);
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->assertEquals(43, $object->deref());
|
$this->assertEquals(43, $object->unwrap());
|
||||||
$object->free();
|
$object->free();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ class SharedObjectTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testFreeInSeparateProcess()
|
public function testFreeInSeparateProcess()
|
||||||
{
|
{
|
||||||
$object = new SharedObject(42);
|
$object = new Parcel(42);
|
||||||
|
|
||||||
$this->doInFork(function () use ($object) {
|
$this->doInFork(function () use ($object) {
|
||||||
$object->free();
|
$object->free();
|
Loading…
x
Reference in New Issue
Block a user