Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Dec 14, 2017
1 parent 21b1a29 commit c545be1
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 1 deletion.
19 changes: 19 additions & 0 deletions test/Sync/ChannelParserTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
23 changes: 23 additions & 0 deletions test/Sync/ExitFailureTest.php
Original file line number Diff line number Diff line change
@@ -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));
}
}
14 changes: 14 additions & 0 deletions test/Sync/ExitSuccessTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
48 changes: 47 additions & 1 deletion test/Worker/AbstractPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

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 {
/**
Expand Down Expand Up @@ -43,7 +46,7 @@ public function testGetMaxSize() {

public function testWorkersIdleOnStart() {
Loop::run(function () {
$pool = $this->createPool(32);
$pool = $this->createPool();

$this->assertEquals(0, $pool->getIdleWorkerCount());

Expand Down Expand Up @@ -84,4 +87,47 @@ public function testKill() {
$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();
});
}
}
31 changes: 31 additions & 0 deletions test/Worker/DefaultWorkerFactoryTest.php
Original file line number Diff line number Diff line change
@@ -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 changes: 26 additions & 0 deletions test/Worker/JobTest.php
Original file line number Diff line number Diff line change
@@ -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();
}
}
30 changes: 30 additions & 0 deletions test/Worker/TaskFailureTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
21 changes: 21 additions & 0 deletions test/Worker/TaskSuccessTest.php
Original file line number Diff line number Diff line change
@@ -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()));
}
}

0 comments on commit c545be1

Please sign in to comment.