@torrent-tv/proxy 2.21.1 → 2.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/docs/encode-run-state.md +96 -0
- package/package.json +2 -1
- package/scripts/render-run-graph.js +173 -0
- package/services/encode-exit.js +80 -0
- package/services/encode-run-state.js +488 -0
- package/services/hls-session-manager.js +7079 -6841
- package/test/encode-exit.test.js +93 -0
- package/test/encode-run-state.test.js +314 -0
- package/test/run-graph-drift.test.js +37 -0
- package/test/segment-serve-wiring.test.js +46 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The four things an ffmpeg exit can mean, by their field cases.
|
|
3
|
+
*
|
|
4
|
+
* Each test names the release the classification cost. None of them needs a
|
|
5
|
+
* process: what went wrong in every case was the reading of the exit, not the
|
|
6
|
+
* handling of it.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import assert from "node:assert/strict";
|
|
10
|
+
import test from "node:test";
|
|
11
|
+
|
|
12
|
+
import { ENCODE_EXIT, classifyEncodeExit } from "../services/encode-exit.js";
|
|
13
|
+
|
|
14
|
+
test("the predecessor a seek kills says nothing about the session", () => {
|
|
15
|
+
// The handler decides "is this exit mine" by comparing against the session's
|
|
16
|
+
// current process — and during a restart that field still names the process
|
|
17
|
+
// being killed, because the replacement is spawned afterwards. So a SIGKILLed
|
|
18
|
+
// predecessor passed the check and was read as the session's own run dying:
|
|
19
|
+
// a spurious "failed" that a segment request landing in that window is
|
|
20
|
+
// answered 500 for, and on a hardware host a permanent downgrade to libx264.
|
|
21
|
+
assert.equal(
|
|
22
|
+
classifyEncodeExit({ superseded: true, code: null, inputUnavailable: false }),
|
|
23
|
+
ENCODE_EXIT.IGNORED
|
|
24
|
+
);
|
|
25
|
+
// Including when it looks like a clean finish.
|
|
26
|
+
assert.equal(classifyEncodeExit({ superseded: true, code: 0 }), ENCODE_EXIT.IGNORED);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("exit 0 short of the last segment is a failure, not a finished file", () => {
|
|
30
|
+
// 2.9.104. A run that had made 188 segments of 624 reported itself complete,
|
|
31
|
+
// the player consumed what was on disk and froze on the first segment nobody
|
|
32
|
+
// was making.
|
|
33
|
+
assert.equal(
|
|
34
|
+
classifyEncodeExit({ code: 0, producedThrough: 188, lastSegmentIndex: 623 }),
|
|
35
|
+
ENCODE_EXIT.SHORT
|
|
36
|
+
);
|
|
37
|
+
assert.equal(
|
|
38
|
+
classifyEncodeExit({ code: 0, producedThrough: 623, lastSegmentIndex: 623 }),
|
|
39
|
+
ENCODE_EXIT.COMPLETE
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("exit 0 is complete when there is nothing to check it against", () => {
|
|
44
|
+
// No published last segment, or nothing readable on disk: the claim cannot be
|
|
45
|
+
// contradicted, so it stands. Guessing "short" here would fail every session
|
|
46
|
+
// whose directory could not be read.
|
|
47
|
+
assert.equal(classifyEncodeExit({ code: 0, producedThrough: null, lastSegmentIndex: 623 }), ENCODE_EXIT.COMPLETE);
|
|
48
|
+
assert.equal(classifyEncodeExit({ code: 0, producedThrough: 12, lastSegmentIndex: null }), ENCODE_EXIT.COMPLETE);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("data that went away is recoverable, and is not an encoder fault", () => {
|
|
52
|
+
// Field 2026-08-06: a torrent evicted mid-seek took the film with it, the run
|
|
53
|
+
// died on `File 0 not found`, and the session answered 500 from then on while
|
|
54
|
+
// the swarm was there and the data would have come back in seconds.
|
|
55
|
+
assert.equal(
|
|
56
|
+
classifyEncodeExit({ code: 1, inputUnavailable: true }),
|
|
57
|
+
ENCODE_EXIT.INPUT_LOST
|
|
58
|
+
);
|
|
59
|
+
// And the distinction is what keeps a working hardware encoder: the caller
|
|
60
|
+
// downgrades to software on FAILED, so an input that vanished must not
|
|
61
|
+
// arrive there.
|
|
62
|
+
assert.notEqual(classifyEncodeExit({ code: 1, inputUnavailable: true }), ENCODE_EXIT.FAILED);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("anything else is a failure of this target", () => {
|
|
66
|
+
assert.equal(classifyEncodeExit({ code: 1, inputUnavailable: false }), ENCODE_EXIT.FAILED);
|
|
67
|
+
assert.equal(classifyEncodeExit({ code: null, inputUnavailable: false }), ENCODE_EXIT.FAILED);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("every combination answers, and no input produces undefined", () => {
|
|
71
|
+
for (const superseded of [true, false]) {
|
|
72
|
+
for (const code of [0, 1, null]) {
|
|
73
|
+
for (const producedThrough of [null, 0, 10]) {
|
|
74
|
+
for (const lastSegmentIndex of [null, 0, 10]) {
|
|
75
|
+
for (const inputUnavailable of [true, false]) {
|
|
76
|
+
const answer = classifyEncodeExit({
|
|
77
|
+
superseded,
|
|
78
|
+
code,
|
|
79
|
+
producedThrough,
|
|
80
|
+
lastSegmentIndex,
|
|
81
|
+
inputUnavailable
|
|
82
|
+
});
|
|
83
|
+
assert.ok(
|
|
84
|
+
Object.values(ENCODE_EXIT).includes(answer),
|
|
85
|
+
`answered ${String(answer)} for ${JSON.stringify({ superseded, code, producedThrough, lastSegmentIndex, inputUnavailable })}`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
assert.ok(Object.values(ENCODE_EXIT).includes(classifyEncodeExit()));
|
|
93
|
+
});
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The encoder-run table, exercised as a graph.
|
|
3
|
+
*
|
|
4
|
+
* These are properties of the specification, not of any run: no ffmpeg, no
|
|
5
|
+
* torrent, no filesystem, no clock. What they buy is the thing five field
|
|
6
|
+
* failures in a row were — an empty cell — being visible before a release
|
|
7
|
+
* rather than after one.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import test from "node:test";
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
ABSENT_EDGE_INVARIANTS,
|
|
15
|
+
ENCODE_RUN_EVENT,
|
|
16
|
+
ENCODE_RUN_STATE,
|
|
17
|
+
ENCODE_RUN_SUPERSTATE,
|
|
18
|
+
INITIAL_RUN_STATE,
|
|
19
|
+
answerForMissingSegment,
|
|
20
|
+
declaredContainment,
|
|
21
|
+
declaredEdges,
|
|
22
|
+
isInputBeingRead,
|
|
23
|
+
isWithin,
|
|
24
|
+
mayRestart,
|
|
25
|
+
nextState,
|
|
26
|
+
processCanBeSignalled,
|
|
27
|
+
wireState
|
|
28
|
+
} from "../services/encode-run-state.js";
|
|
29
|
+
|
|
30
|
+
const ALL_STATES = Object.values(ENCODE_RUN_STATE);
|
|
31
|
+
const ALL_EVENTS = Object.values(ENCODE_RUN_EVENT);
|
|
32
|
+
|
|
33
|
+
// ------------------------------------------------------------ graph discipline
|
|
34
|
+
|
|
35
|
+
test("the relation is deterministic", () => {
|
|
36
|
+
for (const state of ALL_STATES) {
|
|
37
|
+
for (const event of ALL_EVENTS) {
|
|
38
|
+
assert.equal(nextState(state, event), nextState(state, event));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("every pair is answered, and nothing throws", () => {
|
|
44
|
+
for (const state of ALL_STATES) {
|
|
45
|
+
for (const event of ALL_EVENTS) {
|
|
46
|
+
const target = nextState(state, event);
|
|
47
|
+
assert.ok(
|
|
48
|
+
target === null || ALL_STATES.includes(target),
|
|
49
|
+
`${state} + ${event} answered ${String(target)}, which is neither a state nor "ignored"`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
// Nonsense in, "ignored" out — never an exception. The machine this pattern
|
|
54
|
+
// replaces threw from inside an event handler, which left the caller's work
|
|
55
|
+
// half applied.
|
|
56
|
+
assert.equal(nextState("NOT_A_STATE", ENCODE_RUN_EVENT.SPAWNED), null);
|
|
57
|
+
assert.equal(nextState(ENCODE_RUN_STATE.IDLE, "NOT_AN_EVENT"), null);
|
|
58
|
+
assert.equal(nextState(undefined, undefined), null);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("one target per state and event — no pair is declared twice", () => {
|
|
62
|
+
const seen = new Set();
|
|
63
|
+
for (const edge of declaredEdges()) {
|
|
64
|
+
const pair = `${edge.from}+${edge.event}`;
|
|
65
|
+
assert.ok(!seen.has(pair), `${pair} is declared more than once`);
|
|
66
|
+
seen.add(pair);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("the table cannot be edited through what it hands out", () => {
|
|
71
|
+
const [first] = declaredEdges();
|
|
72
|
+
const mutated = declaredEdges();
|
|
73
|
+
mutated[0].to = "TAMPERED";
|
|
74
|
+
assert.equal(declaredEdges()[0].to, first.to);
|
|
75
|
+
const containment = declaredContainment();
|
|
76
|
+
containment[0].parent = "TAMPERED";
|
|
77
|
+
assert.notEqual(declaredContainment()[0].parent, "TAMPERED");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("every state is reachable from where a run begins", () => {
|
|
81
|
+
const seen = new Set([INITIAL_RUN_STATE]);
|
|
82
|
+
const queue = [INITIAL_RUN_STATE];
|
|
83
|
+
while (queue.length > 0) {
|
|
84
|
+
const state = queue.shift();
|
|
85
|
+
for (const event of ALL_EVENTS) {
|
|
86
|
+
const target = nextState(state, event);
|
|
87
|
+
if (target && !seen.has(target)) {
|
|
88
|
+
seen.add(target);
|
|
89
|
+
queue.push(target);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
assert.deepEqual(
|
|
94
|
+
ALL_STATES.filter((state) => !seen.has(state)),
|
|
95
|
+
[],
|
|
96
|
+
"a state no sequence of events can enter is either dead code or a missing edge"
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("no state is a dead end — every one can start a run again", () => {
|
|
101
|
+
for (const state of ALL_STATES) {
|
|
102
|
+
assert.equal(
|
|
103
|
+
nextState(state, ENCODE_RUN_EVENT.SPAWNED),
|
|
104
|
+
ENCODE_RUN_STATE.STARTING,
|
|
105
|
+
`${state} must be able to spawn a run: a session that cannot restart answers 500 for ever, ` +
|
|
106
|
+
"which is what the roadmap calls the empty cell"
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("the two edges that lead back to their own state are deliberate", () => {
|
|
112
|
+
const selfEdges = [];
|
|
113
|
+
for (const state of ALL_STATES) {
|
|
114
|
+
for (const event of ALL_EVENTS) {
|
|
115
|
+
if (nextState(state, event) === state) {
|
|
116
|
+
selfEdges.push(`${state} + ${event}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
assert.deepEqual(
|
|
121
|
+
selfEdges.sort(),
|
|
122
|
+
[
|
|
123
|
+
// A new run spawned while one was starting: the state is right already,
|
|
124
|
+
// and nothing in the caller re-runs on entry — the spawn has happened.
|
|
125
|
+
`${ENCODE_RUN_STATE.STARTING} + ${ENCODE_RUN_EVENT.SPAWNED}`,
|
|
126
|
+
// Suspending what is suspended. The caller returns early before signalling,
|
|
127
|
+
// so this is the answer to an event that cannot actually be raised twice.
|
|
128
|
+
`${ENCODE_RUN_STATE.SUSPENDED} + ${ENCODE_RUN_EVENT.SUSPEND_ORDERED}`
|
|
129
|
+
].sort(),
|
|
130
|
+
"an edge that leads back to its own state must be argued for, not acquired"
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("the table stays small enough to hold in the head", () => {
|
|
135
|
+
assert.ok(
|
|
136
|
+
declaredEdges().length <= 12,
|
|
137
|
+
`${declaredEdges().length} declared edges — a near-complete digraph asserts nothing`
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// ------------------------------------------------------- what must not happen
|
|
142
|
+
|
|
143
|
+
test("the absent edges are absent", () => {
|
|
144
|
+
for (const invariant of ABSENT_EDGE_INVARIANTS) {
|
|
145
|
+
assert.notEqual(
|
|
146
|
+
nextState(invariant.from, invariant.event),
|
|
147
|
+
invariant.mustNotReach,
|
|
148
|
+
`${invariant.from} + ${invariant.event} reached ${invariant.mustNotReach}: ${invariant.because}`
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("an event that means nothing here is ignored, not obeyed", () => {
|
|
154
|
+
assert.equal(nextState(ENCODE_RUN_STATE.IDLE, ENCODE_RUN_EVENT.FIRST_SEGMENT), null);
|
|
155
|
+
assert.equal(nextState(ENCODE_RUN_STATE.IDLE, ENCODE_RUN_EVENT.EXITED_COMPLETE), null);
|
|
156
|
+
assert.equal(nextState(ENCODE_RUN_STATE.STOPPED, ENCODE_RUN_EVENT.RESUME_ORDERED), null);
|
|
157
|
+
assert.equal(nextState(ENCODE_RUN_STATE.ENDED_COMPLETE, ENCODE_RUN_EVENT.SUSPEND_ORDERED), null);
|
|
158
|
+
assert.equal(nextState(ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_EVENT.RETRY_DUE), null);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// ------------------------------------------------------------------ hierarchy
|
|
162
|
+
|
|
163
|
+
test("an edge on a superstate reaches every state inside it", () => {
|
|
164
|
+
const alive = ALL_STATES.filter((state) => isWithin(state, ENCODE_RUN_SUPERSTATE.ALIVE));
|
|
165
|
+
assert.deepEqual(
|
|
166
|
+
alive.sort(),
|
|
167
|
+
[ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_STATE.STARTING, ENCODE_RUN_STATE.SUSPENDED].sort()
|
|
168
|
+
);
|
|
169
|
+
for (const state of alive) {
|
|
170
|
+
assert.equal(nextState(state, ENCODE_RUN_EVENT.EXITED_FAILED), ENCODE_RUN_STATE.ENDED_FAILED);
|
|
171
|
+
assert.equal(nextState(state, ENCODE_RUN_EVENT.EXITED_INPUT_LOST), ENCODE_RUN_STATE.RETRY_WAIT);
|
|
172
|
+
assert.equal(nextState(state, ENCODE_RUN_EVENT.STOP_ORDERED), ENCODE_RUN_STATE.STOPPED);
|
|
173
|
+
}
|
|
174
|
+
const working = ALL_STATES.filter((state) => isWithin(state, ENCODE_RUN_SUPERSTATE.WORKING));
|
|
175
|
+
assert.deepEqual(working.sort(), [...alive, ENCODE_RUN_STATE.RETRY_WAIT].sort());
|
|
176
|
+
for (const state of ALL_STATES) {
|
|
177
|
+
assert.ok(isWithin(state, ENCODE_RUN_SUPERSTATE.RUN), `${state} must sit inside RUN`);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("a state's own edge wins over the one it inherits", () => {
|
|
182
|
+
// STARTING inherits SPAWNED from RUN and declares FIRST_SEGMENT itself; the
|
|
183
|
+
// walk must stop at the first hit rather than continue up the chain.
|
|
184
|
+
assert.equal(
|
|
185
|
+
nextState(ENCODE_RUN_STATE.STARTING, ENCODE_RUN_EVENT.FIRST_SEGMENT),
|
|
186
|
+
ENCODE_RUN_STATE.PRODUCING
|
|
187
|
+
);
|
|
188
|
+
assert.equal(
|
|
189
|
+
nextState(ENCODE_RUN_STATE.SUSPENDED, ENCODE_RUN_EVENT.RESUME_ORDERED),
|
|
190
|
+
ENCODE_RUN_STATE.PRODUCING
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// -------------------------------------------------------------------- outputs
|
|
195
|
+
|
|
196
|
+
test("only a live run reads its input", () => {
|
|
197
|
+
assert.equal(isInputBeingRead(ENCODE_RUN_STATE.STARTING), true);
|
|
198
|
+
assert.equal(isInputBeingRead(ENCODE_RUN_STATE.PRODUCING), true);
|
|
199
|
+
assert.equal(
|
|
200
|
+
isInputBeingRead(ENCODE_RUN_STATE.SUSPENDED),
|
|
201
|
+
false,
|
|
202
|
+
"a suspended encoder reads nothing — the reader's window is then satisfied, WebTorrent drops " +
|
|
203
|
+
"the selection, and the download dies with the swarm open (eleven minutes, 2026-08-05)"
|
|
204
|
+
);
|
|
205
|
+
for (const state of [
|
|
206
|
+
ENCODE_RUN_STATE.IDLE,
|
|
207
|
+
ENCODE_RUN_STATE.RETRY_WAIT,
|
|
208
|
+
ENCODE_RUN_STATE.STOPPED,
|
|
209
|
+
ENCODE_RUN_STATE.ENDED_COMPLETE,
|
|
210
|
+
ENCODE_RUN_STATE.ENDED_FAILED
|
|
211
|
+
]) {
|
|
212
|
+
assert.equal(isInputBeingRead(state), false, `${state} has no process to read anything`);
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("a signal may be sent only where a process exists", () => {
|
|
217
|
+
for (const state of ALL_STATES) {
|
|
218
|
+
assert.equal(
|
|
219
|
+
processCanBeSignalled(state),
|
|
220
|
+
isWithin(state, ENCODE_RUN_SUPERSTATE.ALIVE),
|
|
221
|
+
`${state} answered the wrong thing about whether it holds a process`
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
test("a missing segment is held everywhere except a terminal failure", () => {
|
|
227
|
+
for (const state of ALL_STATES) {
|
|
228
|
+
assert.equal(
|
|
229
|
+
answerForMissingSegment(state),
|
|
230
|
+
state === ENCODE_RUN_STATE.ENDED_FAILED ? "fail" : "hold",
|
|
231
|
+
`${state} must give one answer for a missing segment, not one per call site`
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("the wire state is a function of the run state alone", () => {
|
|
237
|
+
assert.equal(wireState(ENCODE_RUN_STATE.IDLE), "starting");
|
|
238
|
+
assert.equal(wireState(ENCODE_RUN_STATE.RETRY_WAIT), "starting");
|
|
239
|
+
assert.equal(wireState(ENCODE_RUN_STATE.STARTING), "running");
|
|
240
|
+
assert.equal(wireState(ENCODE_RUN_STATE.PRODUCING), "running");
|
|
241
|
+
assert.equal(wireState(ENCODE_RUN_STATE.SUSPENDED), "running");
|
|
242
|
+
assert.equal(wireState(ENCODE_RUN_STATE.STOPPED), "running");
|
|
243
|
+
assert.equal(wireState(ENCODE_RUN_STATE.ENDED_COMPLETE), "ready");
|
|
244
|
+
assert.equal(wireState(ENCODE_RUN_STATE.ENDED_FAILED), "failed");
|
|
245
|
+
for (const state of ALL_STATES) {
|
|
246
|
+
assert.ok(
|
|
247
|
+
["starting", "running", "ready", "failed"].includes(wireState(state)),
|
|
248
|
+
`${state} produced a wire state the browser has never been told about`
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("only a finished file refuses to be repositioned", () => {
|
|
254
|
+
for (const state of ALL_STATES) {
|
|
255
|
+
assert.equal(mayRestart(state), state !== ENCODE_RUN_STATE.ENDED_COMPLETE);
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// ------------------------------------------------- the failures, by their name
|
|
260
|
+
|
|
261
|
+
test("the sawtooth of 2.9.93 is not expressible", () => {
|
|
262
|
+
// Any segment request released a suspended encoder, and the run drifted from
|
|
263
|
+
// 155 s to 922 s ahead of the viewer in three minutes. Resuming is an output
|
|
264
|
+
// of how far ahead the run is, computed by the caller — never an edge hung on
|
|
265
|
+
// a request arriving.
|
|
266
|
+
assert.equal(nextState(ENCODE_RUN_STATE.SUSPENDED, ENCODE_RUN_EVENT.FIRST_SEGMENT), null);
|
|
267
|
+
assert.equal(
|
|
268
|
+
nextState(ENCODE_RUN_STATE.SUSPENDED, ENCODE_RUN_EVENT.RESUME_ORDERED),
|
|
269
|
+
ENCODE_RUN_STATE.PRODUCING
|
|
270
|
+
);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
test("a dead run cannot be mistaken for one that is covering the seek", () => {
|
|
274
|
+
// `session.ffmpeg` pointed at a corpse, so every later seek was waved through
|
|
275
|
+
// as "already covered by the running encode" and the session answered 500 for
|
|
276
|
+
// as long as the viewer kept trying.
|
|
277
|
+
assert.equal(processCanBeSignalled(ENCODE_RUN_STATE.ENDED_FAILED), false);
|
|
278
|
+
assert.equal(isInputBeingRead(ENCODE_RUN_STATE.ENDED_FAILED), false);
|
|
279
|
+
assert.equal(
|
|
280
|
+
nextState(ENCODE_RUN_STATE.ENDED_FAILED, ENCODE_RUN_EVENT.SPAWNED),
|
|
281
|
+
ENCODE_RUN_STATE.STARTING,
|
|
282
|
+
"and the way out of it is a spawn — the edge whose absence is the empty cell"
|
|
283
|
+
);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
test("a rung the viewer left is not the same as a run that has not started", () => {
|
|
287
|
+
// Both are "no process" in the field set this replaces, and they call for
|
|
288
|
+
// opposite answers: one must not be revived by its own held requests, the
|
|
289
|
+
// other is waiting to be spawned.
|
|
290
|
+
assert.notEqual(ENCODE_RUN_STATE.STOPPED, ENCODE_RUN_STATE.IDLE);
|
|
291
|
+
assert.equal(nextState(ENCODE_RUN_STATE.STOPPED, ENCODE_RUN_EVENT.RESUME_ORDERED), null);
|
|
292
|
+
assert.equal(nextState(ENCODE_RUN_STATE.IDLE, ENCODE_RUN_EVENT.SPAWNED), ENCODE_RUN_STATE.STARTING);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test("an input that dried up is not a finished file", () => {
|
|
296
|
+
// ffmpeg exits 0 for both, and the difference had to be recovered by
|
|
297
|
+
// comparing what was produced against the published playlist (2.9.104).
|
|
298
|
+
assert.equal(
|
|
299
|
+
nextState(ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_EVENT.EXITED_SHORT),
|
|
300
|
+
ENCODE_RUN_STATE.ENDED_FAILED
|
|
301
|
+
);
|
|
302
|
+
assert.equal(
|
|
303
|
+
nextState(ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_EVENT.EXITED_COMPLETE),
|
|
304
|
+
ENCODE_RUN_STATE.ENDED_COMPLETE
|
|
305
|
+
);
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test("losing the input holds the viewer's requests instead of failing them", () => {
|
|
309
|
+
const afterLoss = nextState(ENCODE_RUN_STATE.PRODUCING, ENCODE_RUN_EVENT.EXITED_INPUT_LOST);
|
|
310
|
+
assert.equal(afterLoss, ENCODE_RUN_STATE.RETRY_WAIT);
|
|
311
|
+
assert.equal(answerForMissingSegment(afterLoss), "hold");
|
|
312
|
+
assert.equal(wireState(afterLoss), "starting");
|
|
313
|
+
assert.equal(nextState(afterLoss, ENCODE_RUN_EVENT.RETRY_DUE), ENCODE_RUN_STATE.IDLE);
|
|
314
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file The drawing cannot disagree with the table.
|
|
3
|
+
*
|
|
4
|
+
* `docs/encode-run-state.md` is generated from `services/encode-run-state.js`.
|
|
5
|
+
* A picture kept beside the code drifts — this repository has the receipts —
|
|
6
|
+
* so the check is mechanical: regenerate it and compare. A behavioural change
|
|
7
|
+
* to the table that forgets `npm run graph` fails here, in the same commit.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import assert from "node:assert/strict";
|
|
11
|
+
import { readFileSync } from "node:fs";
|
|
12
|
+
import test from "node:test";
|
|
13
|
+
|
|
14
|
+
import { GRAPH_DOC_PATH, renderRunGraphMarkdown } from "../scripts/render-run-graph.js";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Line endings are not part of the comparison. Git is configured with
|
|
18
|
+
* `autocrlf=true` on the machine this is developed on, so the same file is LF
|
|
19
|
+
* in the index and CRLF in the working tree — and a test failing on that would
|
|
20
|
+
* point at the generator, whose output is always LF, and be unfixable by the
|
|
21
|
+
* remedy it suggests.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} text
|
|
24
|
+
* @returns {string}
|
|
25
|
+
*/
|
|
26
|
+
function withoutLineEndings(text) {
|
|
27
|
+
return text.replace(/\r\n/g, "\n");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
test("the committed picture is the one the table produces", () => {
|
|
31
|
+
const committed = readFileSync(GRAPH_DOC_PATH, "utf8");
|
|
32
|
+
assert.equal(
|
|
33
|
+
withoutLineEndings(committed),
|
|
34
|
+
withoutLineEndings(renderRunGraphMarkdown()),
|
|
35
|
+
"docs/encode-run-state.md is out of date — run `npm run graph` and commit the result"
|
|
36
|
+
);
|
|
37
|
+
});
|
|
@@ -21,6 +21,7 @@ import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
|
21
21
|
import os from "node:os";
|
|
22
22
|
import path from "node:path";
|
|
23
23
|
import { HlsSessionManager } from "../services/hls-session-manager.js";
|
|
24
|
+
import { ENCODE_RUN_STATE } from "../services/encode-run-state.js";
|
|
24
25
|
import { fmp4Format } from "../services/segment-formats/fmp4.js";
|
|
25
26
|
|
|
26
27
|
const MOVIE_TIMESCALE = 1000;
|
|
@@ -276,3 +277,48 @@ test("a segment is found in the run directory that produced it, newest run first
|
|
|
276
277
|
"the newest run's output must win — the older file here is the 8-byte stub a killed run leaves"
|
|
277
278
|
);
|
|
278
279
|
});
|
|
280
|
+
|
|
281
|
+
test("serving a run's own segment moves the run out of STARTING", async (t) => {
|
|
282
|
+
const { manager, session, dirPath } = await managerWithReadySegment();
|
|
283
|
+
t.after(async () => {
|
|
284
|
+
await manager.disposeAll();
|
|
285
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
286
|
+
});
|
|
287
|
+
// The table lives in `encode-run-state.js` and is tested there as a graph.
|
|
288
|
+
// What this pins is that a real serve REACHES it: a state nothing writes
|
|
289
|
+
// describes every run as still starting, for ever, and the log built on it
|
|
290
|
+
// would say so too.
|
|
291
|
+
session.runState = ENCODE_RUN_STATE.STARTING;
|
|
292
|
+
// Where the run in force is writing. The fixture's segments live directly in
|
|
293
|
+
// the session directory, which is exactly what a single run's directory is
|
|
294
|
+
// here.
|
|
295
|
+
session.runDirPath = dirPath;
|
|
296
|
+
|
|
297
|
+
await manager.getFileStream(SESSION_ID, "segment-00000.mp4", { requestSeq: 1 });
|
|
298
|
+
|
|
299
|
+
assert.equal(session.runState, ENCODE_RUN_STATE.PRODUCING);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test("a segment left by an earlier run does not claim the new run has produced", async (t) => {
|
|
303
|
+
const { manager, session, dirPath } = await managerWithReadySegment();
|
|
304
|
+
t.after(async () => {
|
|
305
|
+
await manager.disposeAll();
|
|
306
|
+
await rm(dirPath, { recursive: true, force: true });
|
|
307
|
+
});
|
|
308
|
+
// A seek places the new run in a directory of its own; the previous run's
|
|
309
|
+
// segments stay servable and are served from theirs. They say nothing about
|
|
310
|
+
// what the run now starting has done — and a run believed to be producing is
|
|
311
|
+
// one the look-ahead may suspend and the seek path may wave through as
|
|
312
|
+
// "already covered by the running encode".
|
|
313
|
+
//
|
|
314
|
+
// Deliberately a segment ABOVE the new run's start index, because that is the
|
|
315
|
+
// case an index comparison gets wrong: after a backward seek the old run's
|
|
316
|
+
// output sits ahead of the new run's beginning.
|
|
317
|
+
session.runState = ENCODE_RUN_STATE.STARTING;
|
|
318
|
+
session.encodeStartIndex = 0;
|
|
319
|
+
session.runDirPath = path.join(dirPath, "run-7");
|
|
320
|
+
|
|
321
|
+
await manager.getFileStream(SESSION_ID, "segment-00000.mp4", { requestSeq: 1 });
|
|
322
|
+
|
|
323
|
+
assert.equal(session.runState, ENCODE_RUN_STATE.STARTING);
|
|
324
|
+
});
|