Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 579d4c3

Browse files
committed
Use less trivial tree for pinner tests
1 parent 3018983 commit 579d4c3

File tree

2 files changed

+57
-50
lines changed

2 files changed

+57
-50
lines changed

src/core/pinner.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function Pinner (dagS) {
6565
callback = recursive
6666
recursive = true
6767
}
68-
this.isPinnedWithType(multihash, 'all', (err, pinned, reason) => {
68+
this.isPinnedWithType(multihash, this.types.all, (err, pinned, reason) => {
6969
if (err) { return callback(err) }
7070
if (!pinned) { return callback('not pinned') }
7171
switch (reason) {
@@ -175,7 +175,7 @@ function Pinner (dagS) {
175175
if (typeof childhash === 'object') {
176176
childhash = bs58.encode(childhash).toString()
177177
}
178-
_links = _links || 0
178+
_links = _links || root.links.length
179179
_checked = _checked || 0
180180
_seen = _seen || {}
181181

test/core-tests/test-pin.js

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('pinner', function () {
1313
var ipfs
1414
var repo
1515
var pinner
16-
var objA, objB, objC, objD, objE
16+
var Obj = {}
1717

1818
before((done) => {
1919
repo = createTempRepo()
@@ -22,22 +22,30 @@ describe('pinner', function () {
2222
ipfs.init({ emptyRepo: true }, (err) => {
2323
expect(err).to.not.exist
2424
ipfs.load(() => {
25-
// use node B with child links to A and C for multiple tests
26-
objA = new DAGNode(new Buffer('Node A'))
27-
objB = new DAGNode(new Buffer('Node B'))
28-
objC = new DAGNode(new Buffer('Node C'))
29-
var linkToA = new DAGLink('childA', objA.size(), objA.multihash())
30-
var linkToC = new DAGLink('childC', objC.size(), objC.multihash())
31-
objB.addRawLink(linkToA)
32-
objB.addRawLink(linkToC)
33-
ipfs.object.put(objA, (err) => {
34-
expect(err).to.not.exist
35-
ipfs.object.put(objB, (err) => {
25+
// Use this tree for multiple tests
26+
//
27+
// B E
28+
// / \
29+
// A C
30+
// \
31+
// D
32+
33+
var labels = ['A', 'B', 'C', 'D', 'E']
34+
labels.forEach((label) => {
35+
Obj[label] = new DAGNode(new Buffer('Node ' + label))
36+
})
37+
// make links from bottom up to avoid mutating the hash after linking
38+
Obj.C.addRawLink(new DAGLink('Child D', Obj.D.size(), Obj.D.multihash()))
39+
Obj.B.addRawLink(new DAGLink('Child A', Obj.A.size(), Obj.A.multihash()))
40+
Obj.B.addRawLink(new DAGLink('Child C', Obj.C.size(), Obj.C.multihash()))
41+
var count = 0
42+
labels.forEach((label) => {
43+
ipfs.object.put(Obj[label], (err) => {
3644
expect(err).to.not.exist
37-
ipfs.object.put(objC, (err) => {
38-
expect(err).to.not.exist
45+
count++
46+
if (count === labels.length) {
3947
done()
40-
})
48+
}
4149
})
4250
})
4351
})
@@ -51,9 +59,9 @@ describe('pinner', function () {
5159

5260
describe('pin', () => {
5361
it('pins object directly', (done) => {
54-
pinner.pin(objA, false, (err) => {
62+
pinner.pin(Obj.A, false, (err) => {
5563
expect(err).to.not.exist
56-
pinner.isPinned(objA.multihash(), (err, pinned, reason) => {
64+
pinner.isPinned(Obj.A.multihash(), (err, pinned, reason) => {
5765
expect(err).to.not.exist
5866
expect(pinned).to.be.true
5967
expect(reason).to.equal(pinner.types.direct)
@@ -64,28 +72,28 @@ describe('pinner', function () {
6472

6573
it('pins recursively by default', (done) => {
6674
// direct pin A which is child of B
67-
pinner.pin(objA, false, (err) => {
75+
pinner.pin(Obj.A, false, (err) => {
6876
expect(err).to.not.exist
6977
// recursive pin B which has children A and C
70-
pinner.pin(objB, (err) => {
78+
pinner.pin(Obj.B, (err) => {
7179
expect(err).to.not.exist
7280
// B should be 'recursive' pin
73-
pinner.isPinned(objB.multihash(), (err, pinned, reason) => {
81+
pinner.isPinned(Obj.B.multihash(), (err, pinned, reason) => {
7482
expect(err).to.not.exist
7583
expect(pinned).to.be.true
7684
expect(reason).to.equal(pinner.types.recursive)
7785
// A should still be 'direct' pin
78-
pinner.isPinned(objA.multihash(), (err, pinned, reason) => {
86+
pinner.isPinned(Obj.A.multihash(), (err, pinned, reason) => {
7987
expect(err).to.not.exist
8088
expect(pinned).to.be.true
8189
expect(reason).to.equal(pinner.types.direct)
8290
// C should be 'indirect' pin
83-
pinner.isPinned(objC.multihash(), (err, pinned, reason) => {
91+
pinner.isPinned(Obj.C.multihash(), (err, pinned, reason) => {
8492
expect(err).to.not.exist
8593
expect(pinned).to.be.true
8694
// indirect pin 'reason' is the b58 hash of recursive root pin
8795
expect(reason).to.equal(
88-
bs58.encode(objB.multihash()).toString()
96+
bs58.encode(Obj.B.multihash()).toString()
8997
)
9098
done()
9199
})
@@ -97,14 +105,14 @@ describe('pinner', function () {
97105

98106
it('rejects direct pin if already recursively pinned', (done) => {
99107
// recursive pin B which has children A and C
100-
pinner.pin(objB, (err) => {
108+
pinner.pin(Obj.B, (err) => {
101109
expect(err).to.not.exist
102110
// direct pin B should fail
103-
pinner.pin(objB, false, (err) => {
104-
expect(err).to.equal(bs58.encode(objB.multihash()).toString() +
111+
pinner.pin(Obj.B, false, (err) => {
112+
expect(err).to.equal(bs58.encode(Obj.B.multihash()).toString() +
105113
' already pinned recursively')
106114
// pinning recursively again should succeed
107-
pinner.pin(objB, (err) => {
115+
pinner.pin(Obj.B, (err) => {
108116
expect(err).to.not.exist
109117
done()
110118
})
@@ -113,19 +121,18 @@ describe('pinner', function () {
113121
})
114122

115123
it('rejects recursive pin if child object is not stored', (done) => {
116-
objD = new DAGNode(new Buffer('Node D'))
117-
objE = new DAGNode(new Buffer('Node E'))
118-
var linkToE = new DAGLink('childE', objE.size(), objE.multihash())
119-
objD.addRawLink(linkToE)
120-
ipfs.object.put(objD, (err) => {
124+
Obj.Y = new DAGNode(new Buffer('Node Y'))
125+
Obj.Z = new DAGNode(new Buffer('Node Z'))
126+
Obj.Y.addRawLink(new DAGLink('Child Z', Obj.Z.size(), Obj.Z.multihash()))
127+
ipfs.object.put(Obj.Y, (err) => {
121128
expect(err).to.not.exist
122-
// this should fail because E is not stored
123-
pinner.pin(objD, (err) => {
129+
// this should fail because Z is not stored
130+
pinner.pin(Obj.Y, (err) => {
124131
expect(err).to.exist
125-
ipfs.object.put(objE, (err) => {
132+
ipfs.object.put(Obj.Z, (err) => {
126133
expect(err).to.not.exist
127134
// now it should succeed
128-
pinner.pin(objD, (err) => {
135+
pinner.pin(Obj.Y, (err) => {
129136
expect(err).to.not.exist
130137
done()
131138
})
@@ -137,11 +144,11 @@ describe('pinner', function () {
137144

138145
describe('unpin', () => {
139146
it('unpins directly pinned object', (done) => {
140-
pinner.pin(objA, false, (err) => {
147+
pinner.pin(Obj.A, false, (err) => {
141148
expect(err).to.not.exist
142-
pinner.unpin(objA.multihash(), false, (err) => {
149+
pinner.unpin(Obj.A.multihash(), false, (err) => {
143150
expect(err).to.not.exist
144-
pinner.isPinned(objA.multihash(), (err, pinned) => {
151+
pinner.isPinned(Obj.A.multihash(), (err, pinned) => {
145152
expect(err).to.not.exist
146153
expect(pinned).to.be.false
147154
done()
@@ -151,23 +158,23 @@ describe('pinner', function () {
151158
})
152159

153160
it('unpins recursively by default', (done) => {
154-
const bs58A = bs58.encode(objA.multihash()).toString()
155-
const bs58B = bs58.encode(objB.multihash()).toString()
161+
const bs58A = bs58.encode(Obj.A.multihash()).toString()
162+
const bs58B = bs58.encode(Obj.B.multihash()).toString()
156163
// recursive pin B which has children A and C
157-
pinner.pin(objB, (err) => {
164+
pinner.pin(Obj.B, (err) => {
158165
expect(err).to.not.exist
159166
// indirect pin A should not be unpinnable while B is pinned
160-
pinner.unpin(objA.multihash(), (err) => {
167+
pinner.unpin(Obj.A.multihash(), (err) => {
161168
expect(err).to.equal(
162169
bs58A + ' is pinned indirectly under ' + bs58B
163170
)
164171
// unpinning B should also unpin A
165-
pinner.unpin(objB.multihash(), (err) => {
172+
pinner.unpin(Obj.B.multihash(), (err) => {
166173
expect(err).to.not.exist
167-
pinner.isPinned(objB.multihash(), (err, pinned) => {
174+
pinner.isPinned(Obj.B.multihash(), (err, pinned) => {
168175
expect(err).to.not.exist
169176
expect(pinned).to.be.false
170-
pinner.isPinned(objA.multihash(), (err, pinned) => {
177+
pinner.isPinned(Obj.A.multihash(), (err, pinned) => {
171178
expect(err).to.not.exist
172179
expect(pinned).to.be.false
173180
done()
@@ -181,10 +188,10 @@ describe('pinner', function () {
181188

182189
describe('hasChild', () => {
183190
it('finds if child hash is somewhere in object tree', (done) => {
184-
pinner.hasChild(objB, objC.multihash(), (err, has) => {
191+
pinner.hasChild(Obj.B, Obj.D.multihash(), (err, has) => {
185192
expect(err).to.not.exist
186193
expect(has).to.be.true
187-
pinner.hasChild(objA, objC.multihash(), (err, has) => {
194+
pinner.hasChild(Obj.B, Obj.E.multihash(), (err, has) => {
188195
expect(err).to.not.exist
189196
expect(has).to.be.false
190197
done()

0 commit comments

Comments
 (0)