-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathin-memory-cache.spec.js
More file actions
122 lines (107 loc) · 4.72 KB
/
in-memory-cache.spec.js
File metadata and controls
122 lines (107 loc) · 4.72 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
describe("In memory cache - challenge", () => {
it("should construct a cache with a given size", () => {
const cache = new InMemoryCache(10);
expect(cache).toBeDefined();
expect(cache.capacity).toBe(10);
});
it("should be able to set and get value", () => {
const cache = new InMemoryCache(10);
const data = { keyName: "key1", value: "value1" };
cache.set(data.keyName, data.value);
expect(cache.get(data.keyName)).toBe(data.value);
});
it("should throw an error when trying to set a value with a null key", () => {
const cache = new InMemoryCache(10);
const data = {keyName: null, value: "value1"};
expect(() => cache.set(data.keyName, data.value)).toThrowError("Key must be a non-null string");
});
it("should throw an error when trying to set a value with a null value", () => {
const cache = new InMemoryCache(10);
const data = {keyName: "key1", value: null};
expect(() => cache.set(key, data.value)).toThrowError("Value must be a non-null value");
});
it("should only take buffers as values", () => {
const cache = new InMemoryCache(10);
const data = { keyName: "key1", value: "value1" };
expect(() => cache.set(data.keyName, data.value)).toThrowError("Value must be a buffer");
});
it("should throw an error when trying to set a value with a key that is not a string", () => {
const cache = new InMemoryCache(10);
const data = {keyName: 1, value: "value1"};
expect(() => cache.set(data.keyName, data.value)).toThrowError("Key must be a non-null string");
});
it("should throw an error when trying to set a key that is undefined", () => {
const cache = new InMemoryCache(10);
const data = {keyName: undefined, value: "value1"};
expect(() => cache.set(data.keyName, data.value)).toThrowError("Key must be a non-null string");
});
it("should throw an error when trying to set a value that is undefined", () => {
const cache = new InMemoryCache(10);
const data = {keyName: "key1", value: undefined};
expect(() => cache.set(data.keyName, data.value)).toThrowError("Value must be a non-null value");
});
it("should be able to set and get multiple values", () => {
const cache = new InMemoryCache(10);
const data = [
{ keyName: "key1", value: "value1" },
{ keyName: "key2", value: "value2" },
{ keyName: "key3", value: "value3" }
];
data.forEach(d => cache.set(d.keyName, d.value));
data.forEach(d => expect(cache.get(d.keyName)).toBe(d.value));
});
it("should throw an error when trying to get a non-existing value", () => {
const cache = new InMemoryCache(10);
expect(() => cache.get("key1")).toThrowError("Key not found");
});
it("should throw an error when trying to set a value with a key that already exists", () => {
const cache = new InMemoryCache(10);
const data = { keyName: "key1", value: "value1", overwriteValue: "value2" };
cache.set(data.keyName, data.value);
expect(() => cache.set(data.keyName, data.overwriteValue)).toThrowError("Key already exists");
});
it("should remove the least recently used item when the cache bytes capacity is reached", () => {
const cache = new InMemoryCache(12);
const data = [
{ keyName: "key1", value: "value1" },
{ keyName: "key2", value: "value2" },
{ keyName: "key3", value: "value3" }
];
data.forEach(d => cache.set(d.keyName, d.value));
expect(cache.get(data[0].keyName)).toBeUndefined();
data.slice(1).forEach(d => expect(cache.get(d.keyName)).toBe(d.value));
});
it("should be able to remove a value based on its key", () => {
const cache = new InMemoryCache(10);
const data = [
{ keyName: "key1", value: "value1" },
{ keyName: "key2", value: "value2" },
{ keyName: "key3", value: "value3" }
];
data.forEach(d => cache.set(d.keyName, d.value));
cache.remove(data[0].keyName);
expect(cache.get(data[0].keyName)).toBeUndefined();
data.slice(1).forEach(d => expect(cache.get(d.keyName)).toBe(d.value));
});
it("should be able to remove all values", () => {
const cache = new InMemoryCache(10);
const data = [
{ keyName: "key1", value: "value1" },
{ keyName: "key2", value: "value2" },
{ keyName: "key3", value: "value3" }
];
data.forEach(d => cache.set(d.keyName, d.value));
cache.clear();
data.forEach(d => expect(cache.get(d.keyName)).toBeUndefined());
});
it("should be able to get current capacity", () => {
const cache = new InMemoryCache(10);
expect(cache.getCapacity()).toBe(10);
});
it("should be able to set new capacity", () => {
const cache = new InMemoryCache(10);
expect(cache.getCapacity()).toBe(10);
cache.setCapacity(12);
expect(cache.getCapacity()).toBe(12);
});
});