-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matteo Collina <[email protected]>
- Loading branch information
Showing
4 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
|
||
const { parentPort } = require('worker_threads') | ||
const fastify = require('fastify') | ||
const { wire } = require('../../') | ||
|
||
const app = fastify() | ||
|
||
app.get('/example', async (request, reply) => { | ||
return { hello: 'world' } | ||
}) | ||
|
||
app.get('/crash', async (request, reply) => { | ||
process.exit(1) | ||
}) | ||
|
||
// TODO(mcollina): there is a race condition here | ||
const { replaceServer } = wire({ port: parentPort }) | ||
app.listen({ port: 0 }).then((url) => { | ||
process._rawDebug('server started on', url) | ||
replaceServer(url) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict' | ||
|
||
const { test } = require('node:test') | ||
const { strictEqual, rejects } = require('node:assert') | ||
const { join } = require('node:path') | ||
const { Worker } = require('node:worker_threads') | ||
const { setTimeout: sleep } = require('node:timers/promises') | ||
const { createThreadInterceptor } = require('../') | ||
const { Agent, request } = require('undici') | ||
|
||
test('two service in a mesh, one is terminated with an inflight message', async (t) => { | ||
const worker1 = new Worker(join(__dirname, 'fixtures', 'network-crash.js'), { | ||
workerData: { message: 'mesh' }, | ||
}) | ||
t.after(() => worker1.terminate()) | ||
const worker2 = new Worker(join(__dirname, 'fixtures', 'network-crash.js')) | ||
t.after(() => worker2.terminate()) | ||
|
||
const interceptor = createThreadInterceptor({ | ||
domain: '.local', | ||
}) | ||
interceptor.route('myserver', worker1) | ||
interceptor.route('myserver2', worker2) | ||
|
||
const agent = new Agent().compose(interceptor) | ||
|
||
await sleep(1000) | ||
|
||
{ | ||
const res = await request('http://myserver.local/example', { | ||
dispatcher: agent, | ||
}) | ||
|
||
strictEqual(res.statusCode, 200) | ||
await res.body.dump() | ||
} | ||
|
||
{ | ||
const res = await request('http://myserver2.local/example', { | ||
dispatcher: agent, | ||
}) | ||
|
||
strictEqual(res.statusCode, 200) | ||
await res.body.dump() | ||
} | ||
|
||
console.log('all successful request completed') | ||
|
||
await rejects(request('http://myserver2.local/crash', { | ||
dispatcher: agent, | ||
})) | ||
|
||
console.log('service crashed') | ||
|
||
const worker2bis = new Worker(join(__dirname, 'fixtures', 'network-crash.js')) | ||
t.after(() => worker2bis.terminate()) | ||
|
||
interceptor.route('myserver2', worker2bis) | ||
|
||
await sleep(2000) | ||
|
||
console.log('calling worker1') | ||
|
||
{ | ||
const res = await request('http://myserver.local/example', { | ||
dispatcher: agent, | ||
}) | ||
|
||
strictEqual(res.statusCode, 200) | ||
await res.body.dump() | ||
} | ||
|
||
console.log('calling worker2bis') | ||
|
||
{ | ||
const res = await request('http://myserver2.local/example', { | ||
dispatcher: agent, | ||
}) | ||
|
||
strictEqual(res.statusCode, 200) | ||
await res.body.dump() | ||
} | ||
}) |