Skip to content

Commit

Permalink
Add linting and code coverage (#3)
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored Jun 21, 2024
1 parent d6434d6 commit a614549
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 91 deletions.
8 changes: 4 additions & 4 deletions bench/bench.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ const worker = new Worker(join(import.meta.dirname, '..', 'test', 'fixtures', 'w
await once(worker, 'online')

const interceptor = createThreadInterceptor({
domain: '.local'
domain: '.local',
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

console.time('request')
let responses = []
const responses = []
for (let i = 0; i < 100000; i++) {
responses.push(request('http://myserver.local',{
dispatcher: agent
responses.push(request('http://myserver.local', {
dispatcher: agent,
}))
}
await Promise.all(responses)
Expand Down
13 changes: 5 additions & 8 deletions bench/compare.mjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { join } from 'path'
import { Worker } from 'worker_threads'
import { createFastifyInterceptor } from 'fastify-undici-dispatcher'
import { Agent, request } from 'undici'
import fastify from 'fastify'

const app = fastify()

app.get('/', async (req, reply) => {
app.get('/', async (req, reply) => {
reply.send({ hello: 'world' })
})


const interceptor = createFastifyInterceptor({
domain: '.local'
domain: '.local',
})
interceptor.route('myserver', app)

const agent = new Agent().compose(interceptor)

console.time('request')
let responses = []
const responses = []
for (let i = 0; i < 100000; i++) {
responses.push(request('http://myserver.local',{
dispatcher: agent
responses.push(request('http://myserver.local', {
dispatcher: agent,
}))
}
await Promise.all(responses)
Expand Down
3 changes: 3 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = require('neostandard')({})
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
'use strict'

const { Dispatcher } = require('undici')
const RoundRobin = require('./lib/roundrobin')
const hyperid = require('hyperid')
const { getGlobalDispatcher, setGlobalDispatcher} = require('undici')
const { getGlobalDispatcher, setGlobalDispatcher } = require('undici')
const { threadId, MessageChannel } = require('worker_threads')
const inject = require('light-my-request')

Expand Down Expand Up @@ -38,7 +37,7 @@ function createThreadInterceptor (opts) {

const id = nextId()
const newOpts = {
...opts
...opts,
}
delete newOpts.dispatcher

Expand Down Expand Up @@ -156,7 +155,7 @@ function wire (server, port, opts) {
url: opts.path,
headers: opts.headers,
query: opts.query,
body: opts.body
body: opts.body,
}

const onInject = (err, res) => {
Expand All @@ -181,7 +180,7 @@ function wire (server, port, opts) {
const forwardRes = {
type: 'response',
id,
res: newRes
res: newRes,
}

// So we route the message back to the port
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "An Undici interceptor that routes requests over a worker thread",
"main": "index.js",
"scripts": {
"test": "node --test test/*.test.js"
"test": "eslint && borp --coverage"
},
"keywords": [
"undici",
Expand All @@ -15,14 +15,17 @@
"author": "Matteo Collina <[email protected]>",
"license": "MIT",
"devDependencies": {
"borp": "^0.15.0",
"express": "^4.19.2",
"fastify": "^4.27.0",
"fastify-undici-dispatcher": "^0.6.0",
"koa": "^2.15.3"
},
"dependencies": {
"eslint": "^9.5.0",
"hyperid": "^3.2.0",
"light-my-request": "^5.13.0",
"neostandard": "^0.7.2",
"undici": "^6.18.1"
}
}
42 changes: 21 additions & 21 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,58 @@ const { Agent, request } = require('undici')
const { once } = require('events')
const { setTimeout: sleep } = require('timers/promises')

test('basic', async (t) => {
test('basic', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js'))
t.after(() => worker.terminate())

const interceptor = createThreadInterceptor({
domain: '.local'
domain: '.local',
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

const { statusCode, body, headers } = await request('http://myserver.local',{
dispatcher: agent
const { statusCode, body } = await request('http://myserver.local', {
dispatcher: agent,
})

strictEqual(statusCode, 200)
deepStrictEqual(await body.json(), { hello: 'world' })
})

test('two service in a mesh', async (t) => {
test('two service in a mesh', async (t) => {
const worker1 = new Worker(join(__dirname, 'fixtures', 'worker1.js'), {
workerData: { message: 'mesh' }
workerData: { message: 'mesh' },
})
t.after(() => worker1.terminate())
const worker2 = new Worker(join(__dirname, 'fixtures', 'worker2.js'))
t.after(() => worker2.terminate())

const interceptor = createThreadInterceptor({
domain: '.local'
domain: '.local',
})
interceptor.route('myserver', worker1)
interceptor.route('myserver2', worker2)

const agent = new Agent().compose(interceptor)

const { body, headers } = await request('http://myserver2.local',{
dispatcher: agent
const { body } = await request('http://myserver2.local', {
dispatcher: agent,
})

deepStrictEqual(await body.json(), { hello: 'mesh' })
})

test('two service in a mesh, one is terminated with an inflight message', async (t) => {
test('two service in a mesh, one is terminated with an inflight message', async (t) => {
const worker1 = new Worker(join(__dirname, 'fixtures', 'worker1.js'), {
workerData: { message: 'mesh' }
workerData: { message: 'mesh' },
})
t.after(() => worker1.terminate())
const worker2 = new Worker(join(__dirname, 'fixtures', 'worker2.js'))
t.after(() => worker2.terminate())

const interceptor = createThreadInterceptor({
domain: '.local'
domain: '.local',
})
interceptor.route('myserver', worker1)
interceptor.route('myserver2', worker2)
Expand All @@ -69,28 +69,28 @@ test('two service in a mesh, one is terminated with an inflight message', async

worker1.terminate()

const res = await request('http://myserver2.local',{
dispatcher: agent
const res = await request('http://myserver2.local', {
dispatcher: agent,
})

strictEqual(res.statusCode, 500)
deepStrictEqual(await res.body.json(), {
error: 'Internal Server Error',
message: 'Worker exited',
statusCode: 500
statusCode: 500,
})
})

test('two service in a mesh, one is terminated, then a message is sent', async (t) => {
test('two service in a mesh, one is terminated, then a message is sent', async (t) => {
const worker1 = new Worker(join(__dirname, 'fixtures', 'worker1.js'), {
workerData: { message: 'mesh' }
workerData: { message: 'mesh' },
})
t.after(() => worker1.terminate())
const worker2 = new Worker(join(__dirname, 'fixtures', 'worker2.js'))
t.after(() => worker2.terminate())

const interceptor = createThreadInterceptor({
domain: '.local'
domain: '.local',
})
interceptor.route('myserver', worker1)
interceptor.route('myserver2', worker2)
Expand All @@ -102,14 +102,14 @@ test('two service in a mesh, one is terminated, then a message is sent', async (
await once(worker1, 'exit')
await sleep(1000)

const res = await request('http://myserver2.local',{
dispatcher: agent
const res = await request('http://myserver2.local', {
dispatcher: agent,
})

strictEqual(res.statusCode, 500)
deepStrictEqual(await res.body.json(), {
error: 'Internal Server Error',
message: `No server found for myserver.local in ${worker2.threadId}`,
statusCode: 500
statusCode: 500,
})
})
19 changes: 9 additions & 10 deletions test/compat.test.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
'use strict'

const { test } = require('node:test')
const { deepStrictEqual, strictEqual } = require('node:assert')
const { deepStrictEqual } = require('node:assert')
const { join } = require('path')
const { Worker } = require('worker_threads')
const { createThreadInterceptor } = require('../')
const { Agent, request } = require('undici')

test('express', async (t) => {
test('express', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'express1.js'))
t.after(() => worker.terminate())

const interceptor = createThreadInterceptor({
domain: '.local'
domain: '.local',
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

const { body, headers } = await request('http://myserver.local',{
dispatcher: agent
const { body } = await request('http://myserver.local', {
dispatcher: agent,
})

deepStrictEqual(await body.json(), { hello: 'world' })
})

test('koa', async (t) => {
test('koa', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'koa1.js'))
t.after(() => worker.terminate())

const interceptor = createThreadInterceptor({
domain: '.local'
domain: '.local',
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

const { body, headers } = await request('http://myserver.local',{
dispatcher: agent
const { body } = await request('http://myserver.local', {
dispatcher: agent,
})

deepStrictEqual(await body.json(), { hello: 'world' })
})

4 changes: 2 additions & 2 deletions test/fixtures/express1.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict'

const { parentPort, workerData } = require('worker_threads')
const { parentPort, workerData } = require('worker_threads')
const express = require('express')
const { wire } = require('../../')

const app = express()

app.get('/', (req, res) => {
app.get('/', (req, res) => {
res.send({ hello: workerData?.message || 'world' })
})

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/koa1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

const { parentPort, workerData } = require('worker_threads')
const { parentPort, workerData } = require('worker_threads')
const Koa = require('koa')
const { wire } = require('../../')

const app = new Koa()

app.use(ctx => {
ctx.body = { hello: workerData?.message || 'world' }
});
})

wire(app.callback(), parentPort)
6 changes: 3 additions & 3 deletions test/fixtures/worker1.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
'use strict'

const { parentPort, workerData, threadId } = require('worker_threads')
const { parentPort, workerData, threadId } = require('worker_threads')
const fastify = require('fastify')
const { wire } = require('../../')

const app = fastify()

app.get('/', async (req, reply) => {
app.get('/', async (req, reply) => {
reply.send({ hello: workerData?.message || 'world' })
})

app.get('/whoami', async (req, reply) => {
app.get('/whoami', async (req, reply) => {
reply.send({ threadId })
})

Expand Down
13 changes: 6 additions & 7 deletions test/fixtures/worker2.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
'use strict'

const { parentPort, workerData } = require('worker_threads')
const { parentPort } = require('worker_threads')
const fastify = require('fastify')
const { wire, createThreadInterceptor } = require('../../')
const { request, agent } = require('undici')
const { wire } = require('../../')
const { request } = require('undici')

const app = fastify()

wire(app, parentPort, {
domain: '.local'
domain: '.local',
})

app.get('/', async (req, reply) => {
const { body, headers } = await request('http://myserver.local')
app.get('/', async (req, reply) => {
const { body } = await request('http://myserver.local')
return await body.json()
})

Loading

0 comments on commit a614549

Please sign in to comment.