|
23 | 23 | # THE SOFTWARE.
|
24 | 24 | #
|
25 | 25 | #--------------------------------------------------------------------------
|
| 26 | +import asyncio |
26 | 27 | import time
|
27 | 28 | try:
|
28 | 29 | from unittest import mock
|
@@ -89,6 +90,7 @@ async def run(self):
|
89 | 90 | """Empty run, no polling.
|
90 | 91 | """
|
91 | 92 | self._finished = True
|
| 93 | + await asyncio.sleep(self._sleep) # Give me time to add callbacks! |
92 | 94 |
|
93 | 95 | def status(self):
|
94 | 96 | """Return the current status as a string.
|
@@ -129,39 +131,53 @@ def deserialization_callback(response):
|
129 | 131 |
|
130 | 132 | method = AsyncNoPolling()
|
131 | 133 |
|
132 |
| - poller = AsyncLROPoller(client, initial_response, deserialization_callback, method) |
| 134 | + raw_poller = AsyncLROPoller(client, initial_response, deserialization_callback, method) |
| 135 | + poller = asyncio.ensure_future(raw_poller.result()) |
| 136 | + |
| 137 | + done_cb = mock.MagicMock() |
| 138 | + poller.add_done_callback(done_cb) |
133 | 139 |
|
134 |
| - result = await poller.result() |
| 140 | + result = await poller |
135 | 141 | assert poller.done()
|
136 | 142 | assert result == "Treated: "+initial_response
|
137 |
| - assert poller.status() == "succeeded" |
| 143 | + assert raw_poller.status() == "succeeded" |
| 144 | + assert raw_poller.polling_method() is method |
| 145 | + done_cb.assert_called_once_with(poller) |
138 | 146 |
|
139 | 147 | # Test with a basic Model
|
140 | 148 | poller = AsyncLROPoller(client, initial_response, Model, method)
|
141 | 149 | assert poller._polling_method._deserialization_callback == Model.deserialize
|
142 | 150 |
|
143 | 151 | # Test poller that method do a run
|
144 | 152 | method = PollingTwoSteps(sleep=1)
|
145 |
| - poller = AsyncLROPoller(client, initial_response, deserialization_callback, method) |
| 153 | + raw_poller = AsyncLROPoller(client, initial_response, deserialization_callback, method) |
| 154 | + poller = asyncio.ensure_future(raw_poller.result()) |
| 155 | + |
| 156 | + done_cb = mock.MagicMock() |
| 157 | + done_cb2 = mock.MagicMock() |
| 158 | + poller.add_done_callback(done_cb) |
| 159 | + poller.remove_done_callback(done_cb2) |
146 | 160 |
|
147 |
| - result = await poller.result() |
| 161 | + result = await poller |
148 | 162 | assert result == "Treated: "+initial_response
|
149 |
| - assert poller.status() == "succeeded" |
| 163 | + assert raw_poller.status() == "succeeded" |
| 164 | + done_cb.assert_called_once_with(poller) |
| 165 | + done_cb2.assert_not_called() |
150 | 166 |
|
151 | 167 | # Test continuation token
|
152 |
| - cont_token = poller.continuation_token() |
| 168 | + cont_token = raw_poller.continuation_token() |
153 | 169 |
|
154 | 170 | method = PollingTwoSteps(sleep=1)
|
155 | 171 | new_poller = AsyncLROPoller.from_continuation_token(
|
156 | 172 | continuation_token=cont_token,
|
157 | 173 | client=client,
|
158 | 174 | initial_response=initial_response,
|
159 |
| - deserialization_callback=Model, |
| 175 | + deserialization_callback=deserialization_callback, |
160 | 176 | polling_method=method
|
161 | 177 | )
|
162 |
| - result = await poller.result() |
| 178 | + result = await new_poller.result() |
163 | 179 | assert result == "Treated: "+initial_response
|
164 |
| - assert poller.status() == "succeeded" |
| 180 | + assert new_poller.status() == "succeeded" |
165 | 181 |
|
166 | 182 |
|
167 | 183 | @pytest.mark.asyncio
|
|
0 commit comments