Skip to content

Commit cbad5ad

Browse files
authored
Fixes to decorator tests (#6002)
* Fixes to CI * Use highprecision timers to calculate elpased time * Update comments * Drop class * News entry * Update package.json
1 parent bbaf189 commit cbad5ad

2 files changed

Lines changed: 27 additions & 23 deletions

File tree

news/3 Code Health/5085.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes to decorator tests.

src/test/common/utils/decorators.unit.test.ts

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ import { sleep } from '../../core';
1414

1515
// tslint:disable:no-any max-func-body-length no-unnecessary-class
1616
suite('Common Utils - Decorators', () => {
17-
18-
setup(function () {
19-
// This test is flakey.
20-
// tslint:disable-next-line:no-invalid-this
21-
this.skip();
22-
})
23-
2417
teardown(() => {
2518
clearCache();
2619
});
20+
/*
21+
* Time in milliseconds (from some arbitrary point in time for current process).
22+
* Don't use new Date().getTime() to calculate differences in times.
23+
* This has an accuracy of around 2-20ms.
24+
* However we're dealing with tests that need accuracy of 1ms.
25+
* Use API that'll give us better accuracy when dealing with elapsed times.
26+
*
27+
* @returns {number}
28+
*/
29+
function getHighPrecisionTime(): number {
30+
const currentTime = process.hrtime();
31+
// Convert seconds to ms and nanoseconds to ms.
32+
return (currentTime[0] * 1000) + (currentTime[1] / 1000_000);
33+
}
2734
function createMockVSC(pythonPath: string): typeof import('vscode') {
2835
return {
2936
workspace: {
@@ -109,18 +116,19 @@ suite('Common Utils - Decorators', () => {
109116
});
110117

111118
// debounce()
119+
// tslint:disable-next-line: max-classes-per-file
112120
class Base {
113121
public created: number;
114122
public calls: string[];
115123
public timestamps: number[];
116124
constructor() {
117-
this.created = Date.now();
125+
this.created = getHighPrecisionTime();
118126
this.calls = [];
119127
this.timestamps = [];
120128
}
121129
protected _addCall(funcname: string, timestamp?: number): void {
122130
if (!timestamp) {
123-
timestamp = Date.now();
131+
timestamp = getHighPrecisionTime();
124132
}
125133
this.calls.push(funcname);
126134
this.timestamps.push(timestamp);
@@ -149,7 +157,7 @@ suite('Common Utils - Decorators', () => {
149157
}
150158
const one = new One();
151159

152-
const start = Date.now();
160+
const start = getHighPrecisionTime();
153161
one.run();
154162
await waitForCalls(one.timestamps, 1);
155163
const delay = one.timestamps[0] - start;
@@ -169,7 +177,7 @@ suite('Common Utils - Decorators', () => {
169177
}
170178
const one = new One();
171179

172-
const start = Date.now();
180+
const start = getHighPrecisionTime();
173181
let errored = false;
174182
one.run().catch(() => errored = true);
175183
await waitForCalls(one.timestamps, 1);
@@ -191,7 +199,7 @@ suite('Common Utils - Decorators', () => {
191199
}
192200
const one = new One();
193201

194-
const start = Date.now();
202+
const start = getHighPrecisionTime();
195203
await one.run();
196204
await waitForCalls(one.timestamps, 1);
197205
const delay = one.timestamps[0] - start;
@@ -212,7 +220,7 @@ suite('Common Utils - Decorators', () => {
212220
}
213221
const one = new One();
214222

215-
const start = Date.now();
223+
const start = getHighPrecisionTime();
216224
let capturedEx: Error | undefined;
217225
await one.run().catch(ex => capturedEx = ex);
218226
await waitForCalls(one.timestamps, 1);
@@ -234,7 +242,7 @@ suite('Common Utils - Decorators', () => {
234242
}
235243
const one = new One();
236244

237-
const start = Date.now();
245+
const start = getHighPrecisionTime();
238246
let errored = false;
239247
one.run().catch(() => errored = true);
240248
one.run().catch(() => errored = true);
@@ -260,7 +268,7 @@ suite('Common Utils - Decorators', () => {
260268
}
261269
const one = new One();
262270

263-
const start = Date.now();
271+
const start = getHighPrecisionTime();
264272
await Promise.all([one.run(), one.run(), one.run(), one.run()]);
265273
await waitForCalls(one.timestamps, 1);
266274
const delay = one.timestamps[0] - start;
@@ -280,7 +288,7 @@ suite('Common Utils - Decorators', () => {
280288
}
281289
const one = new One();
282290

283-
const start = Date.now();
291+
const start = getHighPrecisionTime();
284292
let errored = false;
285293
one.run().catch(() => errored = true);
286294
await one.run();
@@ -305,7 +313,7 @@ suite('Common Utils - Decorators', () => {
305313
}
306314
const one = new One();
307315

308-
const start = Date.now();
316+
const start = getHighPrecisionTime();
309317
one.run();
310318
one.run();
311319
one.run();
@@ -316,12 +324,7 @@ suite('Common Utils - Decorators', () => {
316324
expect(one.calls).to.deep.equal(['run']);
317325
expect(one.timestamps).to.have.lengthOf(one.calls.length);
318326
});
319-
test('Debounce: multiple calls spread', async function () {
320-
321-
// This test is flakey.
322-
// tslint:disable-next-line:no-invalid-this
323-
this.skip();
324-
327+
test('Debounce: multiple calls spread', async () => {
325328
const wait = 100;
326329
// tslint:disable-next-line:max-classes-per-file
327330
class One extends Base {

0 commit comments

Comments
 (0)