Skip to content

Commit fbf4316

Browse files
committed
fix: cater for LROs without a result
1 parent 7f0a3a8 commit fbf4316

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

google/api_core/operation.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,10 @@ def _set_result_from_operation(self):
144144
)
145145
self.set_exception(exception)
146146
else:
147-
exception = exceptions.GoogleAPICallError(
148-
"Unexpected state: Long-running operation had neither "
149-
"response nor error set."
150-
)
151-
self.set_exception(exception)
147+
# As per the link below, `Some services might not provide a result`.
148+
# Neither `error` or `response`.
149+
# See https://github.com/googleapis/googleapis/blob/a6c9ed2d33105cb3dc9a0867a0a5d761b049b932/google/longrunning/operations.proto#L141
150+
self.set_result(None)
152151

153152
def _refresh_and_update(self, retry=None):
154153
"""Refresh the operation and update the result if needed.

google/api_core/operation_async.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,10 @@ def _set_result_from_operation(self):
136136
)
137137
self.set_exception(exception)
138138
else:
139-
exception = exceptions.GoogleAPICallError(
140-
"Unexpected state: Long-running operation had neither "
141-
"response nor error set."
142-
)
143-
self.set_exception(exception)
139+
# As per the link below, `Some services might not provide a result`.
140+
# Neither `error` or `response`.
141+
# See https://github.com/googleapis/googleapis/blob/a6c9ed2d33105cb3dc9a0867a0a5d761b049b932/google/longrunning/operations.proto#L141
142+
self.set_result(None)
144143

145144
async def _refresh_and_update(self, retry=async_future.DEFAULT_RETRY):
146145
"""Refresh the operation and update the result if needed.

tests/asyncio/test_operation_async.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,20 @@ async def test_exception():
164164

165165
@mock.patch("asyncio.sleep", autospec=True)
166166
@pytest.mark.asyncio
167-
async def test_unexpected_result(unused_sleep):
167+
async def test_no_result(unused_sleep):
168+
# As per the link below, `Some services might not provide a result`.
169+
# Neither `error` or `response`.
170+
# See https://github.com/googleapis/googleapis/blob/a6c9ed2d33105cb3dc9a0867a0a5d761b049b932/google/longrunning/operations.proto#L141
168171
responses = [
169172
make_operation_proto(),
170173
# Second operation response is done, but has not error or response.
171174
make_operation_proto(done=True),
172175
]
173176
future, _, _ = make_operation_future(responses)
174177

175-
exception = await future.exception()
178+
response = await future.result()
176179

177-
assert "Unexpected state" in "{!r}".format(exception)
180+
assert response is None
178181

179182

180183
@pytest.mark.asyncio

tests/unit/test_operation.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,20 @@ def test_exception_with_error_code():
170170
assert isinstance(exception, exceptions.NotFound)
171171

172172

173-
def test_unexpected_result():
173+
def test_no_result():
174+
# As per the link below, `Some services might not provide a result`.
175+
# Neither `error` or `response`.
176+
# See https://github.com/googleapis/googleapis/blob/a6c9ed2d33105cb3dc9a0867a0a5d761b049b932/google/longrunning/operations.proto#L141
174177
responses = [
175178
make_operation_proto(),
176179
# Second operation response is done, but has not error or response.
177180
make_operation_proto(done=True),
178181
]
179182
future, _, _ = make_operation_future(responses)
180183

181-
exception = future.exception()
184+
response = future.result()
182185

183-
assert "Unexpected state" in "{!r}".format(exception)
186+
assert response is None
184187

185188

186189
def test__refresh_http():

0 commit comments

Comments
 (0)