|
1 | 1 | import { cpuSamplerStart, encode, getJdkSourceRoots, getTmpDir, getWorkspaceSourceRoots, goToSource, heapDump, jdkHome, jfrRecordingDump, jfrRecordingStart, jfrRecordingStop, memorySamplerStart, openPid, resolveSamplingFilter, samplerSnapshot, samplerStop, threadDump, vmArgDisplayName, vmArgId } from '../../parameters';
|
2 | 2 | import { buildJavaProject, clean,duplicate,installExtensions, setupSelectEnvironment } from './utils';
|
3 |
| -import { GetRunningJavaProcesses } from '../../runningProcesses'; |
| 3 | +import { getUsingJps } from '../../runningProcesses'; |
| 4 | +import { getPath, getJpsPath } from '../../jdk'; |
4 | 5 | import { getSourceRoots } from '../../projectUtils';
|
5 | 6 | import { findLauncher } from '../../vscodeUtils';
|
6 | 7 | import { invoke, select} from '../../visualvm';
|
@@ -59,34 +60,44 @@ suite('VisualVm Suite Tests', function () {
|
59 | 60 |
|
60 | 61 | });
|
61 | 62 |
|
62 |
| - let testPid: number = 0; |
63 |
| - let childProcess:cp.ChildProcessWithoutNullStreams; |
| 63 | + let testPid: number = 0; // pid of a test Java process |
| 64 | + let childProcess: cp.ChildProcessWithoutNullStreams; // test Java process launcher |
| 65 | + let visualvmPid: number = 0; // pid of a VisualVM process |
64 | 66 | test('Manually Selecting Project Process', async function () {
|
65 | 67 |
|
66 | 68 | this.timeout(20000);
|
67 | 69 |
|
68 | 70 | const projectPath = path.resolve(__dirname, '../../../fixtures/test projects/demo');
|
69 | 71 |
|
70 | 72 | // Run Java Project
|
| 73 | + const TEST_JAVA_PROCESS_PARAMETER = '-Dtest.java.process=true'; |
71 | 74 | try {
|
72 | 75 | const jarFilePath = path.join(projectPath, 'oci/target/oci-1.0-SNAPSHOT.jar');
|
73 | 76 | if (fs.existsSync(jarFilePath)) {
|
74 |
| - childProcess = cp.spawn('java', ['-jar', 'oci/target/oci-1.0-SNAPSHOT.jar'], { cwd: projectPath }); |
| 77 | + childProcess = cp.spawn('java', [TEST_JAVA_PROCESS_PARAMETER, '-jar', 'oci/target/oci-1.0-SNAPSHOT.jar'], { cwd: projectPath }); |
75 | 78 | } else {
|
76 | 79 | assert(undefined, 'JAR File does not exist ... The build does not done correctly');
|
77 | 80 | }
|
78 | 81 | } catch (error) {
|
79 | 82 | console.error('Error running JAR file:', error);
|
80 | 83 | }
|
81 | 84 |
|
82 |
| - const processes = await GetRunningJavaProcesses(); |
| 85 | + // Wait for the test process to fully start |
| 86 | + await new Promise(f => setTimeout(f, 1500)); |
| 87 | + |
| 88 | + const jdkPath = await getPath(false); |
| 89 | + assert(jdkPath, 'no JDK available'); |
| 90 | + const jpsPath = getJpsPath(jdkPath); |
| 91 | + assert(jpsPath, 'no jps available'); |
| 92 | + const processes = await getUsingJps(jpsPath); |
83 | 93 | assert(processes, 'Can\'t get running java processes');
|
84 | 94 |
|
85 |
| - let isProcessExist:boolean = false; |
| 95 | + let isProcessExist: boolean = false; |
86 | 96 | for (const process of processes) {
|
87 |
| - if (process.label === 'oci-1.0-SNAPSHOT.jar') { |
| 97 | + if (process.displayName.includes(TEST_JAVA_PROCESS_PARAMETER)) { |
88 | 98 | isProcessExist = true;
|
89 | 99 | testPid = process.pid;
|
| 100 | + break; |
90 | 101 | }
|
91 | 102 | }
|
92 | 103 | assert.strictEqual(isProcessExist, true, 'Java test process not found !');
|
@@ -176,6 +187,11 @@ suite('VisualVm Suite Tests', function () {
|
176 | 187 | }
|
177 | 188 | });
|
178 | 189 |
|
| 190 | + test('Test open process', () => { |
| 191 | + const parameter = openPid(testPid); |
| 192 | + assert.strictEqual(parameter, `--openpid ${testPid.toString()}@2`, 'Test open process failed'); |
| 193 | + }); |
| 194 | + |
179 | 195 | test('Test thread Dump', () => {
|
180 | 196 | const parameter = threadDump(testPid);
|
181 | 197 | assert.strictEqual(parameter, `--threaddump ${testPid.toString()}`, 'Test thread Dump failed');
|
@@ -240,23 +256,50 @@ suite('VisualVm Suite Tests', function () {
|
240 | 256 | }
|
241 | 257 |
|
242 | 258 | const spacePath = await jdkHome(spaceMockPath);
|
243 |
| - assert.strictEqual(spacePath, `--jdkhome ${spaceMockPath}`); |
| 259 | + assert.strictEqual(spacePath, `--jdkhome "${spaceMockPath}"`); |
244 | 260 |
|
245 | 261 |
|
246 | 262 | assert(wf);
|
247 |
| - let params = '--window-to-front'; |
248 |
| - params += ` ${openPid(testPid)}`; |
249 |
| - const isShow = await invoke(params, wf[0], spaceMockPath); |
| 263 | + |
| 264 | + const TEST_VISUALVM_PROCESS_PARAMETER = '-Dvisualvm.test.process=true'; |
| 265 | + const isShow = await invoke(`-J${TEST_VISUALVM_PROCESS_PARAMETER}`, wf[0], spaceMockPath); |
| 266 | + if (isShow) { |
| 267 | + await new Promise(f => setTimeout(f, 3000)); |
| 268 | + |
| 269 | + const jdkPath = await getPath(false); |
| 270 | + assert(jdkPath, 'no JDK available'); |
| 271 | + const jpsPath = getJpsPath(jdkPath); |
| 272 | + assert(jpsPath, 'no jps available'); |
| 273 | + const processes = await getUsingJps(jpsPath); |
| 274 | + assert(processes, 'Can\'t get running java processes'); |
| 275 | + |
| 276 | + for (const process of processes) { |
| 277 | + if (process.displayName.includes(TEST_VISUALVM_PROCESS_PARAMETER)) { |
| 278 | + visualvmPid = process.pid; |
| 279 | + break; |
| 280 | + } |
| 281 | + } |
| 282 | + } |
250 | 283 |
|
251 |
| - assert.strictEqual(isShow, true, 'VisualVM can\'t started'); |
| 284 | + assert.strictEqual(isShow && !!visualvmPid, true, 'VisualVM can\'t started'); |
252 | 285 | });
|
253 | 286 |
|
254 | 287 |
|
255 | 288 | this.afterAll(async () => {
|
256 |
| - this.timeout(10000); |
| 289 | + this.timeout(15000); |
| 290 | + if (childProcess) { |
| 291 | + // TODO: does this throw an error if the process has already finished? |
| 292 | + childProcess.kill(); |
| 293 | + } |
| 294 | + if (testPid) { |
| 295 | + process.kill(testPid); |
| 296 | + } |
| 297 | + if (visualvmPid) { |
| 298 | + process.kill(visualvmPid); |
| 299 | + } |
| 300 | + // Wait for a while to have all resources released before the final cleanup |
| 301 | + await new Promise(f => setTimeout(f, 3000)); |
257 | 302 | // Clean the test installations
|
258 | 303 | await clean(downloadPaths.dirPath);
|
259 |
| - // Kill java project process |
260 |
| - childProcess.kill(); |
261 | 304 | });
|
262 | 305 | });
|
0 commit comments