screentinker/server/test/tcp-keepalive.test.js
ScreenTinker bcfe3eaf8b fix(#148) Items 2-4: mark-offline closes the socket + tighten ping + TCP keepalive
Item 2: when the heartbeat checker marks a device offline it now also disconnects any socket
it still holds for it, so DB-offline can't diverge from socket-state into a silent half-open
(defensive — the live-socket guard already defers genuinely-live sockets).

Item 3: tighten half-open detection WITHOUT reintroducing the TV-WebKit decode-load risk the
30s pong-timeout was chosen for — lower only pingInterval 30s->15s (probe more often), KEEP
pingTimeout at 30s. Detection = interval+timeout = 45s (was 60s), and the client inherits
these via the handshake so BOTH ends detect a dead peer ~25% sooner. (Deliberately did NOT
drop pingTimeout to ~20s: MAXHUB is a video-playing TV-class device and the code comment
warns tighter timeouts cause spurious drops under decode load.)

Item 4: SO_KEEPALIVE on every accepted connection (lib/tcp-keepalive.js) so a half-open TCP
can't persist indefinitely at the OS layer, independent of the app ping.

Tests: server closes a non-ponging peer within ~pingInterval+pingTimeout while a ponging peer
survives; a device whose transport dies ends offline with its connection torn down; keepalive
applied to each accepted connection (and never breaks setup on error). Suite 328/328.
2026-07-02 14:59:25 -05:00

24 lines
972 B
JavaScript

'use strict';
// #148 Item 4 — SO_KEEPALIVE is applied to every accepted connection.
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { EventEmitter } = require('node:events');
const { applyTcpKeepAlive } = require('../lib/tcp-keepalive');
test('applyTcpKeepAlive enables keepalive on each accepted connection', () => {
const server = new EventEmitter();
applyTcpKeepAlive(server, 20000);
const calls = [];
server.emit('connection', { setKeepAlive: (enable, ms) => calls.push([enable, ms]) });
server.emit('connection', { setKeepAlive: (enable, ms) => calls.push([enable, ms]) });
assert.deepEqual(calls, [[true, 20000], [true, 20000]]);
});
test('a socket that throws on setKeepAlive never breaks connection setup', () => {
const server = new EventEmitter();
applyTcpKeepAlive(server, 20000);
assert.doesNotThrow(() => server.emit('connection', { setKeepAlive: () => { throw new Error('boom'); } }));
});