|
| 1 | +"""Tests for atomic polling snapshots.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +from types import SimpleNamespace |
| 5 | +from typing import Any, cast |
| 6 | +from unittest.mock import AsyncMock, Mock |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from custom_components.solax_modbus import SolaXModbusHub |
| 11 | + |
| 12 | + |
| 13 | +def make_hub() -> Any: |
| 14 | + """Build the minimal hub state required by polling tests.""" |
| 15 | + hub = cast(Any, object.__new__(SolaXModbusHub)) |
| 16 | + hub._name = "test" |
| 17 | + hub.data = {"_repeatUntil": {}, "raw": 1} |
| 18 | + hub.computedSensors = {} |
| 19 | + hub.computedEntities = {} |
| 20 | + hub.sensorEntities = {} |
| 21 | + hub.writeLocals = {} |
| 22 | + hub.writequeue = {} |
| 23 | + hub.localsUpdated = False |
| 24 | + hub.localsLoaded = True |
| 25 | + hub.plugin = SimpleNamespace( |
| 26 | + isAwake=Mock(return_value=True), |
| 27 | + localDataCallback=Mock(return_value=True), |
| 28 | + ) |
| 29 | + hub._poll_data_lock = asyncio.Lock() |
| 30 | + hub.slowdown = 1 |
| 31 | + return hub |
| 32 | + |
| 33 | + |
| 34 | +def make_group(*, follow_up: Any = None) -> Any: |
| 35 | + """Build a polling group with two holding-register blocks.""" |
| 36 | + return SimpleNamespace( |
| 37 | + holdingBlocks=[ |
| 38 | + SimpleNamespace(start=1), |
| 39 | + SimpleNamespace(start=2), |
| 40 | + ], |
| 41 | + inputBlocks=[], |
| 42 | + readPreparation=None, |
| 43 | + readFollowUp=follow_up, |
| 44 | + publish_updates=False, |
| 45 | + sensors=[], |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.asyncio |
| 50 | +async def test_failed_group_discards_all_partial_values() -> None: |
| 51 | + hub = make_hub() |
| 52 | + group = make_group() |
| 53 | + computed_sensor = Mock() |
| 54 | + hub.computedSensors["computed"] = SimpleNamespace( |
| 55 | + key="computed", |
| 56 | + internal=False, |
| 57 | + value_function=lambda initval, descr, data: data["raw"] * 2, |
| 58 | + ) |
| 59 | + hub.sensorEntities["computed"] = computed_sensor |
| 60 | + |
| 61 | + async def read_block(data: dict[str, Any], block: Any, typ: str) -> bool: |
| 62 | + data["raw"] = block.start * 10 |
| 63 | + return bool(block.start == 1) |
| 64 | + |
| 65 | + hub.async_read_modbus_block = read_block |
| 66 | + original_data_object = hub.data |
| 67 | + |
| 68 | + result = await hub.async_read_modbus_registers_all(group) |
| 69 | + |
| 70 | + assert result is False |
| 71 | + assert hub.data is original_data_object |
| 72 | + assert hub.data["raw"] == 1 |
| 73 | + assert "computed" not in hub.data |
| 74 | + assert group.publish_updates is False |
| 75 | + computed_sensor.modbus_data_updated.assert_not_called() |
| 76 | + |
| 77 | + |
| 78 | +@pytest.mark.asyncio |
| 79 | +async def test_tolerated_block_failure_commits_rest_of_snapshot() -> None: |
| 80 | + hub = make_hub() |
| 81 | + hub.data["unavailable"] = 5 |
| 82 | + group = make_group() |
| 83 | + |
| 84 | + async def read_block(data: dict[str, Any], block: Any, typ: str) -> bool: |
| 85 | + if block.start == 1: |
| 86 | + data["raw"] = 10 |
| 87 | + else: |
| 88 | + data.pop("unavailable", None) |
| 89 | + return True |
| 90 | + |
| 91 | + hub.async_read_modbus_block = read_block |
| 92 | + |
| 93 | + result = await hub.async_read_modbus_registers_all(group) |
| 94 | + |
| 95 | + assert result is True |
| 96 | + assert hub.data["raw"] == 10 |
| 97 | + assert "unavailable" not in hub.data |
| 98 | + assert group.publish_updates is True |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.asyncio |
| 102 | +async def test_successful_group_commits_raw_and_computed_values_together() -> None: |
| 103 | + hub = make_hub() |
| 104 | + computed_sensor = Mock() |
| 105 | + follow_up_observations: list[tuple[int, int, int]] = [] |
| 106 | + |
| 107 | + async def follow_up(old_data: dict[str, Any], new_data: dict[str, Any]) -> bool: |
| 108 | + follow_up_observations.append((old_data["raw"], new_data["raw"], hub.data["raw"])) |
| 109 | + return True |
| 110 | + |
| 111 | + group = make_group(follow_up=follow_up) |
| 112 | + hub.computedSensors["computed"] = SimpleNamespace( |
| 113 | + key="computed", |
| 114 | + internal=False, |
| 115 | + value_function=lambda initval, descr, data: data["raw"] * 2, |
| 116 | + ) |
| 117 | + hub.sensorEntities["computed"] = computed_sensor |
| 118 | + |
| 119 | + async def read_block(data: dict[str, Any], block: Any, typ: str) -> bool: |
| 120 | + data["raw"] = block.start |
| 121 | + return True |
| 122 | + |
| 123 | + hub.async_read_modbus_block = read_block |
| 124 | + original_data_object = hub.data |
| 125 | + |
| 126 | + result = await hub.async_read_modbus_registers_all(group) |
| 127 | + |
| 128 | + assert result is True |
| 129 | + assert follow_up_observations == [(1, 2, 1)] |
| 130 | + assert hub.data is original_data_object |
| 131 | + assert hub.data["raw"] == 2 |
| 132 | + assert hub.data["computed"] == 4 |
| 133 | + assert group.publish_updates is True |
| 134 | + computed_sensor.modbus_data_updated.assert_called_once_with() |
| 135 | + |
| 136 | + |
| 137 | +@pytest.mark.asyncio |
| 138 | +async def test_failed_follow_up_discards_snapshot_without_publishing() -> None: |
| 139 | + hub = make_hub() |
| 140 | + computed_sensor = Mock() |
| 141 | + group = make_group(follow_up=AsyncMock(return_value=False)) |
| 142 | + hub.computedSensors["computed"] = SimpleNamespace( |
| 143 | + key="computed", |
| 144 | + internal=False, |
| 145 | + value_function=lambda initval, descr, data: data["raw"] * 2, |
| 146 | + ) |
| 147 | + hub.sensorEntities["computed"] = computed_sensor |
| 148 | + |
| 149 | + async def read_block(data: dict[str, Any], block: Any, typ: str) -> bool: |
| 150 | + data["raw"] = 9 |
| 151 | + return True |
| 152 | + |
| 153 | + hub.async_read_modbus_block = read_block |
| 154 | + |
| 155 | + result = await hub.async_read_modbus_registers_all(group) |
| 156 | + |
| 157 | + assert result is True |
| 158 | + assert hub.data["raw"] == 1 |
| 159 | + assert "computed" not in hub.data |
| 160 | + assert group.publish_updates is False |
| 161 | + computed_sensor.modbus_data_updated.assert_not_called() |
| 162 | + |
| 163 | + |
| 164 | +def test_snapshot_commit_preserves_concurrent_local_change() -> None: |
| 165 | + hub = make_hub() |
| 166 | + previous_data = {"_repeatUntil": {}, "raw": 1, "removed": 5} |
| 167 | + new_data = {"_repeatUntil": {}, "raw": 2, "added": 7} |
| 168 | + hub.data = {"_repeatUntil": {}, "raw": 99, "removed": 5} |
| 169 | + |
| 170 | + hub._commit_poll_snapshot(previous_data, new_data) |
| 171 | + |
| 172 | + assert hub.data["raw"] == 99 |
| 173 | + assert hub.data["added"] == 7 |
| 174 | + assert "removed" not in hub.data |
| 175 | + |
| 176 | + |
| 177 | +@pytest.mark.asyncio |
| 178 | +async def test_group_reads_are_serialized() -> None: |
| 179 | + hub = make_hub() |
| 180 | + active_reads = 0 |
| 181 | + maximum_active_reads = 0 |
| 182 | + |
| 183 | + async def read_group(group: Any) -> bool: |
| 184 | + nonlocal active_reads, maximum_active_reads |
| 185 | + active_reads += 1 |
| 186 | + maximum_active_reads = max(maximum_active_reads, active_reads) |
| 187 | + await asyncio.sleep(0) |
| 188 | + active_reads -= 1 |
| 189 | + group.publish_updates = True |
| 190 | + return True |
| 191 | + |
| 192 | + hub.async_read_modbus_registers_all = read_group |
| 193 | + first_group = make_group() |
| 194 | + second_group = make_group() |
| 195 | + |
| 196 | + first_result, second_result = await asyncio.gather( |
| 197 | + hub.async_read_modbus_data(first_group), |
| 198 | + hub.async_read_modbus_data(second_group), |
| 199 | + ) |
| 200 | + |
| 201 | + assert first_result is True |
| 202 | + assert second_result is True |
| 203 | + assert maximum_active_reads == 1 |
| 204 | + |
| 205 | + |
| 206 | +@pytest.mark.asyncio |
| 207 | +async def test_successful_but_discarded_snapshot_does_not_publish_group() -> None: |
| 208 | + hub = make_hub() |
| 209 | + sensor = Mock() |
| 210 | + group = make_group() |
| 211 | + group.sensors = [sensor] |
| 212 | + interval_group = SimpleNamespace(device_groups={"test": group}) |
| 213 | + hub.blocks_changed = False |
| 214 | + hub.cyclecount = 1 |
| 215 | + hub.sleepnone = [] |
| 216 | + hub.sleepzero = [] |
| 217 | + |
| 218 | + async def read_group(current_group: Any) -> bool: |
| 219 | + current_group.publish_updates = False |
| 220 | + return True |
| 221 | + |
| 222 | + hub.async_read_modbus_data = read_group |
| 223 | + |
| 224 | + result, updated_sensors = await hub._refresh_interval_group_once(interval_group) |
| 225 | + |
| 226 | + assert result is True |
| 227 | + assert updated_sensors == 0 |
| 228 | + sensor.modbus_data_updated.assert_not_called() |
0 commit comments