-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
55 lines (51 loc) · 1.07 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
import test from 'tapava';
import AsyncCache from './lib';
import Promise from 'bluebird';
test('AsyncCache', function * (t) {
let count = 0;
const cache = new AsyncCache({
load: key => {
count++;
return Promise.resolve(key);
}
});
t.is(yield cache.get(123), 123);
t.is(count, 1);
t.is(yield cache.get(123), 123);
t.is(count, 1);
t.is(yield cache.get(234), 234);
t.is(count, 2);
cache.set(345, 345);
t.is(count, 2);
t.is(yield cache.get(345), 345);
});
test('AsyncCache, using promises', t => {
let count = 0;
const cache = new AsyncCache({
load: key => {
count++;
return Promise.resolve(key);
}
});
return cache.get(123)
.then(res => {
t.is(res, 123);
t.is(count, 1);
return cache.get(123);
})
.then(res => {
t.is(res, 123);
t.is(count, 1);
return cache.get(234);
})
.then(res => {
t.is(res, 234);
t.is(count, 2);
cache.set(345, 345);
return cache.get(345);
})
.then(res => {
t.is(res, 345);
t.is(count, 2);
});
});