@@ -383,8 +383,10 @@ def test_samples_all_with_both_experiments(self):
383383 MAIN_TRANSFORM_ID , descriptor , self .primitives_coder_factory )
384384
385385 # Get the samples for the two outputs.
386- a_sampler = self .data_sampler .sampler_for_output (MAIN_TRANSFORM_ID , 0 )
387- b_sampler = self .data_sampler .sampler_for_output (MAIN_TRANSFORM_ID , 1 )
386+ # N.B. the order of the samplers is not guaranteed due to Protobuf not
387+ # guaranteeing map iteration order.
388+ first_sampler = self .data_sampler .sampler_for_output (MAIN_TRANSFORM_ID , 0 )
389+ second_sampler = self .data_sampler .sampler_for_output (MAIN_TRANSFORM_ID , 1 )
388390
389391 # Sample an exception for the output 'a', this will show up in the final
390392 # samples response.
@@ -393,19 +395,25 @@ def test_samples_all_with_both_experiments(self):
393395 raise Exception ('test' )
394396 except Exception :
395397 exc_info = sys .exc_info ()
396- a_sampler .sample_exception ('a' , exc_info , MAIN_TRANSFORM_ID , 'instid' )
398+ first_sampler .sample_exception (
399+ 'first' , exc_info , MAIN_TRANSFORM_ID , 'instid' )
397400
398401 # Sample a normal element for the output 'b', this will not show up in the
399402 # final samples response.
400- b_sampler .element_sampler .el = 'b '
401- b_sampler .element_sampler .has_element = True
403+ second_sampler .element_sampler .el = 'second '
404+ second_sampler .element_sampler .has_element = True
402405
403406 samples = self .data_sampler .wait_for_samples (['a' , 'b' ])
404407 self .assertEqual (len (samples .element_samples ), 2 )
405- self .assertTrue (
406- samples .element_samples ['a' ].elements [0 ].HasField ('exception' ))
407- self .assertFalse (
408- samples .element_samples ['b' ].elements [0 ].HasField ('exception' ))
408+ sample_elements = list (
409+ s .elements [0 ] for s in samples .element_samples .values ())
410+ num_exceptions = sum (
411+ 1 for element in sample_elements if element .HasField ('exception' ))
412+ self .assertEqual (
413+ num_exceptions ,
414+ 1 ,
415+ "Only one of the samples should have an exception, found: {}" .format (
416+ sample_elements ))
409417
410418 def test_only_sample_exceptions (self ):
411419 """Tests that the exception sampling experiment only samples exceptions."""
@@ -420,8 +428,10 @@ def test_only_sample_exceptions(self):
420428 MAIN_TRANSFORM_ID , descriptor , self .primitives_coder_factory )
421429
422430 # Get the samples for the two outputs.
423- a_sampler = self .data_sampler .sampler_for_output (MAIN_TRANSFORM_ID , 0 )
424- b_sampler = self .data_sampler .sampler_for_output (MAIN_TRANSFORM_ID , 1 )
431+ # N.B. the order of the samplers is not guaranteed due to Protobuf not
432+ # guaranteeing map iteration order.
433+ first_sampler = self .data_sampler .sampler_for_output (MAIN_TRANSFORM_ID , 0 )
434+ second_sampler = self .data_sampler .sampler_for_output (MAIN_TRANSFORM_ID , 1 )
425435
426436 # Sample an exception for the output 'a', this will show up in the final
427437 # samples response.
@@ -430,16 +440,18 @@ def test_only_sample_exceptions(self):
430440 raise Exception ('test' )
431441 except Exception :
432442 exc_info = sys .exc_info ()
433- a_sampler .sample_exception ('a' , exc_info , MAIN_TRANSFORM_ID , 'instid' )
443+ first_sampler .sample_exception (
444+ 'first' , exc_info , MAIN_TRANSFORM_ID , 'instid' )
434445
435446 # Sample a normal element for the output 'b', this will not show up in the
436447 # final samples response.
437- b_sampler .element_sampler .el = 'b '
438- b_sampler .element_sampler .has_element = True
448+ second_sampler .element_sampler .el = 'second '
449+ second_sampler .element_sampler .has_element = True
439450
440451 samples = self .data_sampler .wait_for_samples ([])
441452 self .assertEqual (len (samples .element_samples ), 1 )
442- self .assertIsNotNone (samples .element_samples ['a' ].elements [0 ].exception )
453+ value = list (samples .element_samples .values ())[0 ]
454+ self .assertIsNotNone (value .elements [0 ].exception )
443455
444456
445457class OutputSamplerTest (unittest .TestCase ):
0 commit comments