Skip to content

Commit 5681ac7

Browse files
committed
Improved unit test performance of timer based scenarios.
1 parent 5d3c88e commit 5681ac7

3 files changed

Lines changed: 294 additions & 245 deletions

File tree

tests/bufferedQueue.test.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import { BufferedQueue } from '../src/utils/bufferedQueue';
33
import { waitForExpect } from './helpers/expectations';
44

55
describe('BufferedQueue', () => {
6+
beforeEach(() => {
7+
jest.useFakeTimers();
8+
});
9+
610
it('Should add items to the queue, and then process them once the buffer has expired', async () => {
711
// Setup
812
const onItem = jest.fn(() => Promise.resolve(true)), onChanges = jest.fn(() => { });
913
const queue = new BufferedQueue<string>(onItem, onChanges);
10-
jest.useFakeTimers();
1114

1215
// Run
1316
queue.enqueue('a');
@@ -17,6 +20,7 @@ describe('BufferedQueue', () => {
1720
// Assert
1821
expect(queue['queue']).toStrictEqual(['a', 'b', 'c']);
1922
expect(queue['processing']).toBe(false);
23+
expect(clearTimeout).toHaveBeenCalledTimes(2);
2024

2125
// Run
2226
jest.runOnlyPendingTimers();
@@ -40,7 +44,6 @@ describe('BufferedQueue', () => {
4044
// Setup
4145
const onItem = jest.fn(() => Promise.resolve(true)), onChanges = jest.fn(() => { });
4246
const queue = new BufferedQueue<string>(onItem, onChanges);
43-
jest.useFakeTimers();
4447

4548
// Run
4649
queue.enqueue('a');
@@ -69,7 +72,6 @@ describe('BufferedQueue', () => {
6972
// Setup
7073
const onItem = jest.fn(() => Promise.resolve(false)), onChanges = jest.fn(() => { });
7174
const queue = new BufferedQueue<string>(onItem, onChanges);
72-
jest.useFakeTimers();
7375

7476
// Run
7577
queue.enqueue('a');
@@ -113,7 +115,6 @@ describe('BufferedQueue', () => {
113115
// Setup
114116
const onItem = jest.fn(() => Promise.resolve(true)), onChanges = jest.fn(() => { });
115117
const queue = new BufferedQueue<string>(onItem, onChanges);
116-
jest.useFakeTimers();
117118

118119
// Run
119120
queue.enqueue('a');
@@ -132,4 +133,50 @@ describe('BufferedQueue', () => {
132133
expect(jest.getTimerCount()).toBe(0);
133134
expect(queue['timeout']).toBe(null);
134135
});
136+
137+
describe('bufferDuration', () => {
138+
it('Should use the default buffer duration of 1000ms', async () => {
139+
// Setup
140+
const onItem = jest.fn(() => Promise.resolve(true)), onChanges = jest.fn(() => { });
141+
const queue = new BufferedQueue<string>(onItem, onChanges);
142+
143+
// Run
144+
queue.enqueue('a');
145+
146+
// Assert
147+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1000);
148+
149+
// Run
150+
jest.runOnlyPendingTimers();
151+
jest.useRealTimers();
152+
153+
// Assert
154+
await waitForExpect(() => expect(queue['processing']).toBe(false));
155+
expect(onItem).toHaveBeenCalledTimes(1);
156+
expect(onItem).toHaveBeenCalledWith('a');
157+
expect(onChanges).toHaveBeenCalledTimes(1);
158+
});
159+
160+
it('Should use the specified buffer duration', async () => {
161+
// Setup
162+
const onItem = jest.fn(() => Promise.resolve(true)), onChanges = jest.fn(() => { });
163+
const queue = new BufferedQueue<string>(onItem, onChanges, 128);
164+
165+
// Run
166+
queue.enqueue('a');
167+
168+
// Assert
169+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 128);
170+
171+
// Run
172+
jest.runOnlyPendingTimers();
173+
jest.useRealTimers();
174+
175+
// Assert
176+
await waitForExpect(() => expect(queue['processing']).toBe(false));
177+
expect(onItem).toHaveBeenCalledTimes(1);
178+
expect(onItem).toHaveBeenCalledWith('a');
179+
expect(onChanges).toHaveBeenCalledTimes(1);
180+
});
181+
});
135182
});

tests/dataSource.test.ts

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5812,12 +5812,21 @@ describe('DataSource', () => {
58125812

58135813
it('Should launch the interactive rebase of the current branch on a branch in a terminal', async () => {
58145814
// Setup
5815+
jest.useFakeTimers();
58155816
const spyOnOpenGitTerminal = jest.spyOn(utils, 'openGitTerminal');
58165817
spyOnOpenGitTerminal.mockReturnValueOnce();
58175818
vscode.mockExtensionSettingReturnValue('repository.sign.commits', false);
58185819

58195820
// Run
5820-
const result = await dataSource.rebase('/path/to/repo', 'develop', RebaseActionOn.Branch, false, true);
5821+
const resultPromise = dataSource.rebase('/path/to/repo', 'develop', RebaseActionOn.Branch, false, true);
5822+
5823+
// Assert
5824+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1000);
5825+
5826+
// Run
5827+
jest.runOnlyPendingTimers();
5828+
jest.useRealTimers();
5829+
const result = await resultPromise;
58215830

58225831
// Assert
58235832
expect(result).toBe(null);
@@ -5826,12 +5835,21 @@ describe('DataSource', () => {
58265835

58275836
it('Should launch the interactive rebase of the current branch on a commit in a terminal', async () => {
58285837
// Setup
5838+
jest.useFakeTimers();
58295839
const spyOnOpenGitTerminal = jest.spyOn(utils, 'openGitTerminal');
58305840
spyOnOpenGitTerminal.mockReturnValueOnce();
58315841
vscode.mockExtensionSettingReturnValue('repository.sign.commits', false);
58325842

58335843
// Run
5834-
const result = await dataSource.rebase('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', RebaseActionOn.Commit, false, true);
5844+
const resultPromise = dataSource.rebase('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', RebaseActionOn.Commit, false, true);
5845+
5846+
// Assert
5847+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1000);
5848+
5849+
// Run
5850+
jest.runOnlyPendingTimers();
5851+
jest.useRealTimers();
5852+
const result = await resultPromise;
58355853

58365854
// Assert
58375855
expect(result).toBe(null);
@@ -5840,12 +5858,21 @@ describe('DataSource', () => {
58405858

58415859
it('Should launch the interactive rebase of the current branch on a branch in a terminal (signing the new commits)', async () => {
58425860
// Setup
5861+
jest.useFakeTimers();
58435862
const spyOnOpenGitTerminal = jest.spyOn(utils, 'openGitTerminal');
58445863
spyOnOpenGitTerminal.mockReturnValueOnce();
58455864
vscode.mockExtensionSettingReturnValue('repository.sign.commits', true);
58465865

58475866
// Run
5848-
const result = await dataSource.rebase('/path/to/repo', 'develop', RebaseActionOn.Branch, false, true);
5867+
const resultPromise = dataSource.rebase('/path/to/repo', 'develop', RebaseActionOn.Branch, false, true);
5868+
5869+
// Assert
5870+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1000);
5871+
5872+
// Run
5873+
jest.runOnlyPendingTimers();
5874+
jest.useRealTimers();
5875+
const result = await resultPromise;
58495876

58505877
// Assert
58515878
expect(result).toBe(null);
@@ -6484,10 +6511,19 @@ describe('DataSource', () => {
64846511
describe('openExternalDirDiff', () => {
64856512
it('Should launch a gui directory diff (for one commit)', async () => {
64866513
// Setup
6514+
jest.useFakeTimers();
64876515
mockGitSuccessOnce();
64886516

64896517
// Run
6490-
const result = await dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', true);
6518+
const resultPromise = dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', true);
6519+
6520+
// Assert
6521+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1500);
6522+
6523+
// Run
6524+
jest.runOnlyPendingTimers();
6525+
jest.useRealTimers();
6526+
const result = await resultPromise;
64916527

64926528
// Assert
64936529
expect(result).toBe(null);
@@ -6498,10 +6534,19 @@ describe('DataSource', () => {
64986534

64996535
it('Should launch a gui directory diff (between two commits)', async () => {
65006536
// Setup
6537+
jest.useFakeTimers();
65016538
mockGitSuccessOnce();
65026539

65036540
// Run
6504-
const result = await dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', '2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c', true);
6541+
const resultPromise = dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', '2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c', true);
6542+
6543+
// Assert
6544+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1500);
6545+
6546+
// Run
6547+
jest.runOnlyPendingTimers();
6548+
jest.useRealTimers();
6549+
const result = await resultPromise;
65056550

65066551
// Assert
65076552
expect(result).toBe(null);
@@ -6512,10 +6557,19 @@ describe('DataSource', () => {
65126557

65136558
it('Should launch a gui directory diff (for uncommitted changes)', async () => {
65146559
// Setup
6560+
jest.useFakeTimers();
65156561
mockGitSuccessOnce();
65166562

65176563
// Run
6518-
const result = await dataSource.openExternalDirDiff('/path/to/repo', utils.UNCOMMITTED, utils.UNCOMMITTED, true);
6564+
const resultPromise = dataSource.openExternalDirDiff('/path/to/repo', utils.UNCOMMITTED, utils.UNCOMMITTED, true);
6565+
6566+
// Assert
6567+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1500);
6568+
6569+
// Run
6570+
jest.runOnlyPendingTimers();
6571+
jest.useRealTimers();
6572+
const result = await resultPromise;
65196573

65206574
// Assert
65216575
expect(result).toBe(null);
@@ -6526,10 +6580,19 @@ describe('DataSource', () => {
65266580

65276581
it('Should launch a gui directory diff (between a commit and the uncommitted changes)', async () => {
65286582
// Setup
6583+
jest.useFakeTimers();
65296584
mockGitSuccessOnce();
65306585

65316586
// Run
6532-
const result = await dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', utils.UNCOMMITTED, true);
6587+
const resultPromise = dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', utils.UNCOMMITTED, true);
6588+
6589+
// Assert
6590+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1500);
6591+
6592+
// Run
6593+
jest.runOnlyPendingTimers();
6594+
jest.useRealTimers();
6595+
const result = await resultPromise;
65336596

65346597
// Assert
65356598
expect(result).toBe(null);
@@ -6540,11 +6603,20 @@ describe('DataSource', () => {
65406603

65416604
it('Should launch a directory diff in a terminal (between two commits)', async () => {
65426605
// Setup
6606+
jest.useFakeTimers();
65436607
const spyOnOpenGitTerminal = jest.spyOn(utils, 'openGitTerminal');
65446608
spyOnOpenGitTerminal.mockReturnValueOnce();
65456609

65466610
// Run
6547-
const result = await dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', '2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c', false);
6611+
const resultPromise = dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', '2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c', false);
6612+
6613+
// Assert
6614+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1500);
6615+
6616+
// Run
6617+
jest.runOnlyPendingTimers();
6618+
jest.useRealTimers();
6619+
const result = await resultPromise;
65486620

65496621
// Assert
65506622
expect(result).toBe(null);
@@ -6565,11 +6637,20 @@ describe('DataSource', () => {
65656637

65666638
it('Should display the error message when the diff tool doesn\'t exit successfully', async () => {
65676639
// Setup
6640+
jest.useFakeTimers();
65686641
mockGitThrowingErrorOnce('line1\nline2\nline3');
65696642
vscode.window.showErrorMessage.mockResolvedValueOnce(null);
65706643

65716644
// Run
6572-
const result = await dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', utils.UNCOMMITTED, true);
6645+
const resultPromise = dataSource.openExternalDirDiff('/path/to/repo', '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b', utils.UNCOMMITTED, true);
6646+
6647+
// Assert
6648+
expect(setTimeout).toHaveBeenCalledWith(expect.anything(), 1500);
6649+
6650+
// Run
6651+
jest.runOnlyPendingTimers();
6652+
jest.useRealTimers();
6653+
const result = await resultPromise;
65736654

65746655
// Assert
65756656
expect(result).toBe(null);

0 commit comments

Comments
 (0)