@@ -182,72 +182,6 @@ async def test_limit_retry_command(self):
182182 self .assertIn ("RetryableError" , str (error .exception ))
183183
184184
185- class TestRetryPolicy (AsyncPyMongoTestCase ):
186- async def test_retry_policy (self ):
187- capacity = 10
188- retry_policy = _RetryPolicy (_TokenBucket (capacity = capacity ))
189- self .assertEqual (retry_policy .attempts , helpers ._MAX_RETRIES )
190- self .assertEqual (retry_policy .backoff_initial , helpers ._BACKOFF_INITIAL )
191- self .assertEqual (retry_policy .backoff_max , helpers ._BACKOFF_MAX )
192- for i in range (1 , helpers ._MAX_RETRIES + 1 ):
193- self .assertTrue (await retry_policy .should_retry (i , 0 ))
194- self .assertFalse (await retry_policy .should_retry (helpers ._MAX_RETRIES + 1 , 0 ))
195- for i in range (capacity - helpers ._MAX_RETRIES ):
196- self .assertTrue (await retry_policy .should_retry (1 , 0 ))
197- # No tokens left, should not retry.
198- self .assertFalse (await retry_policy .should_retry (1 , 0 ))
199- self .assertEqual (retry_policy .token_bucket .tokens , 0 )
200-
201- # record_success should generate tokens.
202- for _ in range (int (2 / helpers .DEFAULT_RETRY_TOKEN_RETURN )):
203- await retry_policy .record_success (retry = False )
204- self .assertAlmostEqual (retry_policy .token_bucket .tokens , 2 )
205- for i in range (2 ):
206- self .assertTrue (await retry_policy .should_retry (1 , 0 ))
207- self .assertFalse (await retry_policy .should_retry (1 , 0 ))
208-
209- # Recording a successful retry should return 1 additional token.
210- await retry_policy .record_success (retry = True )
211- self .assertAlmostEqual (
212- retry_policy .token_bucket .tokens , 1 + helpers .DEFAULT_RETRY_TOKEN_RETURN
213- )
214- self .assertTrue (await retry_policy .should_retry (1 , 0 ))
215- self .assertFalse (await retry_policy .should_retry (1 , 0 ))
216- self .assertAlmostEqual (retry_policy .token_bucket .tokens , helpers .DEFAULT_RETRY_TOKEN_RETURN )
217-
218- async def test_retry_policy_csot (self ):
219- retry_policy = _RetryPolicy (_TokenBucket ())
220- self .assertTrue (await retry_policy .should_retry (1 , 0.5 ))
221- with pymongo .timeout (0.5 ):
222- self .assertTrue (await retry_policy .should_retry (1 , 0 ))
223- self .assertTrue (await retry_policy .should_retry (1 , 0.1 ))
224- # Would exceed the timeout, should not retry.
225- self .assertFalse (await retry_policy .should_retry (1 , 1.0 ))
226- self .assertTrue (await retry_policy .should_retry (1 , 1.0 ))
227-
228- @async_client_context .require_failCommand_appName
229- async def test_limit_retry_command (self ):
230- client = await self .async_rs_or_single_client ()
231- client ._retry_policy .token_bucket .tokens = 1
232- db = client .pymongo_test
233- await db .t .insert_one ({"x" : 1 })
234-
235- # Ensure command is retried once overload error.
236- fail_many = mock_overload_error .copy ()
237- fail_many ["mode" ] = {"times" : 1 }
238- async with self .fail_point (fail_many ):
239- await db .command ("find" , "t" )
240-
241- # Ensure command stops retrying when there are no tokens left.
242- fail_too_many = mock_overload_error .copy ()
243- fail_too_many ["mode" ] = {"times" : 2 }
244- async with self .fail_point (fail_too_many ):
245- with self .assertRaises (PyMongoError ) as error :
246- await db .command ("find" , "t" )
247-
248- self .assertIn ("Retryable" , str (error .exception ))
249-
250-
251185class TestRetryPolicy (AsyncPyMongoTestCase ):
252186 async def test_retry_policy (self ):
253187 capacity = 10
0 commit comments