1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-04 02:27:55 +01:00
parallel/tests/Sync/ParcelTest.php

119 lines
2.6 KiB
PHP
Raw Normal View History

<?php
namespace Icicle\Tests\Concurrent\Sync;
use Icicle\Concurrent\Sync\Parcel;
/**
* @requires extension shmop
2015-08-31 19:26:11 +02:00
* @requires extension sysvmsg
*/
2015-08-28 23:58:15 +02:00
class ParcelTest 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-08-28 23:58:15 +02:00
$this->parcel = new Parcel($value);
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 testCloneIsNewParcel()
{
$original = $this->createParcel(1);
$clone = clone $original;
$clone->wrap(2);
$this->assertSame(1, $original->unwrap());
$this->assertSame(2, $clone->unwrap());
$clone->free();
}
public function testNewObjectIsNotFreed()
{
$object = new Parcel(new \stdClass());
$this->assertFalse($object->isFreed());
$object->free();
}
public function testFreeReleasesObject()
{
$object = new Parcel(new \stdClass());
$object->free();
$this->assertTrue($object->isFreed());
}
/**
* @expectedException \Icicle\Concurrent\Exception\SharedMemoryException
*/
public function testUnwrapThrowsErrorIfFreed()
{
$object = new Parcel(new \stdClass());
$object->free();
$object->unwrap();
}
public function testCloneIsNewObject()
{
$object = new \stdClass();
$shared = new Parcel($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()
{
$object = new Parcel('hi', 14);
$object->wrap('hello world');
$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()
{
$object = new Parcel(42);
2015-08-10 21:28:47 +02:00
$this->doInFork(function () use ($object) {
$object->wrap(43);
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()
{
$object = new Parcel(42);
2015-08-10 21:28:47 +02:00
$this->doInFork(function () use ($object) {
$object->free();
});
$this->assertTrue($object->isFreed());
}
}