@@ -129,24 +129,55 @@ def test_wrap_stream_errors_invocation():
129129 assert exc_info .value .response == grpc_error
130130
131131
132+ def test_wrap_stream_empty_iterator ():
133+ expected_responses = []
134+ callable_ = mock .Mock (spec = ["__call__" ], return_value = iter (expected_responses ))
135+
136+ wrapped_callable = grpc_helpers ._wrap_stream_errors (callable_ )
137+
138+ got_iterator = wrapped_callable ()
139+
140+ responses = list (got_iterator )
141+
142+ callable_ .assert_called_once_with ()
143+ assert responses == expected_responses
144+
145+
132146class RpcResponseIteratorImpl (object ):
133- def __init__ (self , exception ):
134- self ._exception = exception
147+ def __init__ (self , iterable ):
148+ self ._iterable = iter ( iterable )
135149
136150 def next (self ):
137- raise self ._exception
151+ next_item = next (self ._iterable )
152+ if isinstance (next_item , RpcErrorImpl ):
153+ raise next_item
154+ return next_item
138155
139156 __next__ = next
140157
141158
142- def test_wrap_stream_errors_iterator ():
159+ def test_wrap_stream_errors_iterator_initialization ():
143160 grpc_error = RpcErrorImpl (grpc .StatusCode .UNAVAILABLE )
144- response_iter = RpcResponseIteratorImpl (grpc_error )
161+ response_iter = RpcResponseIteratorImpl ([ grpc_error ] )
145162 callable_ = mock .Mock (spec = ["__call__" ], return_value = response_iter )
146163
147164 wrapped_callable = grpc_helpers ._wrap_stream_errors (callable_ )
148165
166+ with pytest .raises (exceptions .ServiceUnavailable ) as exc_info :
167+ wrapped_callable (1 , 2 , three = "four" )
168+
169+ callable_ .assert_called_once_with (1 , 2 , three = "four" )
170+ assert exc_info .value .response == grpc_error
171+
172+
173+ def test_wrap_stream_errors_during_iteration ():
174+ grpc_error = RpcErrorImpl (grpc .StatusCode .UNAVAILABLE )
175+ response_iter = RpcResponseIteratorImpl ([1 , grpc_error ])
176+ callable_ = mock .Mock (spec = ["__call__" ], return_value = response_iter )
177+
178+ wrapped_callable = grpc_helpers ._wrap_stream_errors (callable_ )
149179 got_iterator = wrapped_callable (1 , 2 , three = "four" )
180+ next (got_iterator )
150181
151182 with pytest .raises (exceptions .ServiceUnavailable ) as exc_info :
152183 next (got_iterator )
0 commit comments