@@ -486,24 +486,20 @@ def test_logs_exported_once_batch_size_reached(self):
486
486
exporter .export .assert_called_once ()
487
487
after_export = time .time_ns ()
488
488
# Shows the worker's 30 second sleep was interrupted within a second.
489
- self .assertTrue (( after_export - before_export ) < 1e9 )
489
+ self .assertLess ( after_export - before_export , 1e9 )
490
490
491
491
# pylint: disable=no-self-use
492
492
def test_logs_exported_once_schedule_delay_reached (self ):
493
493
exporter = Mock ()
494
494
log_record_processor = BatchLogRecordProcessor (
495
495
exporter = exporter ,
496
- # Should not reach this during the test, instead export should be called when delay millis is hit.
497
496
max_queue_size = 15 ,
498
497
max_export_batch_size = 15 ,
499
498
schedule_delay_millis = 100 ,
500
499
)
501
- for _ in range (15 ):
502
- log_record_processor .emit (EMPTY_LOG )
503
- time .sleep (0.11 )
504
- exporter .export .assert_has_calls (
505
- [call ([EMPTY_LOG ]) for _ in range (15 )]
506
- )
500
+ log_record_processor .emit (EMPTY_LOG )
501
+ time .sleep (0.2 )
502
+ exporter .export .assert_called_once_with ([EMPTY_LOG ])
507
503
508
504
def test_logs_flushed_before_shutdown_and_dropped_after_shutdown (self ):
509
505
exporter = Mock ()
@@ -520,13 +516,13 @@ def test_logs_flushed_before_shutdown_and_dropped_after_shutdown(self):
520
516
exporter .export .assert_called_once_with ([EMPTY_LOG ])
521
517
self .assertTrue (exporter ._stopped )
522
518
523
- with self .assertLogs (level = "WARNING " ) as log :
519
+ with self .assertLogs (level = "INFO " ) as log :
524
520
# This log should not be flushed.
525
521
log_record_processor .emit (EMPTY_LOG )
526
522
self .assertEqual (len (log .output ), 1 )
527
523
self .assertEqual (len (log .records ), 1 )
528
524
self .assertIn ("Shutdown called, ignoring log." , log .output [0 ])
529
- exporter .export .assert_called_once_with ([ EMPTY_LOG ] )
525
+ exporter .export .assert_called_once ( )
530
526
531
527
# pylint: disable=no-self-use
532
528
def test_force_flush_flushes_logs (self ):
@@ -555,6 +551,7 @@ def bulk_log_and_flush(num_logs):
555
551
with ThreadPoolExecutor (max_workers = 69 ) as executor :
556
552
for idx in range (69 ):
557
553
executor .submit (bulk_log_and_flush , idx + 1 )
554
+
558
555
executor .shutdown ()
559
556
560
557
finished_logs = exporter .get_finished_logs ()
@@ -564,16 +561,20 @@ def bulk_log_and_flush(num_logs):
564
561
hasattr (os , "fork" ),
565
562
"needs *nix" ,
566
563
)
567
- def test_batch_log_record_processor_fork (self ):
564
+ def test_batch_log_record_processor_fork_clears_logs_from_child (self ):
568
565
exporter = InMemoryLogExporter ()
569
566
log_record_processor = BatchLogRecordProcessor (
570
567
exporter ,
571
568
max_export_batch_size = 64 ,
572
569
schedule_delay_millis = 30000 ,
573
570
)
574
- # These are not expected to be flushed. Calling fork clears any logs not flushed.
571
+ # These logs should be flushed only from the parent process.
572
+ # _at_fork_reinit should be called in the child process, to
573
+ # clear these logs in the child process.
575
574
for _ in range (10 ):
576
575
log_record_processor .emit (EMPTY_LOG )
576
+
577
+ # The below test also needs this, but it can only be set once.
577
578
multiprocessing .set_start_method ("fork" )
578
579
579
580
def child (conn ):
@@ -603,8 +604,10 @@ def test_batch_log_record_processor_fork_doesnot_deadlock(self):
603
604
)
604
605
605
606
def child (conn ):
606
- for _ in range ( 100 ):
607
+ def _target ( ):
607
608
log_record_processor .emit (EMPTY_LOG )
609
+
610
+ ConcurrencyTestBase .run_with_many_threads (_target , 100 )
608
611
log_record_processor .force_flush ()
609
612
logs = exporter .get_finished_logs ()
610
613
conn .send (len (logs ) == 100 )
@@ -615,7 +618,6 @@ def child(conn):
615
618
process .start ()
616
619
self .assertTrue (parent_conn .recv ())
617
620
process .join ()
618
- self .assertTrue (len (exporter .get_finished_logs ()) == 0 )
619
621
620
622
def test_batch_log_record_processor_gc (self ):
621
623
# Given a BatchLogRecordProcessor
@@ -677,4 +679,5 @@ def formatter(record): # pylint: disable=unused-argument
677
679
mock_stdout = Mock ()
678
680
exporter = ConsoleLogExporter (out = mock_stdout , formatter = formatter )
679
681
exporter .export ([EMPTY_LOG ])
682
+
680
683
mock_stdout .write .assert_called_once_with (mock_record_str )
0 commit comments