@@ -12,6 +12,28 @@ const pEvent = require('p-event')
12
12
const env = require ( 'ipfs-utils/src/env' )
13
13
const IPFS = require ( '../../src/core' )
14
14
15
+ // We need to detect when a readLock or writeLock is requested for the tests
16
+ // so we override the Mutex class to emit an event
17
+ const EventEmitter = require ( 'events' )
18
+ const Mutex = require ( '../../src/utils/mutex' )
19
+
20
+ class MutexEmitter extends Mutex {
21
+ constructor ( repoOwner ) {
22
+ super ( repoOwner )
23
+ this . emitter = new EventEmitter ( )
24
+ }
25
+
26
+ readLock ( lockedFn , cb ) {
27
+ this . emitter . emit ( 'readLock request' )
28
+ return super . readLock ( lockedFn , cb )
29
+ }
30
+
31
+ writeLock ( lockedFn , cb ) {
32
+ this . emitter . emit ( 'writeLock request' )
33
+ return super . writeLock ( lockedFn , cb )
34
+ }
35
+ }
36
+
15
37
describe ( 'gc' , function ( ) {
16
38
const fixtures = [ {
17
39
path : 'test/my/path1' ,
@@ -29,6 +51,7 @@ describe('gc', function () {
29
51
30
52
let ipfsd
31
53
let ipfs
54
+ let lockEmitter
32
55
33
56
before ( function ( done ) {
34
57
this . timeout ( 40 * 1000 )
@@ -48,6 +71,11 @@ describe('gc', function () {
48
71
ipfsd = node
49
72
ipfs = ipfsd . api
50
73
74
+ // Replace the Mutex with one that emits events when a readLock or
75
+ // writeLock is requested (needed in the tests below)
76
+ ipfs . _gcLock . mutex = new MutexEmitter ( ipfs . _options . repoOwner )
77
+ lockEmitter = ipfs . _gcLock . mutex . emitter
78
+
51
79
done ( )
52
80
} )
53
81
} )
@@ -79,13 +107,13 @@ describe('gc', function () {
79
107
it ( `garbage collection should wait for pending ${ test . name } to finish` , async ( ) => {
80
108
// Add blocks to IPFS
81
109
// Note: add operation will take a read lock
82
- const addLockRequested = pEvent ( ipfs . _gcLock , 'readLock request' )
110
+ const addLockRequested = pEvent ( lockEmitter , 'readLock request' )
83
111
const add1 = test . add1 ( )
84
112
85
113
// Once add lock has been requested, start GC
86
114
await addLockRequested
87
115
// Note: GC will take a write lock
88
- const gcStarted = pEvent ( ipfs . _gcLock , 'writeLock request' )
116
+ const gcStarted = pEvent ( lockEmitter , 'writeLock request' )
89
117
const gc = ipfs . repo . gc ( )
90
118
91
119
// Once GC has started, start second add
@@ -109,13 +137,13 @@ describe('gc', function () {
109
137
it ( 'garbage collection should wait for pending add + pin to finish' , async ( ) => {
110
138
// Add blocks to IPFS
111
139
// Note: add operation will take a read lock
112
- const addLockRequested = pEvent ( ipfs . _gcLock , 'readLock request' )
140
+ const addLockRequested = pEvent ( lockEmitter , 'readLock request' )
113
141
const add1 = ipfs . add ( fixtures [ 2 ] , { pin : true } )
114
142
115
143
// Once add lock has been requested, start GC
116
144
await addLockRequested
117
145
// Note: GC will take a write lock
118
- const gcStarted = pEvent ( ipfs . _gcLock , 'writeLock request' )
146
+ const gcStarted = pEvent ( lockEmitter , 'writeLock request' )
119
147
const gc = ipfs . repo . gc ( )
120
148
121
149
// Once GC has started, start second add
@@ -142,13 +170,13 @@ describe('gc', function () {
142
170
143
171
// Remove first block from IPFS
144
172
// Note: block rm will take a write lock
145
- const rmLockRequested = pEvent ( ipfs . _gcLock , 'writeLock request' )
173
+ const rmLockRequested = pEvent ( lockEmitter , 'writeLock request' )
146
174
const rm1 = ipfs . block . rm ( cid1 )
147
175
148
176
// Once rm lock has been requested, start GC
149
177
await rmLockRequested
150
178
// Note: GC will take a write lock
151
- const gcStarted = pEvent ( ipfs . _gcLock , 'writeLock request' )
179
+ const gcStarted = pEvent ( lockEmitter , 'writeLock request' )
152
180
const gc = ipfs . repo . gc ( )
153
181
154
182
// Once GC has started, start second rm
@@ -185,7 +213,7 @@ describe('gc', function () {
185
213
186
214
// Pin first block
187
215
// Note: pin add will take a read lock
188
- const pinLockRequested = pEvent ( ipfs . _gcLock , 'readLock request' )
216
+ const pinLockRequested = pEvent ( lockEmitter , 'readLock request' )
189
217
const pin1 = ipfs . pin . add ( cid1 )
190
218
191
219
// Once pin lock has been requested, start GC
@@ -222,13 +250,13 @@ describe('gc', function () {
222
250
223
251
// Unpin first block
224
252
// Note: pin rm will take a read lock
225
- const pinLockRequested = pEvent ( ipfs . _gcLock , 'readLock request' )
253
+ const pinLockRequested = pEvent ( lockEmitter , 'readLock request' )
226
254
const pinRm1 = ipfs . pin . rm ( cid1 )
227
255
228
256
// Once pin lock has been requested, start GC
229
257
await pinLockRequested
230
258
// Note: GC will take a write lock
231
- const gcStarted = pEvent ( ipfs . _gcLock , 'writeLock request' )
259
+ const gcStarted = pEvent ( lockEmitter , 'writeLock request' )
232
260
const gc = ipfs . repo . gc ( )
233
261
234
262
// Once GC has started, start second pin rm
0 commit comments