@@ -27,7 +27,16 @@ function processFile (source, cb) {
27
27
} )
28
28
}
29
29
30
+ function wait ( duration ) {
31
+ return function ( ) {
32
+ return new Promise ( function ( resolve ) {
33
+ setTimeout ( resolve , duration )
34
+ } )
35
+ }
36
+ }
37
+
30
38
const fileA = createFile ( 'a.js' , 'var a = 1' )
39
+ const fileB = createFile ( 'b.js' , 'var b = 1' )
31
40
32
41
test ( 'cache one file' , function ( t ) {
33
42
var persist = Promise . promisify ( brfypersist ( tmpTarget , { } ) )
@@ -43,3 +52,29 @@ test('cache one file', function (t) {
43
52
} )
44
53
} )
45
54
} )
55
+
56
+ test ( 'parallel cache should' , function ( t ) {
57
+ var persist = Promise . promisify ( brfypersist ( tmpTarget , { } ) )
58
+ var _firstFallback
59
+ var fallback = function ( source , cb ) {
60
+ if ( ! _firstFallback ) {
61
+ _firstFallback = cb
62
+ return
63
+ }
64
+ setImmediate ( function ( ) {
65
+ // Triggering a writing of the cache in the same tick
66
+ _firstFallback ( null , { first : true } )
67
+ cb ( null , { first : false } )
68
+ } )
69
+ }
70
+ return Promise . all ( [
71
+ persist ( fileB , null , null , fallback ) ,
72
+ persist ( fileB , null , null , fallback )
73
+ ] )
74
+ . then ( wait ( 100 ) ) // The cache file may not have been written yet
75
+ . then ( function ( results ) {
76
+ const cacheFilePath = path . join ( tmpTarget , 'bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f_d63c02b75372e4e64783538bff55c8f18ce4cf0c.json' )
77
+ t . ok ( fs . existsSync ( cacheFilePath ) , 'tmp created' )
78
+ t . same ( JSON . parse ( fs . readFileSync ( cacheFilePath , 'utf-8' ) ) , { first : true } , 'Only the first write succeeded' )
79
+ } )
80
+ } )
0 commit comments