This repository was archived by the owner on Mar 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpromise.test.js
More file actions
104 lines (90 loc) · 2.3 KB
/
promise.test.js
File metadata and controls
104 lines (90 loc) · 2.3 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
import test from 'blue-tape'
import getCloudCache from './utils'
const cache = getCloudCache('promise')
test('cache.set', () => (
cache.set('somekey', 1337)
))
test('cache.get', (t) => (
cache.get('somekey').then((val) => {
t.equal(val, 1337)
})
))
test('cache.del', (t) => (
cache.del('somekey').then(() => (
cache.get('somekey')
.catch((err) => {
t.ok(err instanceof cache.KeyNotExistsError)
t.ok(err instanceof cache.CloudCacheError)
t.equal(err.key, 'somekey')
})
))
))
test('cache.getOrSet', (t) => {
let callCount = 0
const getLeet = () => new Promise((resolve) => {
callCount++
setTimeout(() => resolve(1337), 100)
})
return cache.getOrSet('leetkey', getLeet).then((val) => {
t.equal(callCount, 1)
t.equal(val, 1337)
return cache.getOrSet('leetkey', getLeet).then((val2) => {
t.equal(callCount, 1)
t.equal(val2, 1337)
})
})
})
test('buffer', (t) => {
const buf = Buffer.from('deadbeefcafebabe', 'hex')
const check = (val) => {
t.ok(Buffer.isBuffer(val))
t.equal(val.toString('hex'), 'deadbeefcafebabe')
}
return cache.getOrSet('bufkey', () => buf)
.then((val) => { check(val) })
.then(() => cache.get('bufkey'))
.then((val) => { check(val) })
})
test('object', (t) => {
const obj = {
some: 'object',
with: {
some: 'random',
properties: true,
},
}
return cache.set('obj', obj)
.then(() => cache.get('obj'))
.then((val) => {
t.deepEqual(val, obj)
t.notEqual(val, obj)
})
})
test('ttl works', (t) => {
const ttlval = '1337'
t.plan(2)
cache.set('ttlval', ttlval, { ttl: 0.2 })
.then(() => cache.get('ttlval'))
.then((val) => {
t.equal(val, ttlval)
})
setTimeout(() => {
cache.get('ttlval').catch((err) => {
t.ok(err instanceof cache.KeyNotExistsError)
})
}, 300)
})
test('cache.getOrSet (refresh works)', (t) => {
const getLeet = () => new Promise((resolve) => {
setTimeout(() => resolve(1337), 100)
})
const getEleet = () => new Promise((resolve) => {
setTimeout(() => resolve(31337), 100)
})
return cache.getOrSet('refresh', getLeet).then((val) => {
t.equal(val, 1337)
return cache.getOrSet('refresh', getEleet, { refresh: true }).then((val2) => {
t.equal(val2, 31337)
})
})
})