Skip to content

Commit

Permalink
try out some concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan-Arrowood committed Nov 26, 2024
1 parent 0c43aba commit 8a7bd2c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
],
"scripts": {
"format": "prettier --write .",
"test": "node --test \"test/**/*.test.js\""
"test": "node --test --test-concurrency=3 --experimental-test-isolation=process \"test/**/*.test.js\""
},
"dependencies": {
"shell-quote": "^1.8.1"
Expand Down
29 changes: 29 additions & 0 deletions test/next-15-node-18.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { suite, test, before, after } from 'node:test';
import { once } from 'node:events';
import { Fixture } from './util.js';

suite('Next.js v15 - Node.js v18', async () => {
const ctx = {};

before(async () => {
ctx.fixture = new Fixture({ nextMajor: '15', nodeMajor: '18' });
await once(ctx.fixture, 'ready');
ctx.rest = `http://${ctx.fixture.portMap.get('9926')}`;
});

await test('should run base component', async (t) => {
const response = await fetch(`${ctx.rest}/Dog/0`, {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic aGRiX2FkbWluOnBhc3N3b3Jk',
},
});
const json = await response.json();

t.assert.deepStrictEqual(json, { id: '0', name: 'Lincoln', breed: 'Shepherd' });
});

after(() => {
ctx.fixture.cleanup();
});
});
19 changes: 10 additions & 9 deletions test/next-15-node-20.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { suite, test, before, after } from 'node:test';
import { deepStrictEqual } from 'node:assert';
import { once } from 'node:events';
import { Fixture } from './util.js';

suite('Next.js v15 - Node.js v20', (t) => {
before(async () => {
t.fixture = new Fixture({ nextMajor: 15, nodeMajor: 20 });
suite('Next.js v15 - Node.js v20', async () => {
const ctx = {};

await once(t.fixture, 'ready');
before(async () => {
ctx.fixture = new Fixture({ nextMajor: '15', nodeMajor: '20' });
await once(ctx.fixture, 'ready');
ctx.rest = `http://${ctx.fixture.portMap.get('9926')}`;
});

test('should run base component', async (t) => {
const response = await fetch('http://localhost:9926/Dog/0', {
await test('should run base component', async (t) => {
const response = await fetch(`${ctx.rest}/Dog/0`, {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic aGRiX2FkbWluOnBhc3N3b3Jk',
},
});
const json = await response.json();

deepStrictEqual(json, { id: '0', name: 'Lincoln', breed: 'Shepherd' });
t.assert.deepStrictEqual(json, { id: '0', name: 'Lincoln', breed: 'Shepherd' });
});

after(() => {
t.fixture.cleanup();
ctx.fixture.cleanup();
});
});
29 changes: 29 additions & 0 deletions test/next-15-node-22.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { suite, test, before, after } from 'node:test';
import { once } from 'node:events';
import { Fixture } from './util.js';

suite('Next.js v15 - Node.js v22', async () => {
const ctx = {};

before(async () => {
ctx.fixture = new Fixture({ nextMajor: '15', nodeMajor: '22' });
await once(ctx.fixture, 'ready');
ctx.rest = `http://${ctx.fixture.portMap.get('9926')}`;
});

await test('should run base component', async (t) => {
const response = await fetch(`${ctx.rest}/Dog/0`, {
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic aGRiX2FkbWluOnBhc3N3b3Jk',
},
});
const json = await response.json();

t.assert.deepStrictEqual(json, { id: '0', name: 'Lincoln', breed: 'Shepherd' });
});

after(() => {
ctx.fixture.cleanup();
});
});
32 changes: 22 additions & 10 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,28 @@ function buildContainer({ nextMajor, nodeMajor, containerEngine }) {

const imageName = `hdb-next-integration-test-image-next-${nextMajor}-node-${nodeMajor}`;

console.log(`🏗️ Building ${imageName}...`);
// console.log(`🏗️ Building ${imageName}...`);

child_process.spawnSync(
containerEngine,
['build', '--build-arg', `NEXT_MAJOR=${nextMajor}`, '--build-arg', `NODE_MAJOR=${nodeMajor}`, '-t', imageName, '.'],
{ cwd: FIXTURE_PATH_URL, stdio: process.env.DEBUG === '1' ? 'inherit' : 'ignore' }
);

console.log(`🏗️ Build complete!`);
// console.log(`🏗️ Build complete!`);

return imageName;
}

function determinePortMapping({ containerName, containerEngine }) {
const portMap = new Map();
for (const port of ['9925', '9926']) {
const { stdout } = child_process.spawnSync(containerEngine, ['port', containerName, port]);
portMap.set(port, stdout.toString().trim());
}
return portMap;
}

function runContainer({ nextMajor, nodeMajor, imageName, containerEngine }) {
if (!containerEngine) {
containerEngine = getContainerEngine();
Expand All @@ -53,12 +62,7 @@ function runContainer({ nextMajor, nodeMajor, imageName, containerEngine }) {

clearContainer({ containerEngine, containerName });

const runProcess = child_process.spawn(
containerEngine,
['run', '-p', '9925:9925', '-p', '9926:9926', '--name', containerName, imageName] /*, {
stdio: process.env.DEBUG ? 'inherit' : 'ignore'
}*/
);
const runProcess = child_process.spawn(containerEngine, ['run', '-P', '--name', containerName, imageName]);

return { containerName, runProcess };
}
Expand All @@ -71,9 +75,10 @@ export class Fixture extends EventEmitter {

this.imageName = buildContainer({ nextMajor, nodeMajor, containerEngine: this.containerEngine });

const { containerName, runProcess } = runContainer({
const { containerName, runProcess, portMap } = runContainer({
nextMajor,
nodeMajor,
imageName: this.imageName,
containerName: this.containerName,
containerEngine: this.containerEngine,
});

Expand All @@ -82,6 +87,11 @@ export class Fixture extends EventEmitter {

this.runProcess.stdout.on('data', (data) => {
if (data.toString().includes('HarperDB 4.4.5 successfully started')) {
this.portMap = determinePortMapping({
containerName: this.containerName,
containerEngine: this.containerEngine,
});

this.emit('ready');
}
});
Expand All @@ -91,3 +101,5 @@ export class Fixture extends EventEmitter {
clearContainer({ containerEngine: this.containerEngine, containerName: this.containerName });
}
}

// const f = new Fixture({ nextMajor: '15', nodeMajor: '20' });

0 comments on commit 8a7bd2c

Please sign in to comment.