@daiso-tech/core 0.32.0 → 0.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/async/async.errors.d.ts +14 -0
- package/dist/async/async.errors.js +14 -0
- package/dist/async/async.errors.js.map +1 -1
- package/dist/async/async.events.d.ts +23 -0
- package/dist/async/async.events.js +23 -0
- package/dist/async/async.events.js.map +1 -1
- package/dist/async/backof-policies/_shared.d.ts +1 -1
- package/dist/async/backof-policies/constant-backoff-policy/constant-backoff-policy.d.ts +2 -2
- package/dist/async/backof-policies/constant-backoff-policy/constant-backoff-policy.js +1 -1
- package/dist/async/backof-policies/exponential-backoff-policy/exponential-backoff-policy.d.ts +2 -2
- package/dist/async/backof-policies/exponential-backoff-policy/exponential-backoff-policy.js +1 -1
- package/dist/async/backof-policies/linear-backoff-policy/linear-backoff-policy.d.ts +2 -2
- package/dist/async/backof-policies/linear-backoff-policy/linear-backoff-policy.js +1 -1
- package/dist/async/backof-policies/polynomial-backoff-policy/polynomial-backoff-policy.d.ts +2 -2
- package/dist/async/backof-policies/polynomial-backoff-policy/polynomial-backoff-policy.js +1 -1
- package/dist/async/utilities/lazy-promise/lazy-promise.d.ts +7 -41
- package/dist/async/utilities/lazy-promise/lazy-promise.js +7 -8
- package/dist/async/utilities/lazy-promise/lazy-promise.js.map +1 -1
- package/dist/cache/contracts/cache-factory.contract.d.ts +3 -3
- package/dist/cache/contracts/cache.contract.d.ts +0 -22
- package/dist/cache/contracts/cache.errors.d.ts +14 -1
- package/dist/cache/contracts/cache.errors.js +14 -1
- package/dist/cache/contracts/cache.errors.js.map +1 -1
- package/dist/cache/contracts/cache.events.d.ts +28 -14
- package/dist/cache/contracts/cache.events.js +28 -3
- package/dist/cache/contracts/cache.events.js.map +1 -1
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.js +3 -3
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter-serde.js.map +1 -1
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js +0 -1
- package/dist/cache/implementations/adapters/mongodb-cache-adapter/mongodb-cache-adapter.js.map +1 -1
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.js +3 -3
- package/dist/cache/implementations/adapters/redis-cache-adapter/redis-cache-adapter-serde.js.map +1 -1
- package/dist/cache/implementations/derivables/cache/cache.d.ts +9 -139
- package/dist/cache/implementations/derivables/cache/cache.js +38 -229
- package/dist/cache/implementations/derivables/cache/cache.js.map +1 -1
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.d.ts +6 -6
- package/dist/cache/implementations/derivables/cache-factory/cache-factory.js.map +1 -1
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.d.ts +2 -2
- package/dist/cache/implementations/test-utilities/cache-adapter.test-suite.js +1 -1
- package/dist/cache/implementations/test-utilities/cache.test-suite.d.ts +4 -4
- package/dist/cache/implementations/test-utilities/cache.test-suite.js +239 -433
- package/dist/cache/implementations/test-utilities/cache.test-suite.js.map +1 -1
- package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.d.ts +2 -2
- package/dist/cache/implementations/test-utilities/database-cache-dapter.test-suite.js +1 -1
- package/dist/collection/contracts/collection.errors.d.ts +17 -0
- package/dist/collection/contracts/collection.errors.js +17 -0
- package/dist/collection/contracts/collection.errors.js.map +1 -1
- package/dist/event-bus/contracts/event-bus-factory.contract.d.ts +3 -3
- package/dist/event-bus/contracts/event-bus.contract.d.ts +2 -44
- package/dist/event-bus/contracts/event-bus.contract.js.map +1 -1
- package/dist/event-bus/contracts/event-bus.errors.d.ts +16 -1
- package/dist/event-bus/contracts/event-bus.errors.js +16 -1
- package/dist/event-bus/contracts/event-bus.errors.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.d.ts +6 -77
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js +11 -131
- package/dist/event-bus/implementations/derivables/event-bus/event-bus.js.map +1 -1
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.d.ts +4 -4
- package/dist/event-bus/implementations/derivables/event-bus-factory/event-bus-factory.js.map +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.d.ts +2 -2
- package/dist/event-bus/implementations/test-utilities/event-bus-adapter.test-suite.js +1 -1
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.d.ts +4 -4
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js +691 -1432
- package/dist/event-bus/implementations/test-utilities/event-bus.test-suite.js.map +1 -1
- package/dist/lock/contracts/lock-provider-factory.contract.d.ts +3 -3
- package/dist/lock/contracts/lock-provider.contract.d.ts +10 -20
- package/dist/lock/contracts/lock.contract.d.ts +1 -10
- package/dist/lock/contracts/lock.errors.d.ts +18 -0
- package/dist/lock/contracts/lock.errors.js +18 -0
- package/dist/lock/contracts/lock.errors.js.map +1 -1
- package/dist/lock/contracts/lock.events.d.ts +22 -0
- package/dist/lock/contracts/lock.events.js +22 -0
- package/dist/lock/contracts/lock.events.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.d.ts +12 -155
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js +31 -228
- package/dist/lock/implementations/derivables/lock-provider/lock-provider.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.d.ts +5 -5
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js +7 -30
- package/dist/lock/implementations/derivables/lock-provider/lock-serde-transformer.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider/lock.d.ts +8 -58
- package/dist/lock/implementations/derivables/lock-provider/lock.js +33 -105
- package/dist/lock/implementations/derivables/lock-provider/lock.js.map +1 -1
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.d.ts +9 -8
- package/dist/lock/implementations/derivables/lock-provider-factory/lock-provider-factory.js.map +1 -1
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.d.ts +2 -2
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js +178 -659
- package/dist/lock/implementations/test-utilities/lock-provider.test-suite.js.map +1 -1
- package/dist/serde/contracts/deserializer.contract.d.ts +1 -1
- package/dist/serde/contracts/deserializer.contract.js +1 -1
- package/dist/serde/contracts/deserializer.contract.js.map +1 -1
- package/dist/serde/contracts/serde.errors.d.ts +14 -2
- package/dist/serde/contracts/serde.errors.js +16 -4
- package/dist/serde/contracts/serde.errors.js.map +1 -1
- package/dist/serde/contracts/serializer.contract.d.ts +1 -1
- package/dist/serde/contracts/serializer.contract.js +1 -1
- package/dist/serde/contracts/serializer.contract.js.map +1 -1
- package/dist/serde/implementations/adapters/mongodb-serde/mongodb-serde.js +3 -3
- package/dist/serde/implementations/adapters/mongodb-serde/mongodb-serde.js.map +1 -1
- package/dist/serde/implementations/adapters/redis-serde/redis-serde.js +3 -3
- package/dist/serde/implementations/adapters/redis-serde/redis-serde.js.map +1 -1
- package/dist/serde/implementations/adapters/sql-serde/sql-serde.js +3 -3
- package/dist/serde/implementations/adapters/sql-serde/sql-serde.js.map +1 -1
- package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.js +3 -3
- package/dist/serde/implementations/adapters/super-json-serde-adapter/super-json-serde-adapter.js.map +1 -1
- package/dist/serde/implementations/test-utilities/flexible-serde-adapter.test-suite.d.ts +2 -2
- package/dist/serde/implementations/test-utilities/flexible-serde-adapter.test-suite.js +1 -1
- package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.d.ts +2 -2
- package/dist/serde/implementations/test-utilities/flexible-serde.test-suite.js +1 -1
- package/dist/utilities/classes/key-prefixer/_module.d.ts +0 -1
- package/dist/utilities/classes/key-prefixer/_module.js +0 -1
- package/dist/utilities/classes/key-prefixer/_module.js.map +1 -1
- package/dist/utilities/classes/key-prefixer/key-prefixer.d.ts +2 -11
- package/dist/utilities/classes/key-prefixer/key-prefixer.js +4 -46
- package/dist/utilities/classes/key-prefixer/key-prefixer.js.map +1 -1
- package/dist/utilities/errors.d.ts +2 -0
- package/dist/utilities/errors.js +2 -0
- package/dist/utilities/errors.js.map +1 -1
- package/package.json +3 -4
- package/dist/async/utilities/lazy-promise/lazy-promise-event-bus.d.ts +0 -15
- package/dist/async/utilities/lazy-promise/lazy-promise-event-bus.js +0 -50
- package/dist/async/utilities/lazy-promise/lazy-promise-event-bus.js.map +0 -1
- package/dist/async/utilities/lazy-promise/lazy-promise-listener.contract.d.ts +0 -23
- package/dist/async/utilities/lazy-promise/lazy-promise-listener.contract.js +0 -44
- package/dist/async/utilities/lazy-promise/lazy-promise-listener.contract.js.map +0 -1
- package/dist/utilities/classes/key-prefixer/key-prefixer.contract.d.ts +0 -28
- package/dist/utilities/classes/key-prefixer/key-prefixer.contract.js +0 -5
- package/dist/utilities/classes/key-prefixer/key-prefixer.contract.js.map +0 -1
|
@@ -10,7 +10,7 @@ import { LazyPromise } from "../../../async/_module-exports.js";
|
|
|
10
10
|
* The <i>cacheTestSuite</i> function simplifies the process of testing your custom implementation of <i>{@link ICache}</i> with <i>vitest</i>.
|
|
11
11
|
*
|
|
12
12
|
* IMPORT_PATH: ```"@daiso-tech/core/cache/test-utilities"```
|
|
13
|
-
* @group
|
|
13
|
+
* @group TestUtilities
|
|
14
14
|
* @example
|
|
15
15
|
* ```ts
|
|
16
16
|
* import { beforeEach, describe, expect, test } from "vitest";
|
|
@@ -36,426 +36,423 @@ import { LazyPromise } from "../../../async/_module-exports.js";
|
|
|
36
36
|
*/
|
|
37
37
|
export function cacheTestSuite(settings) {
|
|
38
38
|
const { expect, test, createCache, describe, beforeEach } = settings;
|
|
39
|
-
let
|
|
40
|
-
let cacheB;
|
|
39
|
+
let cache;
|
|
41
40
|
beforeEach(async () => {
|
|
42
|
-
|
|
43
|
-
cacheA = cache;
|
|
44
|
-
cacheB = cache.withGroup("b");
|
|
41
|
+
cache = await createCache();
|
|
45
42
|
});
|
|
46
43
|
const TTL = TimeSpan.fromMilliseconds(50);
|
|
47
44
|
const DELAY_TIME = TimeSpan.fromMilliseconds(50);
|
|
48
45
|
describe("Api tests:", () => {
|
|
49
46
|
describe("method: exists", () => {
|
|
50
47
|
test("Should return true when key exists", async () => {
|
|
51
|
-
await
|
|
48
|
+
await cache.add("a", 1, null);
|
|
52
49
|
await LazyPromise.delay(TTL.divide(4));
|
|
53
|
-
expect(await
|
|
50
|
+
expect(await cache.exists("a")).toBe(true);
|
|
54
51
|
});
|
|
55
52
|
test("Should return false when keys doesnt exists", async () => {
|
|
56
|
-
expect(await
|
|
53
|
+
expect(await cache.exists("a")).toBe(false);
|
|
57
54
|
});
|
|
58
55
|
test("Should return false when key is experied", async () => {
|
|
59
|
-
await
|
|
56
|
+
await cache.add("a", 1, TTL);
|
|
60
57
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
61
|
-
expect(await
|
|
58
|
+
expect(await cache.exists("a")).toBe(false);
|
|
62
59
|
});
|
|
63
60
|
});
|
|
64
61
|
describe("method: missing", () => {
|
|
65
62
|
test("Should return false when key exists", async () => {
|
|
66
|
-
await
|
|
63
|
+
await cache.add("a", 1, null);
|
|
67
64
|
await LazyPromise.delay(TTL.divide(4));
|
|
68
|
-
expect(await
|
|
65
|
+
expect(await cache.missing("a")).toBe(false);
|
|
69
66
|
});
|
|
70
67
|
test("Should return true when keys doesnt exists", async () => {
|
|
71
|
-
expect(await
|
|
68
|
+
expect(await cache.missing("a")).toBe(true);
|
|
72
69
|
});
|
|
73
70
|
test("Should return true when key is experied", async () => {
|
|
74
|
-
await
|
|
71
|
+
await cache.add("a", 1, TTL);
|
|
75
72
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
76
|
-
expect(await
|
|
73
|
+
expect(await cache.missing("a")).toBe(true);
|
|
77
74
|
});
|
|
78
75
|
});
|
|
79
76
|
describe("method: get", () => {
|
|
80
77
|
test("Should return the value when key exists", async () => {
|
|
81
|
-
await
|
|
78
|
+
await cache.add("a", 1, null);
|
|
82
79
|
await LazyPromise.delay(TTL.divide(4));
|
|
83
|
-
expect(await
|
|
80
|
+
expect(await cache.get("a")).toBe(1);
|
|
84
81
|
});
|
|
85
82
|
test("Should return null when keys doesnt exists", async () => {
|
|
86
|
-
expect(await
|
|
83
|
+
expect(await cache.get("a")).toBeNull();
|
|
87
84
|
});
|
|
88
85
|
test("Should return null when key is experied", async () => {
|
|
89
|
-
await
|
|
86
|
+
await cache.add("a", 1, TTL);
|
|
90
87
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
91
|
-
expect(await
|
|
88
|
+
expect(await cache.get("a")).toBeNull();
|
|
92
89
|
});
|
|
93
90
|
});
|
|
94
91
|
describe("method: getOrFail", () => {
|
|
95
92
|
test("Should return the value when key exists", async () => {
|
|
96
|
-
await
|
|
93
|
+
await cache.add("a", 1, null);
|
|
97
94
|
await LazyPromise.delay(TTL.divide(4));
|
|
98
|
-
expect(await
|
|
95
|
+
expect(await cache.getOrFail("a")).toBe(1);
|
|
99
96
|
});
|
|
100
97
|
test("Should throw an KeyNotFoundCacheError when keys doesnt exists", async () => {
|
|
101
|
-
await expect(
|
|
98
|
+
await expect(cache.getOrFail("a")).rejects.toBeInstanceOf(KeyNotFoundCacheError);
|
|
102
99
|
});
|
|
103
100
|
test("Should throw an KeyNotFoundCacheError when key is experied", async () => {
|
|
104
|
-
await
|
|
101
|
+
await cache.add("a", 1, TTL);
|
|
105
102
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
106
|
-
await expect(
|
|
103
|
+
await expect(cache.getOrFail("a")).rejects.toBeInstanceOf(KeyNotFoundCacheError);
|
|
107
104
|
});
|
|
108
105
|
});
|
|
109
106
|
describe("method: getOr", () => {
|
|
110
107
|
test("Should return value when key exists", async () => {
|
|
111
|
-
await
|
|
112
|
-
expect(await
|
|
108
|
+
await cache.add("a", 1);
|
|
109
|
+
expect(await cache.getOr("a", -1)).toBe(1);
|
|
113
110
|
});
|
|
114
111
|
describe("Should return default value when key doesnt exists", () => {
|
|
115
112
|
test("Value", async () => {
|
|
116
|
-
expect(await
|
|
113
|
+
expect(await cache.getOr("a", -1)).toBe(-1);
|
|
117
114
|
});
|
|
118
115
|
test("Function", async () => {
|
|
119
|
-
expect(await
|
|
116
|
+
expect(await cache.getOr("a", () => -1)).toBe(-1);
|
|
120
117
|
});
|
|
121
118
|
test("Async function", async () => {
|
|
122
|
-
expect(await
|
|
119
|
+
expect(await cache.getOr("a", () => Promise.resolve(-1))).toBe(-1);
|
|
123
120
|
});
|
|
124
121
|
test("LazyPromise", async () => {
|
|
125
|
-
expect(await
|
|
122
|
+
expect(await cache.getOr("a", new LazyPromise(() => Promise.resolve(-1)))).toBe(-1);
|
|
126
123
|
});
|
|
127
124
|
});
|
|
128
125
|
describe("Should return default value when key is expired", () => {
|
|
129
126
|
test("Value", async () => {
|
|
130
|
-
await
|
|
127
|
+
await cache.add("a", 1, TTL);
|
|
131
128
|
await LazyPromise.delay(TTL);
|
|
132
|
-
expect(await
|
|
129
|
+
expect(await cache.getOr("a", -1)).toBe(-1);
|
|
133
130
|
});
|
|
134
131
|
test("Function", async () => {
|
|
135
|
-
await
|
|
132
|
+
await cache.add("a", 1, TTL);
|
|
136
133
|
await LazyPromise.delay(TTL);
|
|
137
|
-
expect(await
|
|
134
|
+
expect(await cache.getOr("a", () => -1)).toBe(-1);
|
|
138
135
|
});
|
|
139
136
|
test("Async function", async () => {
|
|
140
|
-
await
|
|
137
|
+
await cache.add("a", 1, TTL);
|
|
141
138
|
await LazyPromise.delay(TTL);
|
|
142
|
-
expect(await
|
|
139
|
+
expect(await cache.getOr("a", () => Promise.resolve(-1))).toBe(-1);
|
|
143
140
|
});
|
|
144
141
|
test("LazyPromise", async () => {
|
|
145
|
-
await
|
|
142
|
+
await cache.add("a", 1, TTL);
|
|
146
143
|
await LazyPromise.delay(TTL);
|
|
147
|
-
expect(await
|
|
144
|
+
expect(await cache.getOr("a", new LazyPromise(() => Promise.resolve(-1)))).toBe(-1);
|
|
148
145
|
});
|
|
149
146
|
});
|
|
150
147
|
});
|
|
151
148
|
describe("method: getAndRemove", () => {
|
|
152
149
|
test("Should return value when key exists", async () => {
|
|
153
|
-
await
|
|
150
|
+
await cache.add("a", 1, null);
|
|
154
151
|
await LazyPromise.delay(TTL.divide(4));
|
|
155
|
-
expect(await
|
|
152
|
+
expect(await cache.getAndRemove("a")).toBe(1);
|
|
156
153
|
});
|
|
157
154
|
test("Should return null when key doesnt exists", async () => {
|
|
158
|
-
expect(await
|
|
155
|
+
expect(await cache.getAndRemove("a")).toBeNull();
|
|
159
156
|
});
|
|
160
157
|
test("Should return null when key is expired", async () => {
|
|
161
|
-
await
|
|
158
|
+
await cache.add("a", 1, TTL);
|
|
162
159
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
163
|
-
expect(await
|
|
160
|
+
expect(await cache.getAndRemove("a")).toBeNull();
|
|
164
161
|
});
|
|
165
162
|
test("Should persist removal when key exists", async () => {
|
|
166
|
-
await
|
|
163
|
+
await cache.add("a", 1, null);
|
|
167
164
|
await LazyPromise.delay(TTL.divide(4));
|
|
168
|
-
await
|
|
165
|
+
await cache.getAndRemove("a");
|
|
169
166
|
await LazyPromise.delay(TTL.divide(4));
|
|
170
|
-
expect(await
|
|
167
|
+
expect(await cache.get("a")).toBeNull();
|
|
171
168
|
});
|
|
172
169
|
});
|
|
173
170
|
describe("method: getOrAdd", () => {
|
|
174
171
|
test("Should return value when key exists", async () => {
|
|
175
|
-
await
|
|
176
|
-
expect(await
|
|
172
|
+
await cache.add("a", 1);
|
|
173
|
+
expect(await cache.getOrAdd("a", -1)).toBe(1);
|
|
177
174
|
});
|
|
178
175
|
describe("Should persist insertion when key doesnt exists", () => {
|
|
179
176
|
test("Value", async () => {
|
|
180
|
-
await
|
|
181
|
-
expect(await
|
|
177
|
+
await cache.getOrAdd("a", -1);
|
|
178
|
+
expect(await cache.get("a")).toBe(-1);
|
|
182
179
|
});
|
|
183
180
|
test("Function", async () => {
|
|
184
|
-
await
|
|
185
|
-
expect(await
|
|
181
|
+
await cache.getOrAdd("a", () => -1);
|
|
182
|
+
expect(await cache.get("a")).toBe(-1);
|
|
186
183
|
});
|
|
187
184
|
test("Async function", async () => {
|
|
188
|
-
await
|
|
189
|
-
expect(await
|
|
185
|
+
await cache.getOrAdd("a", () => Promise.resolve(-1));
|
|
186
|
+
expect(await cache.get("a")).toBe(-1);
|
|
190
187
|
});
|
|
191
188
|
test("LazyPromise", async () => {
|
|
192
|
-
await
|
|
193
|
-
expect(await
|
|
189
|
+
await cache.getOrAdd("a", new LazyPromise(() => Promise.resolve(-1)));
|
|
190
|
+
expect(await cache.get("a")).toBe(-1);
|
|
194
191
|
});
|
|
195
192
|
});
|
|
196
193
|
describe("Should persist insertion when key is expired", () => {
|
|
197
194
|
test("Value", async () => {
|
|
198
|
-
await
|
|
195
|
+
await cache.add("a", 1, TTL);
|
|
199
196
|
await LazyPromise.delay(TTL);
|
|
200
|
-
await
|
|
201
|
-
expect(await
|
|
197
|
+
await cache.getOrAdd("a", -1);
|
|
198
|
+
expect(await cache.get("a")).toBe(-1);
|
|
202
199
|
});
|
|
203
200
|
test("Function", async () => {
|
|
204
|
-
await
|
|
201
|
+
await cache.add("a", 1, TTL);
|
|
205
202
|
await LazyPromise.delay(TTL);
|
|
206
|
-
await
|
|
207
|
-
expect(await
|
|
203
|
+
await cache.getOrAdd("a", () => -1);
|
|
204
|
+
expect(await cache.get("a")).toBe(-1);
|
|
208
205
|
});
|
|
209
206
|
test("Async function", async () => {
|
|
210
|
-
await
|
|
207
|
+
await cache.add("a", 1, TTL);
|
|
211
208
|
await LazyPromise.delay(TTL);
|
|
212
|
-
await
|
|
213
|
-
expect(await
|
|
209
|
+
await cache.getOrAdd("a", () => Promise.resolve(-1));
|
|
210
|
+
expect(await cache.get("a")).toBe(-1);
|
|
214
211
|
});
|
|
215
212
|
test("LazyPromise", async () => {
|
|
216
|
-
await
|
|
213
|
+
await cache.add("a", 1, TTL);
|
|
217
214
|
await LazyPromise.delay(TTL);
|
|
218
|
-
await
|
|
219
|
-
expect(await
|
|
215
|
+
await cache.getOrAdd("a", new LazyPromise(() => Promise.resolve(-1)));
|
|
216
|
+
expect(await cache.get("a")).toBe(-1);
|
|
220
217
|
});
|
|
221
218
|
});
|
|
222
219
|
});
|
|
223
220
|
describe("method: add", () => {
|
|
224
221
|
test("Should return true when key doesnt exists", async () => {
|
|
225
|
-
const result = await
|
|
222
|
+
const result = await cache.add("a", 1, null);
|
|
226
223
|
await LazyPromise.delay(TTL.divide(4));
|
|
227
224
|
expect(result).toBe(true);
|
|
228
225
|
});
|
|
229
226
|
test("Should return true when key is expired", async () => {
|
|
230
|
-
await
|
|
227
|
+
await cache.add("a", 1, TTL);
|
|
231
228
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
232
|
-
expect(await
|
|
229
|
+
expect(await cache.add("a", 1, null)).toBe(true);
|
|
233
230
|
});
|
|
234
231
|
test("Should persist values when key doesnt exist", async () => {
|
|
235
|
-
await
|
|
232
|
+
await cache.add("a", 1, null);
|
|
236
233
|
await LazyPromise.delay(TTL.divide(4));
|
|
237
|
-
expect(await
|
|
234
|
+
expect(await cache.get("a")).toBe(1);
|
|
238
235
|
});
|
|
239
236
|
test("Should persist values when key is expired", async () => {
|
|
240
|
-
await
|
|
237
|
+
await cache.add("a", -1, TTL);
|
|
241
238
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
242
|
-
await
|
|
243
|
-
expect(await
|
|
239
|
+
await cache.add("a", 1, null);
|
|
240
|
+
expect(await cache.get("a")).toBe(1);
|
|
244
241
|
});
|
|
245
242
|
test("Should return false when key exists", async () => {
|
|
246
|
-
await
|
|
243
|
+
await cache.add("a", 1, null);
|
|
247
244
|
await LazyPromise.delay(TTL.divide(4));
|
|
248
|
-
expect(await
|
|
245
|
+
expect(await cache.add("a", 1, null)).toBe(false);
|
|
249
246
|
});
|
|
250
247
|
test("Should not persist value when key exist", async () => {
|
|
251
|
-
await
|
|
248
|
+
await cache.add("a", 1, null);
|
|
252
249
|
await LazyPromise.delay(TTL.divide(4));
|
|
253
|
-
await
|
|
250
|
+
await cache.add("a", 2, null);
|
|
254
251
|
await LazyPromise.delay(TTL.divide(4));
|
|
255
|
-
expect(await
|
|
252
|
+
expect(await cache.get("a")).toBe(1);
|
|
256
253
|
});
|
|
257
254
|
});
|
|
258
255
|
describe("method: put", () => {
|
|
259
256
|
test("Should return true when key exists", async () => {
|
|
260
|
-
await
|
|
257
|
+
await cache.add("a", 1, null);
|
|
261
258
|
await LazyPromise.delay(TTL.divide(4));
|
|
262
|
-
expect(await
|
|
259
|
+
expect(await cache.put("a", -1, null)).toBe(true);
|
|
263
260
|
});
|
|
264
261
|
test("Should persist value when key exist", async () => {
|
|
265
|
-
await
|
|
262
|
+
await cache.add("a", 1, null);
|
|
266
263
|
await LazyPromise.delay(TTL.divide(4));
|
|
267
|
-
await
|
|
264
|
+
await cache.put("a", -1, null);
|
|
268
265
|
await LazyPromise.delay(TTL.divide(4));
|
|
269
|
-
expect(await
|
|
266
|
+
expect(await cache.get("a")).toBe(-1);
|
|
270
267
|
});
|
|
271
268
|
test("Should return false when key doesnt exists", async () => {
|
|
272
|
-
expect(await
|
|
269
|
+
expect(await cache.put("a", -1, null)).toBe(false);
|
|
273
270
|
});
|
|
274
271
|
test("Should return false when key is expired", async () => {
|
|
275
|
-
await
|
|
272
|
+
await cache.add("a", 1, TTL);
|
|
276
273
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
277
|
-
expect(await
|
|
274
|
+
expect(await cache.put("a", -1, null)).toBe(false);
|
|
278
275
|
});
|
|
279
276
|
test("Should persist values when key doesnt exist", async () => {
|
|
280
|
-
await
|
|
277
|
+
await cache.put("a", -1, null);
|
|
281
278
|
await LazyPromise.delay(TTL.divide(4));
|
|
282
|
-
expect(await
|
|
279
|
+
expect(await cache.get("a")).toBe(-1);
|
|
283
280
|
});
|
|
284
281
|
test("Should persist values when key is expired", async () => {
|
|
285
|
-
await
|
|
282
|
+
await cache.add("a", 1, TTL);
|
|
286
283
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
287
|
-
await
|
|
284
|
+
await cache.put("a", -1, null);
|
|
288
285
|
await LazyPromise.delay(TTL.divide(4));
|
|
289
|
-
expect(await
|
|
286
|
+
expect(await cache.get("a")).toBe(-1);
|
|
290
287
|
});
|
|
291
288
|
test("Should replace the ttl value", async () => {
|
|
292
289
|
const ttlA = TimeSpan.fromMilliseconds(100);
|
|
293
|
-
await
|
|
290
|
+
await cache.add("a", 1, ttlA);
|
|
294
291
|
await LazyPromise.delay(TTL.divide(4));
|
|
295
292
|
const ttlB = TimeSpan.fromMilliseconds(50);
|
|
296
|
-
await
|
|
293
|
+
await cache.put("a", -1, ttlB);
|
|
297
294
|
await LazyPromise.delay(ttlB);
|
|
298
|
-
expect(await
|
|
295
|
+
expect(await cache.get("a")).toBeNull();
|
|
299
296
|
});
|
|
300
297
|
});
|
|
301
298
|
describe("method: update", () => {
|
|
302
299
|
test("Should return true when key exists", async () => {
|
|
303
|
-
await
|
|
300
|
+
await cache.add("a", 1, null);
|
|
304
301
|
await LazyPromise.delay(TTL.divide(4));
|
|
305
|
-
expect(await
|
|
302
|
+
expect(await cache.update("a", -1)).toBe(true);
|
|
306
303
|
});
|
|
307
304
|
test("Should persist value when key exist", async () => {
|
|
308
|
-
await
|
|
305
|
+
await cache.add("a", 1, null);
|
|
309
306
|
await LazyPromise.delay(TTL.divide(4));
|
|
310
|
-
await
|
|
307
|
+
await cache.update("a", -1);
|
|
311
308
|
await LazyPromise.delay(TTL.divide(4));
|
|
312
|
-
expect(await
|
|
309
|
+
expect(await cache.get("a")).toBe(-1);
|
|
313
310
|
});
|
|
314
311
|
test("Should return false when key doesnt exists", async () => {
|
|
315
|
-
expect(await
|
|
312
|
+
expect(await cache.update("a", -1)).toBe(false);
|
|
316
313
|
});
|
|
317
314
|
test("Should return false when key is expired", async () => {
|
|
318
|
-
await
|
|
315
|
+
await cache.add("a", 1, TTL);
|
|
319
316
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
320
|
-
expect(await
|
|
317
|
+
expect(await cache.update("a", -1)).toBe(false);
|
|
321
318
|
});
|
|
322
319
|
test("Should not persist value when key doesnt exist", async () => {
|
|
323
|
-
await
|
|
320
|
+
await cache.update("a", -1);
|
|
324
321
|
await LazyPromise.delay(TTL.divide(4));
|
|
325
|
-
expect(await
|
|
322
|
+
expect(await cache.get("a")).toBeNull();
|
|
326
323
|
});
|
|
327
324
|
test("Should not persist value when key is expired", async () => {
|
|
328
|
-
await
|
|
325
|
+
await cache.add("a", 1, TTL);
|
|
329
326
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
330
|
-
await
|
|
331
|
-
expect(await
|
|
327
|
+
await cache.update("a", -1);
|
|
328
|
+
expect(await cache.get("a")).toBeNull();
|
|
332
329
|
});
|
|
333
330
|
});
|
|
334
331
|
describe("method: increment", () => {
|
|
335
332
|
test("Should return true when key exists", async () => {
|
|
336
|
-
await
|
|
333
|
+
await cache.add("a", 1, null);
|
|
337
334
|
await LazyPromise.delay(TTL.divide(4));
|
|
338
|
-
expect(await
|
|
335
|
+
expect(await cache.increment("a", 1)).toBe(true);
|
|
339
336
|
});
|
|
340
337
|
test("Should persist increment when key exists", async () => {
|
|
341
|
-
await
|
|
338
|
+
await cache.add("a", 1, null);
|
|
342
339
|
await LazyPromise.delay(TTL.divide(4));
|
|
343
|
-
await
|
|
340
|
+
await cache.increment("a", 1);
|
|
344
341
|
await LazyPromise.delay(TTL.divide(4));
|
|
345
|
-
expect(await
|
|
342
|
+
expect(await cache.get("a")).toBe(2);
|
|
346
343
|
});
|
|
347
344
|
test("Should return false when key doesnt exists", async () => {
|
|
348
|
-
expect(await
|
|
345
|
+
expect(await cache.increment("a", 1)).toBe(false);
|
|
349
346
|
});
|
|
350
347
|
test("Should return false when key is expired", async () => {
|
|
351
|
-
await
|
|
348
|
+
await cache.add("a", 1, TTL);
|
|
352
349
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
353
|
-
expect(await
|
|
350
|
+
expect(await cache.increment("a", 1)).toBe(false);
|
|
354
351
|
});
|
|
355
352
|
test("Should not persist increment when key doesnt exists", async () => {
|
|
356
|
-
await
|
|
353
|
+
await cache.increment("a", 1);
|
|
357
354
|
await LazyPromise.delay(TTL.divide(4));
|
|
358
|
-
expect(await
|
|
355
|
+
expect(await cache.get("a")).toBeNull();
|
|
359
356
|
});
|
|
360
357
|
test("Should not persist increment when key is expired", async () => {
|
|
361
|
-
await
|
|
358
|
+
await cache.add("a", 1, TTL);
|
|
362
359
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
363
|
-
await
|
|
364
|
-
expect(await
|
|
360
|
+
await cache.increment("a", 1);
|
|
361
|
+
expect(await cache.get("a")).toBeNull();
|
|
365
362
|
});
|
|
366
363
|
test("Should throw TypeCacheError key value is not number type", async () => {
|
|
367
|
-
await
|
|
364
|
+
await cache.add("a", "str", null);
|
|
368
365
|
await LazyPromise.delay(TTL.divide(4));
|
|
369
|
-
await expect(
|
|
366
|
+
await expect(cache.increment("a", 1)).rejects.toBeInstanceOf(TypeCacheError);
|
|
370
367
|
});
|
|
371
368
|
});
|
|
372
369
|
describe("method: decrement", () => {
|
|
373
370
|
test("Should return true when key exists", async () => {
|
|
374
|
-
await
|
|
371
|
+
await cache.add("a", 1, null);
|
|
375
372
|
await LazyPromise.delay(TTL.divide(4));
|
|
376
|
-
expect(await
|
|
373
|
+
expect(await cache.decrement("a", 1)).toBe(true);
|
|
377
374
|
});
|
|
378
375
|
test("Should persist decrement when key exists", async () => {
|
|
379
|
-
await
|
|
376
|
+
await cache.add("a", 1, null);
|
|
380
377
|
await LazyPromise.delay(TTL.divide(4));
|
|
381
|
-
await
|
|
378
|
+
await cache.decrement("a", 1);
|
|
382
379
|
await LazyPromise.delay(TTL.divide(4));
|
|
383
|
-
expect(await
|
|
380
|
+
expect(await cache.get("a")).toBe(0);
|
|
384
381
|
});
|
|
385
382
|
test("Should return false when key doesnt exists", async () => {
|
|
386
|
-
expect(await
|
|
383
|
+
expect(await cache.decrement("a", 1)).toBe(false);
|
|
387
384
|
});
|
|
388
385
|
test("Should return false when key is expired", async () => {
|
|
389
|
-
await
|
|
386
|
+
await cache.add("a", 1, TTL);
|
|
390
387
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
391
|
-
expect(await
|
|
388
|
+
expect(await cache.decrement("a", 1)).toBe(false);
|
|
392
389
|
});
|
|
393
390
|
test("Should not persist decrement when key doesnt exists", async () => {
|
|
394
|
-
await
|
|
391
|
+
await cache.decrement("a", 1);
|
|
395
392
|
await LazyPromise.delay(TTL.divide(4));
|
|
396
|
-
expect(await
|
|
393
|
+
expect(await cache.get("a")).toBeNull();
|
|
397
394
|
});
|
|
398
395
|
test("Should not persist decrement when key is expired", async () => {
|
|
399
|
-
await
|
|
396
|
+
await cache.add("a", 1, TTL);
|
|
400
397
|
await LazyPromise.delay(TTL.addTimeSpan(TTL.divide(4)));
|
|
401
|
-
await
|
|
402
|
-
expect(await
|
|
398
|
+
await cache.decrement("a", 1);
|
|
399
|
+
expect(await cache.get("a")).toBeNull();
|
|
403
400
|
});
|
|
404
401
|
test("Should throw TypeCacheError key value is not number type", async () => {
|
|
405
|
-
await
|
|
402
|
+
await cache.add("a", "str", null);
|
|
406
403
|
await LazyPromise.delay(TTL.divide(4));
|
|
407
|
-
await expect(
|
|
404
|
+
await expect(cache.decrement("a", 1)).rejects.toBeInstanceOf(TypeCacheError);
|
|
408
405
|
});
|
|
409
406
|
});
|
|
410
407
|
describe("method: remove", () => {
|
|
411
408
|
test("Should return true when key exists", async () => {
|
|
412
|
-
await
|
|
409
|
+
await cache.add("a", 1, null);
|
|
413
410
|
await LazyPromise.delay(TTL.divide(4));
|
|
414
|
-
const result = await
|
|
411
|
+
const result = await cache.remove("a");
|
|
415
412
|
expect(result).toBe(true);
|
|
416
413
|
});
|
|
417
414
|
test("Should persist the key removal when key exists", async () => {
|
|
418
|
-
await
|
|
415
|
+
await cache.add("a", 1, null);
|
|
419
416
|
await LazyPromise.delay(TTL.divide(4));
|
|
420
|
-
await
|
|
417
|
+
await cache.remove("a");
|
|
421
418
|
await LazyPromise.delay(TTL.divide(4));
|
|
422
|
-
expect(await
|
|
419
|
+
expect(await cache.get("a")).toEqual(null);
|
|
423
420
|
});
|
|
424
421
|
});
|
|
425
422
|
describe("method: removeMany", () => {
|
|
426
423
|
test("Should return true when one key exists", async () => {
|
|
427
|
-
await
|
|
424
|
+
await cache.add("a", 1, null);
|
|
428
425
|
await LazyPromise.delay(TTL.divide(4));
|
|
429
|
-
const result = await
|
|
426
|
+
const result = await cache.removeMany(["a", "b", "c"]);
|
|
430
427
|
expect(result).toBe(true);
|
|
431
428
|
});
|
|
432
429
|
test("Should persist removal of the keys that exists", async () => {
|
|
433
|
-
await
|
|
434
|
-
await
|
|
435
|
-
await
|
|
430
|
+
await cache.add("a", 1, null);
|
|
431
|
+
await cache.add("b", 2, null);
|
|
432
|
+
await cache.add("c", 3, null);
|
|
436
433
|
await LazyPromise.delay(TTL.divide(4));
|
|
437
|
-
await
|
|
434
|
+
await cache.removeMany(["a", "b"]);
|
|
438
435
|
await LazyPromise.delay(TTL.divide(4));
|
|
439
436
|
const result = [
|
|
440
|
-
await
|
|
441
|
-
await
|
|
442
|
-
await
|
|
437
|
+
await cache.get("a"),
|
|
438
|
+
await cache.get("b"),
|
|
439
|
+
await cache.get("c"),
|
|
443
440
|
];
|
|
444
441
|
expect(result).toEqual([null, null, 3]);
|
|
445
442
|
});
|
|
446
443
|
});
|
|
447
444
|
describe("method: clear", () => {
|
|
448
445
|
test("Should remove all keys", async () => {
|
|
449
|
-
await
|
|
450
|
-
await
|
|
451
|
-
await
|
|
452
|
-
await
|
|
453
|
-
await
|
|
446
|
+
await cache.add("a", 1);
|
|
447
|
+
await cache.add("b", 2);
|
|
448
|
+
await cache.add("c", 3);
|
|
449
|
+
await cache.add("d", 4);
|
|
450
|
+
await cache.clear();
|
|
454
451
|
const result = [
|
|
455
|
-
await
|
|
456
|
-
await
|
|
457
|
-
await
|
|
458
|
-
await
|
|
452
|
+
await cache.get("a"),
|
|
453
|
+
await cache.get("b"),
|
|
454
|
+
await cache.get("c"),
|
|
455
|
+
await cache.get("d"),
|
|
459
456
|
];
|
|
460
457
|
expect(result).toStrictEqual([null, null, null, null]);
|
|
461
458
|
});
|
|
@@ -465,10 +462,10 @@ export function cacheTestSuite(settings) {
|
|
|
465
462
|
describe("method: exists", () => {
|
|
466
463
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
467
464
|
let event_ = null;
|
|
468
|
-
const unsubscribe = await
|
|
465
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
469
466
|
event_ = event;
|
|
470
467
|
});
|
|
471
|
-
await
|
|
468
|
+
await cache.exists("a");
|
|
472
469
|
await LazyPromise.delay(DELAY_TIME);
|
|
473
470
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
474
471
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -476,11 +473,11 @@ export function cacheTestSuite(settings) {
|
|
|
476
473
|
});
|
|
477
474
|
test("Should dispatch KeyFoundCacheEvent when key exists", async () => {
|
|
478
475
|
let event_ = null;
|
|
479
|
-
const unsubscribe = await
|
|
476
|
+
const unsubscribe = await cache.subscribe(KeyFoundCacheEvent, (event) => {
|
|
480
477
|
event_ = event;
|
|
481
478
|
});
|
|
482
|
-
await
|
|
483
|
-
await
|
|
479
|
+
await cache.add("a", 1);
|
|
480
|
+
await cache.exists("a");
|
|
484
481
|
await LazyPromise.delay(DELAY_TIME);
|
|
485
482
|
expect(event_).toBeInstanceOf(KeyFoundCacheEvent);
|
|
486
483
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -491,10 +488,10 @@ export function cacheTestSuite(settings) {
|
|
|
491
488
|
describe("method: missing", () => {
|
|
492
489
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
493
490
|
let event_ = null;
|
|
494
|
-
const unsubscribe = await
|
|
491
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
495
492
|
event_ = event;
|
|
496
493
|
});
|
|
497
|
-
await
|
|
494
|
+
await cache.missing("a");
|
|
498
495
|
await LazyPromise.delay(DELAY_TIME);
|
|
499
496
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
500
497
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -502,11 +499,11 @@ export function cacheTestSuite(settings) {
|
|
|
502
499
|
});
|
|
503
500
|
test("Should dispatch KeyFoundCacheEvent when key exists", async () => {
|
|
504
501
|
let event_ = null;
|
|
505
|
-
const unsubscribe = await
|
|
502
|
+
const unsubscribe = await cache.subscribe(KeyFoundCacheEvent, (event) => {
|
|
506
503
|
event_ = event;
|
|
507
504
|
});
|
|
508
|
-
await
|
|
509
|
-
await
|
|
505
|
+
await cache.add("a", 1);
|
|
506
|
+
await cache.missing("a");
|
|
510
507
|
await LazyPromise.delay(DELAY_TIME);
|
|
511
508
|
expect(event_).toBeInstanceOf(KeyFoundCacheEvent);
|
|
512
509
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -517,10 +514,10 @@ export function cacheTestSuite(settings) {
|
|
|
517
514
|
describe("method: get", () => {
|
|
518
515
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
519
516
|
let event_ = null;
|
|
520
|
-
const unsubscribe = await
|
|
517
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
521
518
|
event_ = event;
|
|
522
519
|
});
|
|
523
|
-
await
|
|
520
|
+
await cache.get("a");
|
|
524
521
|
await LazyPromise.delay(DELAY_TIME);
|
|
525
522
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
526
523
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -528,11 +525,11 @@ export function cacheTestSuite(settings) {
|
|
|
528
525
|
});
|
|
529
526
|
test("Should dispatch KeyFoundCacheEvent when key exists", async () => {
|
|
530
527
|
let event_ = null;
|
|
531
|
-
const unsubscribe = await
|
|
528
|
+
const unsubscribe = await cache.subscribe(KeyFoundCacheEvent, (event) => {
|
|
532
529
|
event_ = event;
|
|
533
530
|
});
|
|
534
|
-
await
|
|
535
|
-
await
|
|
531
|
+
await cache.add("a", 1);
|
|
532
|
+
await cache.get("a");
|
|
536
533
|
await LazyPromise.delay(DELAY_TIME);
|
|
537
534
|
expect(event_).toBeInstanceOf(KeyFoundCacheEvent);
|
|
538
535
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -543,10 +540,10 @@ export function cacheTestSuite(settings) {
|
|
|
543
540
|
describe("method: getOr", () => {
|
|
544
541
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
545
542
|
let event_ = null;
|
|
546
|
-
const unsubscribe = await
|
|
543
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
547
544
|
event_ = event;
|
|
548
545
|
});
|
|
549
|
-
await
|
|
546
|
+
await cache.getOr("a", 1);
|
|
550
547
|
await LazyPromise.delay(DELAY_TIME);
|
|
551
548
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
552
549
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -554,11 +551,11 @@ export function cacheTestSuite(settings) {
|
|
|
554
551
|
});
|
|
555
552
|
test("Should dispatch KeyFoundCacheEvent when key exists", async () => {
|
|
556
553
|
let event_ = null;
|
|
557
|
-
const unsubscribe = await
|
|
554
|
+
const unsubscribe = await cache.subscribe(KeyFoundCacheEvent, (event) => {
|
|
558
555
|
event_ = event;
|
|
559
556
|
});
|
|
560
|
-
await
|
|
561
|
-
await
|
|
557
|
+
await cache.add("a", 1);
|
|
558
|
+
await cache.getOr("a", 1);
|
|
562
559
|
await LazyPromise.delay(DELAY_TIME);
|
|
563
560
|
expect(event_).toBeInstanceOf(KeyFoundCacheEvent);
|
|
564
561
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -569,11 +566,11 @@ export function cacheTestSuite(settings) {
|
|
|
569
566
|
describe("method: getOrFail", () => {
|
|
570
567
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
571
568
|
let event_ = null;
|
|
572
|
-
const unsubscribe = await
|
|
569
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
573
570
|
event_ = event;
|
|
574
571
|
});
|
|
575
572
|
try {
|
|
576
|
-
await
|
|
573
|
+
await cache.getOrFail("a");
|
|
577
574
|
}
|
|
578
575
|
catch {
|
|
579
576
|
/* Empty */
|
|
@@ -585,11 +582,11 @@ export function cacheTestSuite(settings) {
|
|
|
585
582
|
});
|
|
586
583
|
test("Should dispatch KeyFoundCacheEvent when key exists", async () => {
|
|
587
584
|
let event_ = null;
|
|
588
|
-
const unsubscribe = await
|
|
585
|
+
const unsubscribe = await cache.subscribe(KeyFoundCacheEvent, (event) => {
|
|
589
586
|
event_ = event;
|
|
590
587
|
});
|
|
591
|
-
await
|
|
592
|
-
await
|
|
588
|
+
await cache.add("a", 1);
|
|
589
|
+
await cache.getOrFail("a");
|
|
593
590
|
await LazyPromise.delay(DELAY_TIME);
|
|
594
591
|
expect(event_).toBeInstanceOf(KeyFoundCacheEvent);
|
|
595
592
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -600,11 +597,11 @@ export function cacheTestSuite(settings) {
|
|
|
600
597
|
describe("method: add", () => {
|
|
601
598
|
test("Should dispatch KeyAddedCacheEvent when key doesnt exists", async () => {
|
|
602
599
|
let event_ = null;
|
|
603
|
-
const unsubscribe = await
|
|
600
|
+
const unsubscribe = await cache.subscribe(KeyAddedCacheEvent, (event) => {
|
|
604
601
|
event_ = event;
|
|
605
602
|
});
|
|
606
603
|
const ttl = TimeSpan.fromMilliseconds(20);
|
|
607
|
-
await
|
|
604
|
+
await cache.add("a", 1, ttl);
|
|
608
605
|
await LazyPromise.delay(DELAY_TIME);
|
|
609
606
|
expect(event_).toBeInstanceOf(KeyAddedCacheEvent);
|
|
610
607
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -616,11 +613,11 @@ export function cacheTestSuite(settings) {
|
|
|
616
613
|
describe("method: update", () => {
|
|
617
614
|
test("Should dispatch KeyUpdatedCacheEvent when key exists", async () => {
|
|
618
615
|
let event_ = null;
|
|
619
|
-
const unsubscribe = await
|
|
616
|
+
const unsubscribe = await cache.subscribe(KeyUpdatedCacheEvent, (event) => {
|
|
620
617
|
event_ = event;
|
|
621
618
|
});
|
|
622
|
-
await
|
|
623
|
-
await
|
|
619
|
+
await cache.add("a", 1);
|
|
620
|
+
await cache.update("a", 2);
|
|
624
621
|
await LazyPromise.delay(DELAY_TIME);
|
|
625
622
|
expect(event_).toBeInstanceOf(KeyUpdatedCacheEvent);
|
|
626
623
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -629,10 +626,10 @@ export function cacheTestSuite(settings) {
|
|
|
629
626
|
});
|
|
630
627
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
631
628
|
let event_ = null;
|
|
632
|
-
const unsubscribe = await
|
|
629
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
633
630
|
event_ = event;
|
|
634
631
|
});
|
|
635
|
-
await
|
|
632
|
+
await cache.update("a", 2);
|
|
636
633
|
await LazyPromise.delay(DELAY_TIME);
|
|
637
634
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
638
635
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -642,11 +639,11 @@ export function cacheTestSuite(settings) {
|
|
|
642
639
|
describe("method: put", () => {
|
|
643
640
|
test("Should dispatch KeyAddedCacheEvent when key doesnt exists", async () => {
|
|
644
641
|
let event_ = null;
|
|
645
|
-
const unsubscribe = await
|
|
642
|
+
const unsubscribe = await cache.subscribe(KeyAddedCacheEvent, (event) => {
|
|
646
643
|
event_ = event;
|
|
647
644
|
});
|
|
648
645
|
const ttl = TimeSpan.fromMilliseconds(20);
|
|
649
|
-
await
|
|
646
|
+
await cache.put("a", 1, ttl);
|
|
650
647
|
await LazyPromise.delay(DELAY_TIME);
|
|
651
648
|
expect(event_).toBeInstanceOf(KeyAddedCacheEvent);
|
|
652
649
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -656,11 +653,11 @@ export function cacheTestSuite(settings) {
|
|
|
656
653
|
});
|
|
657
654
|
test("Should dispatch KeyUpdatedCacheEvent when key exists", async () => {
|
|
658
655
|
let event_ = null;
|
|
659
|
-
const unsubscribe = await
|
|
656
|
+
const unsubscribe = await cache.subscribe(KeyUpdatedCacheEvent, (event) => {
|
|
660
657
|
event_ = event;
|
|
661
658
|
});
|
|
662
|
-
await
|
|
663
|
-
await
|
|
659
|
+
await cache.put("a", 1);
|
|
660
|
+
await cache.put("a", 2);
|
|
664
661
|
await LazyPromise.delay(DELAY_TIME);
|
|
665
662
|
expect(event_).toBeInstanceOf(KeyUpdatedCacheEvent);
|
|
666
663
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -671,11 +668,11 @@ export function cacheTestSuite(settings) {
|
|
|
671
668
|
describe("method: remove", () => {
|
|
672
669
|
test("Should dispatch KeyRemovedCacheEvent when key exists", async () => {
|
|
673
670
|
let event_ = null;
|
|
674
|
-
const unsubscribe = await
|
|
671
|
+
const unsubscribe = await cache.subscribe(KeyRemovedCacheEvent, (event) => {
|
|
675
672
|
event_ = event;
|
|
676
673
|
});
|
|
677
|
-
await
|
|
678
|
-
await
|
|
674
|
+
await cache.add("a", 1);
|
|
675
|
+
await cache.remove("a");
|
|
679
676
|
await LazyPromise.delay(DELAY_TIME);
|
|
680
677
|
expect(event_).toBeInstanceOf(KeyRemovedCacheEvent);
|
|
681
678
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -683,10 +680,10 @@ export function cacheTestSuite(settings) {
|
|
|
683
680
|
});
|
|
684
681
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
685
682
|
let event_ = null;
|
|
686
|
-
const unsubscribe = await
|
|
683
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
687
684
|
event_ = event;
|
|
688
685
|
});
|
|
689
|
-
await
|
|
686
|
+
await cache.remove("a");
|
|
690
687
|
await LazyPromise.delay(DELAY_TIME);
|
|
691
688
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
692
689
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -696,11 +693,11 @@ export function cacheTestSuite(settings) {
|
|
|
696
693
|
describe("method: removeMany", () => {
|
|
697
694
|
test("Should dispatch KeyRemovedCacheEvent when key doesnt exists", async () => {
|
|
698
695
|
let event_ = null;
|
|
699
|
-
const unsubscribe = await
|
|
696
|
+
const unsubscribe = await cache.subscribe(KeyRemovedCacheEvent, (event) => {
|
|
700
697
|
event_ = event;
|
|
701
698
|
});
|
|
702
|
-
await
|
|
703
|
-
await
|
|
699
|
+
await cache.add("a", 1);
|
|
700
|
+
await cache.removeMany(["a"]);
|
|
704
701
|
await LazyPromise.delay(DELAY_TIME);
|
|
705
702
|
expect(event_).toBeInstanceOf(KeyRemovedCacheEvent);
|
|
706
703
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -708,10 +705,10 @@ export function cacheTestSuite(settings) {
|
|
|
708
705
|
});
|
|
709
706
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
710
707
|
let event_ = null;
|
|
711
|
-
const unsubscribe = await
|
|
708
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
712
709
|
event_ = event;
|
|
713
710
|
});
|
|
714
|
-
await
|
|
711
|
+
await cache.removeMany(["a"]);
|
|
715
712
|
await LazyPromise.delay(DELAY_TIME);
|
|
716
713
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
717
714
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -721,10 +718,10 @@ export function cacheTestSuite(settings) {
|
|
|
721
718
|
describe("method: getAndRemove", () => {
|
|
722
719
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
723
720
|
let event_ = null;
|
|
724
|
-
const unsubscribe = await
|
|
721
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
725
722
|
event_ = event;
|
|
726
723
|
});
|
|
727
|
-
await
|
|
724
|
+
await cache.getAndRemove("a");
|
|
728
725
|
await LazyPromise.delay(DELAY_TIME);
|
|
729
726
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
730
727
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -732,11 +729,11 @@ export function cacheTestSuite(settings) {
|
|
|
732
729
|
});
|
|
733
730
|
test("Should dispatch KeyFoundCacheEvent when key exists", async () => {
|
|
734
731
|
let event_ = null;
|
|
735
|
-
const unsubscribe = await
|
|
732
|
+
const unsubscribe = await cache.subscribe(KeyFoundCacheEvent, (event) => {
|
|
736
733
|
event_ = event;
|
|
737
734
|
});
|
|
738
|
-
await
|
|
739
|
-
await
|
|
735
|
+
await cache.add("a", 1);
|
|
736
|
+
await cache.getAndRemove("a");
|
|
740
737
|
await LazyPromise.delay(DELAY_TIME);
|
|
741
738
|
expect(event_).toBeInstanceOf(KeyFoundCacheEvent);
|
|
742
739
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -745,11 +742,11 @@ export function cacheTestSuite(settings) {
|
|
|
745
742
|
});
|
|
746
743
|
test("Should dispatch KeyRemovedCacheEvent when key exists", async () => {
|
|
747
744
|
let event_ = null;
|
|
748
|
-
const unsubscribe = await
|
|
745
|
+
const unsubscribe = await cache.subscribe(KeyRemovedCacheEvent, (event) => {
|
|
749
746
|
event_ = event;
|
|
750
747
|
});
|
|
751
|
-
await
|
|
752
|
-
await
|
|
748
|
+
await cache.add("a", 1);
|
|
749
|
+
await cache.getAndRemove("a");
|
|
753
750
|
await LazyPromise.delay(DELAY_TIME);
|
|
754
751
|
expect(event_).toBeInstanceOf(KeyRemovedCacheEvent);
|
|
755
752
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -759,10 +756,10 @@ export function cacheTestSuite(settings) {
|
|
|
759
756
|
describe("method: getOrAdd", () => {
|
|
760
757
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
761
758
|
let event_ = null;
|
|
762
|
-
const unsubscribe = await
|
|
759
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
763
760
|
event_ = event;
|
|
764
761
|
});
|
|
765
|
-
await
|
|
762
|
+
await cache.getOrAdd("a", 1);
|
|
766
763
|
await LazyPromise.delay(DELAY_TIME);
|
|
767
764
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
768
765
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -770,11 +767,11 @@ export function cacheTestSuite(settings) {
|
|
|
770
767
|
});
|
|
771
768
|
test("Should dispatch KeyFoundCacheEvent when key exists", async () => {
|
|
772
769
|
let event_ = null;
|
|
773
|
-
const unsubscribe = await
|
|
770
|
+
const unsubscribe = await cache.subscribe(KeyFoundCacheEvent, (event) => {
|
|
774
771
|
event_ = event;
|
|
775
772
|
});
|
|
776
|
-
await
|
|
777
|
-
await
|
|
773
|
+
await cache.add("a", 1);
|
|
774
|
+
await cache.getOrAdd("a", 1);
|
|
778
775
|
await LazyPromise.delay(DELAY_TIME);
|
|
779
776
|
expect(event_).toBeInstanceOf(KeyFoundCacheEvent);
|
|
780
777
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -783,11 +780,11 @@ export function cacheTestSuite(settings) {
|
|
|
783
780
|
});
|
|
784
781
|
test("Should dispatch KeyAddedCacheEvent when key exists", async () => {
|
|
785
782
|
let event_ = null;
|
|
786
|
-
const unsubscribe = await
|
|
783
|
+
const unsubscribe = await cache.subscribe(KeyAddedCacheEvent, (event) => {
|
|
787
784
|
event_ = event;
|
|
788
785
|
});
|
|
789
786
|
const ttl = TimeSpan.fromMilliseconds(50);
|
|
790
|
-
await
|
|
787
|
+
await cache.getOrAdd("a", 1, ttl);
|
|
791
788
|
await LazyPromise.delay(DELAY_TIME);
|
|
792
789
|
expect(event_).toBeInstanceOf(KeyAddedCacheEvent);
|
|
793
790
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -799,11 +796,11 @@ export function cacheTestSuite(settings) {
|
|
|
799
796
|
describe("method: increment", () => {
|
|
800
797
|
test("Should dispatch KeyIncrementedCacheEvent when key exists", async () => {
|
|
801
798
|
let event_ = null;
|
|
802
|
-
const unsubscribe = await
|
|
799
|
+
const unsubscribe = await cache.subscribe(KeyIncrementedCacheEvent, (event) => {
|
|
803
800
|
event_ = event;
|
|
804
801
|
});
|
|
805
|
-
await
|
|
806
|
-
await
|
|
802
|
+
await cache.add("a", 1);
|
|
803
|
+
await cache.increment("a", 1);
|
|
807
804
|
await LazyPromise.delay(DELAY_TIME);
|
|
808
805
|
expect(event_).toBeInstanceOf(KeyIncrementedCacheEvent);
|
|
809
806
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -812,10 +809,10 @@ export function cacheTestSuite(settings) {
|
|
|
812
809
|
});
|
|
813
810
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
814
811
|
let event_ = null;
|
|
815
|
-
const unsubscribe = await
|
|
812
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
816
813
|
event_ = event;
|
|
817
814
|
});
|
|
818
|
-
await
|
|
815
|
+
await cache.increment("a", 1);
|
|
819
816
|
await LazyPromise.delay(DELAY_TIME);
|
|
820
817
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
821
818
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -825,11 +822,11 @@ export function cacheTestSuite(settings) {
|
|
|
825
822
|
describe("method: decrement", () => {
|
|
826
823
|
test("Should dispatch KeyDecrementedCacheEvent when key exists", async () => {
|
|
827
824
|
let event_ = null;
|
|
828
|
-
const unsubscribe = await
|
|
825
|
+
const unsubscribe = await cache.subscribe(KeyDecrementedCacheEvent, (event) => {
|
|
829
826
|
event_ = event;
|
|
830
827
|
});
|
|
831
|
-
await
|
|
832
|
-
await
|
|
828
|
+
await cache.add("a", 1);
|
|
829
|
+
await cache.decrement("a", 1);
|
|
833
830
|
await LazyPromise.delay(DELAY_TIME);
|
|
834
831
|
expect(event_).toBeInstanceOf(KeyDecrementedCacheEvent);
|
|
835
832
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -838,10 +835,10 @@ export function cacheTestSuite(settings) {
|
|
|
838
835
|
});
|
|
839
836
|
test("Should dispatch KeyNotFoundCacheEvent when key doesnt exists", async () => {
|
|
840
837
|
let event_ = null;
|
|
841
|
-
const unsubscribe = await
|
|
838
|
+
const unsubscribe = await cache.subscribe(KeyNotFoundCacheEvent, (event) => {
|
|
842
839
|
event_ = event;
|
|
843
840
|
});
|
|
844
|
-
await
|
|
841
|
+
await cache.decrement("a", 1);
|
|
845
842
|
await LazyPromise.delay(DELAY_TIME);
|
|
846
843
|
expect(event_).toBeInstanceOf(KeyNotFoundCacheEvent);
|
|
847
844
|
expect(event_?.fields.key).toBe("a");
|
|
@@ -851,209 +848,18 @@ export function cacheTestSuite(settings) {
|
|
|
851
848
|
describe("method: clear", () => {
|
|
852
849
|
test("Should dispatch KeysClearedCacheEvent when key doesnt exists", async () => {
|
|
853
850
|
let event_ = null;
|
|
854
|
-
const unsubscribe = await
|
|
851
|
+
const unsubscribe = await cache.subscribe(KeysClearedCacheEvent, (event) => {
|
|
855
852
|
event_ = event;
|
|
856
853
|
});
|
|
857
|
-
await
|
|
858
|
-
await
|
|
859
|
-
await
|
|
860
|
-
await
|
|
854
|
+
await cache.add("a", 1);
|
|
855
|
+
await cache.add("b", 2);
|
|
856
|
+
await cache.add("c", 3);
|
|
857
|
+
await cache.clear();
|
|
861
858
|
await LazyPromise.delay(DELAY_TIME);
|
|
862
859
|
expect(event_).toBeInstanceOf(KeysClearedCacheEvent);
|
|
863
860
|
await unsubscribe();
|
|
864
861
|
});
|
|
865
862
|
});
|
|
866
863
|
});
|
|
867
|
-
describe("Group tests:", () => {
|
|
868
|
-
test("method: exists", async () => {
|
|
869
|
-
await cacheA.put("a", 1);
|
|
870
|
-
expect(await cacheA.exists("a")).toBe(true);
|
|
871
|
-
expect(await cacheB.exists("a")).toBe(false);
|
|
872
|
-
});
|
|
873
|
-
test("method: missing", async () => {
|
|
874
|
-
await cacheA.put("a", 1);
|
|
875
|
-
expect(await cacheA.missing("a")).toBe(false);
|
|
876
|
-
expect(await cacheB.missing("a")).toBe(true);
|
|
877
|
-
});
|
|
878
|
-
test("method: get", async () => {
|
|
879
|
-
await cacheA.put("a", 1);
|
|
880
|
-
expect(await cacheA.get("a")).toBe(1);
|
|
881
|
-
expect(await cacheB.get("a")).toBeNull();
|
|
882
|
-
});
|
|
883
|
-
test("method: getOr", async () => {
|
|
884
|
-
await cacheA.put("a", 1);
|
|
885
|
-
expect(await cacheA.getOr("a", -1)).toBe(1);
|
|
886
|
-
expect(await cacheB.getOr("a", -1)).toBe(-1);
|
|
887
|
-
});
|
|
888
|
-
test("method: getOrFail", async () => {
|
|
889
|
-
await cacheA.put("a", 1);
|
|
890
|
-
expect(await cacheA.getOrFail("a")).toBe(1);
|
|
891
|
-
await expect(cacheB.getOrFail("a")).rejects.toBeInstanceOf(KeyNotFoundCacheError);
|
|
892
|
-
});
|
|
893
|
-
test("method: add", async () => {
|
|
894
|
-
await cacheA.add("a", 1);
|
|
895
|
-
await cacheB.add("a", 2);
|
|
896
|
-
expect(await cacheA.get("a")).toBe(1);
|
|
897
|
-
expect(await cacheB.get("a")).toBe(2);
|
|
898
|
-
});
|
|
899
|
-
test("method: update", async () => {
|
|
900
|
-
await cacheA.add("a", 1);
|
|
901
|
-
await cacheB.add("a", 1);
|
|
902
|
-
await cacheA.update("a", 2);
|
|
903
|
-
await cacheB.update("a", 3);
|
|
904
|
-
expect(await cacheA.get("a")).toBe(2);
|
|
905
|
-
expect(await cacheB.get("a")).toBe(3);
|
|
906
|
-
});
|
|
907
|
-
test("method: put", async () => {
|
|
908
|
-
await cacheA.put("a", 2);
|
|
909
|
-
await cacheB.put("a", 3);
|
|
910
|
-
expect(await cacheA.get("a")).toBe(2);
|
|
911
|
-
expect(await cacheB.get("a")).toBe(3);
|
|
912
|
-
});
|
|
913
|
-
test("method: remove", async () => {
|
|
914
|
-
await cacheA.add("a", 1);
|
|
915
|
-
await cacheB.add("a", 1);
|
|
916
|
-
await cacheA.remove("a");
|
|
917
|
-
expect(await cacheA.get("a")).toBeNull();
|
|
918
|
-
expect(await cacheB.get("a")).toBe(1);
|
|
919
|
-
});
|
|
920
|
-
test("method: getAndRemove", async () => {
|
|
921
|
-
await cacheA.add("a", 1);
|
|
922
|
-
await cacheB.add("a", 2);
|
|
923
|
-
expect(await cacheA.getAndRemove("a")).toBe(1);
|
|
924
|
-
expect(await cacheA.get("a")).toBeNull();
|
|
925
|
-
expect(await cacheB.get("a")).toBe(2);
|
|
926
|
-
});
|
|
927
|
-
test("method: getOrAdd", async () => {
|
|
928
|
-
await cacheA.getOrAdd("a", 1);
|
|
929
|
-
await cacheB.getOrAdd("a", 2);
|
|
930
|
-
expect(await cacheA.get("a")).toBe(1);
|
|
931
|
-
expect(await cacheB.get("a")).toBe(2);
|
|
932
|
-
});
|
|
933
|
-
test("method: increment", async () => {
|
|
934
|
-
await cacheA.add("a", 1);
|
|
935
|
-
await cacheB.add("a", 1);
|
|
936
|
-
await cacheA.increment("a", 1);
|
|
937
|
-
expect(await cacheA.get("a")).toBe(2);
|
|
938
|
-
expect(await cacheB.get("a")).toBe(1);
|
|
939
|
-
});
|
|
940
|
-
test("method: decrement", async () => {
|
|
941
|
-
await cacheA.add("a", 1);
|
|
942
|
-
await cacheB.add("a", 1);
|
|
943
|
-
await cacheA.decrement("a", 1);
|
|
944
|
-
expect(await cacheA.get("a")).toBe(0);
|
|
945
|
-
expect(await cacheB.get("a")).toBe(1);
|
|
946
|
-
});
|
|
947
|
-
test("method: clear", async () => {
|
|
948
|
-
await cacheA.add("a", 1);
|
|
949
|
-
await cacheA.add("b", 2);
|
|
950
|
-
await cacheB.add("a", 3);
|
|
951
|
-
await cacheB.add("b", 4);
|
|
952
|
-
await cacheA.clear();
|
|
953
|
-
const resultA = [
|
|
954
|
-
await cacheA.get("a"),
|
|
955
|
-
await cacheA.get("b"),
|
|
956
|
-
await cacheB.get("a"),
|
|
957
|
-
await cacheB.get("b"),
|
|
958
|
-
];
|
|
959
|
-
expect(resultA).toEqual([null, null, 3, 4]);
|
|
960
|
-
});
|
|
961
|
-
test("method: addListener / dispatch", async () => {
|
|
962
|
-
let result_a = null;
|
|
963
|
-
await cacheA.addListener(KeyAddedCacheEvent, (event) => {
|
|
964
|
-
result_a = event;
|
|
965
|
-
});
|
|
966
|
-
let result_b = null;
|
|
967
|
-
await cacheB.addListener(KeyAddedCacheEvent, (event) => {
|
|
968
|
-
result_b = event;
|
|
969
|
-
});
|
|
970
|
-
await cacheA.add("a", 1);
|
|
971
|
-
await LazyPromise.delay(DELAY_TIME);
|
|
972
|
-
expect(result_a).toBeInstanceOf(KeyAddedCacheEvent);
|
|
973
|
-
expect(result_b).toBeNull();
|
|
974
|
-
});
|
|
975
|
-
test("method: addListenerMany / dispatch", async () => {
|
|
976
|
-
let result_a = null;
|
|
977
|
-
await cacheA.addListenerMany([KeyAddedCacheEvent], (event) => {
|
|
978
|
-
result_a = event;
|
|
979
|
-
});
|
|
980
|
-
let result_b = null;
|
|
981
|
-
await cacheB.addListenerMany([KeyAddedCacheEvent], (event) => {
|
|
982
|
-
result_b = event;
|
|
983
|
-
});
|
|
984
|
-
await cacheA.add("a", 1);
|
|
985
|
-
await LazyPromise.delay(DELAY_TIME);
|
|
986
|
-
expect(result_a).toBeInstanceOf(KeyAddedCacheEvent);
|
|
987
|
-
expect(result_b).toBeNull();
|
|
988
|
-
});
|
|
989
|
-
test("method: removeListener / addListener / dispatch", async () => {
|
|
990
|
-
let result_a = null;
|
|
991
|
-
await cacheA.addListener(KeyAddedCacheEvent, (event) => {
|
|
992
|
-
result_a = event;
|
|
993
|
-
});
|
|
994
|
-
let result_b = null;
|
|
995
|
-
const listenerB = (event) => {
|
|
996
|
-
result_b = event;
|
|
997
|
-
};
|
|
998
|
-
await cacheB.addListener(KeyAddedCacheEvent, listenerB);
|
|
999
|
-
await cacheB.removeListener(KeyAddedCacheEvent, listenerB);
|
|
1000
|
-
await cacheA.add("a", 1);
|
|
1001
|
-
await cacheB.add("a", 1);
|
|
1002
|
-
await LazyPromise.delay(DELAY_TIME);
|
|
1003
|
-
expect(result_a).toBeInstanceOf(KeyAddedCacheEvent);
|
|
1004
|
-
expect(result_b).toBeNull();
|
|
1005
|
-
});
|
|
1006
|
-
test("method: removeListenerMany / addListener / dispatch", async () => {
|
|
1007
|
-
let result_a = null;
|
|
1008
|
-
await cacheA.addListener(KeyAddedCacheEvent, (event) => {
|
|
1009
|
-
result_a = event;
|
|
1010
|
-
});
|
|
1011
|
-
let result_b = null;
|
|
1012
|
-
const listenerB = (event) => {
|
|
1013
|
-
result_b = event;
|
|
1014
|
-
};
|
|
1015
|
-
await cacheB.addListener(KeyAddedCacheEvent, listenerB);
|
|
1016
|
-
await cacheB.removeListenerMany([KeyAddedCacheEvent], listenerB);
|
|
1017
|
-
await cacheA.add("a", 1);
|
|
1018
|
-
await cacheB.add("a", 1);
|
|
1019
|
-
await LazyPromise.delay(DELAY_TIME);
|
|
1020
|
-
expect(result_a).toBeInstanceOf(KeyAddedCacheEvent);
|
|
1021
|
-
expect(result_b).toBeNull();
|
|
1022
|
-
});
|
|
1023
|
-
test("method: subscribe / dispatch", async () => {
|
|
1024
|
-
let result_a = null;
|
|
1025
|
-
await cacheA.subscribe(KeyAddedCacheEvent, (event) => {
|
|
1026
|
-
result_a = event;
|
|
1027
|
-
});
|
|
1028
|
-
let result_b = null;
|
|
1029
|
-
const listenerB = (event) => {
|
|
1030
|
-
result_b = event;
|
|
1031
|
-
};
|
|
1032
|
-
const unsubscribe = await cacheB.subscribe(KeyAddedCacheEvent, listenerB);
|
|
1033
|
-
await unsubscribe();
|
|
1034
|
-
await cacheA.add("a", 1);
|
|
1035
|
-
await cacheB.add("a", 1);
|
|
1036
|
-
await LazyPromise.delay(DELAY_TIME);
|
|
1037
|
-
expect(result_a).toBeInstanceOf(KeyAddedCacheEvent);
|
|
1038
|
-
expect(result_b).toBeNull();
|
|
1039
|
-
});
|
|
1040
|
-
test("method: subscribeMany / dispatch", async () => {
|
|
1041
|
-
let result_a = null;
|
|
1042
|
-
await cacheA.subscribeMany([KeyAddedCacheEvent], (event) => {
|
|
1043
|
-
result_a = event;
|
|
1044
|
-
});
|
|
1045
|
-
let result_b = null;
|
|
1046
|
-
const listenerB = (event) => {
|
|
1047
|
-
result_b = event;
|
|
1048
|
-
};
|
|
1049
|
-
const unsubscribe = await cacheB.subscribeMany([KeyAddedCacheEvent], listenerB);
|
|
1050
|
-
await unsubscribe();
|
|
1051
|
-
await cacheA.add("a", 1);
|
|
1052
|
-
await cacheB.add("a", 1);
|
|
1053
|
-
await LazyPromise.delay(DELAY_TIME);
|
|
1054
|
-
expect(result_a).toBeInstanceOf(KeyAddedCacheEvent);
|
|
1055
|
-
expect(result_b).toBeNull();
|
|
1056
|
-
});
|
|
1057
|
-
});
|
|
1058
864
|
}
|
|
1059
865
|
//# sourceMappingURL=cache.test-suite.js.map
|