From 0bf7a4248f480032a3fa5b28e22140627980cf96 Mon Sep 17 00:00:00 2001 From: akitaSummer Date: Tue, 23 Jan 2024 21:35:32 +0800 Subject: [PATCH] feat: add deamon test --- integration/index.2.test.js | 93 +++++++++++++++++++++++++++++++++++ packages/deamon/src/server.rs | 1 - 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/integration/index.2.test.js b/integration/index.2.test.js index 50e9070..f9c3b2f 100644 --- a/integration/index.2.test.js +++ b/integration/index.2.test.js @@ -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, @@ -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; @@ -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 diff --git a/packages/deamon/src/server.rs b/packages/deamon/src/server.rs index 20264e2..ef5dd23 100644 --- a/packages/deamon/src/server.rs +++ b/packages/deamon/src/server.rs @@ -85,7 +85,6 @@ async fn handle_kill( ) -> Result, (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,