Skip to content

Commit

Permalink
test: increase test coverage for the stream class
Browse files Browse the repository at this point in the history
Closes #4
  • Loading branch information
RomainLanz committed Mar 5, 2024
1 parent 3c2e1d4 commit 56e1e78
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 20 deletions.
20 changes: 0 additions & 20 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ function dataToString(data: Broadcastable): string {

interface Message {
data: Broadcastable
comment?: string
event?: string
id?: string
retry?: number
}

interface WriteHeaders {
Expand Down Expand Up @@ -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))
}
Expand Down
34 changes: 34 additions & 0 deletions test_helpers/socket.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}
19 changes: 19 additions & 0 deletions tests/stream.spec.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 56e1e78

Please sign in to comment.