-
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.
* removed useless file Signed-off-by: Matteo Collina <[email protected]> * first part of a fix Signed-off-by: Matteo Collina <[email protected]> * working but ugly Signed-off-by: Matteo Collina <[email protected]> * pretty Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]> * concurrency to 1 Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]> * Apply suggestions from code review Co-authored-by: Ivan Tymoshenko <[email protected]> * Update index.js * concurrent testing Signed-off-by: Matteo Collina <[email protected]> --------- Signed-off-by: Matteo Collina <[email protected]> Co-authored-by: Ivan Tymoshenko <[email protected]>
- Loading branch information
1 parent
ee7ac18
commit f311a85
Showing
7 changed files
with
238 additions
and
15 deletions.
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
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,30 @@ | ||
'use strict' | ||
|
||
const { parentPort } = require('worker_threads') | ||
const fastify = require('fastify') | ||
const { wire } = require('../../') | ||
const { request } = require('undici') | ||
|
||
const app = fastify() | ||
|
||
wire({ server: app, port: parentPort, domain: '.local' }) | ||
|
||
app.get('/s1/example', async (req, reply) => { | ||
const { body } = await request('http://myserver.local/example') | ||
return await body.json() | ||
}) | ||
|
||
app.get('/s2/example', async (req, reply) => { | ||
const { body } = await request('http://myserver2.local/example') | ||
return await body.json() | ||
}) | ||
|
||
app.get('/s1/crash', async (req, reply) => { | ||
const { body } = await request('http://myserver.local/crash') | ||
return await body.json() | ||
}) | ||
|
||
app.get('/s2/crash', async (req, reply) => { | ||
const { body } = await request('http://myserver2.local/crash') | ||
return await body.json() | ||
}) |
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,21 @@ | ||
'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) => { | ||
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,154 @@ | ||
'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('service restart with network / 1', 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() | ||
} | ||
|
||
await rejects(request('http://myserver2.local/crash', { | ||
dispatcher: agent, | ||
})) | ||
|
||
const worker2bis = new Worker(join(__dirname, 'fixtures', 'network-crash.js')) | ||
t.after(() => worker2bis.terminate()) | ||
|
||
interceptor.route('myserver2', worker2bis) | ||
|
||
await sleep(2000) | ||
|
||
{ | ||
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() | ||
} | ||
}) | ||
|
||
test('service restart with network / 2', async (t) => { | ||
const composer = new Worker(join(__dirname, 'fixtures', 'composer.js'), { | ||
workerData: { name: 'composer' }, | ||
}) | ||
t.after(() => composer.terminate()) | ||
const worker1 = new Worker(join(__dirname, 'fixtures', 'network-crash.js'), { | ||
workerData: { name: 'worker1' }, | ||
}) | ||
t.after(() => worker1.terminate()) | ||
const worker2 = new Worker(join(__dirname, 'fixtures', 'network-crash.js'), { | ||
workerData: { name: 'worker2' }, | ||
}) | ||
t.after(() => worker2.terminate()) | ||
|
||
const interceptor = createThreadInterceptor({ | ||
domain: '.local', | ||
}) | ||
interceptor.route('composer', composer) | ||
interceptor.route('myserver', worker1) | ||
interceptor.route('myserver2', worker2) | ||
|
||
const agent = new Agent().compose(interceptor) | ||
|
||
await sleep(1000) | ||
|
||
{ | ||
const res = await request('http://composer.local/s1/example', { | ||
dispatcher: agent, | ||
}) | ||
|
||
strictEqual(res.statusCode, 200) | ||
await res.body.dump() | ||
} | ||
|
||
{ | ||
const res = await request('http://composer.local/s2/example', { | ||
dispatcher: agent, | ||
}) | ||
|
||
strictEqual(res.statusCode, 200) | ||
await res.body.dump() | ||
} | ||
|
||
{ | ||
const res = await request('http://composer.local/s2/crash', { | ||
dispatcher: agent, | ||
}) | ||
strictEqual(res.statusCode, 500) | ||
await res.body.dump() | ||
} | ||
|
||
const worker2bis = new Worker(join(__dirname, 'fixtures', 'network-crash.js'), { | ||
workerData: { name: 'worker2bis' }, | ||
}) | ||
t.after(() => worker2bis.terminate()) | ||
|
||
interceptor.route('myserver2', worker2bis) | ||
|
||
await sleep(2000) | ||
|
||
{ | ||
const res = await request('http://composer.local/s1/example', { | ||
dispatcher: agent, | ||
}) | ||
|
||
strictEqual(res.statusCode, 200) | ||
await res.body.dump() | ||
} | ||
|
||
{ | ||
const res = await request('http://composer.local/s2/example', { | ||
dispatcher: agent, | ||
}) | ||
|
||
strictEqual(res.statusCode, 200) | ||
await res.body.dump() | ||
} | ||
}) |