Skip to content

test: deterministic broadcast batching/ordering tests via fakeAsync#37

Merged
danReynolds merged 2 commits into
mainfrom
claude/broadcast-timing-tests
May 31, 2026
Merged

test: deterministic broadcast batching/ordering tests via fakeAsync#37
danReynolds merged 2 commits into
mainfrom
claude/broadcast-timing-tests

Conversation

@danReynolds

Copy link
Copy Markdown
Owner

Summary

Adds deterministic tests for broadcast batching, coalescing, and ordering using fakeAsync.

Loon schedules a broadcast on a zero-duration timer so that all writes in a single event-loop task are delivered to observers as one update. Verifying that behaviour with real time is inherently racy — it depends on a real timer firing before the test's await — which is a known source of flakiness in the suite. These tests advance virtual time explicitly, so the broadcast timer and its microtask stream delivery fire deterministically before each assertion, with no dependence on wall-clock scheduling or CPU load.

What's covered

  • Batching — three creates in one task produce exactly one emission (initial + one batch).
  • Coalescing — create + update in one task collapse to a single emission of the final value.
  • Per-task separation — writes in separate tasks produce separate broadcasts.
  • No-op suppression — an update with an unchanged value does not rebroadcast.

Each test runs inside a single fakeAsync zone, and the file gets its own test isolate (so the global store starts clean and these virtual-time tests are isolated from the rest of the suite). Verified deterministic across repeated runs.

Context / scope

This is the additive half of the "fakeAsync" hardening item: new deterministic ordering tests. I deliberately did not convert the existing real-async suite to fakeAsync. Investigation showed the suite's intermittent failures aren't purely a timing race — there's a cross-test state-bleed component (resetting the global store between tests schedules a broadcast that can race the next test) that a fakeAsync conversion wouldn't cleanly fix, and a wholesale rewrite of a working suite is high-risk for a flake that's already mitigated in CI (--concurrency=1 + re-kick). The deterministic delete+recreate-coalescing case lives with its fix in #36.

Test plan

  • flutter test test/core/broadcast_timing_test.dart — 4 tests green; deterministic across 5+ repeated runs
  • flutter analyze clean on the new file
  • flutter test test/core — green

Generated by Claude Code

Loon schedules broadcasts on a zero-duration timer so writes in one
event-loop task are delivered as a single update. Testing that with real
time is inherently racy (it depends on a real timer firing before the
test's wait), which is a known source of flakiness in the suite.

These tests run under fakeAsync: virtual time is advanced explicitly so
the broadcast timer and its microtask stream delivery fire deterministi-
cally before each assertion, independent of wall-clock scheduling or CPU
load. They cover broadcast batching (many writes -> one emission),
create+update coalescing, per-task broadcast separation, and that a no-op
update does not rebroadcast.

A dedicated file runs in its own test isolate, so the global store starts
clean and these virtual-time tests are isolated from the rest of the
suite. fake_async is pulled in via flutter_test.
Copilot AI review requested due to automatic review settings May 26, 2026 18:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a new deterministic test suite to validate Loon’s broadcast batching/coalescing behavior by advancing virtual time with fakeAsync, avoiding wall-clock timing races and reducing test flakiness around zero-duration broadcast scheduling.

Changes:

  • Adds a dedicated fakeAsync-based test file covering batching, coalescing, per-task separation, and no-op suppression.
  • Introduces small helpers to reset the global store and advance virtual time between assertions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/core/broadcast_timing_test.dart Outdated
Comment on lines +1 to +4
// ignore_for_file: depend_on_referenced_packages
import 'package:fake_async/fake_async.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:loon/loon.dart';
@danReynolds danReynolds merged commit c265dde into main May 31, 2026
1 check passed
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.

3 participants