Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

video-player.test.ts proves stop() closes the socket and the decoder and that no frame is drawn after it - test only #427

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/426-video-player-test-ts-proves-stop-closes → main 1 commit fork

  • Merged. smith/426-video-player-test-ts-proves-stop-closes was merged into main as be904f9.

Review

Sign in to review this change.
2 files changed +34 −3
Software/so101/src/ui/video-player.js +1
@@ -77,6 +77,7 @@ export class VideoPlayer {
7777 }
7878
7979 status(state) {
80 if (state === "none") this.stop(); // giving up releases the socket and the decoder
8081 this.o.onStatus?.({ state, age: this.age, rendition: this.rendition, ladder: this.ladder });
8182 }
8283
Software/so101/src/ui/video-player.test.ts +33 −3
@@ -30,7 +30,10 @@ class FakeSocket {
3030 send(s: string) {
3131 this.sent.push(s);
3232 }
33 close() {}
33 closes = 0;
34 close() {
35 this.closes++;
36 }
3437 }
3538
3639 function setup() {
@@ -39,9 +42,12 @@ function setup() {
3942 let configured: unknown;
4043 let output: (f: unknown) => void = () => {};
4144 const drawn: unknown[] = [];
45 const decoders: FakeDecoder[] = [];
4246 class FakeDecoder {
4347 decodeQueueSize = 0;
48 closes = 0;
4449 constructor(init: { output: (f: unknown) => void }) {
50 decoders.push(this);
4551 output = init.output;
4652 }
4753 configure(c: unknown) {
@@ -51,14 +57,16 @@ function setup() {
5157 decoded.push({ type: chunk.type, timestamp: chunk.timestamp, bytes: chunk.data.length });
5258 output({ displayWidth: 1280, displayHeight: 720, close() {} });
5359 }
54 close() {}
60 close() {
61 this.closes++;
62 }
5563 }
5664 const canvas = { width: 0, height: 0, getContext: () => ({ drawImage: (f: unknown) => drawn.push(f) }) };
5765 let now = 10_000;
5866 const player = new VideoPlayer({ url: "ws://x/video", canvas, onStatus: (s) => states.push(s.state), deps: { WebSocket: FakeSocket, VideoDecoder: FakeDecoder, EncodedVideoChunk: class { constructor(init: object) { Object.assign(this, init); } }, now: () => now } });
5967 player.start();
6068 const ws = FakeSocket.last;
61 return { player, ws, decoded, states, drawn, canvas, get configured() { return configured; }, tick: (ms: number) => (now += ms), set: (n: number) => (now = n), decoder: () => player.decoder };
69 return { player, ws, decoders, decoded, states, drawn, canvas, get configured() { return configured; }, tick: (ms: number) => (now += ms), set: (n: number) => (now = n), decoder: () => player.decoder };
6270 }
6371
6472 describe("video player", () => {
@@ -162,4 +170,26 @@ describe("video player", () => {
162170 expect(t.states).toEqual(["none"]);
163171 t.player.stop();
164172 });
173
174 test("stop() closes the socket and the decoder once, ignores later pictures, and can be called twice", () => {
175 const t = setup();
176 t.ws.onmessage!({ data: frame(true, 0, 10_000, pics[0]!) });
177 expect(t.decoders).toHaveLength(1);
178 t.player.stop();
179 expect(t.ws.closes).toBe(1);
180 expect(t.decoders[0]!.closes).toBe(1);
181 t.ws.onmessage!({ data: frame(true, 0, 10_000, pics[5]!) });
182 expect(t.decoded).toHaveLength(1);
183 expect(t.drawn).toHaveLength(1);
184 expect(() => t.player.stop()).not.toThrow();
185 });
186
187 test("giving up with none releases the decoder it made", () => {
188 const t = setup();
189 t.ws.onmessage!({ data: frame(true, 0, 10_000, pics[0]!) });
190 t.ws.onclose!();
191 expect(t.states.at(-1)).toBe("none");
192 expect(t.decoders).toHaveLength(1);
193 expect(t.decoders[0]!.closes).toBe(1);
194 });
165195 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs