Skip to content

Commit 55ee20b

Browse files
committed
Ensure unittest test results are only recorded once per test run.
Fixes microsoft#2143 - Unregister listeners from socket service after each test run
1 parent b2230f0 commit 55ee20b

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

news/2 Fixes/2143.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure test count value in status bar represents the correct number of tests that were actually discovered and that were run.

src/client/unittests/common/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ export type ParserOptions = TestDiscoveryOptions;
245245
export const IUnitTestSocketServer = Symbol('IUnitTestSocketServer');
246246
export interface IUnitTestSocketServer extends Disposable {
247247
on(event: string | symbol, listener: Function): this;
248+
removeListener(event: string | symbol, listener: Function): this;
249+
removeAllListeners(event: string | symbol): this;
248250
start(options?: { port?: number; host?: string }): Promise<number>;
249251
stop(): void;
250252
}

src/client/unittests/unittest/runner.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
14
'use strict';
5+
26
import { inject, injectable } from 'inversify';
7+
import { Socket } from 'net';
38
import * as path from 'path';
49
import { EXTENSION_ROOT_DIR } from '../../common/constants';
510
import { noop } from '../../common/core.utils';
611
import { ILogger } from '../../common/types';
712
import { IServiceContainer } from '../../ioc/types';
813
import { UNITTEST_PROVIDER } from '../common/constants';
914
import { Options } from '../common/runner';
10-
import { ITestDebugLauncher, ITestManager, ITestResultsService, ITestRunner, IUnitTestSocketServer, LaunchOptions, TestRunOptions, Tests, TestStatus, TestsToRun } from '../common/types';
15+
import {
16+
ITestDebugLauncher, ITestManager, ITestResultsService,
17+
ITestRunner, IUnitTestSocketServer, LaunchOptions,
18+
TestRunOptions, Tests, TestStatus
19+
} from '../common/types';
1120
import { IArgumentsHelper, ITestManagerRunner, IUnitTestHelper } from '../types';
1221

1322
type TestStatusMap = {
@@ -42,7 +51,9 @@ export class TestManagerRunner implements ITestManagerRunner {
4251
this.logger = this.serviceContainer.get<ILogger>(ILogger);
4352
this.helper = this.serviceContainer.get<IUnitTestHelper>(IUnitTestHelper);
4453
}
45-
public async runTest(testResultsService: ITestResultsService, options: TestRunOptions, testManager: ITestManager): Promise<Tests> {
54+
55+
// tslint:disable-next-line:max-func-body-length
56+
public async runTest(testResultsService: ITestResultsService, options: TestRunOptions, testManager: ITestManager): Promise<Tests> {
4657
options.tests.summary.errors = 0;
4758
options.tests.summary.failures = 0;
4859
options.tests.summary.passed = 0;
@@ -53,7 +64,6 @@ export class TestManagerRunner implements ITestManagerRunner {
5364
this.server.on('log', noop);
5465
this.server.on('connect', noop);
5566
this.server.on('start', noop);
56-
this.server.on('socket.disconnected', noop);
5767
this.server.on('result', (data: ITestData) => {
5868
const test = options.tests.testFunctions.find(t => t.testFunction.nameToRun === data.test);
5969
const statusDetails = outcomeMapping.get(data.outcome)!;
@@ -73,6 +83,15 @@ export class TestManagerRunner implements ITestManagerRunner {
7383
}
7484
});
7585

86+
this.server.on('socket.disconnected', (socket: Socket, isSocketDestroyed: boolean) => {
87+
this.server.removeAllListeners('error');
88+
this.server.removeAllListeners('log');
89+
this.server.removeAllListeners('connect');
90+
this.server.removeAllListeners('start');
91+
this.server.removeAllListeners('result');
92+
this.server.removeAllListeners('socket.disconnected');
93+
});
94+
7695
const port = await this.server.start();
7796
const testPaths: string[] = this.helper.getIdsOfTestsToRun(options.tests, options.testsToRun!);
7897
for (let counter = 0; counter < testPaths.length; counter += 1) {
@@ -139,6 +158,7 @@ export class TestManagerRunner implements ITestManagerRunner {
139158
testResultsService.updateResults(options.tests);
140159
return options.tests;
141160
}
161+
142162
private buildTestArgs(args: string[]): string[] {
143163
const startTestDiscoveryDirectory = this.helper.getStartDirectory(args);
144164
let pattern = 'test*.py';

0 commit comments

Comments
 (0)