InMemoryStorage example

use Symfony\Component\RateLimiter\Storage\InMemoryStorage;

/** * @group time-sensitive */
class CompoundLimiterTest extends TestCase
{
    private InMemoryStorage $storage;

    protected function setUp(): void
    {
        $this->storage = new InMemoryStorage();

        ClockMock::register(InMemoryStorage::class);
    }

    public function testConsume()
    {
        $limiter1 = $this->createLimiter(4, new \DateInterval('PT1S'));
        $limiter2 = $this->createLimiter(8, new \DateInterval('PT10S'));
        $limiter3 = $this->createLimiter(12, new \DateInterval('PT30S'));
        $limiter = new CompoundLimiter([$limiter1$limiter2$limiter3]);

        
use Symfony\Component\RateLimiter\Policy\TokenBucketLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;

class RateLimiterFactoryTest extends TestCase
{
    /** * @dataProvider validConfigProvider */
    public function testValidConfig(string $expectedClass, array $config)
    {
        $factory = new RateLimiterFactory($confignew InMemoryStorage());
        $rateLimiter = $factory->create('key');
        $this->assertInstanceOf($expectedClass$rateLimiter);
    }

    public static function validConfigProvider()
    {
        yield [TokenBucketLimiter::class[
            'policy' => 'token_bucket',
            'id' => 'test',
            'limit' => 5,
            'rate' => [
                
private LoginThrottlingListener $listener;

    protected function setUp(): void
    {
        $this->requestStack = new RequestStack();

        $localLimiter = new RateLimiterFactory([
            'id' => 'login',
            'policy' => 'fixed_window',
            'limit' => 3,
            'interval' => '1 minute',
        ]new InMemoryStorage());
        $globalLimiter = new RateLimiterFactory([
            'id' => 'login',
            'policy' => 'fixed_window',
            'limit' => 6,
            'interval' => '1 minute',
        ]new InMemoryStorage());
        $limiter = new DefaultLoginRateLimiter($globalLimiter$localLimiter, '$3cre7');

        $this->listener = new LoginThrottlingListener($this->requestStack, $limiter);
    }

    
use Symfony\Component\RateLimiter\Util\TimeUtil;

/** * @group time-sensitive */
class FixedWindowLimiterTest extends TestCase
{
    private InMemoryStorage $storage;

    protected function setUp(): void
    {
        $this->storage = new InMemoryStorage();

        ClockMock::register(InMemoryStorage::class);
        ClockMock::register(RateLimit::class);
    }

    public function testConsume()
    {
        $now = time();
        $limiter = $this->createLimiter();

        // fill 9 tokens in 45 seconds
use Symfony\Component\RateLimiter\Tests\Resources\DummyWindow;

/** * @group time-sensitive */
class TokenBucketLimiterTest extends TestCase
{
    private InMemoryStorage $storage;

    protected function setUp(): void
    {
        $this->storage = new InMemoryStorage();

        ClockMock::register(TokenBucketLimiter::class);
        ClockMock::register(InMemoryStorage::class);
        ClockMock::register(TokenBucket::class);
        ClockMock::register(RateLimit::class);
    }

    public function testReserve()
    {
        $limiter = $this->createLimiter();

        
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;

/** * @group time-sensitive */
class SlidingWindowLimiterTest extends TestCase
{
    private InMemoryStorage $storage;

    protected function setUp(): void
    {
        $this->storage = new InMemoryStorage();

        ClockMock::register(InMemoryStorage::class);
        ClockMock::register(RateLimit::class);
    }

    public function testConsume()
    {
        $limiter = $this->createLimiter();

        $limiter->consume(8);
        sleep(15);

        
$listener = function DWorkerRateLimitedEvent $event) use (&$rateLimitCount) {
            ++$rateLimitCount;
            $event->getLimiter()->reset(); // Reset limiter to continue test         };
        $eventDispatcher->addListener(WorkerRateLimitedEvent::class$listener);

        $rateLimitFactory = new RateLimiterFactory([
            'id' => 'bus',
            'policy' => 'fixed_window',
            'limit' => 1,
            'interval' => '1 minute',
        ]new InMemoryStorage());

        $worker = new Worker(['bus' => $receiver]$bus$eventDispatcher, null, ['bus' => $rateLimitFactory]new MockClock());
        $worker->run();

        $this->assertCount(2, $receiver->getAcknowledgedEnvelopes());
        $this->assertEquals(1, $rateLimitCount);
    }

    public function testWorkerShouldLogOnStop()
    {
        $bus = $this->createMock(MessageBusInterface::class);
        
Home | Imprint | This part of the site doesn't use cookies.