also looking at this
feat: add bounded VTOL wind braking
#88
13 files
+447
-25
| @@ -0,0 +1,127 @@ | ||
| 1 | import { expect, test } from 'bun:test' | |
| 2 | import { createExecutionCallbackObserver } from '../helpers/execution-worker-observation' | |
| 3 | ||
| 4 | const worker = () => new Worker(new URL('../fixtures/public-safety-access/execution-observer-worker.ts', import.meta.url), { env: {}, preload: [] }) | |
| 5 | ||
| 6 | test('real Worker frames and receiver survive private callback observation without persisting payloads', async () => { | |
| 7 | const source = worker(), markers: unknown[] = [] | |
| 8 | const observer = createExecutionCallbackObserver(value => markers.push(value)) | |
| 9 | const received: string[] = [] | |
| 10 | let timer: ReturnType<typeof setTimeout> | undefined | |
| 11 | try { | |
| 12 | const delivered = new Promise<void>((resolve, reject) => { | |
| 13 | timer = setTimeout(() => reject(new Error('Owned callback fixture did not deliver its frames')), 2000) | |
| 14 | source.onmessage = function (event) { | |
| 15 | try { | |
| 16 | expect(this).toBe(source) | |
| 17 | const frame = event.data as { kind: string, payload: string, bytes: Uint8Array } | |
| 18 | expect(frame.payload).toBe('private-callback-payload') | |
| 19 | expect(frame.bytes).toEqual(new Uint8Array([1, 2, 3])) | |
| 20 | received.push(frame.kind) | |
| 21 | if (received.length === 4) resolve() | |
| 22 | } | |
| 23 | catch (error) { reject(error) } | |
| 24 | } | |
| 25 | source.onerror = () => reject(new Error('Owned callback fixture Worker failed')) | |
| 26 | }) | |
| 27 | observer.observe(source, 1) | |
| 28 | source.postMessage('emit') | |
| 29 | await delivered | |
| 30 | expect(received).toEqual(['event', 'result', 'finished', 'unrecognized']) | |
| 31 | const snapshot = observer.snapshot() | |
| 32 | expect(snapshot.overflow).toBe(false) | |
| 33 | expect(snapshot.unavailable).toBe(false) | |
| 34 | expect(snapshot.observations).toHaveLength(1) | |
| 35 | expect(snapshot.observations[0]!.inCallback).toBe(false) | |
| 36 | expect(snapshot.observations[0]!.capped).toBe(false) | |
| 37 | for (const bucket of Object.values(snapshot.observations[0]!.costs)) { | |
| 38 | expect(bucket.calls).toBe(1) | |
| 39 | expect(Number.isFinite(bucket.totalMs) && bucket.totalMs >= 0).toBe(true) | |
| 40 | expect(bucket.maxMs).toBe(bucket.totalMs) | |
| 41 | } | |
| 42 | expect(markers).toHaveLength(4) | |
| 43 | expect(JSON.stringify({ snapshot, markers })).not.toContain('private-callback-payload') | |
| 44 | expect(JSON.stringify({ snapshot, markers })).not.toContain('bytes') | |
| 45 | } | |
| 46 | finally { clearTimeout(timer); source.terminate() } | |
| 47 | }) | |
| 48 | ||
| 49 | test('observer failures preserve the original callback return, throw identity and opaque fields', () => { | |
| 50 | const first = worker(), second = worker() | |
| 51 | const observer = createExecutionCallbackObserver(() => { throw new Error('Diagnostic marker unavailable') }) | |
| 52 | let getterCalls = 0, received: unknown | |
| 53 | const payload = { kind: 'result', get privateField() { getterCalls++; return 'unread-private-value' } } | |
| 54 | const event = new MessageEvent('message', { data: payload }), result = { originalReturn: true }, originalError = new Error('Original callback failure') | |
| 55 | try { | |
| 56 | first.onmessage = function (value) { expect(this).toBe(first); received = value.data; return result } | |
| 57 | observer.observe(first, 1) | |
| 58 | expect(first.onmessage!.call(first, event)).toBe(result) | |
| 59 | expect(received).toBe(payload) | |
| 60 | expect(getterCalls).toBe(0) | |
| 61 | second.onmessage = function (value) { expect(this).toBe(second); expect(value).toBe(event); throw originalError } | |
| 62 | observer.observe(second, 2) | |
| 63 | let thrown: unknown | |
| 64 | try { second.onmessage!.call(second, event) } | |
| 65 | catch (error) { thrown = error } | |
| 66 | expect(thrown).toBe(originalError) | |
| 67 | expect(observer.snapshot().unavailable).toBe(true) | |
| 68 | expect(observer.snapshot().observations.map(value => value.costs.result.calls)).toEqual([1, 1]) | |
| 69 | expect(JSON.stringify(observer.snapshot())).not.toContain('unread-private-value') | |
| 70 | expect(getterCalls).toBe(0) | |
| 71 | } | |
| 72 | finally { first.terminate(); second.terminate() } | |
| 73 | }) | |
| 74 | ||
| 75 | test('the observer caps actual Worker reservations without changing later callbacks and returns detached snapshots', () => { | |
| 76 | const first = worker(), second = worker(), third = worker() | |
| 77 | const observer = createExecutionCallbackObserver(() => {}) | |
| 78 | const callback = () => {} | |
| 79 | try { | |
| 80 | first.onmessage = callback; second.onmessage = callback; third.onmessage = callback | |
| 81 | observer.observe(first, 1); observer.observe(first, 99); observer.observe(second, 2); observer.observe(third, 3) | |
| 82 | const snapshot = observer.snapshot() | |
| 83 | expect(snapshot.overflow).toBe(true) | |
| 84 | expect(snapshot.unavailable).toBe(false) | |
| 85 | expect(snapshot.observations.map(value => value.ordinal)).toEqual([1, 2]) | |
| 86 | expect(third.onmessage).toBe(callback) | |
| 87 | snapshot.observations[0]!.costs.event.calls = 9999 | |
| 88 | expect(observer.snapshot().observations[0]!.costs.event.calls).toBe(0) | |
| 89 | } | |
| 90 | finally { first.terminate(); second.terminate(); third.terminate() } | |
| 91 | }) | |
| 92 | ||
| 93 | test('marker latency and diagnostic exceptions cannot inflate the original synchronous callback duration', () => { | |
| 94 | const source = worker(), returned = { original: true }, settled: number[] = [] | |
| 95 | let clock = 0 | |
| 96 | const observer = createExecutionCallbackObserver(value => { | |
| 97 | if (value.phase === 'settled' && value.callbackMs !== null) settled.push(value.callbackMs) | |
| 98 | clock += 1000 | |
| 99 | throw new Error('Slow diagnostic sink failed') | |
| 100 | }, () => clock) | |
| 101 | try { | |
| 102 | source.onmessage = () => { clock += 7; return returned } | |
| 103 | observer.observe(source, 1) | |
| 104 | expect(source.onmessage!.call(source, new MessageEvent('message', { data: { kind: 'result' } }))).toBe(returned) | |
| 105 | expect(observer.snapshot().observations[0]!.costs.result).toEqual({ calls: 1, totalMs: 7, maxMs: 7 }) | |
| 106 | expect(settled).toEqual([7]) | |
| 107 | expect(observer.snapshot().unavailable).toBe(true) | |
| 108 | } | |
| 109 | finally { source.terminate() } | |
| 110 | }) | |
| 111 | ||
| 112 | test('repeated terminal-kind messages cannot produce an unbounded diagnostic console stream', () => { | |
| 113 | const source = worker(), markers: unknown[] = [] | |
| 114 | const observer = createExecutionCallbackObserver(value => markers.push(value)) | |
| 115 | let originalCalls = 0 | |
| 116 | try { | |
| 117 | source.onmessage = () => { originalCalls++ } | |
| 118 | observer.observe(source, 1) | |
| 119 | for (let index = 0; index < 10; index++) source.onmessage!.call(source, new MessageEvent('message', { data: { kind: 'result' } })) | |
| 120 | expect(originalCalls).toBe(10) | |
| 121 | expect(markers).toHaveLength(4) | |
| 122 | expect(observer.snapshot().observations[0]!.capped).toBe(true) | |
| 123 | expect(observer.snapshot().observations[0]!.costs.result.calls).toBe(10) | |
| 124 | expect(observer.snapshot().unavailable).toBe(false) | |
| 125 | } | |
| 126 | finally { source.terminate() } | |
| 127 | }) | |