Skip to content

Commit

Permalink
Add a test for disconnecting a client in sensor_update
Browse files Browse the repository at this point in the history
See NGC-1440.
  • Loading branch information
bmerry committed Sep 5, 2024
1 parent fb4eec4 commit 0f3a602
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/aiokatcp/sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2017, 2019, 2022 National Research Foundation (SARAO)
# Copyright 2017, 2019, 2022, 2024 National Research Foundation (SARAO)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -226,9 +226,10 @@ def notify(self, reading: Reading[_T], old_reading: Reading[_T]) -> None:
Users should not usually call this directly. It is called automatically
by :meth:`set_value`.
"""
for classic_observer in self._classic_observers:
# list() is to avoid errors if the set is modified during iteration.
for classic_observer in list(self._classic_observers):
classic_observer(self, reading)
for delta_observer in self._delta_observers:
for delta_observer in list(self._delta_observers):
delta_observer(self, reading, old_reading=old_reading)

def set_value(
Expand Down
29 changes: 26 additions & 3 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ def __init__(self):
self.sensors.add(sensor)
sensor = Sensor(float, "float-sensor", "generic float sensor")
self.sensors.add(sensor)
sensor = Sensor(bytes, "str-sensor", "generic string sensor")
self.sensors.add(sensor)
sensor = Sensor(
int,
"auto-override",
Expand Down Expand Up @@ -333,7 +335,8 @@ async def test_sensor_list_no_filter(
rb"#sensor-list[4] counter-queries number\_of\_?counter\_queries \@ integer" + b"\n",
rb"#sensor-list[4] float-sensor generic\_float\_sensor \@ float" + b"\n",
rb"#sensor-list[4] foo nonsense \@ discrete first-value second-value" + b"\n",
b"!sensor-list[4] ok 4\n",
rb"#sensor-list[4] str-sensor generic\_string\_sensor \@ string" + b"\n",
b"!sensor-list[4] ok 5\n",
],
)

Expand Down Expand Up @@ -368,7 +371,8 @@ async def test_sensor_list_regex_filter(
rb"#sensor-list[6] counter-queries number\_of\_?counter\_queries \@ integer" + b"\n",
rb"#sensor-list[6] float-sensor generic\_float\_sensor \@ float" + b"\n",
rb"#sensor-list[6] foo nonsense \@ discrete first-value second-value" + b"\n",
b"!sensor-list[6] ok 3\n",
rb"#sensor-list[6] str-sensor generic\_string\_sensor \@ string" + b"\n",
b"!sensor-list[6] ok 4\n",
],
)

Expand Down Expand Up @@ -398,7 +402,8 @@ async def test_sensor_value(
b"#sensor-value[9] 123456789.0 1 counter-queries nominal 0\n",
b"#sensor-value[9] 123456789.0 1 float-sensor unknown 0.0\n",
b"#sensor-value[9] 123456789.0 1 foo unknown first-value\n",
b"!sensor-value[9] ok 4\n",
b"#sensor-value[9] 123456789.0 1 str-sensor unknown \\@\n",
b"!sensor-value[9] ok 5\n",
],
)

Expand Down Expand Up @@ -637,6 +642,24 @@ async def test_slow_client(server: DummyServer, reader: asyncio.StreamReader) ->
assert len(server._connections) == 0


async def test_slow_client_sensor(
server: DummyServer, reader: asyncio.StreamReader, writer: asyncio.StreamWriter
) -> None:
server.max_backlog = 32768
big_str = b"x" * 200000
writer.write(b"?sensor-sampling str-sensor auto\n")
await check_reply(
reader,
[
re.compile(rb"^#sensor-status [0-9.]+ 1 str-sensor unknown \\@"),
b"!sensor-sampling ok str-sensor auto\n",
],
)
assert len(server._connections) == 1
server.sensors["str-sensor"].value = big_str
assert len(server._connections) == 0


async def test_sensor_sampling_invalid(
reader: asyncio.StreamReader, writer: asyncio.StreamWriter
) -> None:
Expand Down

0 comments on commit 0f3a602

Please sign in to comment.