Skip to content

Commit 9ad783a

Browse files
jisedlacthurka
authored andcommitted
Fixes and improvements to the extension tests
1 parent fc8b733 commit 9ad783a

File tree

1 file changed

+58
-15
lines changed

1 file changed

+58
-15
lines changed

integrations/vscode/src/test/suite/visualvm.test.ts

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { cpuSamplerStart, encode, getJdkSourceRoots, getTmpDir, getWorkspaceSourceRoots, goToSource, heapDump, jdkHome, jfrRecordingDump, jfrRecordingStart, jfrRecordingStop, memorySamplerStart, openPid, resolveSamplingFilter, samplerSnapshot, samplerStop, threadDump, vmArgDisplayName, vmArgId } from '../../parameters';
22
import { buildJavaProject, clean,duplicate,installExtensions, setupSelectEnvironment } from './utils';
3-
import { GetRunningJavaProcesses } from '../../runningProcesses';
3+
import { getUsingJps } from '../../runningProcesses';
4+
import { getPath, getJpsPath } from '../../jdk';
45
import { getSourceRoots } from '../../projectUtils';
56
import { findLauncher } from '../../vscodeUtils';
67
import { invoke, select} from '../../visualvm';
@@ -59,34 +60,44 @@ suite('VisualVm Suite Tests', function () {
5960

6061
});
6162

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
6466
test('Manually Selecting Project Process', async function () {
6567

6668
this.timeout(20000);
6769

6870
const projectPath = path.resolve(__dirname, '../../../fixtures/test projects/demo');
6971

7072
// Run Java Project
73+
const TEST_JAVA_PROCESS_PARAMETER = '-Dtest.java.process=true';
7174
try {
7275
const jarFilePath = path.join(projectPath, 'oci/target/oci-1.0-SNAPSHOT.jar');
7376
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 });
7578
} else {
7679
assert(undefined, 'JAR File does not exist ... The build does not done correctly');
7780
}
7881
} catch (error) {
7982
console.error('Error running JAR file:', error);
8083
}
8184

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);
8393
assert(processes, 'Can\'t get running java processes');
8494

85-
let isProcessExist:boolean = false;
95+
let isProcessExist: boolean = false;
8696
for (const process of processes) {
87-
if (process.label === 'oci-1.0-SNAPSHOT.jar') {
97+
if (process.displayName.includes(TEST_JAVA_PROCESS_PARAMETER)) {
8898
isProcessExist = true;
8999
testPid = process.pid;
100+
break;
90101
}
91102
}
92103
assert.strictEqual(isProcessExist, true, 'Java test process not found !');
@@ -176,6 +187,11 @@ suite('VisualVm Suite Tests', function () {
176187
}
177188
});
178189

190+
test('Test open process', () => {
191+
const parameter = openPid(testPid);
192+
assert.strictEqual(parameter, `--openpid ${testPid.toString()}@2`, 'Test open process failed');
193+
});
194+
179195
test('Test thread Dump', () => {
180196
const parameter = threadDump(testPid);
181197
assert.strictEqual(parameter, `--threaddump ${testPid.toString()}`, 'Test thread Dump failed');
@@ -240,23 +256,50 @@ suite('VisualVm Suite Tests', function () {
240256
}
241257

242258
const spacePath = await jdkHome(spaceMockPath);
243-
assert.strictEqual(spacePath, `--jdkhome ${spaceMockPath}`);
259+
assert.strictEqual(spacePath, `--jdkhome "${spaceMockPath}"`);
244260

245261

246262
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+
}
250283

251-
assert.strictEqual(isShow, true, 'VisualVM can\'t started');
284+
assert.strictEqual(isShow && !!visualvmPid, true, 'VisualVM can\'t started');
252285
});
253286

254287

255288
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));
257302
// Clean the test installations
258303
await clean(downloadPaths.dirPath);
259-
// Kill java project process
260-
childProcess.kill();
261304
});
262305
});

0 commit comments

Comments
 (0)