Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/DefaultWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
private readonly EventDispatcherInterface $eventDispatcher,
private readonly LoggerInterface|null $logger = null,
) {
$this->timeMeasure = static fn () => (int)round(microtime(true) * 1000);

Check warning on line 38 in src/DefaultWorker.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "RoundingFamily": @@ @@ private readonly EventDispatcherInterface $eventDispatcher, private readonly LoggerInterface|null $logger = null, ) { - $this->timeMeasure = static fn () => (int)round(microtime(true) * 1000); + $this->timeMeasure = static fn () => (int)ceil(microtime(true) * 1000); } /** @PARAM positive-int|0 $sleepTimer in milliseconds */

Check warning on line 38 in src/DefaultWorker.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "RoundingFamily": @@ @@ private readonly EventDispatcherInterface $eventDispatcher, private readonly LoggerInterface|null $logger = null, ) { - $this->timeMeasure = static fn () => (int)round(microtime(true) * 1000); + $this->timeMeasure = static fn () => (int)floor(microtime(true) * 1000); } /** @PARAM positive-int|0 $sleepTimer in milliseconds */

Check warning on line 38 in src/DefaultWorker.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "Multiplication": @@ @@ private readonly EventDispatcherInterface $eventDispatcher, private readonly LoggerInterface|null $logger = null, ) { - $this->timeMeasure = static fn () => (int)round(microtime(true) * 1000); + $this->timeMeasure = static fn () => (int)round(microtime(true) / 1000); } /** @PARAM positive-int|0 $sleepTimer in milliseconds */

Check warning on line 38 in src/DefaultWorker.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ private readonly EventDispatcherInterface $eventDispatcher, private readonly LoggerInterface|null $logger = null, ) { - $this->timeMeasure = static fn () => (int)round(microtime(true) * 1000); + $this->timeMeasure = static fn () => (int)round(microtime(true) * 1001); } /** @PARAM positive-int|0 $sleepTimer in milliseconds */

Check warning on line 38 in src/DefaultWorker.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ private readonly EventDispatcherInterface $eventDispatcher, private readonly LoggerInterface|null $logger = null, ) { - $this->timeMeasure = static fn () => (int)round(microtime(true) * 1000); + $this->timeMeasure = static fn () => (int)round(microtime(true) * 999); } /** @PARAM positive-int|0 $sleepTimer in milliseconds */
}

/** @param positive-int|0 $sleepTimer in milliseconds */
public function run(int $sleepTimer = 1000): void

Check warning on line 42 in src/DefaultWorker.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "IncrementInteger": @@ @@ } /** @PARAM positive-int|0 $sleepTimer in milliseconds */ - public function run(int $sleepTimer = 1000): void + public function run(int $sleepTimer = 1001): void { $this->logger?->debug('Worker starting');

Check warning on line 42 in src/DefaultWorker.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ } /** @PARAM positive-int|0 $sleepTimer in milliseconds */ - public function run(int $sleepTimer = 1000): void + public function run(int $sleepTimer = 999): void { $this->logger?->debug('Worker starting');
{
$this->logger?->debug('Worker starting');

Expand All @@ -60,10 +60,10 @@
$this->eventDispatcher->dispatch(new WorkerRunningEvent($this));

if ($this->shouldStop) {
break;

Check warning on line 63 in src/DefaultWorker.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "Break_": @@ @@ $this->eventDispatcher->dispatch(new WorkerRunningEvent($this)); if ($this->shouldStop) { - break; + continue; } $sleepFor = max($sleepTimer - $ranTime, 0);
}

$sleepFor = max($sleepTimer - $ranTime, 0);

Check warning on line 66 in src/DefaultWorker.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.4, ubuntu-latest)

Escaped Mutant for Mutator "DecrementInteger": @@ @@ break; } - $sleepFor = max($sleepTimer - $ranTime, 0); + $sleepFor = max($sleepTimer - $ranTime, -1); if ($sleepFor <= 0) { continue;

if ($sleepFor <= 0) {
continue;
Expand All @@ -73,6 +73,8 @@
usleep($sleepFor * 1000);
}

$this->shouldStop = false;

$this->logger?->debug('Worker stopped');

$this->eventDispatcher->dispatch(new WorkerStoppedEvent($this));
Expand Down
35 changes: 35 additions & 0 deletions tests/Unit/DefaultWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,39 @@ static function ($stop) use (&$calls): void {

self::assertSame(1, $calls);
}

public function testRunWorkerTwice(): void
{
$calls = 0;

$worker = new DefaultWorker(
static function (callable $stop) use (&$calls): void {
$calls++;
$stop();
},
new EventDispatcher(),
);

$worker->run(0);
$worker->run(0);

self::assertSame(2, $calls);
}

public function testStopBeforeRun(): void
{
$calls = 0;

$worker = new DefaultWorker(
static function () use (&$calls): void {
$calls++;
},
new EventDispatcher(),
);

$worker->stop();
$worker->run(0);

self::assertSame(0, $calls);
}
}
Loading