-
Notifications
You must be signed in to change notification settings - Fork 10
/
ConfigCatClientCacheTests.ts
260 lines (173 loc) · 8.69 KB
/
ConfigCatClientCacheTests.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import { assert } from "chai";
import "mocha";
import { ConfigCatClient, ConfigCatClientCache } from "../src/ConfigCatClient";
import { AutoPollOptions, ManualPollOptions } from "../src/ConfigCatClientOptions";
import { isWeakRefAvailable, setupPolyfills } from "../src/Polyfills";
import "./helpers/ConfigCatClientCacheExtensions";
import { FakeConfigCatKernel, FakeConfigFetcher } from "./helpers/fakes";
import { allowEventLoop } from "./helpers/utils";
describe("ConfigCatClientCache", () => {
it("getOrCreate() should return shared instance when cached instance is alive", (done) => {
// Arrange
const sdkKey = "123";
const configCatKernel: FakeConfigCatKernel = { configFetcher: new FakeConfigFetcher(), sdkType: "common", sdkVersion: "1.0.0" };
const options = new ManualPollOptions(sdkKey, configCatKernel.sdkType, configCatKernel.sdkVersion, {});
const cache = new ConfigCatClientCache();
// Act
const [client1, instanceAlreadyCreated1] = cache.getOrCreate(options, configCatKernel);
const [client2, instanceAlreadyCreated2] = cache.getOrCreate(options, configCatKernel);
// Assert
assert.strictEqual(1, cache.getAliveCount());
assert.strictEqual(1, cache.getSize());
const cachedInstance = cache["instances"][sdkKey][0].deref();
assert.instanceOf(client1, ConfigCatClient);
assert.isFalse(instanceAlreadyCreated1);
assert.strictEqual(cachedInstance, client1);
assert.instanceOf(client2, ConfigCatClient);
assert.isTrue(instanceAlreadyCreated2);
assert.strictEqual(cachedInstance, client2);
done();
});
it("getOrCreate() should return new instance after cached instance is collected", async function() {
// Arrange
setupPolyfills();
if (!isWeakRefAvailable() || typeof gc === "undefined") {
this.skip();
}
const sdkKey = "123";
const configCatKernel: FakeConfigCatKernel = { configFetcher: new FakeConfigFetcher(), sdkType: "common", sdkVersion: "1.0.0" };
const options = new AutoPollOptions(sdkKey, configCatKernel.sdkType, configCatKernel.sdkVersion, {});
const cache = new ConfigCatClientCache();
// Act
const [client1, instanceAlreadyCreated1] = getOrCreateClientWeakRef(cache, options, configCatKernel);
// We need to allow the event loop to run so the runtime can detect there's no more strong references to client1.
await allowEventLoop();
gc();
const [client2, instanceAlreadyCreated2] = cache.getOrCreate(options, configCatKernel);
// Assert
assert.strictEqual(1, cache.getAliveCount());
assert.strictEqual(1, cache.getSize());
const cachedInstance = cache["instances"][sdkKey][0].deref();
assert.isUndefined(client1.deref());
assert.isFalse(instanceAlreadyCreated1);
assert.instanceOf(client2, ConfigCatClient);
assert.isFalse(instanceAlreadyCreated2);
assert.strictEqual(cachedInstance, client2);
});
it("remove() should remove cache entry when cached instance is alive and tokens match", (done) => {
// Arrange
const sdkKey = "123";
const configCatKernel: FakeConfigCatKernel = { configFetcher: new FakeConfigFetcher(), sdkType: "common", sdkVersion: "1.0.0" };
const options = new ManualPollOptions(sdkKey, configCatKernel.sdkType, configCatKernel.sdkVersion, {});
const cache = new ConfigCatClientCache();
const [client1, instanceAlreadyCreated1] = cache.getOrCreate(options, configCatKernel);
// Act
const success = cache.remove(sdkKey, client1["cacheToken"]!);
// Assert
assert.isTrue(success);
assert.strictEqual(0, cache.getAliveCount());
assert.strictEqual(0, cache.getSize());
assert.instanceOf(client1, ConfigCatClient);
assert.isFalse(instanceAlreadyCreated1);
done();
});
it("remove() should not remove cache entry when cached instance is alive and tokens mismatch", (done) => {
// Arrange
const sdkKey = "123";
const configCatKernel: FakeConfigCatKernel = { configFetcher: new FakeConfigFetcher(), sdkType: "common", sdkVersion: "1.0.0" };
const options = new ManualPollOptions(sdkKey, configCatKernel.sdkType, configCatKernel.sdkVersion, {});
const cache = new ConfigCatClientCache();
const [client1, instanceAlreadyCreated1] = cache.getOrCreate(options, configCatKernel);
cache.remove(sdkKey, client1["cacheToken"]!);
const [client2, instanceAlreadyCreated2] = cache.getOrCreate(options, configCatKernel);
// Act
const success = cache.remove(sdkKey, client1["cacheToken"]!);
// Assert
assert.isFalse(success);
assert.strictEqual(1, cache.getAliveCount());
assert.strictEqual(1, cache.getSize());
const cachedInstance = cache["instances"][sdkKey][0].deref();
assert.instanceOf(client1, ConfigCatClient);
assert.isFalse(instanceAlreadyCreated1);
assert.notStrictEqual(cachedInstance, client1);
assert.instanceOf(client2, ConfigCatClient);
assert.isFalse(instanceAlreadyCreated2);
assert.strictEqual(cachedInstance, client2);
done();
});
it("remove() should remove cache entry when cached instance is collected", async function() {
// Arrange
setupPolyfills();
if (!isWeakRefAvailable() || typeof gc === "undefined") {
this.skip();
}
const sdkKey = "123";
const configCatKernel: FakeConfigCatKernel = { configFetcher: new FakeConfigFetcher(), sdkType: "common", sdkVersion: "1.0.0" };
const options = new ManualPollOptions(sdkKey, configCatKernel.sdkType, configCatKernel.sdkVersion, {});
const cache = new ConfigCatClientCache();
const [client1, instanceAlreadyCreated1] = getOrCreateClientWeakRef(cache, options, configCatKernel);
// We need to allow the event loop to run so the runtime can detect there's no more strong references to client1.
await allowEventLoop();
gc();
// Act
const success = cache.remove(sdkKey, {});
// Assert
assert.isFalse(success);
assert.strictEqual(0, cache.getAliveCount());
assert.strictEqual(0, cache.getSize());
assert.isUndefined(client1.deref());
assert.isFalse(instanceAlreadyCreated1);
});
it("remove() should not remove cache entry when SDK Key is not present", (done) => {
// Arrange
const sdkKey = "123";
const configCatKernel: FakeConfigCatKernel = { configFetcher: new FakeConfigFetcher(), sdkType: "common", sdkVersion: "1.0.0" };
const options = new ManualPollOptions(sdkKey, configCatKernel.sdkType, configCatKernel.sdkVersion, {});
const cache = new ConfigCatClientCache();
const [client1, instanceAlreadyCreated1] = cache.getOrCreate(options, configCatKernel);
// Act
const success = cache.remove("321", {});
// Assert
assert.isFalse(success);
assert.strictEqual(1, cache.getAliveCount());
assert.strictEqual(1, cache.getSize());
const cachedInstance = cache["instances"][sdkKey][0].deref();
assert.instanceOf(client1, ConfigCatClient);
assert.isFalse(instanceAlreadyCreated1);
assert.strictEqual(cachedInstance, client1);
done();
});
it("clear() should remove all cached instances", async function() {
// Arrange
setupPolyfills();
if (!isWeakRefAvailable() || typeof gc === "undefined") {
this.skip();
}
const sdkKey1 = "123";
const sdkKey2 = "456";
const configCatKernel: FakeConfigCatKernel = { configFetcher: new FakeConfigFetcher(), sdkType: "common", sdkVersion: "1.0.0" };
const options1 = new ManualPollOptions(sdkKey1, configCatKernel.sdkType, configCatKernel.sdkVersion, {});
const options2 = new ManualPollOptions(sdkKey2, configCatKernel.sdkType, configCatKernel.sdkVersion, {});
const cache = new ConfigCatClientCache();
const [client1, instanceAlreadyCreated1] = cache.getOrCreate(options1, configCatKernel);
const [client2, instanceAlreadyCreated2, cacheCountBefore] = getOrCreateClientWeakRef(cache, options2, configCatKernel);
// We need to allow the event loop to run so the runtime can detect there's no more strong references to client1.
await allowEventLoop();
gc();
// Act
const removedInstances = cache.clear();
// Assert
assert.strictEqual(2, cacheCountBefore);
assert.strictEqual(0, cache.getAliveCount());
assert.strictEqual(0, cache.getSize());
assert.instanceOf(client1, ConfigCatClient);
assert.isFalse(instanceAlreadyCreated1);
assert.isUndefined(client2.deref());
assert.isFalse(instanceAlreadyCreated2);
assert.sameDeepMembers([client1], removedInstances);
});
});
function getOrCreateClientWeakRef(cache: ConfigCatClientCache, options: any, configCatKernel: any): [WeakRef<ConfigCatClient>, boolean, number] {
const [client, instanceAlreadyCreated] = cache.getOrCreate(options, configCatKernel);
return [new WeakRef(client), instanceAlreadyCreated, cache.getAliveCount()];
}