Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rest] correctly pickle rest aiohttp responses #20577

Merged
merged 2 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdk/core/azure-core/azure/core/rest/_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ async def iter_bytes(self) -> AsyncIterator[bytes]:
def __getstate__(self):
state = self.__dict__.copy()
# Remove the unpicklable entries.
state['internal_response'] = None # aiohttp response are not pickable (see headers comments)
state['_internal_response'] = None # aiohttp response are not pickable (see headers comments)
state['headers'] = CIMultiDict(self.headers) # MultiDictProxy is not pickable
return state

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,16 @@ async def test_delete_operation_location(lro_poller):
async def test_request_id(lro_poller):
result = await (await lro_poller(HttpRequest("POST", "/polling/request-id"), request_id="123456789")).result()
assert result['status'] == "Succeeded"

@pytest.mark.asyncio
async def test_continuation_token(client, lro_poller, deserialization_callback):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you tell me which scenario we want to test in these tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we want to test that we can correctly get a continuation token from a poller, and correctly restart polling with that continuation token.

To create the continuation token, we have to pickle the response. As you can tell from the comment, and the corresponding code in azure.core.pipeline.transport.AioHttpTransportResponse, we have to get rid of parts of the response to pickle it. My code is copied from azure.core.pipeline.transport.AioHttpTransportResponse, but because my internal response is a private property, I have to update the code to reflect that

poller = await lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location"))
token = poller.continuation_token()
new_poller = AsyncLROPoller.from_continuation_token(
continuation_token=token,
polling_method=AsyncLROBasePolling(0),
client=client._client,
deserialization_callback=deserialization_callback,
)
result = await new_poller.result()
assert result == {'location_result': True}
12 changes: 12 additions & 0 deletions sdk/core/azure-core/tests/test_rest_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ def test_delete_operation_location(lro_poller):

def test_request_id(lro_poller):
result = lro_poller(HttpRequest("POST", "/polling/request-id"), request_id="123456789").result()

def test_continuation_token(client, lro_poller, deserialization_callback):
poller = lro_poller(HttpRequest("POST", "/polling/post/location-and-operation-location"))
token = poller.continuation_token()
new_poller = LROPoller.from_continuation_token(
continuation_token=token,
polling_method=LROBasePolling(0),
client=client._client,
deserialization_callback=deserialization_callback,
)
result = new_poller.result()
assert result == {'location_result': True}