@@ -129,24 +129,55 @@ def test_wrap_stream_errors_invocation():
129
129
assert exc_info .value .response == grpc_error
130
130
131
131
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
+
132
146
class RpcResponseIteratorImpl (object ):
133
- def __init__ (self , exception ):
134
- self ._exception = exception
147
+ def __init__ (self , iterable ):
148
+ self ._iterable = iter ( iterable )
135
149
136
150
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
138
155
139
156
__next__ = next
140
157
141
158
142
- def test_wrap_stream_errors_iterator ():
159
+ def test_wrap_stream_errors_iterator_initialization ():
143
160
grpc_error = RpcErrorImpl (grpc .StatusCode .UNAVAILABLE )
144
- response_iter = RpcResponseIteratorImpl (grpc_error )
161
+ response_iter = RpcResponseIteratorImpl ([ grpc_error ] )
145
162
callable_ = mock .Mock (spec = ["__call__" ], return_value = response_iter )
146
163
147
164
wrapped_callable = grpc_helpers ._wrap_stream_errors (callable_ )
148
165
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_ )
149
179
got_iterator = wrapped_callable (1 , 2 , three = "four" )
180
+ next (got_iterator )
150
181
151
182
with pytest .raises (exceptions .ServiceUnavailable ) as exc_info :
152
183
next (got_iterator )
0 commit comments