Skip to content

Commit

Permalink
tests: Fix unittest without ev extension
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab authored and nilmerg committed Mar 20, 2023
1 parent f85dea3 commit af643e1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 10 additions & 0 deletions tests/Lib/PromiseBoundTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
8 changes: 5 additions & 3 deletions tests/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit af643e1

Please sign in to comment.