Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
},
"viewsWelcome": [
{
"view": "testExplorer",
"contents": "No test cases are listed because the Java Language Server is currently running in [LightWeight Mode](https://aka.ms/vscode-java-lightweight). To show test cases, click on the button to switch to Standard Mode.\n[Switch to Standard Mode](command:java.server.mode.switch?%5B%22Standard%22,true%5D)",
"when": "java:serverMode == LightWeight"
"view": "testExplorer",
"contents": "No test cases are listed because the Java Language Server is currently running in [LightWeight Mode](https://aka.ms/vscode-java-lightweight). To show test cases, click on the button to switch to Standard Mode.\n[Switch to Standard Mode](command:java.server.mode.switch?%5B%22Standard%22,true%5D)",
"when": "java:serverMode == LightWeight"
}
],
"menus": {
Expand Down Expand Up @@ -398,8 +398,9 @@
"devDependencies": {
"@types/fs-extra": "^5.1.0",
"@types/lodash": "^4.14.150",
"@types/lru-cache": "^5.1.0",
"@types/mocha": "^2.2.48",
"@types/node": "^6.14.10",
"@types/node": "^14.0.1",
"@types/pug": "^2.0.4",
"@types/sinon": "^9.0.3",
"@types/vscode": "1.44.0",
Expand All @@ -414,7 +415,7 @@
"sinon": "^9.0.2",
"ts-loader": "^5.4.5",
"tslint": "^5.20.1",
"typescript": "^2.8.3",
"typescript": "^4.0.2",
"vscode-test": "^1.3.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
Expand All @@ -424,6 +425,7 @@
"get-port": "^4.2.0",
"iconv-lite": "^0.4.24",
"lodash": "^4.17.19",
"lru-cache": "^6.0.0",
"pug": "^2.0.4",
"vscode-extension-telemetry-wrapper": "^0.8.0",
"winston": "^3.2.1",
Expand Down
18 changes: 18 additions & 0 deletions src/codelens/MovingAverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

export class MovingAverage {

private _n: number = 1;
private _val: number = 0;

public update(value: number): this {
this._val = this._val + (value - this._val) / this._n;
this._n += 1;
return this;
}

public get value(): number {
return this._val;
}
}
46 changes: 42 additions & 4 deletions src/codelens/TestCodeLensProvider.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { CancellationToken, CodeLens, CodeLensProvider, Disposable, Event, EventEmitter, TextDocument } from 'vscode';
import * as LRUCache from 'lru-cache';
import {performance} from 'perf_hooks';
import { CancellationToken, CodeLens, CodeLensProvider, Disposable, Event, EventEmitter, TextDocument, Uri } from 'vscode';
import { JavaTestRunnerCommands } from '../constants/commands';
import { logger } from '../logger/logger';
import { ITestItem, TestLevel } from '../protocols';
import { ITestResult, TestStatus } from '../runners/models';
import { testItemModel } from '../testItemModel';
import { testResultManager } from '../testResultManager';
import { MovingAverage } from './MovingAverage';

export class TestCodeLensProvider implements CodeLensProvider, Disposable {
private onDidChangeCodeLensesEmitter: EventEmitter<void> = new EventEmitter<void>();
private lruCache: LRUCache<Uri, MovingAverage> = new LRUCache<Uri, MovingAverage>(32);
private isActivated: boolean = true;

get onDidChangeCodeLenses(): Event<void> {
Expand All @@ -26,14 +30,24 @@ export class TestCodeLensProvider implements CodeLensProvider, Disposable {
this.onDidChangeCodeLensesEmitter.fire();
}

public async provideCodeLenses(document: TextDocument, _token: CancellationToken): Promise<CodeLens[]> {
public async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
if (!this.isActivated) {
return [];
}

try {
const items: ITestItem[] = await testItemModel.getItemsForCodeLens(document.uri);
return this.getCodeLenses(items);
const timeout: number = this.getRequestDelay(document.uri);
return new Promise<CodeLens[]>((resolve: (ids: CodeLens[]) => void): void => {
token.onCancellationRequested(() => {
clearTimeout(timeoutHandle);
resolve([]);
});

const timeoutHandle: NodeJS.Timeout = setTimeout(async () => {
resolve(await this.resolveAllCodeLenses(document.uri, token));
}, timeout);

});
} catch (error) {
logger.error('Failed to provide Code Lens', error);
return [];
Expand All @@ -44,6 +58,30 @@ export class TestCodeLensProvider implements CodeLensProvider, Disposable {
this.onDidChangeCodeLensesEmitter.dispose();
}

private getRequestDelay(uri: Uri): number {
const avg: MovingAverage | undefined = this.lruCache.get(uri);
if (!avg) {
return 350;
}
return Math.max(350, Math.floor(1.3 * avg.value));
}

private async resolveAllCodeLenses(uri: Uri, token: CancellationToken): Promise<CodeLens[]> {
if (token.isCancellationRequested) {
return [];
}

const startTime: number = performance.now();
const items: ITestItem[] = await testItemModel.getItemsForCodeLens(uri);
const result: CodeLens[] = this.getCodeLenses(items);
const executionTime: number = performance.now() - startTime;

const movingAverage: MovingAverage = this.lruCache.get(uri) || new MovingAverage();
movingAverage.update(executionTime);
this.lruCache.set(uri, movingAverage);
return result;
}

private getCodeLenses(items: ITestItem[]): CodeLens[] {
const codeLenses: CodeLens[] = [];
for (const item of items) {
Expand Down
6 changes: 3 additions & 3 deletions src/runners/baseRunner/BaseRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as fse from 'fs-extra';
import { default as getPort } from 'get-port';
import * as iconv from 'iconv-lite';
import { createServer, Server, Socket } from 'net';
import { AddressInfo, createServer, Server, Socket } from 'net';
import * as os from 'os';
import * as path from 'path';
import { debug, DebugConfiguration, DebugSession, Disposable, Uri, workspace } from 'vscode';
Expand Down Expand Up @@ -141,7 +141,7 @@ export abstract class BaseRunner implements ITestRunner {
}

public get serverPort(): number {
const address: { port: number; family: string; address: string; } = this.server.address();
const address: AddressInfo = this.server.address() as AddressInfo;
if (address) {
return address.port;
}
Expand All @@ -151,7 +151,7 @@ export abstract class BaseRunner implements ITestRunner {

public getApplicationArgs(config?: IExecutionConfig): string[] {
const applicationArgs: string[] = [];
applicationArgs.push(`${this.server.address().port}`);
applicationArgs.push(`${(this.server.address() as AddressInfo).port}`);

applicationArgs.push(...this.getRunnerCommandParams(config));

Expand Down
5 changes: 3 additions & 2 deletions src/runners/junitRunner/JunitRunner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import { AddressInfo } from 'net';
import { DebugConfiguration } from 'vscode';
import { BaseRunner } from '../baseRunner/BaseRunner';
import { BaseRunnerResultAnalyzer } from '../baseRunner/BaseRunnerResultAnalyzer';
Expand All @@ -15,9 +16,9 @@ export class JUnitRunner extends BaseRunner {
const args: string[] = launchConfiguration.args as string[];
const portIndex: number = args.lastIndexOf('-port');
if (portIndex > -1 && portIndex + 1 < args.length) {
args[portIndex + 1] = `${this.server.address().port}`;
args[portIndex + 1] = `${(this.server.address() as AddressInfo).port}`;
} else {
args.push('-port', `${this.server.address().port}`);
args.push('-port', `${(this.server.address() as AddressInfo).port}`);
}
}
return super.run(launchConfiguration);
Expand Down