Use CallableProvider in tests

This commit is contained in:
Joop Schilder 2020-12-14 12:57:07 +01:00
parent dd48e20b8f
commit a6a993db3e
3 changed files with 30 additions and 23 deletions

View File

@ -8,10 +8,13 @@ use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\Timer; use React\EventLoop\Timer\Timer;
use Toalett\Multiprocessing\Concurrency; use Toalett\Multiprocessing\Concurrency;
use Toalett\Multiprocessing\Context; use Toalett\Multiprocessing\Context;
use Toalett\Multiprocessing\Tests\Tools\CallableProvider;
use Toalett\Multiprocessing\Workers; use Toalett\Multiprocessing\Workers;
class ContextTest extends TestCase class ContextTest extends TestCase
{ {
use CallableProvider;
public function testItEmitsAnEventWhenBooted(): void public function testItEmitsAnEventWhenBooted(): void
{ {
$concurrency = $this->createMock(Concurrency::class); $concurrency = $this->createMock(Concurrency::class);
@ -52,7 +55,7 @@ class ContextTest extends TestCase
self::assertFalse($congestionRelievedEventHasTakenPlace); self::assertFalse($congestionRelievedEventHasTakenPlace);
$loop->futureTick(fn() => $context->stop()); $loop->futureTick(fn() => $context->stop());
$context->submit(static fn() => null); $context->submit(self::emptyCallable());
$context->run(); $context->run();
self::assertTrue($congestionEventHasTakenPlace); self::assertTrue($congestionEventHasTakenPlace);
@ -68,9 +71,7 @@ class ContextTest extends TestCase
$concurrency->method('isReachedBy')->willReturn(false); $concurrency->method('isReachedBy')->willReturn(false);
$loop->expects(self::once()) $loop->expects(self::once())
->method('futureTick') ->method('futureTick')
->withConsecutive([ ->with(self::emptyCallable());
static fn() => null,
]);
$context->submit(static fn() => null); $context->submit(static fn() => null);
} }
@ -83,11 +84,11 @@ class ContextTest extends TestCase
$loop->expects(self::exactly(2)) $loop->expects(self::exactly(2))
->method('addPeriodicTimer') ->method('addPeriodicTimer')
->withConsecutive( ->withConsecutive(
[Context::INTERVAL_CLEANUP, static fn() => null], [Context::INTERVAL_CLEANUP, self::emptyCallable()],
[Context::INTERVAL_GC, static fn() => null] [Context::INTERVAL_GC, self::emptyCallable()]
)->willReturnOnConsecutiveCalls( )->willReturnOnConsecutiveCalls(
new Timer(Context::INTERVAL_CLEANUP, static fn() => null), new Timer(Context::INTERVAL_CLEANUP, self::emptyCallable()),
new Timer(Context::INTERVAL_GC, static fn() => null), new Timer(Context::INTERVAL_GC, self::emptyCallable()),
); );
$context = new Context($loop, $concurrency); $context = new Context($loop, $concurrency);
@ -103,9 +104,9 @@ class ContextTest extends TestCase
$workers->expects(self::exactly(3)) $workers->expects(self::exactly(3))
->method('on') ->method('on')
->withConsecutive( ->withConsecutive(
['worker_started', static fn() => null], ['worker_started', self::emptyCallable()],
['worker_stopped', static fn() => null], ['worker_stopped', self::emptyCallable()],
['no_workers_remaining', static fn() => null] ['no_workers_remaining', self::emptyCallable()]
); );
new Context($loop, $concurrency, $workers); new Context($loop, $concurrency, $workers);

View File

@ -8,9 +8,12 @@ use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\Timer; use React\EventLoop\Timer\Timer;
use Toalett\Multiprocessing\Task\Interval; use Toalett\Multiprocessing\Task\Interval;
use Toalett\Multiprocessing\Task\RepeatedTask; use Toalett\Multiprocessing\Task\RepeatedTask;
use Toalett\Multiprocessing\Tests\Tools\CallableProvider;
class RepeatedTaskTest extends TestCase class RepeatedTaskTest extends TestCase
{ {
use CallableProvider;
/** /**
* @param $interval * @param $interval
* @dataProvider dataProvider * @dataProvider dataProvider
@ -20,10 +23,10 @@ class RepeatedTaskTest extends TestCase
$loop = $this->createMock(LoopInterface::class); $loop = $this->createMock(LoopInterface::class);
$loop->expects(self::once()) $loop->expects(self::once())
->method('addPeriodicTimer') ->method('addPeriodicTimer')
->with($interval->asFloat(), static fn() => null) ->with($interval->asFloat(), self::emptyCallable())
->willReturn(new Timer($interval->asFloat(), static fn() => null, true)); ->willReturn(new Timer($interval->asFloat(), self::emptyCallable(), true));
$task = new RepeatedTask($interval, static fn() => null); $task = new RepeatedTask($interval, self::emptyCallable());
$task->enable($loop); $task->enable($loop);
} }

View File

@ -7,10 +7,13 @@ use ReflectionObject;
use Toalett\Multiprocessing\ProcessControl\Fork; use Toalett\Multiprocessing\ProcessControl\Fork;
use Toalett\Multiprocessing\ProcessControl\ProcessControl; use Toalett\Multiprocessing\ProcessControl\ProcessControl;
use Toalett\Multiprocessing\ProcessControl\Wait; use Toalett\Multiprocessing\ProcessControl\Wait;
use Toalett\Multiprocessing\Tests\Tools\CallableProvider;
use Toalett\Multiprocessing\Workers; use Toalett\Multiprocessing\Workers;
class WorkersTest extends TestCase class WorkersTest extends TestCase
{ {
use CallableProvider;
public function testItSaysItIsEmptyWhenNoWorkers(): void public function testItSaysItIsEmptyWhenNoWorkers(): void
{ {
$processControl = $this->createMock(ProcessControl::class); $processControl = $this->createMock(ProcessControl::class);
@ -22,7 +25,7 @@ class WorkersTest extends TestCase
{ {
$workers = new Workers(); $workers = new Workers();
$workers->createWorkerFor(fn() => exit(0), []); $workers->createWorkerFor(self::emptyCallable(), []);
self::assertCount(1, $workers); self::assertCount(1, $workers);
} }
@ -30,11 +33,11 @@ class WorkersTest extends TestCase
{ {
$workers = new Workers(); $workers = new Workers();
$workers->createWorkerFor(fn() => exit(0), []); $workers->createWorkerFor(self::emptyCallable(), []);
$workers->createWorkerFor(fn() => exit(0), []); $workers->createWorkerFor(self::emptyCallable(), []);
self::assertCount(2, $workers); self::assertCount(2, $workers);
$workers->createWorkerFor(fn() => exit(0), []); $workers->createWorkerFor(self::emptyCallable(), []);
self::assertCount(3, $workers); self::assertCount(3, $workers);
$workers->stop(); $workers->stop();
@ -51,7 +54,7 @@ class WorkersTest extends TestCase
}); });
self::assertFalse($workerStartedEventHasTakenPlace); self::assertFalse($workerStartedEventHasTakenPlace);
$workers->createWorkerFor(fn() => exit(0), []); $workers->createWorkerFor(self::emptyCallable(), []);
self::assertTrue($workerStartedEventHasTakenPlace); self::assertTrue($workerStartedEventHasTakenPlace);
} }
@ -59,8 +62,8 @@ class WorkersTest extends TestCase
{ {
$workers = new Workers(); $workers = new Workers();
$reflector = new ReflectionObject($workers); $reflector = new ReflectionObject($workers);
$method = $reflector->getMethod('remove'); $remove = $reflector->getMethod('remove');
$method->setAccessible(true); $remove->setAccessible(true);
$workerStoppedEventHasTakenPlace = false; $workerStoppedEventHasTakenPlace = false;
$workers->on('worker_stopped', function () use (&$workerStoppedEventHasTakenPlace) { $workers->on('worker_stopped', function () use (&$workerStoppedEventHasTakenPlace) {
@ -68,7 +71,7 @@ class WorkersTest extends TestCase
}); });
self::assertFalse($workerStoppedEventHasTakenPlace); self::assertFalse($workerStoppedEventHasTakenPlace);
$method->invoke($workers, 0); $remove->invoke($workers, 0);
self::assertTrue($workerStoppedEventHasTakenPlace); self::assertTrue($workerStoppedEventHasTakenPlace);
} }
@ -94,7 +97,7 @@ class WorkersTest extends TestCase
->willReturn(new Fork(1)); ->willReturn(new Fork(1));
$workers = new Workers($processControl); $workers = new Workers($processControl);
$workers->createWorkerFor(fn() => []); $workers->createWorkerFor(self::emptyCallable());
} }
public function testItCallsNonBlockingWaitOnProcessControlWhenPerformingCleanup(): void public function testItCallsNonBlockingWaitOnProcessControlWhenPerformingCleanup(): void