Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky modbus tests #130252

Merged
merged 1 commit into from
Nov 9, 2024
Merged

Fix flaky modbus tests #130252

merged 1 commit into from
Nov 9, 2024

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Nov 9, 2024

Proposed change

Addresses flaky modbus test behavior on Python 3.13 discovered in #129442.

>       assert caplog.text == ""
E       AssertionError: assert '2024-11-09 2... of 2 bytes\n' == ''
E         
E         + 2024-11-09 21:32:13.801 ERROR    MainThread homeassistant.components.modbus.entity:entity.py:241
    Received 4 bytes, unpack error unpack requires a buffer of 2 bytes
tests/components/modbus/test_init.py:136: AssertionError

Background
Python 3.13 changed the event loop scheduler slightly. Under specific circumstances, this caused the async_update method to be run which wouldn't be execute in time for 3.12 to hit the caplog.text == "" assert. Thus the error that also exists on 3.12 just wouldn't be shown.

The error was emitted here:

byte_string = b"".join([x.to_bytes(2, byteorder="big") for x in registers])
if self._data_type == DataType.STRING:
return byte_string.decode()
if byte_string == b"nan\x00":
return None
try:
val = struct.unpack(self._structure, byte_string)
except struct.error as err:
recv_size = len(registers) * 2
msg = f"Received {recv_size} bytes, unpack error {err}"

The register values are converted to a 2 bytes long byte and according to the error message a byte buffer of size 2 is expected. Therefore the register_words fixture should probably only contain one byte to begin with.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@cdce8p
Copy link
Member Author

cdce8p commented Nov 9, 2024

@cdce8p cdce8p merged commit 31a2bb1 into home-assistant:dev Nov 9, 2024
32 checks passed
@cdce8p cdce8p deleted the fix-modbus-tests branch November 9, 2024 21:58
@github-actions github-actions bot locked and limited conversation to collaborators Nov 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants