UNPKG

5.36 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18const assert = require("assert");
19const reference_1 = require("./reference");
20describe('reference', () => {
21 it('dispose a single reference', async () => {
22 const expectation = { disposed: false };
23 const references = new reference_1.ReferenceCollection(key => ({
24 key, dispose: () => {
25 expectation.disposed = true;
26 }
27 }));
28 assert.ok(!references.has('a'));
29 assert.ok(!expectation.disposed);
30 assert.deepStrictEqual(references.keys(), []);
31 const reference = await references.acquire('a');
32 assert.ok(references.has('a'));
33 assert.ok(!expectation.disposed);
34 assert.deepStrictEqual(references.keys(), ['a']);
35 reference.dispose();
36 assert.ok(!references.has('a'));
37 assert.ok(expectation.disposed);
38 assert.deepStrictEqual(references.keys(), []);
39 });
40 it('dispose 2 references', async () => {
41 const expectation = { disposed: false };
42 const references = new reference_1.ReferenceCollection(key => ({
43 key, dispose: () => {
44 expectation.disposed = true;
45 }
46 }));
47 assert.ok(!references.has('a'));
48 assert.ok(!expectation.disposed);
49 assert.deepStrictEqual(references.keys(), []);
50 const reference = await references.acquire('a');
51 const reference2 = await references.acquire('a');
52 assert.ok(references.has('a'));
53 assert.ok(!expectation.disposed);
54 assert.deepStrictEqual(references.keys(), ['a']);
55 reference.dispose();
56 assert.ok(references.has('a'));
57 assert.ok(!expectation.disposed);
58 assert.deepStrictEqual(references.keys(), ['a']);
59 reference2.dispose();
60 assert.ok(!references.has('a'));
61 assert.ok(expectation.disposed);
62 assert.deepStrictEqual(references.keys(), []);
63 });
64 it('dispose an object with 2 references', async () => {
65 const expectation = { disposed: false };
66 const references = new reference_1.ReferenceCollection(key => ({
67 key, dispose: () => {
68 expectation.disposed = true;
69 }
70 }));
71 assert.ok(!references.has('a'));
72 assert.ok(!expectation.disposed);
73 assert.deepStrictEqual(references.keys(), []);
74 await references.acquire('a');
75 const reference = await references.acquire('a');
76 assert.ok(references.has('a'));
77 assert.ok(!expectation.disposed);
78 assert.deepStrictEqual(references.keys(), ['a']);
79 reference.object.dispose();
80 assert.ok(!references.has('a'));
81 assert.ok(expectation.disposed);
82 assert.deepStrictEqual(references.keys(), []);
83 });
84 it("shouldn't call onWillDispose event on create", async () => {
85 const expectation = { disposed: false };
86 const references = new reference_1.ReferenceCollection(key => ({
87 key, dispose: () => {
88 }
89 }));
90 assert.ok(!references.has('a'));
91 assert.ok(!expectation.disposed);
92 references.onWillDispose(e => {
93 expectation.disposed = true;
94 });
95 await references.acquire('a');
96 assert.ok(!expectation.disposed);
97 const reference = await references.acquire('a');
98 reference.object.dispose();
99 assert.ok(expectation.disposed);
100 });
101 it('the same object should be provided by an async factory for the same key', async () => {
102 const references = new reference_1.ReferenceCollection(async (key) => ({
103 key, dispose: () => {
104 }
105 }));
106 const result = await Promise.all([...Array(10).keys()].map(() => references.acquire('a')));
107 result.forEach(v => assert.ok(result[0].object === v.object));
108 });
109 it('should not dispose an object if a reference is pending', async () => {
110 let disposed = false;
111 const references = new reference_1.ReferenceCollection(async (key) => ({
112 key, dispose: () => {
113 disposed = true;
114 }
115 }));
116 assert.ok(!disposed);
117 let reference = await references.acquire('a');
118 const pendingReference = references.acquire('a');
119 reference.dispose();
120 assert.ok(!disposed);
121 reference = await pendingReference;
122 reference.dispose();
123 assert.ok(disposed);
124 });
125});
126//# sourceMappingURL=reference.spec.js.map
\No newline at end of file