diff --git a/src/python/grpcio_tests/tests/unit/_metadata_code_details_test.py b/src/python/grpcio_tests/tests/unit/_metadata_code_details_test.py index a63664ac5d0..073412fc7f5 100644 --- a/src/python/grpcio_tests/tests/unit/_metadata_code_details_test.py +++ b/src/python/grpcio_tests/tests/unit/_metadata_code_details_test.py @@ -255,8 +255,8 @@ class MetadataCodeDetailsTest(unittest.TestCase): response_iterator_call = self._unary_stream( _SERIALIZED_REQUEST, metadata=_CLIENT_METADATA) - received_initial_metadata = response_iterator_call.initial_metadata() list(response_iterator_call) + received_initial_metadata = response_iterator_call.initial_metadata() self.assertTrue( test_common.metadata_transmitted( @@ -347,13 +347,17 @@ class MetadataCodeDetailsTest(unittest.TestCase): self._servicer.set_details(_DETAILS) self._servicer.set_abort_call() + response_iterator_call = self._unary_stream( _SERIALIZED_REQUEST, metadata=_CLIENT_METADATA) - received_initial_metadata = \ - response_iterator_call.initial_metadata() + # NOTE: In the single-threaded case, we cannot grab the initial_metadata + # without running the RPC first (or concurrently, in another + # thread). with self.assertRaises(grpc.RpcError): self.assertEqual(len(list(response_iterator_call)), 0) + received_initial_metadata = \ + response_iterator_call.initial_metadata() self.assertTrue( test_common.metadata_transmitted( _CLIENT_METADATA, @@ -454,9 +458,9 @@ class MetadataCodeDetailsTest(unittest.TestCase): response_iterator_call = self._unary_stream( _SERIALIZED_REQUEST, metadata=_CLIENT_METADATA) - received_initial_metadata = response_iterator_call.initial_metadata() with self.assertRaises(grpc.RpcError): list(response_iterator_call) + received_initial_metadata = response_iterator_call.initial_metadata() self.assertTrue( test_common.metadata_transmitted( @@ -547,9 +551,9 @@ class MetadataCodeDetailsTest(unittest.TestCase): response_iterator_call = self._unary_stream( _SERIALIZED_REQUEST, metadata=_CLIENT_METADATA) - received_initial_metadata = response_iterator_call.initial_metadata() with self.assertRaises(grpc.RpcError): list(response_iterator_call) + received_initial_metadata = response_iterator_call.initial_metadata() self.assertTrue( test_common.metadata_transmitted( diff --git a/src/python/grpcio_tests/tests/unit/_metadata_test.py b/src/python/grpcio_tests/tests/unit/_metadata_test.py index 892df3df08f..de7e1cca75a 100644 --- a/src/python/grpcio_tests/tests/unit/_metadata_test.py +++ b/src/python/grpcio_tests/tests/unit/_metadata_test.py @@ -202,6 +202,9 @@ class MetadataTest(unittest.TestCase): def testUnaryStream(self): multi_callable = self._channel.unary_stream(_UNARY_STREAM) call = multi_callable(_REQUEST, metadata=_INVOCATION_METADATA) + # NOTE(gnossen): In the single-threaded case, we must consume at least + # one message before the initial metadata will show up. + next(call) self.assertTrue( test_common.metadata_transmitted(_EXPECTED_INITIAL_METADATA, call.initial_metadata()))