-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathaddParallelSuite.ts
109 lines (90 loc) · 3.33 KB
/
addParallelSuite.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { delay, nonNullProp } from '../extension.bundle';
import { cleanTestWorkspace, longRunningTestsEnabled } from './global.test';
export interface ParallelTest {
title: string;
callback(): Promise<void>;
task?: Promise<void>;
skip?: boolean;
/**
* Turn off parallel behavior for this test
*/
suppressParallel?: boolean;
}
export interface ParallelSuiteOptions {
title: string;
timeoutMS?: number;
isLongRunning?: boolean;
suiteSetup?: (testContext: Mocha.Context) => Promise<void>;
suiteTeardown?: (testContext: Mocha.Context) => Promise<void>;
/**
* Turn off parallel behavior for this suite
*/
suppressParallel?: boolean;
/**
* Add any other tests you may want for this suite
*/
addTests?: () => void;
}
export function addParallelSuite(parallelTests: ParallelTest[], options: ParallelSuiteOptions): void {
suite(options.title, function (this: Mocha.Suite): void {
this.timeout(options.timeoutMS || 30 * 1000);
suiteSetup(async function (this: Mocha.Context): Promise<void> {
if (options.isLongRunning && !longRunningTestsEnabled) {
this.skip();
}
await cleanTestWorkspace();
if (options.suiteSetup) {
await options.suiteSetup(this);
}
if (!options.suppressParallel) {
for (const t of parallelTests) {
if (!t.suppressParallel) {
const mochaGrep = process.env['MOCHA_grep'];
t.skip = t.skip || (!!mochaGrep && !(new RegExp(mochaGrep).test(`${options.title} ${t.title}`)));
if (!t.skip) {
t.task = t.callback();
}
}
}
}
});
suiteTeardown(async function (this: Mocha.Context): Promise<void> {
if (!options.isLongRunning || longRunningTestsEnabled) {
if (options.suiteTeardown) {
await options.suiteTeardown(this);
}
await cleanTestWorkspace();
}
});
for (const t of parallelTests) {
test(t.title, async function (this: Mocha.Context): Promise<void> {
if (t.skip) {
this.skip();
} else if (options.suppressParallel || t.suppressParallel) {
await t.callback();
} else {
await nonNullProp(t, 'task');
}
});
}
if (options.addTests) {
options.addTests();
}
});
}
const isRunningMap = new Map<string, boolean>();
export async function runInSeries(key: string, callback: () => Promise<void>): Promise<void> {
while (isRunningMap.get(key)) {
await delay(1000);
}
try {
isRunningMap.set(key, true);
await callback();
} finally {
isRunningMap.set(key, false);
}
}