forked from groupon/node-cached
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeout.test.js
83 lines (71 loc) · 2.49 KB
/
timeout.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
'use strict';
const assert = require('assertive');
const Bluebird = require('bluebird');
const identity = val => val;
const Cache = require('../lib/cache');
describe('Cache timeouts', () => {
const cache = new Cache({
backend: {
get() {
return Bluebird.resolve({ d: 'get result' }).delay(150);
},
set() {
return Bluebird.resolve('set result').delay(150);
},
},
name: 'awesome-name',
debug: true,
});
describe('with a timeout <150ms', () => {
before(() => (cache.defaults.timeout = 50));
it('get fails fast', async () => {
const err = await Bluebird.race([
cache.get('my-key').then(null, identity),
Bluebird.delay(100, 'too slow'), // this should not be used
]);
assert.expect(err instanceof Error);
assert.equal('TimeoutError', err.name);
});
it('set fails fast', async () => {
const err = await Bluebird.race([
cache.set('my-key', 'my-value').then(null, identity),
Bluebird.delay(100, 'too slow'), // this should not be used
]);
assert.expect(err instanceof Error);
assert.equal('TimeoutError', err.name);
});
it('getOrElse fails fast', async () => {
const value = await Bluebird.race([
cache.getOrElse('my-key', 'my-value').then(null, identity),
// We need to add a bit of time here because we'll run into the
// timeout twice - once when trying to read and once while writing.
Bluebird.delay(150, 'too slow'), // this should not be used
]);
assert.equal('my-value', value);
});
});
describe('with a timeout >150ms', () => {
before(() => (cache.defaults.timeout = 250));
it('receives the value', async () => {
const value = await Bluebird.race([
cache.get('my-key').then(null, identity),
Bluebird.delay(200, 'too slow'), // this should not be used
]);
assert.equal('get result', value);
});
it('sets the value', async () => {
const value = await Bluebird.race([
cache.set('my-key', 'my-value').then(null, identity),
Bluebird.delay(200, 'too slow'), // this should not be used
]);
assert.equal('set result', value);
});
it('getOrElse can retrieve a value', async () => {
const value = await Bluebird.race([
cache.getOrElse('my-key', 'my-value').then(null, identity),
Bluebird.delay(200, 'too slow'), // this should not be used
]);
assert.equal('get result', value);
});
});
});