Skip to content

Commit 0c60e00

Browse files
Added close/open/destroyed support to index and tests to verify that the events work.
1 parent 3e3fc42 commit 0c60e00

File tree

2 files changed

+132
-8
lines changed

2 files changed

+132
-8
lines changed

index.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ function RASPauseWrapper (ras) {
1010
}
1111
PauseWrapper.call(this, ras)
1212

13-
this.closed = ras.closed
13+
this.opened = false
14+
this.closed = false
15+
this.destroyed = false
1416
this.readable = ras.readable
1517
this.writeable = ras.writeable
1618
this.deletable = ras.deletable
@@ -21,26 +23,43 @@ inherits(RASPauseWrapper, PauseWrapper)
2123

2224
Object.assign(RASPauseWrapper.prototype, {
2325
open: function (cb) {
24-
this._onResumeCb(cb, function (cb2) { this._proxied.open(cb2) })
26+
var scope = this
27+
this._onResumeCb(cb, function (cbProxy) {
28+
this._proxied.open(function (err) {
29+
if (err) return cbProxy(err)
30+
scope.opened = true
31+
cbProxy()
32+
})
33+
})
2534
},
2635
read: function (offset, size, cb) {
27-
this._onResumeCb(cb, function (cb2) { this._proxied.read(offset, size, cb2) })
36+
this._onResumeCb(cb, function (cbProxy) { this._proxied.read(offset, size, cbProxy) })
2837
},
2938
write: function (offset, buffer, cb) {
30-
this._onResumeCb(cb, function (cb2) { this._proxied.write(offset, buffer, cb2) })
39+
this._onResumeCb(cb, function (cbProxy) { this._proxied.write(offset, buffer, cbProxy) })
3140
},
3241
del: function (offset, size, cb) {
33-
this._onResumeCb(cb, function (cb2) { this._proxied.del(offset, size, cb2) })
42+
this._onResumeCb(cb, function (cbProxy) { this._proxied.del(offset, size, cbProxy) })
3443
},
3544
stat: function (cb) {
36-
this._onResumeCb(cb, function (cb2) { this._proxied.stat(cb2) })
45+
this._onResumeCb(cb, function (cbProxy) { this._proxied.stat(cbProxy) })
3746
},
3847
close: function (cb) {
3948
this._closed = true
40-
return this._proxied.close(cb)
49+
var scope = this
50+
return this._proxied.close(function (err) {
51+
if (err) return cb(err)
52+
scope.closed = true
53+
cb()
54+
})
4155
},
4256
destroy: function (cb) {
4357
this._closed = true
44-
return this._proxied.destroy(cb)
58+
var scope = this
59+
return this._proxied.destroy(function (err) {
60+
if (err) return cb(err)
61+
scope.destroyed = true
62+
cb()
63+
})
4564
}
4665
})

index.test.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)