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