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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
"""Unit test for telegram received callback."""
import asyncio
from unittest.mock import AsyncMock, MagicMock, Mock, call, patch
import pytest
from xknx import XKNX
from xknx.dpt import DPTArray, DPTBinary
from xknx.exceptions import CommunicationError, CouldNotParseTelegram
from xknx.telegram import AddressFilter, Telegram, TelegramDirection
from xknx.telegram.address import GroupAddress, InternalGroupAddress
from xknx.telegram.apci import GroupValueWrite
class TestTelegramQueue:
"""Test class for telegram queue."""
#
# TEST START, RUN, STOP
#
async def test_start(self) -> None:
"""Test start, run and stop."""
xknx = XKNX()
telegram_in = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
await xknx.telegram_queue.start()
assert not xknx.telegram_queue._consumer_task.done()
# queue shall now consume telegrams from xknx.telegrams
assert xknx.telegrams.qsize() == 0
xknx.telegrams.put_nowait(telegram_in)
xknx.telegrams.put_nowait(telegram_in)
assert xknx.telegrams.qsize() == 2
# wait until telegrams are consumed
await xknx.telegrams.join()
assert xknx.telegrams.qsize() == 0
await xknx.telegrams.join()
assert xknx.telegrams.qsize() == 0
# stop run() task with stop()
await xknx.telegram_queue.stop()
assert xknx.telegram_queue._consumer_task.done()
@patch("asyncio.sleep", new_callable=AsyncMock)
async def test_rate_limit(self, async_sleep_mock: AsyncMock) -> None:
"""Test rate limit."""
xknx = XKNX(
rate_limit=20, # 50 ms per outgoing telegram
)
sleep_time = 0.05 # 1 / 20
telegram_in = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
telegram_out = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.OUTGOING,
payload=GroupValueWrite(DPTBinary(1)),
)
telegram_internal = Telegram(
direction=TelegramDirection.OUTGOING,
payload=GroupValueWrite(DPTBinary(1)),
destination_address=InternalGroupAddress("i-test"),
)
await xknx.telegram_queue.start()
# no sleep for incoming telegrams
xknx.telegrams.put_nowait(telegram_in)
xknx.telegrams.put_nowait(telegram_in)
await xknx.telegrams.join()
assert async_sleep_mock.call_count == 0
# sleep for outgoing telegrams
xknx.telegrams.put_nowait(telegram_out)
xknx.telegrams.put_nowait(telegram_out)
await xknx.telegrams.join()
assert async_sleep_mock.call_count == 2
async_sleep_mock.assert_called_with(sleep_time)
async_sleep_mock.reset_mock()
# no sleep for internal group address telegrams
xknx.telegrams.put_nowait(telegram_internal)
xknx.telegrams.put_nowait(telegram_internal)
await xknx.telegrams.join()
async_sleep_mock.assert_not_called()
await xknx.telegram_queue.stop()
#
# TEST REGISTER
#
async def test_register(self) -> None:
"""Test telegram_received_callback after state of switch was changed."""
xknx = XKNX()
telegram_received_cb = Mock()
xknx.telegram_queue.register_telegram_received_cb(telegram_received_cb)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
await xknx.telegram_queue.process_telegram_incoming(telegram)
telegram_received_cb.assert_called_once_with(telegram)
async def test_register_with_outgoing_telegrams(self) -> None:
"""Test telegram_received_callback with outgoing telegrams."""
xknx = XKNX()
xknx.cemi_handler = AsyncMock()
telegram_received_cb = Mock()
xknx.telegram_queue.register_telegram_received_cb(
telegram_received_cb, None, None, True
)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.OUTGOING,
payload=GroupValueWrite(DPTBinary(1)),
)
await xknx.telegram_queue.process_telegram_outgoing(telegram)
telegram_received_cb.assert_called_once_with(telegram)
async def test_register_with_outgoing_telegrams_does_not_trigger(self) -> None:
"""Test telegram_received_callback with outgoing telegrams."""
xknx = XKNX()
xknx.cemi_handler = AsyncMock()
telegram_received_cb = Mock()
xknx.telegram_queue.register_telegram_received_cb(telegram_received_cb)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.OUTGOING,
payload=GroupValueWrite(DPTBinary(1)),
)
await xknx.telegram_queue.process_telegram_outgoing(telegram)
telegram_received_cb.assert_not_called()
#
# TEST UNREGISTER
#
async def test_unregister(self) -> None:
"""Test telegram_received_callback after state of switch was changed."""
xknx = XKNX()
telegram_received_cb = Mock()
callback = xknx.telegram_queue.register_telegram_received_cb(
telegram_received_cb
)
xknx.telegram_queue.unregister_telegram_received_cb(callback)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
await xknx.telegram_queue.process_telegram_incoming(telegram)
telegram_received_cb.assert_not_called()
#
# TEST PROCESS
#
@patch("xknx.devices.Devices.devices_by_group_address")
async def test_process_to_device(self, devices_by_ga_mock: Mock) -> None:
"""Test process_telegram_incoming for forwarding telegram to a device."""
xknx = XKNX()
test_device = Mock()
devices_by_ga_mock.return_value = [test_device]
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
await xknx.telegram_queue.process_telegram_incoming(telegram)
devices_by_ga_mock.assert_called_once_with(GroupAddress("1/2/3"))
test_device.process.assert_called_once_with(telegram)
@patch("xknx.devices.Devices.process")
async def test_process_to_callback(self, devices_process: MagicMock) -> None:
"""Test process_telegram_incoming with callback."""
xknx = XKNX()
telegram_received_cb = Mock()
xknx.telegram_queue.register_telegram_received_cb(telegram_received_cb)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
await xknx.telegram_queue.process_telegram_incoming(telegram)
telegram_received_cb.assert_called_once_with(telegram)
devices_process.assert_called_once_with(telegram)
async def test_callback_decoded_telegram_data(self) -> None:
"""Test telegram_received_callback having decoded telegram data."""
xknx = XKNX()
xknx.group_address_dpt.set({"1/2/3": {"main": 5, "sub": 1}})
telegram_received_cb = Mock()
xknx.telegram_queue.register_telegram_received_cb(telegram_received_cb)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(
DPTArray(
0x7F,
)
),
)
await xknx.telegram_queue.start()
xknx.telegrams.put_nowait(telegram)
await xknx.telegrams.join()
await xknx.telegram_queue.stop()
assert telegram_received_cb.call_count == 1
received = telegram_received_cb.call_args_list[0][0][0]
assert received == telegram
assert received.decoded_data is not None
assert received.decoded_data.value == 50
async def test_outgoing(self) -> None:
"""Test outgoing telegrams in telegram queue."""
xknx = XKNX()
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.OUTGOING,
payload=GroupValueWrite(DPTBinary(1)),
)
# log a warning if there is no KNXIP interface instantiated
with pytest.raises(CommunicationError):
await xknx.telegram_queue.process_telegram_outgoing(telegram)
# if we have an interface send the telegram (doesn't raise)
xknx.cemi_handler.send_telegram = AsyncMock()
await xknx.telegram_queue.process_telegram_outgoing(telegram)
xknx.cemi_handler.send_telegram.assert_called_once_with(telegram)
@patch("logging.Logger.error")
@patch("xknx.core.TelegramQueue.process_telegram_incoming", new_callable=MagicMock)
async def test_process_exception(
self, process_tg_in_mock: MagicMock, logging_error_mock: MagicMock
) -> None:
"""Test process_telegram exception handling."""
xknx = XKNX()
async def process_exception() -> None:
raise CouldNotParseTelegram(
"Something went wrong when receiving the telegram."
)
process_tg_in_mock.return_value = asyncio.ensure_future(process_exception())
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
xknx.telegrams.put_nowait(telegram)
await xknx.telegram_queue._process_all_telegrams()
logging_error_mock.assert_called_once_with(
"Error while processing telegram %s",
CouldNotParseTelegram("Something went wrong when receiving the telegram."),
)
@patch("xknx.core.TelegramQueue.process_telegram_outgoing", new_callable=AsyncMock)
@patch("xknx.core.TelegramQueue.process_telegram_incoming", new_callable=AsyncMock)
async def test_process_all_telegrams(
self,
process_telegram_incoming_mock: AsyncMock,
process_telegram_outgoing_mock: AsyncMock,
) -> None:
"""Test _process_all_telegrams for clearing the queue."""
xknx = XKNX()
telegram_in = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
telegram_out = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.OUTGOING,
payload=GroupValueWrite(DPTBinary(1)),
)
xknx.telegrams.put_nowait(telegram_in)
xknx.telegrams.put_nowait(telegram_out)
await xknx.telegram_queue._process_all_telegrams()
process_telegram_incoming_mock.assert_called_once()
process_telegram_outgoing_mock.assert_called_once()
#
# TEST NO FILTERS
#
async def test_callback_no_filters(self) -> None:
"""Test telegram_received_callback after state of switch was changed."""
xknx = XKNX()
telegram_received_cb = Mock()
xknx.telegram_queue.register_telegram_received_cb(telegram_received_cb)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
xknx.telegrams.put_nowait(telegram)
await xknx.telegram_queue._process_all_telegrams()
telegram_received_cb.assert_called_with(telegram)
#
# TEST POSITIVE FILTERS
#
async def test_callback_positive_address_filters(self) -> None:
"""Test telegram_received_callback after state of switch was changed."""
xknx = XKNX()
telegram_received_cb = Mock()
xknx.telegram_queue.register_telegram_received_cb(
telegram_received_cb,
address_filters=[AddressFilter("2/4-8/*"), AddressFilter("1/2/-8")],
)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
xknx.telegrams.put_nowait(telegram)
await xknx.telegram_queue._process_all_telegrams()
telegram_received_cb.assert_called_with(telegram)
#
# TEST NEGATIVE FILTERS
#
async def test_callback_negative_address_filters(self) -> None:
"""Test telegram_received_callback after state of switch was changed."""
xknx = XKNX()
telegram_received_cb = Mock()
xknx.telegram_queue.register_telegram_received_cb(
telegram_received_cb,
address_filters=[AddressFilter("2/4-8/*"), AddressFilter("1/2/8-")],
)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
xknx.telegrams.put_nowait(telegram)
await xknx.telegram_queue._process_all_telegrams()
telegram_received_cb.assert_not_called()
async def test_callback_group_addresses(self) -> None:
"""Test telegram_received_callback after state of switch was changed."""
xknx = XKNX()
telegram_received_cb_one = Mock()
telegram_received_cb_two = Mock()
callback_one = xknx.telegram_queue.register_telegram_received_cb(
telegram_received_cb_one,
address_filters=[],
group_addresses=[GroupAddress("1/2/3")],
)
callback_two = xknx.telegram_queue.register_telegram_received_cb(
telegram_received_cb_two, address_filters=[], group_addresses=[]
)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
await xknx.telegram_queue.process_telegram_incoming(telegram)
telegram_received_cb_one.assert_called_once_with(telegram)
telegram_received_cb_two.assert_not_called()
telegram_received_cb_one.reset_mock()
# modify the filters - add/remove a GroupAddress
callback_one.group_addresses.remove(GroupAddress("1/2/3"))
callback_two.group_addresses.append(GroupAddress("1/2/3"))
await xknx.telegram_queue.process_telegram_incoming(telegram)
telegram_received_cb_one.assert_not_called()
telegram_received_cb_two.assert_called_once_with(telegram)
#
# TEST EXCEPTION HANDLING
#
@patch("logging.Logger.exception")
@patch("xknx.xknx.Devices.process", side_effect=Exception)
async def test_process_raising(
self, process_mock: MagicMock, logging_exception_mock: MagicMock
) -> None:
"""Test unexpected exception handling in telegram queues."""
xknx = XKNX()
telegram_in = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
# InternalGroupAddress to avoid CommunicationError for missing knxip_interface
telegram_out = Telegram(
destination_address=InternalGroupAddress("i-outgoing"),
direction=TelegramDirection.OUTGOING,
payload=GroupValueWrite(DPTBinary(0)),
)
xknx.telegrams.put_nowait(telegram_in)
xknx.telegrams.put_nowait(telegram_out)
await xknx.telegram_queue.start()
await xknx.telegram_queue.stop()
log_calls = [
call(
"Unexpected error while processing incoming telegram %s",
telegram_in,
),
call(
"Unexpected error while processing outgoing telegram %s",
telegram_out,
),
]
logging_exception_mock.assert_has_calls(log_calls)
@patch("logging.Logger.exception")
async def test_callback_raising(self, logging_exception_mock: MagicMock) -> None:
"""Test telegram_received_callback raising an exception."""
xknx = XKNX()
good_callback_1 = Mock()
bad_callback = Mock(side_effect=Exception("Boom"))
good_callback_2 = Mock()
xknx.telegram_queue.register_telegram_received_cb(good_callback_1)
xknx.telegram_queue.register_telegram_received_cb(bad_callback)
xknx.telegram_queue.register_telegram_received_cb(good_callback_2)
telegram = Telegram(
destination_address=GroupAddress("1/2/3"),
direction=TelegramDirection.INCOMING,
payload=GroupValueWrite(DPTBinary(1)),
)
await xknx.telegram_queue.process_telegram_incoming(telegram)
good_callback_1.assert_called_once_with(telegram)
bad_callback.assert_called_once_with(telegram)
good_callback_2.assert_called_once_with(telegram)
logging_exception_mock.assert_called_once_with(
"Unexpected error while processing telegram_received_cb for %s",
telegram,
)
|