Skip to content

Commit

Permalink
debug code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
matepek committed Oct 27, 2023
1 parent 1d7d5e9 commit d919c61
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

## [4.6.2]

## [4.6.1] - 2023-10-19

### Changed
Expand Down
7 changes: 7 additions & 0 deletions src/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,10 @@ export function getModiTime(path: string): Promise<number | undefined> {
() => undefined,
);
}

export function waitWithTimout<T>(f: Promise<T>, timeoutMs: number, errMsg?: string): Promise<T> {
return Promise.race([
f,
new Promise<T>((_r, rej) => setTimeout(() => rej(Error(errMsg ?? `Timeout ${timeoutMs} has expired`)), timeoutMs)),
]);
}
24 changes: 9 additions & 15 deletions src/WorkspaceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { sep as osPathSeparator } from 'path';
import { TaskQueue } from './util/TaskQueue';
import { AbstractExecutable, TestsToRun } from './framework/AbstractExecutable';
import { ConfigOfExecGroup } from './ConfigOfExecGroup';
import { generateId, Version } from './Util';
import { generateId, Version, waitWithTimout } from './Util';
import { AbstractTest } from './framework/AbstractTest';
import { TestItemManager } from './TestItemManager';
import { ProgressReporter } from './util/ProgressReporter';
Expand Down Expand Up @@ -380,23 +380,17 @@ export class WorkspaceManager implements vscode.Disposable {
}
}

debug(
test: AbstractTest,
cancellation: vscode.CancellationToken,
run: vscode.TestRun,
setDebugArgs: (exec: string, args: string[]) => void,
): Promise<void> {
debug(test: AbstractTest, run: vscode.TestRun, setDebugArgs: (exec: string, args: string[]) => void): Promise<void> {
run.enqueued(test.item);

return this._debugInner(test, cancellation, run, setDebugArgs).catch(e => {
return this._debugInner(test, run, setDebugArgs).catch(e => {
this.log.errorS('error during debug', e);
throw e;
});
}

async _debugInner(
test: AbstractTest,
cancellation: vscode.CancellationToken,
run: vscode.TestRun,
setDebugArgs: (exec: string, args: string[]) => void,
): Promise<void> {
Expand Down Expand Up @@ -493,8 +487,8 @@ export class WorkspaceManager implements vscode.Disposable {

this._shared.log.info('resolved debugConfig:', debugConfig);

await this._runTasks('before', [executable], cancellation);
await executable.runTasks('beforeEach', this._shared.taskPool, cancellation);
await this._runTasks('before', [executable], run.token);
await executable.runTasks('beforeEach', this._shared.taskPool, run.token);

let currentSession: vscode.DebugSession | undefined = undefined;

Expand Down Expand Up @@ -529,19 +523,19 @@ export class WorkspaceManager implements vscode.Disposable {
this._shared.log.info('debugSessionStarted');
await started;
if (currentSession) {
cancellation.onCancellationRequested(() => {
run.token.onCancellationRequested(() => {
vscode.debug.stopDebugging(currentSession);
});
}
await terminated;
await waitWithTimout(terminated, 5000, 'Abandoning waiting for termination of debug session');
} else {
throw Error(
'Failed starting the debug session. Maybe something wrong with "testMate.cpp.debug.configTemplate".',
);
}

await executable.runTasks('afterEach', this._shared.taskPool, cancellation);
await this._runTasks('after', [executable], cancellation);
await executable.runTasks('afterEach', this._shared.taskPool, run.token);
await this._runTasks('after', [executable], run.token);
} catch (err) {
this._shared.log.warn(err);
throw err;
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
const debugProfile = controller.createRunProfile(
'Debug Test',
vscode.TestRunProfileKind.Debug,
async (request: vscode.TestRunRequest, cancellation: vscode.CancellationToken): Promise<void> => {
async (request: vscode.TestRunRequest, _cancellation: vscode.CancellationToken): Promise<void> => {
if (runCount) {
vscode.window.showWarningMessage('Cannot debug test while running test(s).');
return;
Expand Down Expand Up @@ -255,7 +255,7 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
const test = testsToRun.direct[0];
runQueue.push(
manager
.debug(test, cancellation, testRun, setCurrentDebugVars)
.debug(test, testRun, setCurrentDebugVars)
.catch(e => {
vscode.window.showErrorMessage('Unexpected error from debug: ' + e);
})
Expand Down

0 comments on commit d919c61

Please sign in to comment.