Skip to content

Test/ws backpressure drain#547

Merged
Baskarayelu merged 9 commits into
CredenceOrg:mainfrom
444notdotun:test/ws-backpressure-drain
Jun 25, 2026
Merged

Test/ws backpressure drain#547
Baskarayelu merged 9 commits into
CredenceOrg:mainfrom
444notdotun:test/ws-backpressure-drain

Conversation

@444notdotun

@444notdotun 444notdotun commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Bug fix (src/routes/ws.ts): drainWsConnections compared closed against wss.clients.size inside the close listener, but the ws library removes each client from the Set before that listener fires – so for N > 0 connections the equality check never reached the initial total and the drain always fell back to the hard-terminate timeout. Fixed by snapshotting const total = wss.clients.size before attaching listeners and gating on closed >= total.

New tests (src/tests/wsScoreStream.test.ts): 14 new integration tests across 5 suites covering the previously untested paths in ws.ts:

closes #515

Adds all 11 routes that were in the hardcoded drift-check list but absent
from docs/openapi.yaml, so the openapiDrift CI test passes.
Adds two exact-arithmetic primitives needed by the wallet debit fix:
- compareDecimals(a, b) — BigInt-scaled integer comparison that never
  touches Number(); returns -1 | 0 | 1.
- isValidPositiveDecimal(value) — validates that a decimal string is
  strictly greater than zero (rejects zero, negatives, and garbage).
The FOR UPDATE row lock made the write atomic but the funds check used
Number(), which loses precision on large/high-scale balances and could
permit an overdraft (e.g. Number("9007199254740992") ===
Number("9007199254740993") is true, so a debit of the larger value
against the smaller balance was silently allowed).

Comparison now uses compareDecimals() from decimalMath.ts, which uses
BigInt-scaled integer arithmetic and is always exact. Also adds
isValidPositiveDecimal() guards in both debit() and credit() to reject
zero, negative, or malformed amounts before the row lock is acquired.
- Overdraft-by-precision regression: confirms Number() collapses
  9007199254740992 and 9007199254740993 to the same float, then asserts
  compareDecimals() correctly rejects the overdraft.
- Exact-balance debit succeeds (boundary condition).
- Sub-unit precision (9-decimal-place amounts accepted / rejected).
- 30+ digit balances: debit succeeds and overdraft is caught.
- Upfront validation for debit() and credit(): zero, negative, garbage.
- InsufficientBalanceError field contents verified.
- Wallet-not-found and no-pool error paths.
- Extends vitest.config.ts include patterns to cover tests/repositories/.
Adds the wallets table to docs/schema.md with column definitions,
constraints, and a note explaining why compareDecimals() is used
instead of Number() for the sufficiency check in WalletsRepository.
… premature resolve

drainWsConnections compared closed against wss.clients.size inside the
close listener, but the ws library removes each client from the Set before
the listener fires — so for N > 0 connections the equality check could never
reach the initial total and the drain always fell back to the hard terminate
timeout.

Fix: snapshot const total = wss.clients.size before attaching listeners and
gate on closed >= total instead.
Forces a slow-consumer buffer overflow and asserts the message is silently
dropped; verifies drainWsConnections closes all sockets within the grace
window (not falling back to the hard timeout); confirms one noisy connection
is rate-throttled without affecting an independent connection; and tests auth
rejection (missing/invalid key, missing identity segment, Authorization
header) plus identity lowercasing.

Adds shared helpers createTestServer, connectAndSubscribe, and
collectMessages used across all new suites.
@444notdotun 444notdotun force-pushed the test/ws-backpressure-drain branch from aed932c to 01de1d3 Compare June 24, 2026 06:56
@drips-wave

drips-wave Bot commented Jun 24, 2026

Copy link
Copy Markdown

@444notdotun Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@Baskarayelu Baskarayelu merged commit c0fd4c6 into CredenceOrg:main Jun 25, 2026
0 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add backpressure and drain integration tests for the trust-score WebSocket server

2 participants