JitterTrigger example

try {
            $description = $message instanceof \Stringable ? (string) $message : serialize($message);
        } catch (\Exception) {
            $description = $message::class;
        }

        return new self($triggernew StaticMessageProvider([$message]$description));
    }

    public function withJitter(int $maxSeconds = 60): self
    {
        return new self(new JitterTrigger($this->trigger, $maxSeconds)$this->provider);
    }

    /** * Unique identifier for this message's context. */
    public function getId(): string
    {
        if (isset($this->id)) {
            return $this->id;
        }

        
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

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);
        }

        
namespace Symfony\Component\Scheduler\Tests\Trigger;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Scheduler\Trigger\ExcludeTimeTrigger;
use Symfony\Component\Scheduler\Trigger\JitterTrigger;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

class AbstractDecoratedTriggerTest extends TestCase
{
    public function testCanGetInnerTrigger()
    {
        $trigger = new JitterTrigger($inner = $this->createMock(TriggerInterface::class));

        $this->assertSame($inner$trigger->inner());
        $this->assertSame([$trigger]iterator_to_array($trigger->decorators()));
    }

    public function testCanGetNestedInnerTrigger()
    {
        $trigger = new ExcludeTimeTrigger(
            $jitter = new JitterTrigger($inner = $this->createMock(TriggerInterface::class)),
            new \DateTimeImmutable(),
            new \DateTimeImmutable(),
        );
Home | Imprint | This part of the site doesn't use cookies.