Skip to content

WIP: Add test for filter calls between fetch() #175

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

Open
wants to merge 7 commits into
base: v0.x.x
Choose a base branch
from

Conversation

Marenz
Copy link
Contributor

@Marenz Marenz commented Jul 17, 2025

  • Add test for periodic fetch and merging

ONLY LOOK AT THE LAST COMMIT. The rest is in another PR

Marenz added 7 commits July 16, 2025 17:37
Signed-off-by: Mathias L. Baumann <[email protected]>
Signed-off-by: Mathias L. Baumann <[email protected]>
By using non-dict-assigning dispatch update flow.

When fetch() which runs periodically would assign
a fresh map to `self._dispatches` the filter would
from the non operate on an old non-updated map.

Signed-off-by: Mathias L. Baumann <[email protected]>
Signed-off-by: Mathias L. Baumann <[email protected]>
@Copilot Copilot AI review requested due to automatic review settings July 17, 2025 17:25
@Marenz Marenz requested a review from a team as a code owner July 17, 2025 17:25
@github-actions github-actions bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) part:dispatcher Affects the high-level dispatcher interface labels Jul 17, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a test to verify that the dispatch filtering mechanism correctly handles overlapping dispatches that are created between fetch operations. The test specifically verifies that when two dispatches overlap in time but are created at different points relative to the fetch cycle, the merge strategy correctly filters events and maintains proper state.

  • Adds a comprehensive test for sequential overlapping dispatches between fetch operations
  • Updates the merge strategy to use consistent time references and improved logging
  • Fixes fetch logic to properly handle dispatch updates and deletions

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/test_frequenz_dispatch.py Adds new test case for overlapping dispatches between fetch operations
src/frequenz/dispatch/_merge_strategies.py Updates merge filter to use consistent time reference and improved logging
src/frequenz/dispatch/_dispatch.py Adds started_at method for time-aware dispatch state checking
src/frequenz/dispatch/_bg_service.py Fixes fetch logic to properly handle dispatch updates and scheduling
src/frequenz/dispatch/_actor_dispatcher.py Improves logging for actor stop operations
pyproject.toml Updates frequenz-client-dispatch dependency version
RELEASE_NOTES.md Updates release notes with bug fixes
Comments suppressed due to low confidence (1)

tests/test_frequenz_dispatch.py:759

  • The test expects a TimeoutError but doesn't specify a timeout parameter for the receiver.receive() call. This could cause the test to hang indefinitely if the expected timeout doesn't occur.
    with pytest.raises(asyncio.TimeoutError):

Comment on lines +739 to +740
import logging

Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import statements should be at the top of the file, not inside functions. Move this import to the top with other imports.

Suggested change
import logging

Copilot uses AI. Check for mistakes.

await client.create(**to_create_params(microgrid_id, dispatch1))

timer = Timer(timedelta(seconds=100), SkipMissedAndResync(), auto_start=False)
await service._fetch(timer) # pylint: disable=protected-access
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Accessing protected methods in tests can make tests brittle. Consider exposing a public method for testing or using dependency injection to make the fetch operation testable.

Copilot uses AI. Check for mistakes.

Comment on lines +739 to +741
import logging

logging.debug("We see: %s", service._dispatches)
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug logging and protected member access should be removed from test code. This appears to be leftover debugging code.

Suggested change
import logging
logging.debug("We see: %s", service._dispatches)

Copilot uses AI. Check for mistakes.

await asyncio.sleep(1)

with pytest.raises(asyncio.TimeoutError):
logging.debug("Wait for now starts %s", _now())
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug logging should be removed from test code. This appears to be leftover debugging code.

Suggested change
logging.debug("Wait for now starts %s", _now())

Copilot uses AI. Check for mistakes.

Comment on lines +405 to 416
# Delete old dispatches
for dispatch_id in old_dispatches:
if dispatch_id in new_dispatches:
continue

dispatch = self._dispatches.pop(dispatch_id)
_logger.debug("Deleted dispatch: %s", dispatch)
await self._lifecycle_events_tx.send(Deleted(dispatch=dispatch))
await self._update_dispatch_schedule_and_notify(None, dispatch, timer)

# Set deleted only here as it influences the result of dispatch.started
# which is used in above in _running_state_change
dispatch._set_deleted() # pylint: disable=protected-access
await self._lifecycle_events_tx.send(Deleted(dispatch=dispatch))
self._dispatches.update(new_dispatches)

Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dispatches dictionary is updated after processing deletions, but the deletion logic uses the old dispatches dictionary. This could lead to inconsistent state if the same dispatch ID exists in both old and new dispatches.

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:dispatcher Affects the high-level dispatcher interface part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant