1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
import sys
import asyncio
import pytest
if sys.version_info[:2] < (3, 11):
from async_timeout import timeout as asyncio_timeout # pragma: no cover
else:
from asyncio import timeout as asyncio_timeout # pragma: no cover
import zigpy_znp.types as t
import zigpy_znp.commands as c
from zigpy_znp.utils import deduplicate_commands
async def test_responses(connected_znp):
znp, znp_server = connected_znp
assert not any(znp._listeners.values())
future = znp.wait_for_response(c.SYS.Ping.Rsp(partial=True))
assert any(znp._listeners.values())
response = c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS)
znp_server.send(response)
assert (await future) == response
# Our listener will have been cleaned up after a step
await asyncio.sleep(0)
assert not any(znp._listeners.values())
async def test_responses_multiple(connected_znp):
znp, _ = connected_znp
assert not any(znp._listeners.values())
future1 = znp.wait_for_response(c.SYS.Ping.Rsp(partial=True))
future2 = znp.wait_for_response(c.SYS.Ping.Rsp(partial=True))
future3 = znp.wait_for_response(c.SYS.Ping.Rsp(partial=True))
response = c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS)
znp.frame_received(response.to_frame())
await future1
await asyncio.sleep(0)
await asyncio.sleep(0)
await asyncio.sleep(0)
assert not future2.done()
assert not future3.done()
assert any(znp._listeners.values())
async def test_response_timeouts(connected_znp):
znp, _ = connected_znp
response = c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS)
async def send_soon(delay):
await asyncio.sleep(delay)
znp.frame_received(response.to_frame())
asyncio.create_task(send_soon(0.1))
async with asyncio_timeout(0.5):
assert (await znp.wait_for_response(c.SYS.Ping.Rsp(partial=True))) == response
# The response was successfully received so we should have no outstanding listeners
await asyncio.sleep(0)
assert not any(znp._listeners.values())
asyncio.create_task(send_soon(0.6))
with pytest.raises(asyncio.TimeoutError):
async with asyncio_timeout(0.5):
assert (
await znp.wait_for_response(c.SYS.Ping.Rsp(partial=True))
) == response
# Our future still completed, albeit unsuccessfully.
# We should have no leaked listeners here.
assert not any(znp._listeners.values())
async def test_response_matching_partial(connected_znp):
znp, _ = connected_znp
future = znp.wait_for_response(
c.SYS.ResetInd.Callback(
partial=True, Reason=t.ResetReason.PowerUp, MaintRel=0x04
)
)
response1 = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x12,
MajorRel=0x01,
MinorRel=0x02,
MaintRel=0x03,
)
response2 = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x12,
MajorRel=0x01,
MinorRel=0x02,
MaintRel=0x04,
)
response3 = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.External,
TransportRev=0x00,
ProductId=0x12,
MajorRel=0x01,
MinorRel=0x02,
MaintRel=0x04,
)
znp.frame_received(response1.to_frame())
znp.frame_received(response2.to_frame())
znp.frame_received(response3.to_frame())
assert future.done()
assert (await future) == response2
async def test_response_matching_exact(connected_znp):
znp, _ = connected_znp
response1 = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x12,
MajorRel=0x01,
MinorRel=0x02,
MaintRel=0x03,
)
response2 = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x12,
MajorRel=0x01,
MinorRel=0x02,
MaintRel=0x04,
)
response3 = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.External,
TransportRev=0x00,
ProductId=0x12,
MajorRel=0x01,
MinorRel=0x02,
MaintRel=0x04,
)
future = znp.wait_for_response(response2)
znp.frame_received(response1.to_frame())
znp.frame_received(response2.to_frame())
znp.frame_received(response3.to_frame())
# Future should be immediately resolved
assert future.done()
assert (await future) == response2
async def test_response_not_matching_out_of_order(connected_znp):
znp, _ = connected_znp
response = c.SYS.ResetInd.Callback(
Reason=t.ResetReason.PowerUp,
TransportRev=0x00,
ProductId=0x12,
MajorRel=0x01,
MinorRel=0x02,
MaintRel=0x03,
)
znp.frame_received(response.to_frame())
future = znp.wait_for_response(response)
# This future will never resolve because we were not
# expecting a response and discarded it
assert not future.done()
async def test_wait_responses_empty(connected_znp):
znp, _ = connected_znp
# You shouldn't be able to wait for an empty list of responses
with pytest.raises(ValueError):
await znp.wait_for_responses([])
async def test_response_callback_simple(connected_znp, mocker):
znp, _ = connected_znp
sync_callback = mocker.Mock()
good_response = c.SYS.SetExtAddr.Rsp(Status=t.Status.FAILURE)
bad_response = c.SYS.SetExtAddr.Rsp(Status=t.Status.SUCCESS)
znp.callback_for_response(good_response, sync_callback)
znp.frame_received(bad_response.to_frame())
assert sync_callback.call_count == 0
znp.frame_received(good_response.to_frame())
sync_callback.assert_called_once_with(good_response)
async def test_response_callbacks(connected_znp, mocker):
znp, _ = connected_znp
sync_callback = mocker.Mock()
bad_sync_callback = mocker.Mock(
side_effect=RuntimeError
) # Exceptions should not interfere with other callbacks
async_callback_responses = []
# XXX: I can't get AsyncMock().call_count to work, even though
# the callback is definitely being called
async def async_callback(response):
await asyncio.sleep(0)
async_callback_responses.append(response)
good_response1 = c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS)
good_response2 = c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.APP)
good_response3 = c.UTIL.TimeAlive.Rsp(Seconds=12)
bad_response1 = c.SYS.SetExtAddr.Rsp(Status=t.Status.SUCCESS)
bad_response2 = c.SYS.NVWrite.Req(
SysId=0x12, ItemId=0x3456, SubId=0x7890, Offset=0x00, Value=b"asdfoo"
)
responses = [
# Duplicating matching responses shouldn't do anything
c.SYS.Ping.Rsp(partial=True),
c.SYS.Ping.Rsp(partial=True),
# Matching against different response types should also work
c.UTIL.TimeAlive.Rsp(Seconds=12),
c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS),
c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS),
c.UTIL.TimeAlive.Rsp(Seconds=10),
]
assert set(deduplicate_commands(responses)) == {
c.SYS.Ping.Rsp(partial=True),
c.UTIL.TimeAlive.Rsp(Seconds=12),
c.UTIL.TimeAlive.Rsp(Seconds=10),
}
# We shouldn't see any effects from receiving a frame early
znp.frame_received(good_response1.to_frame())
for callback in [bad_sync_callback, async_callback, sync_callback]:
znp.callback_for_responses(responses, callback)
znp.frame_received(good_response1.to_frame())
znp.frame_received(bad_response1.to_frame())
znp.frame_received(good_response2.to_frame())
znp.frame_received(bad_response2.to_frame())
znp.frame_received(good_response3.to_frame())
await asyncio.sleep(0)
assert sync_callback.call_count == 3
assert bad_sync_callback.call_count == 3
await asyncio.sleep(0.1)
# assert async_callback.call_count == 3 # XXX: this always returns zero
assert len(async_callback_responses) == 3
async def test_wait_for_responses(connected_znp):
znp, _ = connected_znp
response1 = c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS)
response2 = c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.APP)
response3 = c.UTIL.TimeAlive.Rsp(Seconds=12)
response4 = c.SYS.SetExtAddr.Rsp(Status=t.Status.SUCCESS)
response5 = c.SYS.NVWrite.Req(
SysId=0x12, ItemId=0x3456, SubId=0x7890, Offset=0x00, Value=b"asdfoo"
)
# We shouldn't see any effects from receiving a frame early
znp.frame_received(response1.to_frame())
# Will match the first response1 and detach
future1 = znp.wait_for_responses(
[c.SYS.Ping.Rsp(partial=True), c.SYS.Ping.Rsp(partial=True)]
)
# Will match the first response3 and detach
future2 = znp.wait_for_responses(
[
c.UTIL.TimeAlive.Rsp(Seconds=12),
c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.UTIL),
]
)
# Will not match anything
future3 = znp.wait_for_responses([c.UTIL.TimeAlive.Rsp(Seconds=10)])
# Will match response1 the second time around
future4 = znp.wait_for_responses(
[
# Matching against different response types should also work
c.UTIL.TimeAlive.Rsp(Seconds=12),
c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS),
c.SYS.Ping.Rsp(Capabilities=t.MTCapabilities.SYS),
c.UTIL.TimeAlive.Rsp(Seconds=10),
]
)
znp.frame_received(response1.to_frame())
znp.frame_received(response2.to_frame())
znp.frame_received(response3.to_frame())
znp.frame_received(response4.to_frame())
znp.frame_received(response5.to_frame())
assert future1.done()
assert future2.done()
assert not future3.done()
assert not future4.done()
await asyncio.sleep(0)
znp.frame_received(response1.to_frame())
znp.frame_received(response2.to_frame())
znp.frame_received(response3.to_frame())
znp.frame_received(response4.to_frame())
znp.frame_received(response5.to_frame())
assert future1.done()
assert future2.done()
assert not future3.done()
assert future4.done()
assert (await future1) == response1
assert (await future2) == response3
assert (await future4) == response1
await asyncio.sleep(0)
znp.frame_received(c.UTIL.TimeAlive.Rsp(Seconds=10).to_frame())
assert future3.done()
assert (await future3) == c.UTIL.TimeAlive.Rsp(Seconds=10)
|