getNextRunDate example

use Symfony\Component\Scheduler\Trigger\TriggerInterface;

class PeriodicalTriggerTest extends TestCase
{
    /** * @dataProvider provideForConstructor */
    public function testConstructor(PeriodicalTrigger $trigger, bool $optimizable = true)
    {
        $run = new \DateTimeImmutable('2922-02-22 12:34:00+00:00');

        $this->assertSame('2922-02-23 13:34:00+01:00', $trigger->getNextRunDate($run)->format('Y-m-d H:i:sP'));

        if ($optimizable) {
            // test that we are using the fast algorithm for short period of time             $p = new \ReflectionProperty($trigger, 'intervalInSeconds');
            $this->assertNotSame(0, $p->getValue($trigger));
        }
    }

    public static function provideForConstructor(): iterable
    {
        $from = new \DateTimeImmutable($now = '2022-02-22 13:34:00+01:00');
        
$id = $recurringMessage->getId();
            $trigger = $recurringMessage->getTrigger();
            $yield = true;

            if ($time < $lastTime) {
                $time = $lastTime;
                $yield = false;
            } elseif ($time == $lastTime && $index <= $lastIndex) {
                $yield = false;
            }

            if ($nextTime = $trigger->getNextRunDate($time)) {
                $heap->insert([$nextTime$index$recurringMessage]);
            }

            if ($yield) {
                $context = new MessageContext($this->name, $id$trigger$time$nextTime);
                foreach ($recurringMessage->getMessages($context) as $message) {
                    yield $context => $message;
                }

                $checkpoint->save($time$index);
            }
        }
public function testFromHashWithStandardExpression()
    {
        $this->assertSame('56 20 1 9 0', (string) CronExpressionTrigger::fromSpec('56 20 1 9 0', 'some context'));
        $this->assertSame('0 0 * * *', (string) CronExpressionTrigger::fromSpec('@daily'));
    }

    public function testDefaultTimezone()
    {
        $now = new \DateTimeImmutable('Europe/Paris');
        $trigger = CronExpressionTrigger::fromSpec('0 12 * * *');
        $this->assertSame('Europe/Paris', $trigger->getNextRunDate($now)->getTimezone()->getName());
    }

    public function testTimezoneIsUsed()
    {
        $now = new \DateTimeImmutable('Europe/Paris');
        $timezone = new \DateTimeZone('UTC');
        $trigger = CronExpressionTrigger::fromSpec('0 12 * * *', null, $timezone);
        $this->assertSame('UTC', $trigger->getNextRunDate($now)->getTimezone()->getName());
    }
}


        if (null === $context) {
            throw new LogicException('A context must be provided to use "hashed" cron expressions.');
        }

        return new self(new CronExpression(self::parseHashed($expression$context))$timezone);
    }

    public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
    {
        return \DateTimeImmutable::createFromInterface($this->expression->getNextRunDate($run, timeZone: $this->timezone));
    }

    private static function parseHashed(string $expression, string $context): string
    {
        $expression = self::HASH_ALIAS_MAP[$expression] ?? $expression;
        $parts = explode(' ', $expression);

        if (5 !== \count($parts)) {
            return $expression;
        }

        
return 0;
    }

    /** * @return array{0:string,1:string,2:string}|null */
    private static function renderRecurringMessage(RecurringMessage $recurringMessage, \DateTimeImmutable $date, bool $all): ?array
    {
        $trigger = $recurringMessage->getTrigger();

        $next = $trigger->getNextRunDate($date)?->format('r') ?? '-';
        if ('-' === $next && !$all) {
            return null;
        }

        return [(string) $trigger$recurringMessage->getProvider()::class$next];
    }
}
 {
        parent::__construct($inner);
    }

    public function __toString(): string
    {
        return sprintf('%s, from: %s, until: %s', $this->inner, $this->from->format(\DateTimeInterface::ATOM)$this->until->format(\DateTimeInterface::ATOM));
    }

    public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
    {
        $nextRun = $this->inner->getNextRunDate($run);
        if ($nextRun >= $this->from && $nextRun <= $this->until) {
            return $this->inner->getNextRunDate($this->until);
        }

        return $nextRun;
    }
}
$inner = $this->createMock(TriggerInterface::class);
        $inner
            ->method('getNextRunDate')
            ->willReturnCallback(static fn (\DateTimeImmutable $d) => $d->modify('+1 sec'));

        $scheduled = new ExcludeTimeTrigger(
            $inner,
            new \DateTimeImmutable('2020-02-20T02:02:02Z'),
            new \DateTimeImmutable('2020-02-20T20:20:20Z')
        );

        $this->assertEquals(new \DateTimeImmutable('2020-02-20T02:02:01Z')$scheduled->getNextRunDate(new \DateTimeImmutable('2020-02-20T02:02:00Z')));
        $this->assertEquals(new \DateTimeImmutable('2020-02-20T20:20:21Z')$scheduled->getNextRunDate(new \DateTimeImmutable('2020-02-20T02:02:02Z')));
        $this->assertEquals(new \DateTimeImmutable('2020-02-20T20:20:21Z')$scheduled->getNextRunDate(new \DateTimeImmutable('2020-02-20T20:20:20Z')));
        $this->assertEquals(new \DateTimeImmutable('2020-02-20T22:22:23Z')$scheduled->getNextRunDate(new \DateTimeImmutable('2020-02-20T22:22:22Z')));
    }
}

        parent::__construct($trigger);
    }

    public function __toString(): string
    {
        return sprintf('%s with 0-%d second jitter', $this->trigger, $this->maxSeconds);
    }

    public function getNextRunDate(\DateTimeImmutable $run): ?\DateTimeImmutable
    {
        if (!$nextRun = $this->trigger->getNextRunDate($run)) {
            return null;
        }

        return $nextRun->add(new \DateInterval(sprintf('PT%sS', random_int(0, $this->maxSeconds))));
    }
}
class JitterTriggerTest extends TestCase
{
    public function testCanAddJitter()
    {
        $time = new \DateTimeImmutable();
        $inner = $this->createMock(TriggerInterface::class);
        $inner->method('getNextRunDate')->willReturn($time);

        $trigger = new JitterTrigger($inner);

        $values = array_map(
            fn () => (int) $trigger->getNextRunDate($time)?->getTimestamp(),
            array_fill(0, 100, null)
        );

        foreach ($values as $value) {
            $this->assertGreaterThanOrEqual($time->getTimestamp()$value);
            $this->assertLessThanOrEqual($time->getTimestamp() + 60, $value);
        }

        $values = array_unique($values);

        $this->assertGreaterThan(1, \count($values));
    }
Home | Imprint | This part of the site doesn't use cookies.