11'use strict' ;
22var assert = require ( 'assert' ) ,
33 path = require ( 'path' ) ,
4+ fs = require ( 'fs' ) ,
45 rimraf = require ( 'rimraf' ) ,
56 Cache = require ( '../lib/cache' ) ;
67
78describe ( 'cache' , function ( ) {
89
910 var opts ;
1011 var filepath = path . join ( __dirname , '/cache' ) ;
12+
13+ var dummy = 'Lorem ipsum dolor sit amet ...\n' ;
14+
1115 beforeEach ( function ( ) {
12- opts = { path : filepath , ttl : 10 } ;
16+ opts = { path : filepath , ttl : 10 } ;
1317 } ) ;
1418
1519 before ( function ( done ) {
@@ -29,24 +33,47 @@ describe('cache', function() {
2933 } ) ;
3034
3135
32- describe ( 'set()' , function ( ) {
33- it ( 'should create new write stream' , function ( ) {
36+ describe ( 'write()' , function ( ) {
37+ var readStream = fs . createReadStream ( path . join ( __dirname , 'dummy.data' ) ) ;
38+
39+ it ( 'should write the file' , function ( done ) {
3440 var cache = new Cache ( opts ) ;
35- var file = cache . write ( '/-/foo/bar.dat' ) ;
36- file . end ( new Buffer ( 'This is a test' ) ) ;
41+ var key = '/-/foo/bar.dat' ;
42+ var pathInfo = cache . getPath ( key ) ;
43+ cache . write ( key , readStream , function ( err , meta ) {
44+ assert . equal ( meta . size , 31 ) ;
45+ assert . equal ( meta . status , 4 ) ;
46+ assert . equal ( fs . readFileSync ( pathInfo . full , 'utf8' ) , dummy ) ;
47+ done ( ) ;
48+ } ) ;
49+ } ) ;
50+
51+ it ( 'should handle locks' , function ( done ) {
52+ var cache = new Cache ( opts ) ;
53+ var readStream = fs . createReadStream ( path . join ( __dirname , 'dummy.data' ) ) ;
54+ var key = '/-/foo/baz.dat' ;
55+ var pathInfo = cache . getPath ( key ) ;
56+ cache . write ( key , readStream , function ( err , meta ) {
57+ assert ( ! cache . locks [ key ] , 'Lock should be released' ) ;
58+ assert ( fs . existsSync ( pathInfo . full ) ) ;
59+ done ( ) ;
60+ } ) ;
61+
62+ assert ( cache . locks [ key ] , 'Lock should be set' ) ;
63+ assert ( ! fs . existsSync ( pathInfo . full ) ) ;
3764 } ) ;
3865 } ) ;
3966
4067
41- describe ( 'get ()' , function ( ) {
68+ describe ( 'read ()' , function ( ) {
4269 it ( 'should create new read stream' , function ( done ) {
4370 var cache = new Cache ( opts ) ;
4471 var readable = cache . read ( '/-/foo/bar.dat' ) ;
4572
4673 readable . setEncoding ( 'utf8' ) ;
4774 readable . on ( 'data' , function ( data ) {
4875 assert . equal ( typeof data , 'string' ) ;
49- assert . equal ( data . toString ( ) , 'This is a test' ) ;
76+ assert . equal ( data . toString ( ) , dummy ) ;
5077 done ( ) ;
5178 } ) ;
5279
@@ -60,7 +87,7 @@ describe('cache', function() {
6087 var cache = new Cache ( opts ) ;
6188 cache . meta ( '/-/foo/bar.dat' , function ( err , meta ) {
6289 if ( err ) return done ( err ) ;
63- assert . equal ( meta . size , 14 ) ;
90+ assert . equal ( meta . size , 31 ) ;
6491 assert . equal ( meta . type , 'application/octet-stream' ) ;
6592 assert . equal ( meta . status , Cache . FRESH ) ;
6693 done ( ) ;
0 commit comments