From 56e1e78b18974c260371e50c88ca39d8ecd45192 Mon Sep 17 00:00:00 2001 From: Romain Lanz Date: Tue, 5 Mar 2024 17:44:00 +0100 Subject: [PATCH] test: increase test coverage for the stream class Closes https://github.com/adonisjs/transmit/issues/4 --- src/stream.ts | 20 -------------------- test_helpers/socket.ts | 34 ++++++++++++++++++++++++++++++++++ tests/stream.spec.ts | 19 +++++++++++++++++++ 3 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 test_helpers/socket.ts diff --git a/src/stream.ts b/src/stream.ts index ac460c6..1af4cdd 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -28,10 +28,6 @@ function dataToString(data: Broadcastable): string { interface Message { data: Broadcastable - comment?: string - event?: string - id?: string - retry?: number } interface WriteHeaders { @@ -91,22 +87,6 @@ export class Stream extends Transform { _encoding: string, callback: (error?: Error | null, data?: any) => void ) { - if (message.comment) { - this.push(`: ${message.comment}\n`) - } - - if (message.event) { - this.push(`event: ${message.event}\n`) - } - - if (message.id) { - this.push(`id: ${message.id}\n`) - } - - if (message.retry) { - this.push(`retry: ${message.retry}\n`) - } - if (message.data) { this.push(dataToString(message.data)) } diff --git a/test_helpers/socket.ts b/test_helpers/socket.ts new file mode 100644 index 0000000..4de0e31 --- /dev/null +++ b/test_helpers/socket.ts @@ -0,0 +1,34 @@ +import { Socket } from 'node:net' + +export class FakeSocket extends Socket { + #keepAlive = false + #noDelay = false + #timeout = 0 + + getKeepAlive() { + return this.#keepAlive + } + + getNoDelay() { + return this.#noDelay + } + + getTimeout() { + return this.#timeout + } + + setKeepAlive(enable?: boolean, initialDelay?: number): this { + this.#keepAlive = enable === true + return super.setKeepAlive(enable, initialDelay) + } + + setNoDelay(noDelay?: boolean): this { + this.#noDelay = noDelay === true + return super.setNoDelay(noDelay) + } + + setTimeout(timeout: number, callback?: () => void): this { + this.#timeout = timeout + return super.setTimeout(timeout, callback) + } +} diff --git a/tests/stream.spec.ts b/tests/stream.spec.ts index b6bd100..e57e31f 100644 --- a/tests/stream.spec.ts +++ b/tests/stream.spec.ts @@ -1,9 +1,18 @@ +import { IncomingMessage } from 'node:http' import { randomUUID } from 'node:crypto' import { test } from '@japa/runner' import { Stream } from '../src/stream.js' import { Sink } from '../test_helpers/sink.js' +import { FakeSocket } from '../test_helpers/socket.js' test.group('Stream', () => { + test('should get back the uid', async ({ assert }) => { + const uid = randomUUID() + const stream = new Stream(uid) + + assert.equal(stream.getUid(), uid) + }) + test('should write multiple chunks to the stream', async ({ assert }) => { const stream = new Stream(randomUUID()) const sink = new Sink() @@ -65,6 +74,16 @@ test.group('Stream', () => { stream.pipe(sink, undefined, { 'X-Foo': 'bar' }) }) + test('should set the keep alive, no delay and timeout on the socket', async ({ assert }) => { + const socket = new FakeSocket() + const incomingMessage = new IncomingMessage(socket) + const stream = new Stream(randomUUID(), incomingMessage) + + assert.isTrue(socket.getKeepAlive()) + assert.isTrue(socket.getNoDelay()) + assert.equal(socket.getTimeout(), 0) + }) + test('should correctly send the data when it is an object', async ({ assert }) => { const stream = new Stream(randomUUID()) const sink = new Sink()