Skip to content

test: wait for conditions instead of fixed delays in e2ee tests - #2296

Open
1egoman wants to merge 1 commit into
mainfrom
test/vi-waitfor-instead-of-delays
Open

1egoman wants to merge 1 commit into
mainfrom
test/vi-waitfor-instead-of-delays

Conversation

@1egoman

@1egoman 1egoman commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to the review on #2285: replaces fixed 10–20ms sleeps that let tests win a race with vi.waitFor on the condition the test is actually waiting for.

Changes

src/e2ee/subscriberBlackScreen.test.ts

  • "rebuilds a dead pipeline…": wait until hasActiveTransform() is false instead of sleeping 10ms. (setupTransform sets the transform synchronously and the pipe's finally clears it, so the condition can't be true before the work happens.)
  • "still forwards plaintext…": wait until the frame reaches the decoder.

src/e2ee/worker/e2ee.worker.test.ts

  • The postMessage stub now records the worker's replies.
  • The "wait until the worker is through" sleeps are replaced by a barrier: post an enable message for a throwaway identity and wait for its echo. The worker runs messages one at a time through an AsyncQueue, so once the echo arrives every earlier message has been handled.
  • Wait for the encrypted frame to be sent (the same change as test: wait for the encrypted frame in the e2ee worker test #2285; whichever merges second needs a trivial rebase).
  • The "frame is not decoded" check used to be "sleep 20ms, assert nothing arrived". It now first waits for the worker's MissingKey error for alice, which is a positive sign that the frame was processed and dropped, and then asserts nothing was decoded. I checked that flipping the track to unencrypted still makes this test fail.

Not changed

  • subscriberBlackScreen.test.ts "must not forward ciphertext…" and "drops silently…" also use 10ms sleeps before checking that nothing happened. They need a positive "frame was consumed" signal first (vi.waitFor on a negative assertion passes immediately), so they're left for a follow-up.
  • SignalClient.e2e.test.ts sleep(4_000): the sleep is the test (stay connected past the server's 3s ping timeout), so there's no condition to poll.

Reliability check

A temporary stress workflow, since removed from the branch, ran both files 160 times per setup with the old (main) and new versions. It ran them idle and with vitest pinned to one core alongside 4 stress-ng CPU hogs. The old versions failed all 160 starved runs; the new versions passed every run. See the results comment.

The barrier relies on the worker echoing enable messages back. That echo is an existing ack the main thread already depends on, so I didn't add a dedicated ping/pong message just for tests. If the echo is ever removed, vi.waitFor times out and the test fails loudly rather than passing silently.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
dist/livekit-client.esm.mjs 118.41 KB (0%)
dist/livekit-client.umd.js 127.94 KB (0%)
dist/core/core.mjs 79.82 KB (0%)

@1egoman

1egoman commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Stress results (run)

Both test files were run 40 times in each of 4 shards, comparing the fixed-delay versions from main ("old") with this branch ("new"). In the "starved" runs, vitest is pinned to core 0 alongside 4 stress-ng --cpu workers. That makes a run take about 16s instead of about 1.5s.

variant load runs failed runs
old idle 160 0
old starved 160 160
new idle 160 0
new starved 160 0
  • Every failure on main under load is e2ee worker > keeps sender and receiver cryptors with the same track id apart (expected [] to have a length of 1 but got +0). This is the race that test: wait for the encrypted frame in the e2ee worker test #2285 targets: 20ms isn't long enough for the encrypt step when the CPU is contended.
  • The new versions never failed, including the "frame is not decoded" check. It now waits for the worker's MissingKey error before checking that nothing was decoded. On main, that test "passes" under load whether or not the frame was ever processed.
  • vi.waitFor's default 1s timeout held up even at about 10x slowdown. I didn't need to raise it.

I'll drop the temporary stress-waitfor.yaml commit before merge. I added the internal label because this PR only touches tests and needs no changeset.

🤖 Generated with Claude Code

Replace the 10ms/20ms sleeps that let these tests win a race with vi.waitFor
on the condition they are waiting for:

- subscriberBlackScreen: wait for the dead transform to be torn down and for
  the plaintext frame to reach the decoder.
- e2ee.worker: record the worker's replies. Wait until the worker has handled
  its setup messages with a barrier (an `enable` message, which the worker
  echoes back once every earlier message is through), wait for the encrypted
  frame, and wait for the MissingKey error before checking that the frame was
  not decoded.
@1egoman
1egoman force-pushed the test/vi-waitfor-instead-of-delays branch from b721831 to 6965cbd Compare November 22, 2026 21:03
Comment on lines +18 to +28
const waitUntilWorkerHasProcessedAllMessages = async () => {
const participantIdentity = `barrier-${(barrierCount += 1)}`;
post({ kind: 'enable', data: { enabled: false, participantIdentity } });
await vi.waitFor(() =>
expect(
replies.some(
(r) => r.kind === 'enable' && r.data.participantIdentity === participantIdentity,
),
).toBe(true),
);
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thought: I read this and briefly considered adding a ping / pong types of event s to the e2ee worker, but I think decided it is probably a bit too heavyweight to add something like this just for a test. But I could be convinced otherwise.

@1egoman
1egoman marked this pull request as ready for review November 22, 2026 21:12

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔍 Devin Review: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

@lukasIO lukasIO left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nice!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants