forked from groupon/node-cached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-set.test.js
84 lines (73 loc) · 2.41 KB
/
get-set.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
'use strict';
const assert = require('assertive');
const Bluebird = require('bluebird');
const withBackends = require('./_backends');
describe('Cache::{get,set,unset}', () => {
withBackends(cache => {
it('get/set (callback style)', done => {
cache.set('callback-key', 'callback-value', setError => {
if (setError) return done(setError);
cache.get('callback-key', (getError, value) => {
if (getError) return done(getError);
let assertError = null;
try {
assert.equal('callback-value', value);
} catch (error) {
assertError = error;
}
return done(assertError);
});
return null;
});
});
it('get/set (promise style)', async () => {
await cache.set('promise-key', 'promise-value', { expire: 1 });
assert.equal('promise-value', await cache.get('promise-key'));
});
it('set/unset (callback style', done => {
cache.set('callback-key', 'callback-value', setError => {
if (setError) return done(setError);
cache.unset('callback-key', unsetError => {
if (unsetError) return done(unsetError);
cache.get('callback-key', (getError, value) => {
if (getError) return done(getError);
let assertError = null;
try {
assert.equal(null, value);
} catch (error) {
assertError = error;
}
return done(assertError);
});
return null;
});
return null;
});
});
it('set/unset (promise style)', async () => {
await cache.set('promise-key', 'promise-value', { expire: 1 });
await cache.unset('promise-key');
assert.equal(null, await cache.get('promise-key'));
});
it('honors expires', async () => {
const values = {
key1: 'Value 1',
key2: 'Value 2',
key3: 'Value 3',
};
await Bluebird.all([
cache.set('key1', values.key1, { expire: 1 }),
cache.set('key2', values.key2, { expire: 0 }),
cache.set('key3', values.key3, { expire: 4 }),
]);
await Bluebird.delay(2000);
const [expired, eternal, hit] = await Bluebird.map(
['key', 'key2', 'key3'],
key => cache.get(key)
);
assert.equal(null, expired);
assert.equal(values.key2, eternal);
assert.equal(values.key3, hit);
});
});
});