77import time
88import unittest
99
10- from urllib3 .exceptions import MaxRetryError , ConnectTimeoutError
10+ from urllib3 .exceptions import MaxRetryError , ConnectTimeoutError , TimeoutError as Url3TimeoutError
1111
1212from influxdb_client_3 import InfluxDBClient3 , write_client_options , WriteOptions , SYNCHRONOUS , flight_client_options , \
1313 WriteType
@@ -251,22 +251,14 @@ def test_get_server_version(self):
251251 version = self .client .get_server_version ()
252252 assert version is not None
253253
254- # TODO set sync test, also investigate behavior with batcher and retry
255- # TODO do these need to be run with integration - won't mock suffice?
256254 def test_write_timeout_sync (self ):
257255
258- ErrorRecord = None
259- def set_error_record (error ):
260- nonlocal ErrorRecord
261- ErrorRecord = error
262-
263- with pytest .raises (ConnectTimeoutError ) as e :
256+ with pytest .raises (Url3TimeoutError ):
264257 localClient = InfluxDBClient3 (
265258 host = self .host ,
266259 database = self .database ,
267260 token = self .token ,
268- write_client_options = flight_client_options (
269- error_callback = set_error_record ,
261+ write_client_options = write_client_options (
270262 write_options = WriteOptions (
271263 max_retry_time = 0 ,
272264 timeout = 20 ,
@@ -277,83 +269,105 @@ def set_error_record(error):
277269
278270 localClient .write ("test_write_timeout,location=harfa fVal=3.14,iVal=42i" )
279271
280-
281- @pytest .mark .skip (reason = "placeholder - partially implemented" )
282272 @asyncio_run
283273 async def test_write_timeout_async (self ):
284- # fco = flight_client_options(max_retries=10, timeout=30_000)
285- # print(f"DEBUG fco: {fco}")
286- # TODO ensure API can handle either callback or thrown exception
287- # TODO asserts based on solution
288274
289- ErrorRecord = None
290- def set_error_record (error ):
291- nonlocal ErrorRecord
292- ErrorRecord = error
275+ with pytest .raises (Url3TimeoutError ):
276+ localClient = InfluxDBClient3 (
277+ host = self .host ,
278+ database = self .database ,
279+ token = self .token ,
280+ write_client_options = write_client_options (
281+ # error_callback=set_error_record,
282+ write_options = WriteOptions (
283+ max_retry_time = 0 , # disable retries
284+ timeout = 20 ,
285+ write_type = WriteType .asynchronous
286+ )
287+ )
288+ )
289+
290+ applyResult = localClient .write ("test_write_timeout,location=harfa fVal=3.14,iVal=42i" )
291+ applyResult .get ()
293292
293+ def test_write_timeout_batching (self ):
294+
295+ ErrorResult = {"rt" : None , "rd" : None , "rx" : None }
296+
297+ def set_error_result (rt , rd , rx ):
298+ nonlocal ErrorResult
299+ ErrorResult = {"rt" : rt , "rd" : rd , "rx" : rx }
294300
295301 localClient = InfluxDBClient3 (
296302 host = self .host ,
297303 database = self .database ,
298304 token = self .token ,
299- write_client_options = flight_client_options (
300- error_callback = set_error_record ,
305+ write_client_options = write_client_options (
306+ error_callback = set_error_result ,
301307 write_options = WriteOptions (
302- max_retry_time = 0 ,
308+ max_retry_time = 0 , # disable retries
303309 timeout = 20 ,
304- write_type = WriteType .asynchronous
310+ write_type = WriteType .batching ,
311+ max_retries = 1 ,
312+ batch_size = 1 ,
305313 )
306314 )
307315 )
316+ lp = "test_write_timeout,location=harfa fVal=3.14,iVal=42i"
317+ localClient .write (lp )
308318
309- print (f"DEBUG localClient._write_client_options: { localClient ._write_client_options ['write_options' ].__dict__ } " )
310- print (f"DEBUG localClient._client._base._Configuration { localClient ._client .conf .timeout } " )
311-
312- applyResult = localClient .write ("test_write_timeout,location=harfa fVal=3.14,iVal=42i" )
313- print (f"DEBUG applyResult: { applyResult } " )
314- result = applyResult .get ()
315- print (f"DEBUG result: { result } " )
319+ # wait for batcher attempt last write retry
320+ time .sleep (0.1 )
316321
322+ assert ErrorResult ["rt" ] == (self .database , 'default' , 'ns' )
323+ assert ErrorResult ["rd" ] is not None
324+ assert isinstance (ErrorResult ["rd" ], bytes )
325+ assert ErrorResult ["rd" ].decode ('utf-8' ) == lp
326+ assert ErrorResult ["rx" ] is not None
327+ assert isinstance (ErrorResult ["rx" ], MaxRetryError )
328+ mre = ErrorResult ["rx" ]
329+ assert isinstance (mre .reason , Url3TimeoutError )
317330
318- def test_write_timeout_batching (self ):
331+ def test_write_timeout_retry (self ):
319332
320333 ErrorResult = {"rt" : None , "rd" : None , "rx" : None }
321-
322334 def set_error_result (rt , rd , rx ):
323335 nonlocal ErrorResult
324336 ErrorResult = {"rt" : rt , "rd" : rd , "rx" : rx }
325337
338+ retry_ct = 0
339+ def retry_cb (args , data , excp ):
340+ nonlocal retry_ct
341+ retry_ct += 1
342+
326343 localClient = InfluxDBClient3 (
327344 host = self .host ,
328345 database = self .database ,
329346 token = self .token ,
330- write_client_options = flight_client_options (
347+ write_client_options = write_client_options (
331348 error_callback = set_error_result ,
349+ retry_callback = retry_cb ,
332350 write_options = WriteOptions (
333- max_retry_time = 0 ,
351+ max_retry_time = 10000 ,
352+ max_retry_delay = 100 ,
353+ retry_interval = 100 ,
334354 timeout = 20 ,
335- write_type = WriteType .batching ,
336- max_retries = 1 ,
355+ max_retries = 3 ,
337356 batch_size = 1 ,
338357 )
339358 )
340359 )
360+
341361 lp = "test_write_timeout,location=harfa fVal=3.14,iVal=42i"
342362 localClient .write (lp )
363+ time .sleep (1 ) # await all retries
343364
344- # wait for batcher attempt last write retry
345- time .sleep (0.1 )
346-
365+ assert retry_ct == 3
347366 assert ErrorResult ["rt" ] == (self .database , 'default' , 'ns' )
348367 assert ErrorResult ["rd" ] is not None
349368 assert isinstance (ErrorResult ["rd" ], bytes )
350369 assert ErrorResult ["rd" ].decode ('utf-8' ) == lp
351370 assert ErrorResult ["rx" ] is not None
352371 assert isinstance (ErrorResult ["rx" ], MaxRetryError )
353372 mre = ErrorResult ["rx" ]
354- assert isinstance (mre .reason , ConnectTimeoutError )
355-
356- @pytest .mark .skip ("place holder" )
357- def test_write_timeout_retry (self ):
358- # TODO
359- print ("DEBUG test_write_timeout_retry" )
373+ assert isinstance (mre .reason , Url3TimeoutError )
0 commit comments