Fix RefreshAddressToGeographicalUnitMaterializedViewCronJobTest: use ClockInterface to handle properly now and use more random tests

This commit is contained in:
Julien Fastré 2023-07-27 23:52:38 +02:00
parent 5f6e506300
commit dd21694544
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 30 additions and 43 deletions

View File

@ -16,12 +16,15 @@ use Chill\MainBundle\Entity\CronJobExecution;
use DateInterval; use DateInterval;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Symfony\Component\Clock\ClockInterface;
use UnexpectedValueException; use UnexpectedValueException;
class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJobInterface final readonly class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJobInterface
{ {
public function __construct(private readonly Connection $connection) public function __construct(
{ private Connection $connection,
private ClockInterface $clock,
) {
} }
public function canRun(?CronJobExecution $cronJobExecution): bool public function canRun(?CronJobExecution $cronJobExecution): bool
@ -34,7 +37,7 @@ class RefreshAddressToGeographicalUnitMaterializedViewCronJob implements CronJob
throw new UnexpectedValueException(); throw new UnexpectedValueException();
} }
$now = new DateTimeImmutable('now'); $now = $this->clock->now();
return $cronJobExecution->getLastStart() < $now->sub(new DateInterval('P1D')) return $cronJobExecution->getLastStart() < $now->sub(new DateInterval('P1D'))
// introduce a random component to ensure a roll when multiple instances are hosted on same machines // introduce a random component to ensure a roll when multiple instances are hosted on same machines

View File

@ -14,7 +14,9 @@ namespace Chill\MainBundle\Tests\Services\AddressGeographicalUnit;
use Chill\MainBundle\Entity\CronJobExecution; use Chill\MainBundle\Entity\CronJobExecution;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Clock\MockClock;
/** /**
* @internal * @internal
@ -22,68 +24,50 @@ use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
*/ */
final class RefreshAddressToGeographicalUnitMaterializedViewCronJobTest extends KernelTestCase final class RefreshAddressToGeographicalUnitMaterializedViewCronJobTest extends KernelTestCase
{ {
private Connection $connection; use ProphecyTrait;
protected function setUp(): void
{
parent::bootKernel();
$this->connection = self::$container->get(Connection::class);
}
public function testCanRun(): void public function testCanRun(): void
{ {
// As the can run is executed one of ten, this should be executed at least one after // As the can run is executed one of ten, this should be executed at least one after
// 10 + 5 executions // 10 + 5 executions
$job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob( $job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob(
$this->connection $this->prophesize(Connection::class)->reveal(),
new MockClock(new DateTimeImmutable('2023-07-01T00:00:00'))
); );
$lastExecution = new CronJobExecution($job->getKey()); $hoursAgo23 = new CronJobExecution($job->getKey());
$lastExecution->setLastStart(new DateTimeImmutable('2 days ago')); $hoursAgo23->setLastStart(new DateTimeImmutable('2023-06-30T01:00:00'));
$hoursAgo25 = new CronJobExecution($job->getKey());
$hoursAgo25->setLastStart(new DateTimeImmutable('2023-06-29T23:00:00'));
$executedForFirstTime = 0; $executedForFirstTime = $executedAfter23 = $executedAfter25 = 0;
$executedAfterPreviousExecution = 0;
for ($round = 0; 20 > $round; ++$round) { for ($round = 0; 30 > $round; ++$round) {
if ($job->canRun(null)) { if ($job->canRun(null)) {
++$executedForFirstTime; ++$executedForFirstTime;
} }
if ($job->canRun($lastExecution)) { if ($job->canRun($hoursAgo23)) {
++$executedAfterPreviousExecution; ++$executedAfter23;
}
if ($job->canRun($hoursAgo25)) {
++$executedAfter25;
} }
} }
$this->assertGreaterThan(0, $executedForFirstTime); $this->assertGreaterThan(0, $executedForFirstTime);
$this->assertGreaterThan(0, $executedAfterPreviousExecution); $this->assertEquals(0, $executedAfter23);
} $this->assertGreaterThan(0, $executedAfter25);
public function testCanRunShouldReturnFalse(): void
{
// As the can run is executed one of ten, this should be executed at least one after
// 10 + 5 executions
$job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob(
$this->connection
);
$lastExecution = new CronJobExecution($job->getKey());
$lastExecution->setLastStart(new DateTimeImmutable('2 hours ago'));
$executedAfterPreviousExecution = 0;
for ($round = 0; 20 > $round; ++$round) {
if ($job->canRun($lastExecution)) {
++$executedAfterPreviousExecution;
}
}
$this->assertEquals(0, $executedAfterPreviousExecution);
} }
public function testFullRun(): void public function testFullRun(): void
{ {
self::bootKernel();
$job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob( $job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob(
$this->connection self::$container->get(Connection::class),
new MockClock()
); );
$lastExecution = new CronJobExecution($job->getKey()); $lastExecution = new CronJobExecution($job->getKey());