Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate to node test runner #32

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .taprc

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "npm run test:unit && npm run test:typescript",
"test:unit": "tap",
"test:unit": "c8 --100 node --test",
"test:typescript": "tsd"
},
"repository": {
Expand Down Expand Up @@ -60,10 +60,10 @@
],
"devDependencies": {
"@fastify/pre-commit": "^2.1.0",
"c8": "^10.1.3",
"eslint": "^9.17.0",
"fastify": "^5.0.0",
"neostandard": "^0.12.0",
"tap": "^20.0.3",
"tsd": "^0.31.1"
},
"dependencies": {
Expand Down
7 changes: 3 additions & 4 deletions test/error.throttling.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const t = require('tap')
const test = t.test
const { test } = require('node:test')
const Fastify = require('fastify')
const { fastifyThrottle } = require('../index')
const { assertTimespan } = require('./utils/assert-timespan')
Expand Down Expand Up @@ -42,8 +41,8 @@ test('stream (error)', async t => {
// Once by fastify and once by the failed pipeline
const err = data?.err ?? data

t.type(err, Error)
t.same(err.message, message)
t.assert.ok(err instanceof Error)
t.assert.deepStrictEqual(err.message, message)
}
})
})
Expand Down
7 changes: 3 additions & 4 deletions test/global-throttle.throttling.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const t = require('tap')
const test = t.test
const { test } = require('node:test')
const Fastify = require('fastify')
const { fastifyThrottle } = require('../index')
const { assertTimespan } = require('./utils/assert-timespan')
Expand Down Expand Up @@ -45,7 +44,7 @@ test('should throttle globally and set the bytesPerSecond function', async t =>

await fastify.register(fastifyThrottle, {
bytesPerSecond: (request) => {
t.equal(request.headers['x-throttle-speed'], '10000')
t.assert.deepStrictEqual(request.headers['x-throttle-speed'], '10000')
const bps = parseInt(request.headers['x-throttle-speed'], 10)
return () => {
return bps
Expand All @@ -72,7 +71,7 @@ test('should throttle globally and set the bytesPerSecond async function', async

await fastify.register(fastifyThrottle, {
bytesPerSecond: async (request) => {
t.equal(request.headers['x-throttle-speed'], '10000')
t.assert.deepStrictEqual(request.headers['x-throttle-speed'], '10000')
const bps = parseInt(request.headers['x-throttle-speed'], 10)
return () => {
return bps
Expand Down
6 changes: 3 additions & 3 deletions test/lib/is-async-function.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { isAsyncFunction } = require('../../lib/is-async-function.js')

test('isAsyncFunction returns true for async functions', (t) => {
t.plan(1)
t.ok(isAsyncFunction(async () => { }))
t.assert.ok(isAsyncFunction(async () => { }))
})

test('isAsyncFunction returns false for non-async functions', (t) => {
t.plan(1)
t.notOk(isAsyncFunction(() => { }))
t.assert.ok(!isAsyncFunction(() => { }))
})
23 changes: 17 additions & 6 deletions test/lib/throttle-stream.back-pressure.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { assertTimespan } = require('../utils/assert-timespan')
const { ThrottleStream } = require('../../lib/throttle-stream')
const { RandomStream } = require('../utils/random-stream')
const { SlowRandomStream } = require('../utils/slow-random-stream')
const { withResolvers } = require('../utils/promise')
const { pipeline } = require('node:stream')

test('should work as expected with a slow readable', t => {
test('should work as expected with a slow readable', async t => {
t.plan(3)

const slowRandomStream = new SlowRandomStream(10) // should take ~1 second
Expand All @@ -20,16 +21,21 @@ test('should work as expected with a slow readable', t => {
bytes += data.length
})

const { resolve, promise } = withResolvers()

throttleStream.on('end', function () {
assertTimespan(t, startTime, Date.now(), 1000)
t.equal(10, bytes)
t.assert.deepStrictEqual(10, bytes)
resolve()
})

pipeline(
slowRandomStream,
throttleStream,
t.error
t.assert.ifError
)

await promise
})

test('should work as expected with a when input stream is providing bigger chunk than bytesPerSecond', t => {
Expand All @@ -44,14 +50,19 @@ test('should work as expected with a when input stream is providing bigger chunk
bytes += data.length
})

const { resolve, promise } = withResolvers()

throttleStream.on('end', function () {
assertTimespan(t, startTime, Date.now(), 2000)
t.equal(3000, bytes)
t.assert.deepStrictEqual(3000, bytes)
resolve()
})

pipeline(
randomStream,
throttleStream,
t.error
t.assert.ifError
)

return promise
})
12 changes: 6 additions & 6 deletions test/lib/throttle-stream.bytes-per-second.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { ThrottleStream } = require('../../lib/throttle-stream')

test('should use by default 16384 as return value of bytesPerSecondFn', t => {
t.plan(5)
const throttleStream = new ThrottleStream()

t.equal(throttleStream.bytesPerSecondFn(0, 0), 16384)
t.equal(throttleStream.bytesPerSecondFn(1, 1024), 16384)
t.equal(throttleStream.bytesPerSecondFn(2, 1024), 16384)
t.equal(throttleStream.bytesPerSecondFn(3, 1024), 16384)
t.equal(throttleStream.bytesPerSecondFn(4, 1024), 16384)
t.assert.deepStrictEqual(throttleStream.bytesPerSecondFn(0, 0), 16384)
t.assert.deepStrictEqual(throttleStream.bytesPerSecondFn(1, 1024), 16384)
t.assert.deepStrictEqual(throttleStream.bytesPerSecondFn(2, 1024), 16384)
t.assert.deepStrictEqual(throttleStream.bytesPerSecondFn(3, 1024), 16384)
t.assert.deepStrictEqual(throttleStream.bytesPerSecondFn(4, 1024), 16384)
})
12 changes: 6 additions & 6 deletions test/lib/throttle-stream.delay.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { assertTimespan } = require('../utils/assert-timespan')
const { ThrottleStream } = require('../../lib/throttle-stream')
const { RandomStream } = require('../utils/random-stream')
Expand All @@ -23,12 +23,12 @@ test('should delay the stream for 2 seconds', { skip: true }, t => {
const startTime = Date.now()
let bytes = 0
throttleStream.on('data', function (data) {
t.ok(Date.now() - startTime > 2000)
t.assert.ok(Date.now() - startTime > 2000)
bytes += data.length
})
throttleStream.on('end', function () {
assertTimespan(t, startTime, Date.now(), 2000)
t.equal(16384 * 2, bytes)
t.assert.deepStrictEqual(16384 * 2, bytes)
})

pipeline(
Expand All @@ -37,7 +37,7 @@ test('should delay the stream for 2 seconds', { skip: true }, t => {
t.error
)

t.equal(throttleStream.bytesPerSecondFn(0, 0), 0)
t.equal(throttleStream.bytesPerSecondFn(1.999, 0), 0)
t.equal(throttleStream.bytesPerSecondFn(2, 0), Infinity)
t.assert.deepStrictEqual(throttleStream.bytesPerSecondFn(0, 0), 0)
t.assert.deepStrictEqual(throttleStream.bytesPerSecondFn(1.999, 0), 0)
t.assert.deepStrictEqual(throttleStream.bytesPerSecondFn(2, 0), Infinity)
})
75 changes: 40 additions & 35 deletions test/lib/throttle-stream.resilience.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const { ThrottleStream } = require('../../lib/throttle-stream')
const { RandomStream } = require('../utils/random-stream')
const { pipeline } = require('node:stream')
const { pipeline } = require('node:stream/promises')

test('_init is resilient against errors', t => {
test('_init is resilient against errors', async t => {
t.plan(3)

const bytesPerSecondFn = function () {
Expand All @@ -15,16 +15,18 @@ test('_init is resilient against errors', t => {
const randomStream = new RandomStream(10000)
const throttleStream = new ThrottleStream({ bytesPerSecond: bytesPerSecondFn })

pipeline(
randomStream,
throttleStream,
err => { t.equal(err.message, 'ArbitraryError') }
await t.assert.rejects(
pipeline(
randomStream,
throttleStream
), 'ArbitratyError'
)
t.equal(throttleStream._buffer, null)
t.equal(throttleStream._interval, null)

t.assert.deepStrictEqual(throttleStream._buffer, null)
t.assert.deepStrictEqual(throttleStream._interval, null)
})

test('intervalHandler is resilient against errors', t => {
test('intervalHandler is resilient against errors', async t => {
t.plan(3)

const bytesPerSecondFn = function (elapsedTime) {
Expand All @@ -38,18 +40,18 @@ test('intervalHandler is resilient against errors', t => {
const randomStream = new RandomStream(10000)
const throttleStream = new ThrottleStream({ bytesPerSecond: bytesPerSecondFn })

pipeline(
randomStream,
throttleStream,
err => {
t.equal(err.message, 'ArbitraryError')
}
t.assert.deepStrictEqual(throttleStream._buffer, null)
t.assert.deepStrictEqual(throttleStream._interval, null)

await t.assert.rejects(
pipeline(
randomStream,
throttleStream
), 'ArbitratyError'
)
t.equal(throttleStream._buffer, null)
t.equal(throttleStream._interval, null)
})

test('_transform is resilient against errors', t => {
test('_transform is resilient against errors', async t => {
t.plan(3)

const randomStream = new RandomStream(10000)
Expand All @@ -59,18 +61,18 @@ test('_transform is resilient against errors', t => {
throw new Error('ArbitraryError')
}

pipeline(
randomStream,
throttleStream,
err => {
t.equal(err.message, 'ArbitraryError')
}
t.assert.equal(throttleStream._buffer, null)
t.assert.equal(throttleStream._interval, null)

await t.assert.rejects(
pipeline(
randomStream,
throttleStream
), 'ArbitratyError'
)
t.equal(throttleStream._buffer, null)
t.equal(throttleStream._interval, null)
})

test('should handle emitted errors properly', t => {
test('should handle emitted errors properly', async t => {
t.plan(5)

const start = Date.now()
Expand All @@ -79,15 +81,18 @@ test('should handle emitted errors properly', t => {
const throttleStream = new ThrottleStream({ bytesPerSecond: 1000 })

throttleStream.on('data', function () {
t.ok(Date.now() - start < 1500)
t.assert.ok(Date.now() - start < 1500)
})

setTimeout(() => throttleStream.emit('error', new Error('ArbitraryError')), 1500)
pipeline(
randomStream,
throttleStream,
err => { t.equal(err.message, 'ArbitraryError') }

t.assert.deepStrictEqual(throttleStream._buffer, null)
t.assert.deepStrictEqual(throttleStream._interval, null)

await t.assert.rejects(
pipeline(
randomStream,
throttleStream
), 'ArbitratyError'
)
t.equal(throttleStream._buffer, null)
t.equal(throttleStream._interval, null)
})
Loading