11import test from 'blue-tape'
22import fs from 'fs'
33import concat from 'concat-stream'
4+ import mem from 'mem'
45
56import getCloudCache from './utils'
67
78const cache = getCloudCache ( 'stream' )
89
9- const poemPath = `${ __dirname } /files/poem.txt`
10- const poem = ( ) => fs . createReadStream ( poemPath )
11- const poemStr = fs . readFileSync ( poemPath )
10+ const file = mem ( ( name ) => {
11+ const filepath = `${ __dirname } /files/${ name } `
12+ const rs = ( ) => fs . createReadStream ( filepath )
13+ const buf = fs . readFileSync ( filepath )
14+ return { path : filepath , rs, buf }
15+ } )
16+
17+ const poem = file ( 'poem.txt' )
18+ const image = file ( 'large.jpg' )
19+
1220// const devnull = () => fs.createWriteStream('/dev/null')
1321
1422test ( 'cache.sets' , ( t ) => {
15- poem ( ) . pipe ( cache . sets ( 'poem' ) ) . on ( 'finish' , ( ) => {
23+ poem . rs ( ) . pipe ( cache . sets ( 'poem' ) ) . on ( 'finish' , ( ) => {
1624 t . end ( )
1725 } )
1826} )
@@ -21,8 +29,8 @@ test('cache.gets', (t) => {
2129 cache
2230 . gets ( 'poem' )
2331 . on ( 'error' , t . fail )
24- . pipe ( concat ( ( str ) => {
25- t . equal ( str . toString ( ) , poemStr . toString ( ) )
32+ . pipe ( concat ( ( buf ) => {
33+ t . equal ( buf . toString ( ) , poem . buf . toString ( ) )
2634 t . end ( )
2735 } ) )
2836} )
@@ -31,11 +39,11 @@ test('cache.getOrSets', (t) => {
3139 let callCount = 0
3240 const getPoemStream = ( ) => {
3341 callCount ++
34- return poem ( )
42+ return poem . rs ( )
3543 }
3644
37- const check = ( str ) => {
38- t . equal ( str . toString ( ) , poemStr . toString ( ) )
45+ const check = ( buf ) => {
46+ t . equal ( buf . toString ( ) , poem . buf . toString ( ) )
3947 t . equal ( callCount , 1 )
4048 }
4149
@@ -56,11 +64,11 @@ test('cache.getOrSets, pipe later', (t) => {
5664 let callCount = 0
5765 const getPoemStream = ( ) => {
5866 callCount ++
59- return poem ( )
67+ return poem . rs ( )
6068 }
6169
6270 const check = ( str ) => {
63- t . equal ( str . toString ( ) , poemStr . toString ( ) )
71+ t . equal ( str . toString ( ) , poem . buf . toString ( ) )
6472 t . equal ( callCount , 1 )
6573 }
6674
@@ -79,3 +87,33 @@ test('cache.getOrSets, pipe later', (t) => {
7987 } ) )
8088 } , 300 )
8189} )
90+
91+ test ( 'cache.getOrSets, refresh=true' , ( t ) => {
92+ const check = ( { buf } , buf2 ) => {
93+ t . ok ( Buffer . compare ( buf , buf2 ) === 0 )
94+ }
95+
96+ const refresh = ( ) => {
97+ cache . getOrSets ( 'refresh' , image . rs , { refresh : true } )
98+ . on ( 'error' , t . fail )
99+ . pipe ( concat ( ( buf ) => {
100+ check ( image , buf )
101+ } ) )
102+ . on ( 'finish' , ( ) => {
103+ cache . gets ( 'refresh' ) . pipe ( concat ( ( buf2 ) => {
104+ check ( image , buf2 )
105+ t . end ( )
106+ } ) )
107+ } )
108+ }
109+
110+ cache . getOrSets ( 'refresh' , poem . rs )
111+ . on ( 'error' , t . fail )
112+ . on ( 'data' , ( ) => { } ) // make sure poem.rs is consumed
113+ . on ( 'end' , ( ) => {
114+ cache . gets ( 'refresh' ) . pipe ( concat ( ( buf ) => {
115+ check ( poem , buf )
116+ refresh ( )
117+ } ) )
118+ } )
119+ } )
0 commit comments