1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-12 09:09:35 +01:00
parallel/tests/Sync/SharedMemoryParcelTest.php

112 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace Icicle\Tests\Concurrent\Sync;
2015-12-05 06:50:32 +01:00
use Icicle\Concurrent\Sync\SharedMemoryParcel;
use Icicle\Coroutine\Coroutine;
/**
* @requires extension shmop
2015-08-31 19:26:11 +02:00
* @requires extension sysvmsg
*/
2015-12-05 06:50:32 +01:00
class SharedMemoryParcelTest extends AbstractParcelTest
{
2015-08-28 23:58:15 +02:00
private $parcel;
2015-08-28 23:58:15 +02:00
protected function createParcel($value)
{
2015-12-05 06:50:32 +01:00
$this->parcel = new SharedMemoryParcel($value);
2015-08-28 23:58:15 +02:00
return $this->parcel;
}
2015-08-28 23:58:15 +02:00
public function tearDown()
{
2015-08-28 23:58:15 +02:00
if ($this->parcel !== null) {
$this->parcel->free();
}
}
public function testNewObjectIsNotFreed()
{
2015-12-05 06:50:32 +01:00
$object = new SharedMemoryParcel(new \stdClass());
$this->assertFalse($object->isFreed());
$object->free();
}
public function testFreeReleasesObject()
{
2015-12-05 06:50:32 +01:00
$object = new SharedMemoryParcel(new \stdClass());
$object->free();
$this->assertTrue($object->isFreed());
}
/**
* @expectedException \Icicle\Concurrent\Exception\SharedMemoryException
*/
public function testUnwrapThrowsErrorIfFreed()
{
2015-12-05 06:50:32 +01:00
$object = new SharedMemoryParcel(new \stdClass());
$object->free();
$object->unwrap();
}
public function testCloneIsNewObject()
{
$object = new \stdClass();
2015-12-05 06:50:32 +01:00
$shared = new SharedMemoryParcel($object);
$clone = clone $shared;
$this->assertNotSame($shared, $clone);
$this->assertNotSame($object, $clone->unwrap());
$this->assertNotEquals($shared->__debugInfo()['id'], $clone->__debugInfo()['id']);
$clone->free();
$shared->free();
}
2015-08-10 21:28:47 +02:00
public function testObjectOverflowMoved()
{
2015-12-05 06:50:32 +01:00
$object = new SharedMemoryParcel('hi', 14);
$coroutine = new Coroutine($object->synchronized(function () {
return 'hello world';
}));
$coroutine->wait();
$this->assertEquals('hello world', $object->unwrap());
$object->free();
}
2015-08-10 21:28:47 +02:00
/**
* @group posix
* @requires extension pcntl
2015-08-10 21:28:47 +02:00
*/
public function testSetInSeparateProcess()
{
2015-12-05 06:50:32 +01:00
$object = new SharedMemoryParcel(42);
2015-08-10 21:28:47 +02:00
$this->doInFork(function () use ($object) {
$coroutine = new Coroutine($object->synchronized(function () {
return 43;
}));
$coroutine->wait();
2015-08-10 21:28:47 +02:00
});
$this->assertEquals(43, $object->unwrap());
2015-08-10 21:28:47 +02:00
$object->free();
}
/**
* @group posix
* @requires extension pcntl
2015-08-10 21:28:47 +02:00
*/
public function testFreeInSeparateProcess()
{
2015-12-05 06:50:32 +01:00
$object = new SharedMemoryParcel(42);
2015-08-10 21:28:47 +02:00
$this->doInFork(function () use ($object) {
$object->free();
});
$this->assertTrue($object->isFreed());
}
}