-
Notifications
You must be signed in to change notification settings - Fork 268
Implement negotiation tracking based on offerId #1927
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
lukasIO
wants to merge
6
commits into
main
Choose a base branch
from
lukas/negotiaton-loo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+354
−34
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5aae9c1
add unit tests for negotiation tracking
lukasIO 160723b
implement negotiation tracking based on offerId
lukasIO 6baddb6
Create eight-eggs-obey.md
lukasIO a349cfa
comments
lukasIO 90bbb2e
Merge branch 'lukas/negotiaton-loo' of github.com:livekit/client-sdk-…
lukasIO 106574e
tighten event payload
lukasIO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "livekit-client": patch | ||
| --- | ||
|
|
||
| Implement negotiation tracking based on offerId |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,282 @@ | ||
| import { EventEmitter } from 'events'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { PCEvents } from './PCTransport'; | ||
| import { PCTransportManager } from './PCTransportManager'; | ||
|
|
||
| class StubPC { | ||
| iceConnectionState: RTCIceConnectionState = 'new'; | ||
|
|
||
| signalingState: RTCSignalingState = 'stable'; | ||
|
|
||
| connectionState: RTCPeerConnectionState = 'new'; | ||
|
|
||
| onicecandidate: ((ev: RTCPeerConnectionIceEvent) => void) | null = null; | ||
|
|
||
| onicecandidateerror: ((ev: Event) => void) | null = null; | ||
|
|
||
| oniceconnectionstatechange: (() => void) | null = null; | ||
|
|
||
| onsignalingstatechange: (() => void) | null = null; | ||
|
|
||
| onconnectionstatechange: (() => void) | null = null; | ||
|
|
||
| ondatachannel: ((ev: RTCDataChannelEvent) => void) | null = null; | ||
|
|
||
| ontrack: ((ev: RTCTrackEvent) => void) | null = null; | ||
|
|
||
| getTransceivers() { | ||
| return []; | ||
| } | ||
|
|
||
| getSenders() { | ||
| return []; | ||
| } | ||
|
|
||
| close() {} | ||
|
|
||
| setConfiguration() {} | ||
| } | ||
|
|
||
| class FakePublisher extends EventEmitter { | ||
| latestOfferId = 0; | ||
|
|
||
| latestAcknowledgedOfferId = 0; | ||
|
|
||
| negotiate = vi.fn(async (_onError?: (e: Error) => void) => {}); | ||
|
|
||
| /** Simulate a publisher offer cycle: bump latestOfferId. */ | ||
| startOffer() { | ||
| this.latestOfferId += 1; | ||
| return this.latestOfferId; | ||
| } | ||
|
|
||
| /** Simulate a successful answer for the given offerId. */ | ||
| answer(offerId: number) { | ||
| this.latestAcknowledgedOfferId = offerId; | ||
| this.emit(PCEvents.OfferAnswered, offerId); | ||
| } | ||
| } | ||
|
|
||
| describe('PCTransportManager.negotiate', () => { | ||
| let originalRTCPeerConnection: unknown; | ||
|
|
||
| beforeEach(() => { | ||
| originalRTCPeerConnection = (globalThis as unknown as { RTCPeerConnection?: unknown }) | ||
| .RTCPeerConnection; | ||
| (globalThis as unknown as { RTCPeerConnection: unknown }).RTCPeerConnection = StubPC; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| (globalThis as unknown as { RTCPeerConnection: unknown }).RTCPeerConnection = | ||
| originalRTCPeerConnection; | ||
| }); | ||
|
|
||
| function makeManager() { | ||
| const manager = new PCTransportManager('publisher-only', {}); | ||
| const fake = new FakePublisher(); | ||
| (manager as unknown as { publisher: FakePublisher }).publisher = fake; | ||
| manager.peerConnectionTimeout = 200; | ||
| return { manager, pub: fake }; | ||
| } | ||
|
|
||
| it('resolves when an offer past the checkpoint is answered', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| const p = manager.negotiate(new AbortController()); | ||
| setTimeout(() => { | ||
| const id = pub.startOffer(); | ||
| pub.answer(id); | ||
| }, 10); | ||
| await expect(p).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('does not resolve on answers for offers at or before the checkpoint', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| // Some prior cycle is in flight with id=5 at the moment we capture our | ||
| // checkpoint. Its answer must NOT satisfy our request — our changes | ||
| // weren't in offer 5. | ||
| pub.latestOfferId = 5; | ||
| const ac = new AbortController(); | ||
| const p = manager.negotiate(ac); | ||
|
|
||
| let settled = false; | ||
| p.then( | ||
| () => { | ||
| settled = true; | ||
| }, | ||
| () => { | ||
| settled = true; | ||
| }, | ||
| ); | ||
|
|
||
| pub.answer(5); | ||
| await new Promise((r) => setTimeout(r, 50)); | ||
| expect(settled).toBe(false); | ||
|
|
||
| ac.abort(); | ||
| await expect(p).rejects.toThrow(/aborted/); | ||
| }); | ||
|
|
||
| it('resolves through the renegotiate-recursion path', async () => { | ||
| // Reproduces the field shape: we capture checkpoint=N while an offer N is | ||
| // in flight. The answer for N arrives (renegotiate=true on the publisher, | ||
| // so it doesn't satisfy us), then a follow-up offer N+1 is created and | ||
| // answered. We resolve on the second answer. | ||
| const { manager, pub } = makeManager(); | ||
| pub.latestOfferId = 1; | ||
| const p = manager.negotiate(new AbortController()); | ||
|
|
||
| setTimeout(() => pub.answer(1), 10); // does not satisfy checkpoint=1 | ||
| setTimeout(() => { | ||
| const id = pub.startOffer(); // 2 | ||
| pub.answer(id); | ||
| }, 30); | ||
|
Comment on lines
+128
to
+132
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thought: Two things:
// Some sort of explanation here
// does not satisfy checkpoint=1
pub.answer(1);
await sleep(20); // Or ideally if the delay isn't important, `sleep(0)` / `setImmediate`.
// Some sort of explanation here
const id = pub.startOffer(); // 2
pub.answer(id); |
||
|
|
||
| await expect(p).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('resolves immediately when an answer past the checkpoint already arrived', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| pub.latestOfferId = 3; | ||
| pub.latestAcknowledgedOfferId = 4; | ||
| await expect(manager.negotiate(new AbortController())).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it('resolves concurrent callers independently at their own checkpoints', async () => { | ||
| const { manager, pub } = makeManager(); | ||
|
|
||
| // A captures checkpoint=0 | ||
| const a = manager.negotiate(new AbortController()); | ||
| let aResolved = false; | ||
| a.then(() => { | ||
| aResolved = true; | ||
| }); | ||
|
|
||
| // First cycle starts and answers — A is satisfied (1 > 0) | ||
| const id1 = pub.startOffer(); | ||
|
|
||
| // B captures checkpoint=1 (an offer is now in flight) | ||
| const b = manager.negotiate(new AbortController()); | ||
| let bResolved = false; | ||
| b.then(() => { | ||
| bResolved = true; | ||
| }); | ||
|
|
||
| pub.answer(id1); | ||
| await new Promise((r) => setTimeout(r, 0)); | ||
| expect(aResolved).toBe(true); | ||
| expect(bResolved).toBe(false); | ||
|
|
||
| // B should resolve only on the next cycle | ||
| const id2 = pub.startOffer(); | ||
| pub.answer(id2); | ||
| await b; | ||
| expect(bResolved).toBe(true); | ||
| }); | ||
|
|
||
| it('rejects when the deadline elapses', async () => { | ||
| const { manager } = makeManager(); | ||
| await expect(manager.negotiate(new AbortController())).rejects.toThrow(/timed out/); | ||
| }); | ||
|
|
||
| it('rejects when the abort signal fires', async () => { | ||
| const { manager } = makeManager(); | ||
| const ac = new AbortController(); | ||
| setTimeout(() => ac.abort(), 10); | ||
| await expect(manager.negotiate(ac)).rejects.toThrow(/aborted/); | ||
| }); | ||
|
|
||
| it('rejects when publisher.negotiate invokes its error callback', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| pub.negotiate.mockImplementationOnce(async (onError?: (e: Error) => void) => { | ||
| onError?.(new Error('publisher boom')); | ||
| }); | ||
| await expect(manager.negotiate(new AbortController())).rejects.toThrow(/publisher boom/); | ||
| }); | ||
|
|
||
| describe('listener cleanup', () => { | ||
| it('after success', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| const p = manager.negotiate(new AbortController()); | ||
| const id = pub.startOffer(); | ||
| pub.answer(id); | ||
| await p; | ||
| expect(pub.listenerCount(PCEvents.OfferAnswered)).toBe(0); | ||
| }); | ||
|
|
||
| it('after non-matching answer (still pending), then abort', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| pub.latestOfferId = 5; | ||
| const ac = new AbortController(); | ||
| const p = manager.negotiate(ac); | ||
| pub.answer(5); // does not satisfy checkpoint=5 | ||
| expect(pub.listenerCount(PCEvents.OfferAnswered)).toBe(1); | ||
| ac.abort(); | ||
| await expect(p).rejects.toThrow(/aborted/); | ||
| expect(pub.listenerCount(PCEvents.OfferAnswered)).toBe(0); | ||
| }); | ||
|
|
||
| it('after deadline', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| await expect(manager.negotiate(new AbortController())).rejects.toThrow(/timed out/); | ||
| expect(pub.listenerCount(PCEvents.OfferAnswered)).toBe(0); | ||
| }); | ||
|
|
||
| it('after abort', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| const ac = new AbortController(); | ||
| const p = manager.negotiate(ac); | ||
| setTimeout(() => ac.abort(), 10); | ||
| await expect(p).rejects.toThrow(/aborted/); | ||
| expect(pub.listenerCount(PCEvents.OfferAnswered)).toBe(0); | ||
| }); | ||
|
|
||
| it('after publisher.negotiate errors', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| pub.negotiate.mockImplementationOnce(async (onError?: (e: Error) => void) => { | ||
| onError?.(new Error('publisher boom')); | ||
| }); | ||
| await expect(manager.negotiate(new AbortController())).rejects.toThrow(/publisher boom/); | ||
| expect(pub.listenerCount(PCEvents.OfferAnswered)).toBe(0); | ||
| }); | ||
|
|
||
| it('does not leak across many sequential negotiate() calls', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| for (let i = 0; i < 12; i += 1) { | ||
| const p = manager.negotiate(new AbortController()); | ||
| const id = pub.startOffer(); | ||
| pub.answer(id); | ||
| await p; | ||
| } | ||
| expect(pub.listenerCount(PCEvents.OfferAnswered)).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| // Regression test for publishing call getting stuck | ||
| // With the old design, NegotiationStarted firing faster than | ||
| // peerConnectionTimeout kept resetting the timer indefinitely while | ||
| // NegotiationComplete was suppressed by an unconverging `renegotiate` cycle, | ||
| // wedging the publishTrack Promise. The offerId-checkpoint design resolves | ||
| // on the first answer past the checkpoint, regardless of how many cycles | ||
| // start in between. | ||
| it('does not hang when many spurious cycles start without converging on the checkpoint', async () => { | ||
| const { manager, pub } = makeManager(); | ||
| manager.peerConnectionTimeout = 1500; | ||
| pub.latestOfferId = 1; // an unrelated cycle is in flight | ||
| const p = manager.negotiate(new AbortController()); | ||
|
|
||
| // Lots of NegotiationStarted noise (not listened to anymore) and a few | ||
| // answers that don't satisfy the checkpoint. | ||
| const noise = setInterval(() => pub.emit(PCEvents.NegotiationStarted), 30); | ||
| setTimeout(() => pub.answer(1), 50); // doesn't satisfy | ||
| setTimeout(() => { | ||
| const id = pub.startOffer(); // 2 | ||
| pub.answer(id); | ||
| }, 200); | ||
|
|
||
| try { | ||
| await expect(p).resolves.toBeUndefined(); | ||
| } finally { | ||
| clearInterval(noise); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really glad to see some tests here for this! Looking forward to seeing this grow moving forward.