@@ -250,7 +250,7 @@ def test_start(self):
250250 self .assertTrue (worker ._thread .daemon )
251251 self .assertEqual (worker ._thread ._target , worker ._thread_main )
252252 self .assertEqual (worker ._thread ._name , background_thread ._WORKER_THREAD_NAME )
253- self .assertIn (worker ._close , atexit_mock .registered_funcs )
253+ self .assertIn (worker ._handle_exit , atexit_mock .registered_funcs )
254254
255255 # Calling start again should not start a new thread.
256256 current_thread = worker ._thread
@@ -291,21 +291,25 @@ def test__close(self):
291291 worker = self ._make_one (_Logger (self .NAME ))
292292
293293 self ._start_with_thread_patch (worker )
294- worker ._close ()
294+ worker ._close ("" )
295295
296296 self .assertFalse (worker .is_alive )
297297
298298 # Calling twice should not be an error
299- worker ._close ()
299+ worker ._close ("" )
300300
301301 def test__close_non_empty_queue (self ):
302302 worker = self ._make_one (_Logger (self .NAME ))
303+ msg = "My Message"
303304
304305 self ._start_with_thread_patch (worker )
305306 record = mock .Mock ()
306307 record .created = time .time ()
307308 worker .enqueue (record , "" )
308- worker ._close ()
309+
310+ with mock .patch ("sys.stderr" , new_callable = StringIO ) as stderr_mock :
311+ worker ._close (msg )
312+ self .assertIn (msg , stderr_mock .getvalue ())
309313
310314 self .assertFalse (worker .is_alive )
311315
@@ -317,11 +321,11 @@ def test__close_did_not_join(self):
317321 record = mock .Mock ()
318322 record .created = time .time ()
319323 worker .enqueue (record , "" )
320- worker ._close ()
324+ worker ._close ("" )
321325
322326 self .assertFalse (worker .is_alive )
323327
324- def test__close_main_thread_not_alive (self ):
328+ def test__handle_exit (self ):
325329 from google .cloud .logging_v2 .handlers .transports .background_thread import (
326330 _CLOSE_THREAD_SHUTDOWN_ERROR_MSG ,
327331 )
@@ -333,21 +337,45 @@ def test__close_main_thread_not_alive(self):
333337 with self ._init_atexit_mock ():
334338 self ._start_with_thread_patch (worker )
335339 self ._enqueue_record (worker , "test" )
336- worker ._close ()
340+ worker ._handle_exit ()
337341
338342 self .assertRegex (
339343 stderr_mock .getvalue (),
340344 re .compile ("^%s$" % _CLOSE_THREAD_SHUTDOWN_ERROR_MSG , re .MULTILINE ),
341345 )
342346
347+ self .assertRegex (
348+ stderr_mock .getvalue (),
349+ re .compile (r"^Failed to send %d pending logs\.$" % worker ._queue .qsize (), re .MULTILINE ),
350+ )
351+
352+ def test__handle_exit_no_items (self ):
353+ worker = self ._make_one (_Logger (self .NAME ))
354+
355+ with mock .patch ("sys.stderr" , new_callable = StringIO ) as stderr_mock :
356+ with self ._init_main_thread_is_alive_mock (False ):
357+ with self ._init_atexit_mock ():
358+ self ._start_with_thread_patch (worker )
359+ worker ._handle_exit ()
360+
361+ self .assertEqual (stderr_mock .getvalue (), "" )
362+
343363 def test_close_unregister_atexit (self ):
344364 worker = self ._make_one (_Logger (self .NAME ))
345365
346- with self ._init_atexit_mock () as atexit_mock :
347- self ._start_with_thread_patch (worker )
348- self .assertIn (worker ._close , atexit_mock .registered_funcs )
349- worker .close ()
350- self .assertNotIn (worker ._close , atexit_mock .registered_funcs )
366+ with mock .patch ("sys.stderr" , new_callable = StringIO ) as stderr_mock :
367+ with self ._init_atexit_mock () as atexit_mock :
368+ self ._start_with_thread_patch (worker )
369+ self .assertIn (worker ._handle_exit , atexit_mock .registered_funcs )
370+ worker .close ()
371+ self .assertNotIn (worker ._handle_exit , atexit_mock .registered_funcs )
372+
373+ self .assertNotRegex (
374+ stderr_mock .getvalue (),
375+ re .compile (r"^Failed to send %d pending logs\.$" % worker ._queue .qsize (), re .MULTILINE ),
376+ )
377+
378+ self .assertFalse (worker .is_alive )
351379
352380 @staticmethod
353381 def _enqueue_record (worker , message , levelno = logging .INFO , ** kw ):
0 commit comments