forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiItemCache.unittest.js
65 lines (59 loc) · 1.87 KB
/
MultiItemCache.unittest.js
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
"use strict";
const Cache = require("../lib/Cache");
const { ItemCacheFacade, MultiItemCache } = require("../lib/CacheFacade");
describe("MultiItemCache", () => {
it("Throws when getting items from an empty Cache", () => {
const multiItemCache = new MultiItemCache(generateItemCaches(0));
expect(() => multiItemCache.get(_ => _())).toThrowError();
});
it("Returns the single ItemCacheFacade when passed an array of length 1", () => {
const itemCaches = generateItemCaches(1);
const multiItemCache = new MultiItemCache(itemCaches);
expect(multiItemCache).toBe(itemCaches[0]);
});
it("Retrieves items from the underlying Cache when get is called", () => {
const itemCaches = generateItemCaches(10);
const multiItemCache = new MultiItemCache(itemCaches);
const callback = (err, res) => {
expect(err).toBeNull();
expect(res).toBeInstanceOf(Object);
};
for (let i = 0; i < 10; ++i) {
multiItemCache.get(callback);
}
});
it("Can get() a large number of items without exhausting the stack", () => {
const itemCaches = generateItemCaches(10000, () => undefined);
const multiItemCache = new MultiItemCache(itemCaches);
let callbacks = 0;
const callback = (err, res) => {
expect(err).toBeNull();
expect(res).toBeUndefined();
++callbacks;
};
multiItemCache.get(callback);
expect(callbacks).toEqual(1);
});
function generateItemCaches(howMany, dataGenerator) {
const ret = [];
for (let i = 0; i < howMany; ++i) {
const name = `ItemCache${i}`;
const tag = `ItemTag${i}`;
const dataGen =
dataGenerator ||
(() => {
return { name: tag };
});
const cache = new Cache();
cache.hooks.get.tapAsync(
"DataReturner",
(_identifier, _etag, _gotHandlers, callback) => {
callback(undefined, dataGen());
}
);
const itemCache = new ItemCacheFacade(cache, name, tag);
ret[i] = itemCache;
}
return ret;
}
});