-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an async lazy class and lazy lifetimes (#1861)
* Add an async lazy class and lazy lifetimes * Add unit tests
- Loading branch information
1 parent
df38c83
commit d0b342d
Showing
4 changed files
with
181 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE.md in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as assert from 'assert'; | ||
import { Lazy, AsyncLazy } from "../../extension.bundle"; | ||
import { delay } from '../../extension.bundle'; | ||
|
||
suite('(unit) Lazy tests', () => { | ||
suite('Lazy<T>', () => { | ||
test('Normal', async () => { | ||
let factoryCallCount = 0; | ||
const lazy: Lazy<boolean> = new Lazy(() => { | ||
factoryCallCount++; | ||
return true; | ||
}); | ||
|
||
lazy.value; | ||
lazy.value; | ||
|
||
assert.equal(factoryCallCount, 1, 'Incorrect number of value factory calls.'); | ||
}); | ||
|
||
test('With lifetime', async () => { | ||
let factoryCallCount = 0; | ||
const lazy: Lazy<boolean> = new Lazy(() => { | ||
factoryCallCount++; | ||
return true; | ||
}, 5); | ||
|
||
lazy.value; | ||
lazy.value; | ||
|
||
assert.equal(factoryCallCount, 1, 'Incorrect number of value factory calls.'); | ||
|
||
await delay(10); | ||
lazy.value; | ||
lazy.value; | ||
|
||
assert.equal(factoryCallCount, 2, 'Incorrect number of value factory calls.'); | ||
}); | ||
}); | ||
|
||
suite('AsyncLazy<T>', () => { | ||
test('Normal', async () => { | ||
let factoryCallCount = 0; | ||
const lazy: AsyncLazy<boolean> = new AsyncLazy(async () => { | ||
factoryCallCount++; | ||
await delay(5); | ||
return true; | ||
}); | ||
|
||
await lazy.getValue(); | ||
await lazy.getValue(); | ||
|
||
assert.equal(factoryCallCount, 1, 'Incorrect number of value factory calls.'); | ||
}); | ||
|
||
test('Simultaneous callers', async () => { | ||
let factoryCallCount = 0; | ||
const lazy: AsyncLazy<boolean> = new AsyncLazy(async () => { | ||
factoryCallCount++; | ||
await delay(5); | ||
return true; | ||
}); | ||
|
||
const p1 = lazy.getValue(); | ||
const p2 = lazy.getValue(); | ||
await Promise.all([p1, p2]); | ||
|
||
assert.equal(factoryCallCount, 1, 'Incorrect number of value factory calls.'); | ||
}); | ||
|
||
test('With lifetime', async () => { | ||
let factoryCallCount = 0; | ||
const lazy: AsyncLazy<boolean> = new AsyncLazy(async () => { | ||
factoryCallCount++; | ||
await delay(5); | ||
return true; | ||
}, 10); | ||
|
||
await lazy.getValue(); | ||
await lazy.getValue(); | ||
|
||
assert.equal(factoryCallCount, 1, 'Incorrect number of value factory calls.'); | ||
|
||
await delay(15); | ||
await lazy.getValue(); | ||
await lazy.getValue(); | ||
|
||
assert.equal(factoryCallCount, 2, 'Incorrect number of value factory calls.'); | ||
}); | ||
|
||
test('Simultaneous callers with lifetime', async () => { | ||
let factoryCallCount = 0; | ||
const lazy: AsyncLazy<boolean> = new AsyncLazy(async () => { | ||
factoryCallCount++; | ||
await delay(5); | ||
return true; | ||
}, 10); | ||
|
||
const p1 = lazy.getValue(); | ||
const p2 = lazy.getValue(); | ||
await Promise.all([p1, p2]); | ||
|
||
assert.equal(factoryCallCount, 1, 'Incorrect number of value factory calls.'); | ||
|
||
await delay(15); | ||
const p3 = lazy.getValue(); | ||
const p4 = lazy.getValue(); | ||
await Promise.all([p3, p4]); | ||
|
||
assert.equal(factoryCallCount, 2, 'Incorrect number of value factory calls.'); | ||
}); | ||
}); | ||
}); |