@@ -72,3 +72,108 @@ tape('buffer constructor', function (t) {
7272 t . end ( )
7373 } )
7474} )
75+
76+ tape ( 'pause after read' , function ( t ) {
77+ const mem = ram ( Buffer . from ( 'contents' ) )
78+ const file = pauseWrap ( mem )
79+ const stack = [ ]
80+ const _read = mem . read
81+ mem . read = function ( offset , size , cb ) {
82+ stack . push ( 'read() - internal' )
83+ _read . call ( mem , offset , size , cb )
84+ }
85+
86+ t . equals ( file . pauseable , true )
87+ t . equals ( file . paused , false )
88+ stack . push ( '.paused = true' )
89+ stack . push ( 'read()#1' )
90+ file . read ( 0 , 7 , function readCb1 ( err , buf ) {
91+ stack . push ( 'readCb1()' )
92+ t . error ( err )
93+ t . deepEqual ( buf , Buffer . from ( 'content' ) )
94+ } )
95+ file . paused = true
96+ stack . push ( 'read()#2' )
97+ file . read ( 0 , 7 , function readCb ( err , buf ) {
98+ stack . push ( 'readCb2()' )
99+ t . error ( err )
100+ t . deepEqual ( buf , Buffer . from ( 'content' ) )
101+ t . deepEqual ( stack , [
102+ '.paused = true' ,
103+ 'read()#1' ,
104+ 'read() - internal' ,
105+ 'read()#2' ,
106+ 'setTimeout()' ,
107+ '.paused = false' ,
108+ 'read() - internal' ,
109+ 'readCb1()' ,
110+ 'readCb2()'
111+ ] )
112+ t . end ( )
113+ } )
114+ stack . push ( 'setTimeout()' )
115+ setTimeout ( function ( ) {
116+ stack . push ( '.paused = false' )
117+ file . paused = false
118+ } , 10 )
119+ } )
120+
121+ tape ( 'open/close/destroy' , function ( t ) {
122+ const mem = ram ( )
123+ const file = pauseWrap ( mem )
124+ testState ( false , false , false )
125+ file . open ( function ( err ) {
126+ t . error ( err , 'open successful' )
127+ testState ( true , false , false )
128+ file . close ( function ( err ) {
129+ t . error ( err , 'close successful' )
130+ testState ( true , true , false )
131+ file . destroy ( function ( err ) {
132+ t . error ( err , 'destroy successful' )
133+ testState ( true , true , true )
134+ t . end ( )
135+ } )
136+ } )
137+ } )
138+ testState ( false , false , false )
139+ function testState ( opened , closed , destroyed ) {
140+ t . equals ( file . opened , opened , 'file.opened == ' + opened )
141+ t . equals ( file . closed , closed , 'file.closed == ' + closed )
142+ t . equals ( file . destroyed , destroyed , 'file.destroyed == ' + destroyed )
143+ t . equals ( mem . opened , opened , 'mem.opened == ' + opened )
144+ t . equals ( mem . closed , closed , 'mem.closed == ' + closed )
145+ t . equals ( mem . destroyed , destroyed , 'mem.destroyed == ' + destroyed )
146+ }
147+ } )
148+
149+ tape ( 'open/close while pause' , function ( t ) {
150+ const ram = pauseRam ( )
151+ const stack = [ ]
152+ stack . push ( 'pause()' )
153+ ram . pause ( )
154+ stack . push ( 'open()' )
155+ ram . open ( function openCb ( ) {
156+ stack . push ( 'openCb()' )
157+ stack . push ( 'pause()' )
158+ ram . pause ( )
159+ stack . push ( 'close()' )
160+ ram . close ( function ( ) {
161+ t . equals ( ram . closed , true , 'now it is closed' )
162+ t . equals ( ram . paused , true , 'close works while pause' )
163+ t . deepEqual ( stack , [
164+ 'pause()' ,
165+ 'open()' ,
166+ 'resume()' ,
167+ 'openCb()' ,
168+ 'pause()' ,
169+ 'close()'
170+ ] )
171+ t . end ( )
172+ } )
173+ } )
174+ setTimeout ( function ( ) {
175+ t . equals ( ram . opened , false )
176+ stack . push ( 'resume()' )
177+ ram . resume ( )
178+ } , 10 )
179+ } )
0 commit comments