Skip to content

Commit

Permalink
feat: add deamon test
Browse files Browse the repository at this point in the history
  • Loading branch information
akitaSummer committed Jan 23, 2024
1 parent 2363847 commit 0bf7a42
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
93 changes: 93 additions & 0 deletions integration/index.2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const assert = require('node:assert');
const coffee = require('coffee');
const semver = require('semver');
const execa = require('execa');
const util = require('node:util');
const exec = util.promisify(require('node:child_process').exec);
const { execSync } = require('node:child_process');
const rapid = path.join(__dirname, '../node_modules/.bin/rapid');
const {
clean,
Expand All @@ -15,6 +18,58 @@ const {
forceExitDaemon,
} = require('@cnpmjs/rapid/lib/nydusd/nydusd_api');


class Pids {
constructor(nodeModulesDir) {
this.nodeModulesDir = nodeModulesDir;
}

async getPsSnapshot() {
try {
const { stdout } = await exec('ps aux');
return stdout;
} catch (error) {
throw new Error(`Failed to execute 'ps aux': ${error.message}`);
}
}

async getPids() {
const pids = [];

try {
const snapshot = await this.getPsSnapshot();

const overlayPattern = new RegExp(`overlay.*?${this.nodeModulesDir}`, 'i');
const nfsPattern = new RegExp(
`/usr/local/bin/go-nfsv4.*?${this.nodeModulesDir}`, 'i'
);

snapshot.split('\n').forEach(line => {
if (overlayPattern.test(line)) {
const fields = line.split(/\s+/);
if (fields.length >= 11) {
const pid = parseInt(fields[1], 10) || 0;
pids.push(pid);
}
}

if (nfsPattern.test(line)) {
const fields = line.split(/\s+/);
if (fields.length >= 11) {
const pid = parseInt(fields[1], 10) || 0;
pids.push(pid);
}
}
});

return pids;
} catch (error) {
console.log(error);
throw new Error(`Failed to get PIDs: ${error.message}`);
}
}
}

describe('test/index.v2.test.js', () => {
let cwd;

Expand Down Expand Up @@ -148,6 +203,44 @@ describe('test/index.v2.test.js', () => {
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');
});

it('deamon should working', async () => {
cwd = path.join(__dirname, './fixtures/esbuild');
await coffee
.fork(rapid, [
'install',
'--ignore-scripts',
], {
cwd,
})
.debug()
.expect('code', 0)
.end();

const dirs = await fs.readdir(path.join(cwd, 'node_modules'));
assert.strictEqual(dirs.filter(dir => dir.includes('esbuild')).length, 2);
await assert.doesNotReject(fs.stat(path.join(cwd, 'node_modules/esbuild')));
assert.strictEqual(require(path.join(cwd, 'node_modules', 'esbuild/package.json')).version, '0.15.14');


const pidsInstance = new Pids(dirs);
let pids = await pidsInstance.getPids();
assert(pids.length > 0);
pids.forEach(pid => {
try {
execSync(`kill -9 ${pid}`);
} catch (err) {
console.error(`Failed to kill process with PID ${pid}. Error: ${err.message}`);
}
});
await new Promise(resolve => {
setTimeout(() => {
resolve();
}, 10000);
});
pids = await pidsInstance.getPids();
assert(pids.length > 0);
});

it('should install optional deps successfully use npminstall', async () => {
cwd = path.join(__dirname, './fixtures/esbuild');
await coffee
Expand Down
1 change: 0 additions & 1 deletion packages/deamon/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ async fn handle_kill(
) -> Result<Json<Res>, (StatusCode, String)> {
info!("echo kill");
let state = state.lock().await;
kill_projects(state.project_tree.clone()).await;
let _ = state.sender.send(0).await;
Ok(Json(Res {
code: 0,
Expand Down

0 comments on commit 0bf7a42

Please sign in to comment.