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 pathstream.test.js
More file actions
123 lines (106 loc) · 2.64 KB
/
stream.test.js
File metadata and controls
123 lines (106 loc) · 2.64 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import test from 'blue-tape'
import fs from 'fs'
import concat from 'concat-stream'
import mem from 'mem'
import getCloudCache from './utils'
const cache = getCloudCache('stream')
const file = mem((name) => {
const filepath = `${__dirname}/files/${name}`
const rs = () => fs.createReadStream(filepath)
const buf = fs.readFileSync(filepath)
return { path: filepath, rs, buf }
})
const poem = file('poem.txt')
const image = file('large.jpg')
// const devnull = () => fs.createWriteStream('/dev/null')
test('cache.sets', (t) => {
poem.rs().pipe(cache.sets('poem'))
.on('error', t.fail)
.on('finish', () => {
t.end()
})
})
test('cache.gets', (t) => {
cache
.gets('poem')
.on('error', t.fail)
.pipe(concat((buf) => {
t.equal(buf.toString(), poem.buf.toString())
t.end()
}))
})
test('cache.getOrSets', (t) => {
let callCount = 0
const getPoemStream = () => {
callCount++
return poem.rs()
}
const check = (buf) => {
t.equal(buf.toString(), poem.buf.toString())
t.equal(callCount, 1, 'getPoemStream only called once')
}
cache
.getOrSets('poem-get-or-sets', getPoemStream)
.on('finish', () => {
cache
.getOrSets('poem-get-or-sets', getPoemStream)
.pipe(concat((str2) => {
check(str2)
t.end()
}))
})
.pipe(concat((str) => {
check(str)
}))
})
test('cache.getOrSets, pipe later', (t) => {
let callCount = 0
const getPoemStream = () => {
callCount++
return poem.rs()
}
const check = (str) => {
t.equal(str.toString(), poem.buf.toString())
t.equal(callCount, 1)
}
const rs = cache.getOrSets('poem-get-or-sets-pipe-later', getPoemStream)
setTimeout(() => {
rs.pipe(concat((buf) => {
check(buf)
cache
.get('poem-get-or-sets-pipe-later')
.then((buf2) => {
check(buf2)
t.end()
})
.catch(t.fail)
}))
}, 300)
})
test('cache.getOrSets, refresh=true', (t) => {
const check = ({ buf }, buf2) => {
t.ok(Buffer.compare(buf, buf2) === 0)
}
const refresh = () => {
cache.getOrSets('refresh', image.rs, { refresh: true })
.on('error', t.fail)
.on('finish', () => {
cache.gets('refresh').pipe(concat((buf2) => {
check(image, buf2)
t.end()
}))
})
.pipe(concat((buf) => {
check(image, buf)
}))
}
cache.getOrSets('refresh', poem.rs)
.on('error', t.fail)
.on('data', () => {}) // make sure poem.rs is consumed
.on('finish', () => {
cache.gets('refresh').pipe(concat((buf) => {
check(poem, buf)
refresh()
}))
})
})