-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
346 lines (263 loc) · 9.36 KB
/
index.test.js
File metadata and controls
346 lines (263 loc) · 9.36 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import t from 'tap';
import { Cache } from './index.js';
import * as fs from 'fs';
// fs.rmSync('./cache', { recursive: true, force: true });
t.skip('testing non-existent queries', async (t) => {
// Create a semantic cache with a 1 sec time to live
const cache = new Cache({
ttl: 1000
});
await cache.init();
let d;
d = await cache.queries({});
t.same(d.length, 0, "QUERIES one query in cache");
d = await cache.get();
t.equal(d, false, "GET no query provided");
d = await cache.set();
t.equal(d, false, "SET no query provided");
d = await cache.set({ query: "Query with no response" });
t.equal(d, false, "SET no response provided");
d = await cache.get({ query: "non-existing query" });
t.throws(d, false, "GET non-existing query");
d = await cache.rm();
t.throws(d, false, "RM non-existing query");
d = await cache.del();
t.throws(d, false, "DEL non-existing query");
d = await cache.delete();
t.throws(d, false, "DELETE non-existing query");
t.end();
});
t.skip('testing non-semantic cache', async (t) => {
// Create a new semantic cache with a 1 sec time to live
const cache = new Cache({
ttl: 1000
});
await cache.init();
let d = await cache.set({
segment: 'foo',
query: 'What is the speed of a turtle?',
response: 250
});
t.equal(d.response, 250, "SET non-semantic query");
d = await cache.get({
segment: 'foo',
query: 'What is the speed of a turtle?'
});
t.equal(d.response, 250, "GET non-semantic query");
t.equal(await cache.has(), false, "HAS query required to located it");
t.equal(await cache.has({}), false, "HAS query required to located it");
t.equal(
await cache.has({
segment: 'foo',
query: 'What is the speed of a turtle?'
}),
true,
"HAS non-semantic query"
);
t.same(
await cache.queries({ segment: 'foo' }),
[ 'What is the speed of a turtle?' ],
"QUERIES non-semantic"
);
t.end();
});
t.skip('testing semantic cache', async (t) => {
// Create a new semantic cache with a 1 sec time to live
const cache = new Cache({
ttl: 1000
});
await cache.init();
let d = await cache.set({
query: 'What is the speed of a turtle?',
response: 250,
isSemantic: true
});
t.equal(d.response, 250, "SET semantic query");
d = await cache.get({
query: 'What is the speed of a turtle?',
isSemantic: true
});
t.equal(d.response, 250, "GET semantic query");
d = await cache.get({
query: 'How fast does a turtle move?',
isSemantic: true
});
t.equal(d.response, 250, "GET semantic query");
d = await cache.has({
query: 'What is the speed of a turtle?'
});
t.equal(d, false, "HAS non-semantic query");
d = await cache.set({
query: 'What is the speed of a swallow?',
response: 500,
isSemantic: true
});
t.equal(d.response, 500, "SET semantic query");
d = await cache.get({
query: 'How fast does the swallow fly?',
isSemantic: true
});
t.equal(d.response, 500, "GET semantic query");
})
// t.test('testing semantic cache', async (t) => {
// // Create a new semantic cache with a 1 sec time to live
// const cache = new Cache({
// ttl: 1000
// });
// await cache.init();
// let query;
// let response;
// let isSemantic = true;
// let d;
// query = 'What is the speed of a turtle?';
// response = 250;
// d = await cache.set({ query, response, isSemantic });
// t.equal(d.response, response, "6. SET semantic query");
// d = await cache.get({ query, isSemantic });
// t.equal(d.response, response, "7. GET semantic query");
// d = await cache.get({
// query: 'How fast does a turtle move?',
// isSemantic
// });
// t.equal(d.response, response, "7. GET semantic query");
// t.equal(await cache.has({ query }), true, "10. HAS non-semantic query");
// query = 'What is the speed of a swallow?';
// response = 500;
// d = await cache.set({ query, response, isSemantic });
// t.equal(d.response, response, "9. SET semantic query");
// d = await cache.get({
// query: 'How fast does the swallow fly?',
// isSemantic
// });
// t.equal(d.response, response, "12. GET semantic query");
// setTimeout(async function() {
// const d = await cache.get({
// query: 'What is the speed of a swallow?',
// isSemantic
// });
// t.equal(d, false, "13. GET wait 1.1s so the expired query is removed");
// }, 1100);
// query = 'Capital of France';
// response = 'Paris';
// d = await cache.set({ query, response });
// t.equal(d.response, response, "14. SET non-semantic query");
// d = await cache.queries({});
// t.same(
// d,
// [ "Capital of France" ],
// "15. QUERIES list non-semantic queries in cache"
// );
// d = await cache.queries({ isSemantic });
// t.same(
// d,
// [
// "What is the speed of a swallow?",
// "What is the speed of a turtle?"
// ],
// "16. QUERIES list semantic queries in cache"
// );
// d = await cache.del({ query });
// t.equal(d, true, "17. DEL non-semantic query");
// query = 'Water boils at';
// response = 100;
// d = await cache.set({ query, response });
// t.equal(d.response, response, "18. SET non-semantic query");
// d = await cache.delete({});
// t.equal(d, false, "19. DELETE no query provided");
// d = await cache.delete({ query });
// t.equal(d, true, "20. DELETE non-semantic query");
// d = await cache.set({ query, response });
// t.equal(d.response, response, "21. SET non-semantic query");
// setTimeout(async function() {
// const d = await cache.get({ query });
// t.equal(d, false, "22. GET wait 1s, then remove expired query");
// }, 1100);
// d = await cache.prune({});
// t.equal(d, 0, "23. PRUNE remove expired non-semantic queries");
// setTimeout(async function() {
// const d = await cache.queries({});
// t.same(d, [], "24. QUERIES wait 5 seconds, then list queries in cache");
// }, 5000);
// d = await cache.set({ query, response });
// t.equal(d.response, 100, "25. SET non-semantic query");
// setTimeout(async function() {
// const d = await cache.prune({});
// t.equal(d, 0, "26. PRUNE wait 1s, then remove expired non-semantic queries");
// }, 1100);
// query = 'How long does time live?';
// response = 'forever';
// ttl = -1;
// d = await cache.set({ query, response, ttl });
// t.equal(d.response, 'forever', "27. SET non-semantic forever query");
// setTimeout(async function() {
// const d = await cache.prune({});
// t.equal(d, 0, "28. PRUNE wait 1s, then remove expired non-semantic queries");
// }, 1100);
// t.end();
// })
t.skip('testing cache expiry', async (t) => {
t.plan(3);
// Create a new semantic cache with a 1 sec time to live
const cache = new Cache({
ttl: 1000
});
await cache.init();
let query = 'What is the speed of a turtle?';
let response = 250;
let isSemantic = true;
let d = await cache.set({ query, response, isSemantic });
t.equal(d.response, response, "SET semantic query");
d = await cache.get({
query: 'How fast does the turtle move?',
isSemantic
});
t.equal(d.response, response, "GET semantic query");
setTimeout(async function() {
const d = await cache.get({ query, isSemantic });
t.equal(d, false, "GET wait 1.1s so the expired query is removed");
}, 1100);
});
t.skip('testing cache prune', async (t) => {
t.plan(4);
// Create a new semantic cache with a 1 sec time to live
const cache = new Cache({
ttl: 1000
});
await cache.init();
let isSemantic = true;
let d = await cache.set({
query: 'What is the speed of a turtle?',
response: 250,
isSemantic
});
t.equal(d.response, 250, "SET semantic query");
d = await cache.set({
query: 'What is the boiling point of water?',
response: 100
});
t.equal(d.response, 100, "SET semantic query");
d = await cache.prune({ isSemantic });
t.equal(d, 0, "PRUNE expired semantic queries");
setTimeout(async () => {
const d = await cache.prune({ });
t.equal(d, 1, "PRUNE expired non-semantic queries");
}, 1100)
});
// t.test('testing cache', async (t) => {
// // Create a new semantic cache with a 1 sec time to live
// const cache = new Cache({
// ttl: 1000
// });
// await cache.init();
// let res = await cache.set('What is the speed of a turtle?', 'slow');
// t.equal(res, 'slow', "set query 1");
// res = await cache.set('Boiling point of water', 100);
// t.equal(res, 100, "set query 2");
// setTimeout(async function() {
// res = await cache.prune();
// t.equal(res, 2, "prune expired queries");
// }, 1100);
// res = await cache.queries();
// t.equal(res.length, 0, "queries in cache");
// t.end();
// });