-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest.js
237 lines (187 loc) · 5.56 KB
/
test.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
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
const test = require('ava');
const Cache = require('./src/index');
const sleep = ms => new Promise(r => setTimeout(r, ms));
test('initializes a cache correctly', t => {
const cache = new Cache(3);
t.is(cache.limit, 3);
t.is(cache.size, 0);
t.is(typeof cache, 'object')
});
test('sets new keys in cache without expiry', t => {
const cache = new Cache(3);
cache.set("Sapiens", 5);
t.is(Object.keys(cache.hashMap).length, 1);
cache.set("Book Thief", 4);
t.is(Object.keys(cache.hashMap).length, 2);
cache.set("Catcher In The Rye", 0);
t.is(Object.keys(cache.hashMap).length, 3);
cache.set("Thus Spoke Zarathustra", 4);
t.is(Object.keys(cache.hashMap).length, 3);
t.is(cache.hashMap.Sapiens, undefined);
});
test('get key without expiry', async t => {
const cache = new Cache(3);
cache.set("Sapiens", 5);
t.is(Object.keys(cache.hashMap).length, 1);
cache.set("Book Thief", 4);
t.is(Object.keys(cache.hashMap).length, 2);
cache.set("Catcher In The Rye", 0);
t.is(Object.keys(cache.hashMap).length, 3);
await sleep(10);
const sapiensRating = cache.get("Sapiens");
t.is(sapiensRating, 5);
cache.set("Thus Spoke Zarathustra", 4);
t.is(Object.keys(cache.hashMap).length, 3);
t.is(cache.hashMap.Sapiens.content.value, 5);
t.is(cache.hashMap["Book Thief"], undefined);
});
test('sets with expiry', t => {
const cache = new Cache(3, 10);
cache.set("Sapiens", 5);
t.is(cache.maxAge, 10);
t.is(cache.hashMap.Sapiens.maxAge, 10);
cache.set("Book Thief", 4, 7);
t.is(cache.hashMap["Book Thief"].maxAge, 7);
cache.set("Catcher In The Rye", 0);
t.is(cache.hashMap["Catcher In The Rye"].maxAge, 10);
});
test('throws for undefinded key and value', t => {
const cache = new Cache(3, 10);
const errorKey = t.throws(() => {
cache.set(undefined, 5);
});
t.is(errorKey.message, 'Key not provided');
const errorValue = t.throws(() => {
cache.set('Book Thief', undefined);
});
t.is(errorValue.message, 'Value not provided');
});
test('gets with expiry', async t => {
const cache = new Cache(3, 10);
cache.set("Sapiens", 5);
const sapiens = cache.get("Sapiens");
t.is(sapiens, 5);
await sleep(3);
const sapiensAfter3s = await cache.get("Sapiens");
t.is(sapiensAfter3s, 5);
await sleep(11);
const sapiensAfter11s = await cache.get("Sapiens");
t.is(sapiensAfter11s, null);
t.is(cache.hashMap.Sapiens, undefined);
cache.set("Book Thief", 4, 25);
const bookThief = cache.get("Book Thief");
t.is(bookThief, 4);
await sleep(10);
const bookThiefAfter10s = await cache.get("Book Thief");
t.is(bookThiefAfter10s, 4);
await sleep(10);
const bookThiefAfter20s = await cache.get("Book Thief");
t.is(bookThiefAfter20s, 4);
await sleep(10);
const bookThiefAfter30s = await cache.get("Book Thief");
t.is(bookThiefAfter30s, 4);
await sleep(30);
const bookThiefAfter30sWithoutReset = await cache.get("Book Thief");
t.is(bookThiefAfter30sWithoutReset, null);
});
test('stale', async t => {
const cache = new Cache(3, 10, true);
cache.set("Sapiens", 5);
await sleep(11)
t.is(cache.get("Sapiens"), 5);
t.is(cache.get("Sapiens"), null);
t.is(cache.hashMap.Sapiens, undefined);
});
test('peek', t => {
const cache = new Cache(3);
cache.set("Sapiens", 5);
const sapiens = cache.peek("Sapiens");
t.is(sapiens, 5);
const test = cache.peek("test");
t.is(test, null);
cache.set("Book Thief", 4);
cache.set("Catcher In The Rye", 0);
cache.peek("test");
cache.set("Thus Spoke Zarathustra", 4);
t.is(cache.hashMap["Sapiens"], undefined);
});
test('reset', t => {
const cache = new Cache(3, 10, true);
cache.set("Sapiens", 5);
cache.set("Book Thief", 4);
cache.set("Catcher In The Rye", 0);
cache.reset();
t.is(cache.size, 0);
t.is(cache.limit, 3);
t.is(cache.maxAge, 10);
t.is(cache.stale, true);
t.deepEqual(cache.hashMap, {});
t.is(cache.head, null);
t.is(cache.tail, null);
});
test('toArray', t => {
const cache = new Cache(3, 10, true);
cache.set("Sapiens", 5);
cache.set("Book Thief", 4);
cache.set("Catcher In The Rye", 0);
t.deepEqual(cache.toArray(), [{key: 'Catcher In The Rye', value: 0},
{key: 'Book Thief', value: 4},
{key: 'Sapiens', value: 5}])
});
test('contains', t => {
const cache = new Cache(3, 10, true);
cache.set("Sapiens", 5);
cache.set("Book Thief", 4);
cache.set("Catcher In The Rye", 0);
t.is(cache.contains("Sapiens"), true);
t.is(cache.contains("x"), false);
t.is(cache.contains("Catcher In The Rye"), true);
});
test('has', t => {
const cache = new Cache(3, 10, true);
cache.set("Sapiens", 5);
cache.set("Book Thief", 4);
cache.set("Catcher In The Rye", 0);
t.is(cache.has("Sapiens"), true);
t.is(cache.has("x"), false);
t.is(cache.has("Catcher In The Rye"), true);
});
test('forEach', t => {
const cache = new Cache(3, 10, true);
cache.set("Sapiens", 5);
cache.set("Book Thief", 4);
cache.set("Catcher In The Rye", 0);
cache.forEach((key, value, index) => {
if(index === 0){
t.is(key, "Catcher In The Rye");
t.is(value, 0);
}
if(index === 1){
t.is(key, "Book Thief");
t.is(value, 4);
}
if(index === 2){
t.is(key, "Sapiens");
t.is(value, 5);
}
})
});
test('getSize', t => {
const cache = new Cache(3, 10, true);
cache.set("Sapiens", 5);
t.is(cache.getSize(), 1);
cache.set("Book Thief", 4);
t.is(cache.getSize(), 2);
cache.set("Catcher In The Rye", 0);
t.is(cache.getSize(), 3);
});
test('delete', t => {
const cache = new Cache(3, 10, true);
cache.set("Sapiens", 5);
cache.set("Book Thief", 4);
cache.set("Catcher In The Rye", 0);
cache.delete("Catcher In The Rye");
t.is(cache.size, 2);
t.is(cache.hashMap["Catcher In The Rye"], undefined);
t.is(cache.head.content.key, "Book Thief")
});