1717
1818import pytest
1919from grpc import RpcError
20+ from grpc import ClientCallDetails
2021from google .cloud .bigtable .data ._metrics .data_model import OperationState
2122from google .cloud .bigtable .data ._cross_sync import CrossSync
2223
@@ -103,11 +104,33 @@ def test_on_operation_cancelled(self):
103104 op .cancel ()
104105 assert op .uuid not in instance .operation_map
105106
107+ def test_strip_operation_id_metadata (self ):
108+ """After operation id is detected in metadata, the field should be stripped out before calling continuation"""
109+ from google .cloud .bigtable .data ._metrics .data_model import (
110+ OPERATION_INTERCEPTOR_METADATA_KEY ,
111+ )
112+
113+ instance = self ._make_one ()
114+ op = mock .Mock ()
115+ op .uuid = "test-uuid"
116+ op .state = OperationState .ACTIVE_ATTEMPT
117+ instance .operation_map [op .uuid ] = op
118+ continuation = CrossSync ._Sync_Impl .Mock ()
119+ details = ClientCallDetails ()
120+ details .metadata = [
121+ (OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid ),
122+ ("other_key" , "other_value" ),
123+ ]
124+ instance .intercept_unary_unary (continuation , details , mock .Mock ())
125+ assert details .metadata == [("other_key" , "other_value" )]
126+ assert continuation .call_count == 1
127+ assert continuation .call_args [0 ][0 ].metadata == [("other_key" , "other_value" )]
128+
106129 def test_unary_unary_interceptor_op_not_found (self ):
107130 """Test that interceptor call cuntinuation if op is not found"""
108131 instance = self ._make_one ()
109132 continuation = CrossSync ._Sync_Impl .Mock ()
110- details = mock . Mock ()
133+ details = ClientCallDetails ()
111134 details .metadata = []
112135 request = mock .Mock ()
113136 instance .intercept_unary_unary (continuation , details , request )
@@ -128,7 +151,7 @@ def test_unary_unary_interceptor_success(self):
128151 call = continuation .return_value
129152 call .trailing_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("a" , "b" )])
130153 call .initial_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("c" , "d" )])
131- details = mock . Mock ()
154+ details = ClientCallDetails ()
132155 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
133156 request = mock .Mock ()
134157 result = instance .intercept_unary_unary (continuation , details , request )
@@ -152,7 +175,7 @@ def test_unary_unary_interceptor_failure(self):
152175 exc .trailing_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("a" , "b" )])
153176 exc .initial_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("c" , "d" )])
154177 continuation = CrossSync ._Sync_Impl .Mock (side_effect = exc )
155- details = mock . Mock ()
178+ details = ClientCallDetails ()
156179 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
157180 request = mock .Mock ()
158181 with pytest .raises (RpcError ) as e :
@@ -178,7 +201,7 @@ def test_unary_unary_interceptor_failure_no_metadata(self):
178201 call = continuation .return_value
179202 call .trailing_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("a" , "b" )])
180203 call .initial_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("c" , "d" )])
181- details = mock . Mock ()
204+ details = ClientCallDetails ()
182205 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
183206 request = mock .Mock ()
184207 with pytest .raises (RpcError ) as e :
@@ -204,7 +227,7 @@ def test_unary_unary_interceptor_failure_generic(self):
204227 call = continuation .return_value
205228 call .trailing_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("a" , "b" )])
206229 call .initial_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("c" , "d" )])
207- details = mock . Mock ()
230+ details = ClientCallDetails ()
208231 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
209232 request = mock .Mock ()
210233 with pytest .raises (ValueError ) as e :
@@ -218,7 +241,7 @@ def test_unary_stream_interceptor_op_not_found(self):
218241 """Test that interceptor calls continuation if op is not found"""
219242 instance = self ._make_one ()
220243 continuation = CrossSync ._Sync_Impl .Mock ()
221- details = mock . Mock ()
244+ details = ClientCallDetails ()
222245 details .metadata = []
223246 request = mock .Mock ()
224247 instance .intercept_unary_stream (continuation , details , request )
@@ -243,7 +266,7 @@ def test_unary_stream_interceptor_success(self):
243266 call = continuation .return_value
244267 call .trailing_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("a" , "b" )])
245268 call .initial_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("c" , "d" )])
246- details = mock . Mock ()
269+ details = ClientCallDetails ()
247270 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
248271 request = mock .Mock ()
249272 wrapper = instance .intercept_unary_stream (continuation , details , request )
@@ -274,7 +297,7 @@ def test_unary_stream_interceptor_failure_mid_stream(self):
274297 call = continuation .return_value
275298 call .trailing_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("a" , "b" )])
276299 call .initial_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("c" , "d" )])
277- details = mock . Mock ()
300+ details = ClientCallDetails ()
278301 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
279302 request = mock .Mock ()
280303 wrapper = instance .intercept_unary_stream (continuation , details , request )
@@ -304,7 +327,7 @@ def test_unary_stream_interceptor_failure_start_stream(self):
304327 exc .initial_metadata = CrossSync ._Sync_Impl .Mock (return_value = [("c" , "d" )])
305328 continuation = CrossSync ._Sync_Impl .Mock ()
306329 continuation .side_effect = exc
307- details = mock . Mock ()
330+ details = ClientCallDetails ()
308331 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
309332 request = mock .Mock ()
310333 with pytest .raises (RpcError ) as e :
@@ -331,7 +354,7 @@ def test_unary_stream_interceptor_failure_start_stream_no_metadata(self):
331354 exc = RpcError ("test" )
332355 continuation = CrossSync ._Sync_Impl .Mock ()
333356 continuation .side_effect = exc
334- details = mock . Mock ()
357+ details = ClientCallDetails ()
335358 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
336359 request = mock .Mock ()
337360 with pytest .raises (RpcError ) as e :
@@ -358,7 +381,7 @@ def test_unary_stream_interceptor_failure_start_stream_generic(self):
358381 exc = ValueError ("test" )
359382 continuation = CrossSync ._Sync_Impl .Mock ()
360383 continuation .side_effect = exc
361- details = mock . Mock ()
384+ details = ClientCallDetails ()
362385 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
363386 request = mock .Mock ()
364387 with pytest .raises (ValueError ) as e :
@@ -387,7 +410,7 @@ def test_unary_unary_interceptor_start_operation(self, initial_state):
387410 call = continuation .return_value
388411 call .trailing_metadata = CrossSync ._Sync_Impl .Mock (return_value = [])
389412 call .initial_metadata = CrossSync ._Sync_Impl .Mock (return_value = [])
390- details = mock . Mock ()
413+ details = ClientCallDetails ()
391414 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
392415 request = mock .Mock ()
393416 instance .intercept_unary_unary (continuation , details , request )
@@ -413,7 +436,7 @@ def test_unary_stream_interceptor_start_operation(self, initial_state):
413436 call = continuation .return_value
414437 call .trailing_metadata = CrossSync ._Sync_Impl .Mock (return_value = [])
415438 call .initial_metadata = CrossSync ._Sync_Impl .Mock (return_value = [])
416- details = mock . Mock ()
439+ details = ClientCallDetails ()
417440 details .metadata = [(OPERATION_INTERCEPTOR_METADATA_KEY , op .uuid )]
418441 request = mock .Mock ()
419442 instance .intercept_unary_stream (continuation , details , request )
0 commit comments