@@ -171,6 +171,75 @@ def test_force_flush_flushes_telemetry(
171171 exporter .export .assert_called_once_with ([telemetry for _ in range (10 )])
172172 batch_processor .shutdown ()
173173
174+ # pylint: disable=no-self-use
175+ def test_force_flush_returns_true_when_all_exported (
176+ self , batch_processor_class , telemetry
177+ ):
178+ exporter = Mock ()
179+ batch_processor = batch_processor_class (
180+ exporter ,
181+ max_queue_size = 15 ,
182+ max_export_batch_size = 15 ,
183+ schedule_delay_millis = 30000 ,
184+ export_timeout_millis = 500 ,
185+ )
186+ for _ in range (10 ):
187+ batch_processor ._batch_processor .emit (telemetry )
188+ result = batch_processor .force_flush (timeout_millis = 5000 )
189+ assert result is True
190+ exporter .export .assert_called_once ()
191+ batch_processor .shutdown ()
192+
193+ # pylint: disable=no-self-use
194+ def test_force_flush_returns_false_when_timeout_exceeded (
195+ self , batch_processor_class , telemetry
196+ ):
197+ call_count = 0
198+
199+ def slow_export (batch ):
200+ nonlocal call_count
201+ call_count += 1
202+ # Sleep long enough that the deadline is exceeded after first batch.
203+ time .sleep (0.2 )
204+
205+ exporter = Mock ()
206+ exporter .export .side_effect = slow_export
207+ batch_processor = batch_processor_class (
208+ exporter ,
209+ max_queue_size = 50 ,
210+ max_export_batch_size = 1 ,
211+ schedule_delay_millis = 30000 ,
212+ export_timeout_millis = 500 ,
213+ )
214+ for _ in range (10 ):
215+ batch_processor ._batch_processor .emit (telemetry )
216+ # 100ms timeout, each export takes 200ms, so deadline is hit after first batch.
217+ result = batch_processor .force_flush (timeout_millis = 100 )
218+ assert result is False
219+ # Exporter was called at least once but not for all batches.
220+ assert 1 <= call_count < 10
221+ batch_processor .shutdown ()
222+
223+ # pylint: disable=no-self-use
224+ def test_force_flush_returns_false_when_shutdown (
225+ self , batch_processor_class , telemetry
226+ ):
227+ exporter = Mock ()
228+ batch_processor = batch_processor_class (
229+ exporter ,
230+ max_queue_size = 15 ,
231+ max_export_batch_size = 15 ,
232+ schedule_delay_millis = 30000 ,
233+ export_timeout_millis = 500 ,
234+ )
235+ batch_processor .shutdown ()
236+ for _ in range (10 ):
237+ batch_processor ._batch_processor .emit (telemetry )
238+ result = batch_processor .force_flush (timeout_millis = 5000 )
239+ assert result is False
240+ # Nothing should have been exported after shutdown.
241+ exporter .export .assert_not_called ()
242+
174243 @unittest .skipUnless (
175244 hasattr (os , "fork" ),
176245 "needs *nix" ,
0 commit comments