1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 14:01:14 +01:00

More tests

This commit is contained in:
coderstephen 2015-08-28 16:58:15 -05:00
parent 9a4cdf7b40
commit 725d94f379
8 changed files with 147 additions and 34 deletions

View File

@ -0,0 +1,41 @@
<?php
namespace Icicle\Tests\Concurrent\Sync;
use Icicle\Concurrent\Sync\Parcel;
use Icicle\Tests\Concurrent\TestCase;
abstract class AbstractParcelTest extends TestCase
{
/**
* @return \Icicle\Concurrent\Sync\ParcelInterface
*/
abstract protected function createParcel($value);
public function testConstructor()
{
$object = $this->createParcel(new \stdClass());
$this->assertInternalType('object', $object->unwrap());
}
public function testUnwrapIsOfCorrectType()
{
$object = $this->createParcel(new \stdClass());
$this->assertInstanceOf('stdClass', $object->unwrap());
}
public function testUnwrapIsEqual()
{
$object = new \stdClass();
$shared = $this->createParcel($object);
$this->assertEquals($object, $shared->unwrap());
}
public function testWrap()
{
$shared = $this->createParcel(3);
$this->assertEquals(3, $shared->unwrap());
$shared->wrap(4);
$this->assertEquals(4, $shared->unwrap());
}
}

View File

@ -2,33 +2,25 @@
namespace Icicle\Tests\Concurrent\Sync;
use Icicle\Concurrent\Sync\Parcel;
use Icicle\Tests\Concurrent\TestCase;
/**
* @requires extension shmop
*/
class ParcelTest extends TestCase
class ParcelTest extends AbstractParcelTest
{
public function testConstructor()
private $parcel;
protected function createParcel($value)
{
$object = new Parcel(new \stdClass());
$this->assertInternalType('object', $object->unwrap());
$object->free();
$this->parcel = new Parcel($value);
return $this->parcel;
}
public function testUnwrapIsOfCorrectType()
public function tearDown()
{
$object = new Parcel(new \stdClass());
$this->assertInstanceOf('stdClass', $object->unwrap());
$object->free();
}
public function testUnwrapIsEqual()
{
$object = new \stdClass();
$shared = new Parcel($object);
$this->assertEquals($object, $shared->unwrap());
$shared->free();
if ($this->parcel !== null) {
$this->parcel->free();
}
}
public function testNewObjectIsNotFreed()
@ -45,17 +37,6 @@ class ParcelTest extends TestCase
$this->assertTrue($object->isFreed());
}
public function testWrap()
{
$shared = new Parcel(3);
$this->assertEquals(3, $shared->unwrap());
$shared->wrap(4);
$this->assertEquals(4, $shared->unwrap());
$shared->free();
}
/**
* @expectedException \Icicle\Concurrent\Exception\SharedMemoryException
*/

View File

@ -26,6 +26,8 @@ class ThreadedMutexTest extends TestCase
public function testAcquireMultiple()
{
Loop\loop();
$this->assertRunTimeBetween(function () {
Coroutine\create(function () {
$mutex = new ThreadedMutex();
@ -47,6 +49,6 @@ class ThreadedMutexTest extends TestCase
});
Loop\run();
}, 1.5, 1.7);
}, 1.5, 1.65);
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Icicle\Tests\Concurrent\Sync;
use Icicle\Concurrent\Sync\ThreadedParcel;
/**
* @requires extension pthreads
*/
class ThreadedParcelTest extends AbstractParcelTest
{
protected function createParcel($value)
{
return new ThreadedParcel($value);
}
}

View File

@ -32,6 +32,8 @@ class ThreadedSemaphoreTest extends TestCase
public function testAcquireMultiple()
{
Loop\loop();
$this->assertRunTimeBetween(function () {
Coroutine\create(function () {
$semaphore = new ThreadedSemaphore(1);
@ -53,6 +55,6 @@ class ThreadedSemaphoreTest extends TestCase
});
Loop\run();
}, 1.5, 1.7);
}, 1.5, 1.65);
}
}

View File

@ -7,6 +7,7 @@ use Icicle\Tests\Concurrent\TestCase;
/**
* @group threading
* @requires extension pthreads
*/
class LocalObjectTest extends TestCase
{
@ -82,9 +83,6 @@ class LocalObjectTest extends TestCase
$this->assertSame($object, $local->deref());
}
/**
* @requires extension pthreads
*/
public function testPromiseInThread()
{
$thread = \Thread::from(function () {

View File

@ -6,6 +6,7 @@ use Icicle\Tests\Concurrent\TestCase;
/**
* @group threading
* @requires extension pthreads
*/
class LocalStorageTest extends TestCase
{

View File

@ -0,0 +1,73 @@
<?php
namespace Icicle\Tests\Concurrent\Sync;
use Icicle\Concurrent\Threading\Thread;
use Icicle\Coroutine;
use Icicle\Loop;
use Icicle\Tests\Concurrent\TestCase;
/**
* @group threading
* @requires extension pthreads
*/
class ThreadTest extends TestCase
{
public function testIsRunning()
{
Coroutine\create(function () {
$thread = Thread::spawn(function () {
sleep(1);
});
$this->assertTrue($thread->isRunning());
yield $thread->join();
})->done();
Loop\run();
}
public function testKill()
{
$thread = Thread::spawn(function () {
sleep(1);
});
$thread->kill();
$this->assertFalse($thread->isRunning());
Loop\run();
}
/**
* @expectedException \Icicle\Concurrent\Exception\PanicError
*/
public function testExceptionInThreadPanics()
{
Coroutine\create(function () {
$thread = Thread::spawn(function () {
throw new \Exception('Exception in thread.');
});
yield $thread->join();
})->done();
Loop\run();
}
public function testJoinWaitsForChild()
{
Loop\loop(Loop\create());
$this->assertRunTimeBetween(function () {
Coroutine\create(function () {
$thread = Thread::spawn(function () {
sleep(1);
});
yield $thread->join();
})->done();
Loop\run();
}, 1, 1.1);
}
}