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:
Aaron Piotrowski 2017-12-13 23:06:38 -06:00
parent 21b1a29748
commit c545be1dc5
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
8 changed files with 211 additions and 1 deletions

View File

@ -0,0 +1,19 @@
<?php
namespace Amp\Parallel\Test\Sync;
use Amp\Parallel\Sync\ChannelParser;
use Amp\PHPUnit\TestCase;
class ChannelParserTest extends TestCase {
/**
* @expectedException \Amp\Parallel\Sync\SerializationException
* @expectedExceptionMessage Exception thrown when unserializing data
*/
public function testCorruptedData() {
$data = "Invalid serialized data";
$data = \pack("CL", 0, \strlen($data)) . $data;
$parser = new ChannelParser($this->createCallback(0));
$parser->push($data);
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Amp\Parallel\Test\Sync;
use Amp\Parallel\Sync\ExitFailure;
use Amp\Parallel\Sync\PanicError;
use Amp\PHPUnit\TestCase;
class ExitFailureTest extends TestCase {
public function testGetResult() {
$message = "Test message";
$exception = new \Exception($message);
$result = new ExitFailure($exception);
try {
$result->getResult();
} catch (PanicError $caught) {
$this->assertGreaterThan(0, \stripos($caught->getMessage(), $message));
return;
}
$this->fail(\sprintf("Exception should be thrown from %s::getResult()", ExitFailure::class));
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace Amp\Parallel\Test\Sync;
use Amp\Parallel\Sync\ExitSuccess;
use Amp\PHPUnit\TestCase;
class ExitSuccessTest extends TestCase {
public function testGetResult() {
$value = 1;
$result = new ExitSuccess($value);
$this->assertSame($value, $result->getResult());
}
}

View File

@ -4,7 +4,10 @@ namespace Amp\Parallel\Test\Worker;
use Amp\Loop;
use Amp\Parallel\Worker\Pool;
use Amp\Parallel\Worker\Task;
use Amp\Parallel\Worker\Worker;
use Amp\PHPUnit\TestCase;
use Amp\Promise;
abstract class AbstractPoolTest extends TestCase {
/**
@ -43,7 +46,7 @@ abstract class AbstractPoolTest extends TestCase {
public function testWorkersIdleOnStart() {
Loop::run(function () {
$pool = $this->createPool(32);
$pool = $this->createPool();
$this->assertEquals(0, $pool->getIdleWorkerCount());
@ -84,4 +87,47 @@ abstract class AbstractPoolTest extends TestCase {
$this->assertRunTimeLessThan([$pool, 'kill'], 1000);
$this->assertFalse($pool->isRunning());
}
public function testGet() {
Loop::run(function () {
$pool = $this->createPool();
$worker = $pool->get();
$this->assertInstanceOf(Worker::class, $worker);
$this->assertFalse($worker->isRunning());
$this->assertTrue($worker->isIdle());
$this->assertSame(42, yield $worker->enqueue(new TestTask(42)));
yield $worker->shutdown();
$worker->kill();
});
}
public function testBusyPool() {
Loop::run(function () {
$pool = $this->createPool(2);
$values = [42, 56, 72];
$tasks = \array_map(function (int $value): Task {
return new TestTask($value);
}, $values);
$promises = \array_map(function (Task $task) use ($pool): Promise {
return $pool->enqueue($task);
}, $tasks);
$this->assertSame($values, yield $promises);
$promises = \array_map(function (Task $task) use ($pool): Promise {
return $pool->enqueue($task);
}, $tasks);
$this->assertSame($values, yield $promises);
yield $pool->shutdown();
});
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace Amp\Parallel\Test\Worker;
use Amp\Parallel\Worker\DefaultWorkerFactory;
use Amp\Parallel\Worker\Worker;
use Amp\PHPUnit\TestCase;
class DefaultWorkerFactoryTest extends TestCase {
/**
* @expectedException \Error
* @expectedExceptionMessage Invalid environment class name 'Invalid'
*/
public function testInvalidClassName() {
$factory = new DefaultWorkerFactory("Invalid");
}
/**
* @expectedException \Error
* @expectedExceptionMessage does not implement 'Amp\Parallel\Worker\Environment'
*/
public function testNonEnvironmentClassName() {
$factory = new DefaultWorkerFactory(DefaultWorkerFactory::class);
}
public function testCreate() {
$factory = new DefaultWorkerFactory;
$this->assertInstanceOf(Worker::class, $factory->create());
}
}

26
test/Worker/JobTest.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace Amp\Parallel\Test\Worker;
use Amp\Parallel\Worker\Internal\Job;
use Amp\PHPUnit\TestCase;
class JobTest extends TestCase {
public function testGetJob() {
$task = new TestTask(42);
$job = new Job($task);
$this->assertSame($task, $job->getTask());
}
/**
* @expectedException \Error
* @expectedExceptionMessage Classes implementing Amp\Parallel\Worker\Task must be autoloadable by the Composer autoloader
*/
public function testUnserialiableClass() {
$task = new TestTask(42);
$job = new Job($task);
$serialized = \serialize($job);
$job = \unserialize($serialized, ['allowed_classes' => [Job::class]]);
$task = $job->getTask();
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace Amp\Parallel\Test\Worker;
use Amp\Parallel\Worker\Internal\TaskFailure;
use Amp\Parallel\Worker\Worker;
use Amp\PHPUnit\TestCase;
use Amp\Promise;
class TaskFailureTest extends TestCase {
/**
* @expectedException \Amp\Parallel\Worker\TaskException
* @expectedExceptionMessage Uncaught Exception in worker
*/
public function testWithException() {
$exception = new \Exception("Message", 1);
$result = new TaskFailure('a', $exception);
Promise\wait($result->promise());
}
/**
* @expectedException \Amp\Parallel\Worker\TaskError
* @expectedExceptionMessage Uncaught Error in worker
*/
public function testWithError() {
$exception = new \Error("Message", 1);
$result = new TaskFailure('a', $exception);
Promise\wait($result->promise());
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Amp\Parallel\Test\Worker;
use Amp\Parallel\Worker\Internal\TaskSuccess;
use Amp\PHPUnit\TestCase;
use Amp\Promise;
class TaskSuccessTest extends TestCase {
public function testGetId() {
$id = 'a';
$result = new TaskSuccess($id, 1);
$this->assertSame($id, $result->getId());
}
public function testPromise() {
$value = 1;
$result = new TaskSuccess('a', $value);
$this->assertSame($value, Promise\wait($result->promise()));
}
}