From af643e1a9576dcb7940835ccaeff3f7f4c754662 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Mon, 13 Mar 2023 14:56:11 +0100 Subject: [PATCH] tests: Fix unittest without `ev` extension --- tests/Lib/PromiseBoundTask.php | 10 ++++++++++ tests/SchedulerTest.php | 8 +++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/Lib/PromiseBoundTask.php b/tests/Lib/PromiseBoundTask.php index d645d19..4e6a68d 100644 --- a/tests/Lib/PromiseBoundTask.php +++ b/tests/Lib/PromiseBoundTask.php @@ -14,6 +14,9 @@ class PromiseBoundTask implements Task /** @var ExtendedPromiseInterface */ protected $promise; + /** @var int */ + protected $startedPromises = 0; + public function __construct(ExtendedPromiseInterface $promise) { $this->promise = $promise; @@ -23,8 +26,15 @@ public function __construct(ExtendedPromiseInterface $promise) $this->setUuid($uuid); } + public function getStartedPromises(): int + { + return $this->startedPromises; + } + public function run(): ExtendedPromiseInterface { + $this->startedPromises++; + return $this->promise; } } diff --git a/tests/SchedulerTest.php b/tests/SchedulerTest.php index cd85809..f58bd1d 100644 --- a/tests/SchedulerTest.php +++ b/tests/SchedulerTest.php @@ -114,8 +114,9 @@ function (Task $_, array $promises) use (&$canceledPromises): void { } ); - // Wait .001ms for the scheduler to run the task at least 2x before removing it und stopping the event loop. - Loop::addTimer(.001, function () use ($task): void { + // Wait 0.01s for the scheduler to run the task a couple of times before + // removing it und stopping the event loop. + Loop::addTimer(.01, function () use ($task): void { $this->scheduler->remove($task); Loop::stop(); @@ -125,7 +126,8 @@ function (Task $_, array $promises) use (&$canceledPromises): void { // When a task is due before the ongoing promise gets resolved, the scheduler will // not cancel it. Instead, it will start a new one for the new operations. - $this->assertCount(2, $canceledPromises, 'Scheduler::remove() did not cancel running tasks'); + $startedPromises = $task->getStartedPromises(); + $this->assertCount($startedPromises, $canceledPromises, 'Scheduler::remove() did not cancel running tasks'); $scheduledTimers = $this->scheduler->countTimers(); $scheduledTasks = $this->scheduler->count();