Skip to content

test(engine): force overdue branch in alarm-during-sleep test with negative offset#4758

Merged
NathanFlurry merged 1 commit intomainfrom
sleep-cleanup/13-alarm-test-deterministic
Apr 27, 2026
Merged

test(engine): force overdue branch in alarm-during-sleep test with negative offset#4758
NathanFlurry merged 1 commit intomainfrom
sleep-cleanup/13-alarm-test-deterministic

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 24, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown

claude Bot commented Apr 24, 2026

Code Review: PR #4758

Summary

This PR makes the alarm_overdue_during_sleep_transition_fires_via_reallocation test deterministic by replacing a timing-based approach (short 100ms alarm offset that races the scheduler) with a negative offset (-1000ms) that guarantees the alarm is already overdue when Decision::Sleep evaluates it. The #[ignore] attribute is removed as the test no longer depends on a timing race. This is a test-only change with no production code impact.


Correctness

The core idea is sound. The alarm timestamp is computed as:

let alarm_time = get_current_timestamp_ms() + self.alarm_offset_ms;

With alarm_offset_ms = -1000, alarm_time is 1 second in the past at the moment on_start runs. By the time handle_stopped evaluates Decision::Sleep, now >= alarm_ts is trivially true on every run, eliminating the race entirely. This is the right approach.


Potential Issue: Subscription Ordering Race

The subscribe_lifecycle_events() call still happens after ready_rx.await, which means the generation-0 actor has already called send_set_alarm(...) and send_sleep_intent(). With a negative offset, reallocation fires immediately after handle_stopped, so there is a window between ready_rx resolving and subscribe_lifecycle_events() executing during which the Started { generation: 1 } event could fire and be missed.

The comment says "Subscribe before the actor enters sleep," but the subscription happens after the ready signal, which comes from inside on_start for generation 0 - the same call that dispatches the sleep intent. This window is very small, but it is not zero.

A safer sequence would be:

  1. Call subscribe_lifecycle_events() before create_actor.
  2. Await ready_rx.
  3. Then await the generation-1 Started event.

Whether this is a real concern depends on the channel buffer size of subscribe_lifecycle_events(). If the broadcast::Receiver buffers the event before the subscription is created, it will be received fine. But if the event is lagged/dropped, the test would time out instead of failing with a clear error.

Recommendation: Move subscribe_lifecycle_events() before create_actor to make the subscription ordering provably correct, matching the defensive pattern used elsewhere in the test suite.


Code Quality

  • The updated doc comment is clearer and more accurate than the original. Explicitly mentioning the -1000ms offset is helpful.
  • Removing #[ignore] is correct given the test is now deterministic.
  • Inline comments are well-written and explain the intent clearly.

Minor Nit

There is no assertion that the actor did not enter sleep state. The test only asserts the positive path (generation-1 wake via reallocation). This is acceptable given the timeout semantics, but a brief comment explaining this choice would be helpful.


Summary

Severity Issue
Minor subscribe_lifecycle_events() is called after ready_rx.await; the generation-1 Started event could theoretically be missed before the subscription is registered. Consider subscribing before create_actor.
Nit No comment explaining that the negative sleep path is implicitly tested via timeout.

The core change is correct and a clear improvement over the previous timing-dependent approach.

@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/12-sleep-destroy-mid-shutdown-diagnostic branch from 1939547 to 68ddecb Compare April 24, 2026 13:16
@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/13-alarm-test-deterministic branch from 3f88401 to 0e7355d Compare April 24, 2026 13:16
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 24, 2026

Preview packages published to npm

Install with:

npm install rivetkit@pr-4758

All packages published as 0.0.0-pr.4758.f601302 with tag pr-4758.

Engine binary is shipped via @rivetkit/engine-cli on linux-x64-musl, linux-arm64-musl, darwin-x64, and darwin-arm64. Windows users should use the release installer or set RIVET_ENGINE_BINARY.

Docker images:

docker pull rivetdev/engine:slim-f601302
docker pull rivetdev/engine:full-f601302
Individual packages
npm install rivetkit@pr-4758
npm install @rivetkit/react@pr-4758
npm install @rivetkit/rivetkit-napi@pr-4758
npm install @rivetkit/workflow-engine@pr-4758

@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/12-sleep-destroy-mid-shutdown-diagnostic branch from 68ddecb to a4740bd Compare April 26, 2026 02:34
@NathanFlurry NathanFlurry force-pushed the sleep-cleanup/13-alarm-test-deterministic branch from 0e7355d to 14910da Compare April 26, 2026 02:34
Base automatically changed from sleep-cleanup/12-sleep-destroy-mid-shutdown-diagnostic to main April 27, 2026 07:13
@NathanFlurry NathanFlurry merged commit 14910da into main Apr 27, 2026
18 of 22 checks passed
@NathanFlurry NathanFlurry deleted the sleep-cleanup/13-alarm-test-deterministic branch April 27, 2026 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant