Skip to content

[QA] Add tests for payload handler - #73

Merged
Haralishev77 merged 2 commits into
mainfrom
feature/test-payload-handler
Mar 23, 2026
Merged

[QA] Add tests for payload handler#73
Haralishev77 merged 2 commits into
mainfrom
feature/test-payload-handler

Conversation

@mayaparov

@mayaparov mayaparov commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

Summary

Add deterministic unit tests for PayloadHandler (OpenCDA ↔ Artery payload building/parsing) using fully stubbed protobuf modules and controlled imports, keeping the suite CI-friendly and independent from protoc output at runtime.

This PR contains tests only and does not modify payload_handler.py.

Motivation

PayloadHandler is responsible for assembling outgoing OpenCDA messages and decoding incoming Artery messages. The logic is stateful (it accumulates per-tick data) and relies on serialization (pickle) to transport structured dictionaries through protobuf byte fields. This is easy to break silently during refactors.

Additionally, the module imports protobuf code at import time and mutates sys.path, which makes naive unit tests fragile and dependent on whether generated *_pb2.py files exist locally. The goal of this PR is to lock down contract-level behavior with fast unit tests that run in minimal CI environments.

Changes

New unit tests

opencda/core/common/communication/test/test_payload_handler.py adds contract-level tests for:

  • both context-manager APIs (handle_opencda_payload, handle_artery_payload):
    • deterministic initialization of nested dict structures;
    • persistence of mutations across context exits;
    • isolation across entity IDs and ego IDs.
  • make_opencda_message():
    • empty state produces a message with zero entities;
    • multi-entity messages validated without ordering assumptions;
    • auxillary is bytes-like and decodes back to the original per-entity dict via pickle.loads (without comparing raw pickle bytes).
  • make_artery_payload():
    • supports multiple transmissions (multiple ego IDs) and multiple entities per transmission;
    • accumulates and overwrites per (ego_id, entity_id) as expected for dict-based storage;
    • does not unexpectedly clear existing state when receiving an empty message.
  • clear_messages():
    • resets internal stores to avoid data leakage between ticks.

Notes on planned changes

The current implementation uses pickle for serialization. If the project later replaces pickle with a safer serialization format, these unit tests will need to be updated accordingly (the tests intentionally assert decoded content, not raw serialized bytes).

How to run

uv run pytest -q opencda/core/common/communication/test/test_payload_handler.py
uv run pytest -q

@Haralishev77 Haralishev77 added QA Some changes in QA 982 CAPI labels Mar 19, 2026
Comment thread opencda/core/common/communication/payload_handler.py
Base automatically changed from feature/refactor-MessageHandler to main March 21, 2026 15:25
@mayaparov
mayaparov force-pushed the feature/test-payload-handler branch from dfc035e to 22a3042 Compare March 22, 2026 21:25
@Haralishev77
Haralishev77 self-requested a review March 23, 2026 12:46
@Haralishev77
Haralishev77 merged commit edd4545 into main Mar 23, 2026
8 checks passed
@Haralishev77
Haralishev77 deleted the feature/test-payload-handler branch March 23, 2026 12:48
Haralishev77 added a commit that referenced this pull request Jul 21, 2026
Co-authored-by: Khoroshilov Grigoriy <khorogri@gmail.com>
meaning-0f-life pushed a commit to meaning-0f-life/OpenCDA that referenced this pull request Jul 25, 2026
Co-authored-by: Khoroshilov Grigoriy <khorogri@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

982 CAPI QA Some changes in QA

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants