mirror of
https://github.com/danog/parallel.git
synced 2024-11-26 20:34:40 +01:00
Enhance worker tests (#43)
This commit is contained in:
parent
526916d13c
commit
4b438814f3
29
test/Worker/AbstractPoolErrorTest.php
Normal file
29
test/Worker/AbstractPoolErrorTest.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Parallel\Test\Worker;
|
||||
|
||||
use Amp\Loop;
|
||||
use Amp\Parallel\Worker\Pool;
|
||||
use Amp\PHPUnit\TestCase;
|
||||
|
||||
abstract class AbstractPoolErrorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @param int $min
|
||||
* @param int $max
|
||||
*
|
||||
* @return \Amp\Parallel\Worker\Pool
|
||||
*/
|
||||
abstract protected function createPool($max = Pool::DEFAULT_MAX_SIZE): Pool;
|
||||
|
||||
/**
|
||||
* @expectedException \Error
|
||||
* @expectedExceptionMessage Maximum size must be a non-negative integer
|
||||
*/
|
||||
public function testCreatePoolShouldThrowError()
|
||||
{
|
||||
Loop::run(function () {
|
||||
$this->createPool();
|
||||
});
|
||||
}
|
||||
}
|
@ -42,6 +42,39 @@ abstract class AbstractPoolTest extends TestCase
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Amp\Parallel\Context\StatusError
|
||||
* @expectedExceptionMessage The pool was shutdown
|
||||
*/
|
||||
public function testShutdownShouldThrowStatusError()
|
||||
{
|
||||
Loop::run(function () {
|
||||
$pool = $this->createPool();
|
||||
|
||||
$this->assertTrue($pool->isIdle());
|
||||
|
||||
yield $pool->shutdown();
|
||||
yield $pool->shutdown();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Amp\Parallel\Context\StatusError
|
||||
* @expectedExceptionMessage The pool was shutdown
|
||||
*/
|
||||
public function testPullShouldThrowStatusError()
|
||||
{
|
||||
Loop::run(function () {
|
||||
$pool = $this->createPool();
|
||||
|
||||
$this->assertTrue($pool->isIdle());
|
||||
|
||||
yield $pool->shutdown();
|
||||
|
||||
$pool->get();
|
||||
});
|
||||
}
|
||||
|
||||
public function testGetMaxSize()
|
||||
{
|
||||
$pool = $this->createPool(17);
|
||||
|
@ -60,6 +60,22 @@ abstract class AbstractWorkerTest extends TestCase
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Amp\Parallel\Context\StatusError
|
||||
* @expectedExceptionMessage The worker has been shut down
|
||||
*/
|
||||
public function testEnqueueShouldThrowStatusError()
|
||||
{
|
||||
Loop::run(function () {
|
||||
$worker = $this->createWorker();
|
||||
|
||||
$this->assertTrue($worker->isIdle());
|
||||
|
||||
yield $worker->shutdown();
|
||||
yield $worker->enqueue(new TestTask(42));
|
||||
});
|
||||
}
|
||||
|
||||
public function testEnqueue()
|
||||
{
|
||||
Loop::run(function () {
|
||||
|
@ -29,6 +29,25 @@ class BasicEnvironmentTest extends TestCase
|
||||
$this->assertNull($environment->get($key));
|
||||
}
|
||||
|
||||
public function testSetWithNullValue()
|
||||
{
|
||||
$environment = new BasicEnvironment;
|
||||
$key = "key";
|
||||
|
||||
$this->assertNull($environment->set($key, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Error
|
||||
* @expectedExceptionMessage The time-to-live must be a positive integer or null
|
||||
*/
|
||||
public function testSetShouleThrowError()
|
||||
{
|
||||
$environment = new BasicEnvironment;
|
||||
$key = "key";
|
||||
$environment->set($key, 1, 0);
|
||||
}
|
||||
|
||||
public function testArrayAccess()
|
||||
{
|
||||
$environment = new BasicEnvironment;
|
||||
|
24
test/Worker/ProcessPoolErrorTest.php
Normal file
24
test/Worker/ProcessPoolErrorTest.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Parallel\Test\Worker;
|
||||
|
||||
use Amp\Parallel\Worker\DefaultPool;
|
||||
use Amp\Parallel\Worker\Pool;
|
||||
use Amp\Parallel\Worker\WorkerFactory;
|
||||
use Amp\Parallel\Worker\WorkerProcess;
|
||||
|
||||
/**
|
||||
* @group process
|
||||
*/
|
||||
class ProcessPoolErrorTest extends AbstractPoolErrorTest
|
||||
{
|
||||
protected function createPool($max = Pool::DEFAULT_MAX_SIZE): Pool
|
||||
{
|
||||
$factory = $this->createMock(WorkerFactory::class);
|
||||
$factory->method('create')->will($this->returnCallback(function () {
|
||||
return new WorkerProcess;
|
||||
}));
|
||||
|
||||
return new DefaultPool(-1, $factory);
|
||||
}
|
||||
}
|
16
test/Worker/TaskErrorTest.php
Normal file
16
test/Worker/TaskErrorTest.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Parallel\Test\Worker;
|
||||
|
||||
use Amp\Parallel\Worker\TaskError;
|
||||
use Amp\PHPUnit\TestCase;
|
||||
|
||||
class TaskErrorTest extends TestCase
|
||||
{
|
||||
public function testGetWorkerTrace()
|
||||
{
|
||||
$taskError = new TaskError('name', 'error_message', 'error_message_trace');
|
||||
|
||||
$this->assertSame('error_message_trace', $taskError->getWorkerTrace());
|
||||
}
|
||||
}
|
23
test/Worker/TaskExceptionTest.php
Normal file
23
test/Worker/TaskExceptionTest.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Parallel\Test\Worker;
|
||||
|
||||
use Amp\Parallel\Worker\TaskException;
|
||||
use Amp\PHPUnit\TestCase;
|
||||
|
||||
class TaskExceptionTest extends TestCase
|
||||
{
|
||||
public function testGetName()
|
||||
{
|
||||
$taskException = new TaskException('task_exception_name', 'task_exception_message', 'task_trace_message');
|
||||
|
||||
$this->assertSame('task_exception_name', $taskException->getName());
|
||||
}
|
||||
|
||||
public function testGetWorkerTrace()
|
||||
{
|
||||
$taskException = new TaskException('task_exception_name', 'task_exception_message', 'task_trace_message');
|
||||
|
||||
$this->assertSame('task_trace_message', $taskException->getWorkerTrace());
|
||||
}
|
||||
}
|
16
test/Worker/WorkerExceptionTest.php
Normal file
16
test/Worker/WorkerExceptionTest.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Amp\Parallel\Test\Worker;
|
||||
|
||||
use Amp\Parallel\Worker\WorkerException;
|
||||
use Amp\PHPUnit\TestCase;
|
||||
|
||||
class WorkerExceptionTest extends TestCase
|
||||
{
|
||||
public function testConstructorShouldBeInstance()
|
||||
{
|
||||
$workerException = new WorkerException('work_exception_message');
|
||||
|
||||
$this->assertInstanceOf(WorkerException::class, $workerException);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user