@@ -343,18 +343,38 @@ def md_thread():
343
343
344
344
with test .closure ( 'Two threads, slow callback -> different callbacks intervined' ):
345
345
"""
346
- Test correct handling of this scenario: (incorrect handling can trigger an exception)
347
- Thread A enqueues frame0
348
- Thread A enqueues frame1
349
- Thread B enqueues metadata1
350
- hadndle_frame_without_metadata( frame0 ) calls user callback in thread B context
351
- while the callback is handled thread A enqueues frame2
352
- handle_match( frame1, metadata1 ) calls user callback in thread A context
346
+ Test correct handling of intervined callbacks (incorrect handling can trigger an exception)
347
+ Based on machine and scheduler several scenarios that test this can happen:
348
+ Scenario 1:
349
+ Thread A enqueues frame0
350
+ Thread A enqueues frame1
351
+ Thread B enqueues metadata1
352
+ handle_frame_without_metadata( frame0 ) calls user callback in thread B context
353
+ while the callback is handled thread A enqueues frame2
354
+ handle_match( frame1, metadata1 ) calls user callback in thread A context
355
+ Scenario 2:
356
+ Thread A enqueues frame0
357
+ Thread B enqueues metadata1
358
+ handle_frame_without_metadata( frame0 ) calls user callback in thread B context
359
+ while the callback is handled thread A enqueues frame1
360
+ handle_match( frame1, metadata1 ) calls user callback in thread A context
361
+ Thread A enqueues frame2
362
+ Scenario 3:
363
+ Thread A enqueues frame0
364
+ Thread A enqueues frame1
365
+ Thread A enqueues frame2
366
+ handle_frame_without_metadata( frame0 ) calls user callback in thread A context
367
+ while the callback is handled thread B enqueues metadata1
368
+ handle_match( frame1, metadata1 ) calls user callback in thread B context
369
+
370
+ There is a small chance that frame1 will be appended to received_frames before frame0 so remove checks that depend on order.
353
371
"""
354
372
355
373
def frame_callback ( image , metadata ):
356
- on_frame_ready ( image , metadata ) # for reporting
357
- sleep ( 0.2 )
374
+ # Not using on_frame_ready function to avoid last_image < image_id check defined there.
375
+ log .d ( f'{ image_id (image ):-<4} > { dds .now ()} [{ threading .get_native_id ()} ] frame ready: { image = } { metadata = } ' )
376
+ received_frames .append ( frame ( image , metadata ))
377
+ sleep ( 0.1 )
358
378
log .d ( f'<{ image_id (image ):->4} { dds .now ()} [{ threading .get_native_id ()} ]' )
359
379
360
380
syncer = new_syncer ( on_frame_ready = frame_callback )
@@ -366,18 +386,16 @@ def frame_thread():
366
386
idstr = f'i{ image_id (image )} '
367
387
log .d ( f'{ idstr :>5} { dds .now ()} [{ threadid } ] enqueue { image } ' )
368
388
syncer .enqueue_frame ( i , image )
369
- sleep ( 0.1 )
389
+ sleep ( 0.05 )
370
390
371
391
threadA = threading .Thread ( target = frame_thread )
372
392
threadA .start ()
373
- sleep ( 0.22 ) # Between 2nd and 3rd enqueue_frame
393
+ sleep ( 0.12 ) # Between 2nd and 3rd enqueue_frame
374
394
md = new_metadata ( 1 , time_stamp ( 1 ) )
375
395
syncer .enqueue_metadata ( 1 , md )
376
396
threadA .join ()
377
397
378
- if test .check ( len (received_frames ), 2 ):
379
- test .check_equal ( image_id ( last_image () ), 1 )
380
- test .check_equal ( md_id ( last_metadata () ), 1 )
398
+ test .check_equal ( len (received_frames ), 2 )
381
399
test .check_equal ( len (dropped_metadata ), 0 )
382
400
383
401
0 commit comments