1111
1212namespace Symfony \AI \Platform \Tests ;
1313
14- use PHPUnit \Framework \Attributes \Group ;
1514use PHPUnit \Framework \TestCase ;
1615use Psr \Log \LoggerInterface ;
1716use Symfony \AI \Platform \Exception \RuntimeException ;
2524use Symfony \AI \Platform \ResultConverterInterface ;
2625use Symfony \AI \Platform \Test \InMemoryPlatform ;
2726use Symfony \Component \Clock \MonotonicClock ;
27+ use Symfony \Component \RateLimiter \RateLimiterFactory ;
28+ use Symfony \Component \RateLimiter \Storage \InMemoryStorage ;
2829use Symfony \Contracts \HttpClient \ResponseInterface ;
2930
30- #[Group('time-sensitive ' )]
3131final class FailoverPlatformTest extends TestCase
3232{
3333 public function testPlatformCannotPerformInvokeWithoutRemainingPlatform ()
@@ -46,7 +46,12 @@ public function testPlatformCannotPerformInvokeWithoutRemainingPlatform()
4646 $ failoverPlatform = new FailoverPlatform ([
4747 $ failedPlatform ,
4848 $ mainPlatform ,
49- ], logger: $ logger );
49+ ], new RateLimiterFactory ([
50+ 'policy ' => 'sliding_window ' ,
51+ 'id ' => 'failover ' ,
52+ 'interval ' => '60 seconds ' ,
53+ 'limit ' => 3 ,
54+ ], new InMemoryStorage ()), logger: $ logger );
5055
5156 $ this ->expectException (RuntimeException::class);
5257 $ this ->expectExceptionMessage ('All platforms failed. ' );
@@ -70,7 +75,12 @@ public function testPlatformCannotRetrieveModelCatalogWithoutRemainingPlatform()
7075 $ failoverPlatform = new FailoverPlatform ([
7176 $ failedPlatform ,
7277 $ mainPlatform ,
73- ], logger: $ logger );
78+ ], new RateLimiterFactory ([
79+ 'policy ' => 'sliding_window ' ,
80+ 'id ' => 'failover ' ,
81+ 'interval ' => '60 seconds ' ,
82+ 'limit ' => 3 ,
83+ ], new InMemoryStorage ()), logger: $ logger );
7484
7585 $ this ->expectException (RuntimeException::class);
7686 $ this ->expectExceptionMessage ('All platforms failed. ' );
@@ -90,7 +100,12 @@ public function testPlatformCanPerformInvokeWithRemainingPlatform()
90100 $ failoverPlatform = new FailoverPlatform ([
91101 $ failedPlatform ,
92102 new InMemoryPlatform (static fn (): string => 'foo ' ),
93- ], logger: $ logger );
103+ ], new RateLimiterFactory ([
104+ 'policy ' => 'sliding_window ' ,
105+ 'id ' => 'failover ' ,
106+ 'interval ' => '60 seconds ' ,
107+ 'limit ' => 3 ,
108+ ], new InMemoryStorage ()), logger: $ logger );
94109
95110 $ result = $ failoverPlatform ->invoke ('foo ' , 'foo ' );
96111
@@ -109,7 +124,12 @@ public function testPlatformCanRetrieveModelCatalogWithRemainingPlatform()
109124 $ failoverPlatform = new FailoverPlatform ([
110125 $ failedPlatform ,
111126 new InMemoryPlatform (static fn (): string => 'foo ' ),
112- ], logger: $ logger );
127+ ], new RateLimiterFactory ([
128+ 'policy ' => 'sliding_window ' ,
129+ 'id ' => 'failover ' ,
130+ 'interval ' => '60 seconds ' ,
131+ 'limit ' => 3 ,
132+ ], new InMemoryStorage ()), logger: $ logger );
113133
114134 $ this ->assertInstanceOf (FallbackModelCatalog::class, $ failoverPlatform ->getModelCatalog ());
115135 }
@@ -142,7 +162,12 @@ public function testPlatformCanPerformInvokeWhileRemovingPlatformAfterRetryPerio
142162 $ failoverPlatform = new FailoverPlatform ([
143163 $ failedPlatform ,
144164 new InMemoryPlatform (static fn (): string => 'bar ' ),
145- ], clock: $ clock , retryPeriod: 1 , logger: $ logger );
165+ ], new RateLimiterFactory ([
166+ 'policy ' => 'sliding_window ' ,
167+ 'id ' => 'failover ' ,
168+ 'interval ' => '2 seconds ' ,
169+ 'limit ' => 1 ,
170+ ], new InMemoryStorage ()), logger: $ logger );
146171
147172 $ firstResult = $ failoverPlatform ->invoke ('foo ' , 'foo ' );
148173
@@ -177,7 +202,12 @@ public function testPlatformCanRetrieveModelCatalogWhileRemovingPlatformAfterRet
177202 $ failoverPlatform = new FailoverPlatform ([
178203 $ failedPlatform ,
179204 new InMemoryPlatform (static fn (): string => 'bar ' ),
180- ], clock: $ clock , retryPeriod: 1 , logger: $ logger );
205+ ], new RateLimiterFactory ([
206+ 'policy ' => 'sliding_window ' ,
207+ 'id ' => 'failover ' ,
208+ 'interval ' => '60 seconds ' ,
209+ 'limit ' => 1 ,
210+ ], new InMemoryStorage ()), logger: $ logger );
181211
182212 $ this ->assertInstanceOf (FallbackModelCatalog::class, $ failoverPlatform ->getModelCatalog ());
183213
@@ -221,12 +251,17 @@ public function testPlatformCannotPerformInvokeWhileAllPlatformFailedDuringRetry
221251 });
222252
223253 $ logger = $ this ->createMock (LoggerInterface::class);
224- $ logger ->expects ($ this ->exactly (2 ))->method ('error ' );
254+ $ logger ->expects ($ this ->exactly (1 ))->method ('error ' );
225255
226256 $ failoverPlatform = new FailoverPlatform ([
227257 $ failedPlatform ,
228258 $ firstPlatform ,
229- ], clock: $ clock , retryPeriod: 6 , logger: $ logger );
259+ ], new RateLimiterFactory ([
260+ 'policy ' => 'sliding_window ' ,
261+ 'id ' => 'failover ' ,
262+ 'interval ' => '60 seconds ' ,
263+ 'limit ' => 3 ,
264+ ], new InMemoryStorage ()), logger: $ logger );
230265
231266 $ firstResult = $ failoverPlatform ->invoke ('foo ' , 'foo ' );
232267
@@ -269,12 +304,17 @@ public function testPlatformCannotRetrieveModelCatalogWhileAllPlatformFailedDuri
269304 });
270305
271306 $ logger = $ this ->createMock (LoggerInterface::class);
272- $ logger ->expects ($ this ->exactly (2 ))->method ('error ' );
307+ $ logger ->expects ($ this ->exactly (1 ))->method ('error ' );
273308
274309 $ failoverPlatform = new FailoverPlatform ([
275310 $ failedPlatform ,
276311 $ firstPlatform ,
277- ], clock: $ clock , retryPeriod: 6 , logger: $ logger );
312+ ], new RateLimiterFactory ([
313+ 'policy ' => 'sliding_window ' ,
314+ 'id ' => 'failover ' ,
315+ 'interval ' => '60 seconds ' ,
316+ 'limit ' => 3 ,
317+ ], new InMemoryStorage ()), logger: $ logger );
278318
279319 $ this ->assertInstanceOf (FallbackModelCatalog::class, $ failoverPlatform ->getModelCatalog ());
280320
0 commit comments