16
16
use Symfony \Component \RateLimiter \Policy \FixedWindowLimiter ;
17
17
use Symfony \Component \RateLimiter \Storage \InMemoryStorage ;
18
18
use Symfony \Component \RateLimiter \Tests \Resources \DummyWindow ;
19
+ use Symfony \Component \RateLimiter \Util \TimeUtil ;
19
20
20
21
/**
21
22
* @group time-sensitive
@@ -49,16 +50,19 @@ public function testConsume()
49
50
$ this ->assertSame (10 , $ rateLimit ->getLimit ());
50
51
}
51
52
52
- public function testConsumeOutsideInterval ()
53
+ /**
54
+ * @dataProvider provideConsumeOutsideInterval
55
+ */
56
+ public function testConsumeOutsideInterval (string $ dateIntervalString )
53
57
{
54
- $ limiter = $ this ->createLimiter ();
58
+ $ limiter = $ this ->createLimiter ($ dateIntervalString );
55
59
56
60
// start window...
57
61
$ limiter ->consume ();
58
- // ...add a max burst at the end of the window...
59
- sleep (55 );
62
+ // ...add a max burst, 5 seconds before the end of the window...
63
+ sleep (TimeUtil:: dateIntervalToSeconds ( new \ DateInterval ( $ dateIntervalString )) - 5 );
60
64
$ limiter ->consume (9 );
61
- // ...try bursting again at the start of the next window
65
+ // ...try bursting again at the start of the next window, 10 seconds later
62
66
sleep (10 );
63
67
$ rateLimit = $ limiter ->consume (10 );
64
68
$ this ->assertEquals (0 , $ rateLimit ->getRemainingTokens ());
@@ -74,8 +78,21 @@ public function testWrongWindowFromCache()
74
78
$ this ->assertEquals (9 , $ rateLimit ->getRemainingTokens ());
75
79
}
76
80
77
- private function createLimiter (): FixedWindowLimiter
81
+ private function createLimiter (string $ dateIntervalString = 'PT1M ' ): FixedWindowLimiter
82
+ {
83
+ return new FixedWindowLimiter ('test ' , 10 , new \DateInterval ($ dateIntervalString ), $ this ->storage );
84
+ }
85
+
86
+ public function provideConsumeOutsideInterval (): \Generator
78
87
{
79
- return new FixedWindowLimiter ('test ' , 10 , new \DateInterval ('PT1M ' ), $ this ->storage );
88
+ yield ['PT15S ' ];
89
+
90
+ yield ['PT1M ' ];
91
+
92
+ yield ['PT1H ' ];
93
+
94
+ yield ['P1M ' ];
95
+
96
+ yield ['P1Y ' ];
80
97
}
81
98
}
0 commit comments