From 579d4c3d726574ebd8f76020bf165a151222788b Mon Sep 17 00:00:00 2001 From: AdamStone Date: Sun, 10 Apr 2016 12:15:25 -0700 Subject: [PATCH] Use less trivial tree for pinner tests --- src/core/pinner.js | 4 +- test/core-tests/test-pin.js | 103 +++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 50 deletions(-) diff --git a/src/core/pinner.js b/src/core/pinner.js index 43812bcf16..c40f52cb31 100644 --- a/src/core/pinner.js +++ b/src/core/pinner.js @@ -65,7 +65,7 @@ function Pinner (dagS) { callback = recursive recursive = true } - this.isPinnedWithType(multihash, 'all', (err, pinned, reason) => { + this.isPinnedWithType(multihash, this.types.all, (err, pinned, reason) => { if (err) { return callback(err) } if (!pinned) { return callback('not pinned') } switch (reason) { @@ -175,7 +175,7 @@ function Pinner (dagS) { if (typeof childhash === 'object') { childhash = bs58.encode(childhash).toString() } - _links = _links || 0 + _links = _links || root.links.length _checked = _checked || 0 _seen = _seen || {} diff --git a/test/core-tests/test-pin.js b/test/core-tests/test-pin.js index 01e3b29fd2..a615d7a280 100644 --- a/test/core-tests/test-pin.js +++ b/test/core-tests/test-pin.js @@ -13,7 +13,7 @@ describe('pinner', function () { var ipfs var repo var pinner - var objA, objB, objC, objD, objE + var Obj = {} before((done) => { repo = createTempRepo() @@ -22,22 +22,30 @@ describe('pinner', function () { ipfs.init({ emptyRepo: true }, (err) => { expect(err).to.not.exist ipfs.load(() => { - // use node B with child links to A and C for multiple tests - objA = new DAGNode(new Buffer('Node A')) - objB = new DAGNode(new Buffer('Node B')) - objC = new DAGNode(new Buffer('Node C')) - var linkToA = new DAGLink('childA', objA.size(), objA.multihash()) - var linkToC = new DAGLink('childC', objC.size(), objC.multihash()) - objB.addRawLink(linkToA) - objB.addRawLink(linkToC) - ipfs.object.put(objA, (err) => { - expect(err).to.not.exist - ipfs.object.put(objB, (err) => { + // Use this tree for multiple tests + // + // B E + // / \ + // A C + // \ + // D + + var labels = ['A', 'B', 'C', 'D', 'E'] + labels.forEach((label) => { + Obj[label] = new DAGNode(new Buffer('Node ' + label)) + }) + // make links from bottom up to avoid mutating the hash after linking + Obj.C.addRawLink(new DAGLink('Child D', Obj.D.size(), Obj.D.multihash())) + Obj.B.addRawLink(new DAGLink('Child A', Obj.A.size(), Obj.A.multihash())) + Obj.B.addRawLink(new DAGLink('Child C', Obj.C.size(), Obj.C.multihash())) + var count = 0 + labels.forEach((label) => { + ipfs.object.put(Obj[label], (err) => { expect(err).to.not.exist - ipfs.object.put(objC, (err) => { - expect(err).to.not.exist + count++ + if (count === labels.length) { done() - }) + } }) }) }) @@ -51,9 +59,9 @@ describe('pinner', function () { describe('pin', () => { it('pins object directly', (done) => { - pinner.pin(objA, false, (err) => { + pinner.pin(Obj.A, false, (err) => { expect(err).to.not.exist - pinner.isPinned(objA.multihash(), (err, pinned, reason) => { + pinner.isPinned(Obj.A.multihash(), (err, pinned, reason) => { expect(err).to.not.exist expect(pinned).to.be.true expect(reason).to.equal(pinner.types.direct) @@ -64,28 +72,28 @@ describe('pinner', function () { it('pins recursively by default', (done) => { // direct pin A which is child of B - pinner.pin(objA, false, (err) => { + pinner.pin(Obj.A, false, (err) => { expect(err).to.not.exist // recursive pin B which has children A and C - pinner.pin(objB, (err) => { + pinner.pin(Obj.B, (err) => { expect(err).to.not.exist // B should be 'recursive' pin - pinner.isPinned(objB.multihash(), (err, pinned, reason) => { + pinner.isPinned(Obj.B.multihash(), (err, pinned, reason) => { expect(err).to.not.exist expect(pinned).to.be.true expect(reason).to.equal(pinner.types.recursive) // A should still be 'direct' pin - pinner.isPinned(objA.multihash(), (err, pinned, reason) => { + pinner.isPinned(Obj.A.multihash(), (err, pinned, reason) => { expect(err).to.not.exist expect(pinned).to.be.true expect(reason).to.equal(pinner.types.direct) // C should be 'indirect' pin - pinner.isPinned(objC.multihash(), (err, pinned, reason) => { + pinner.isPinned(Obj.C.multihash(), (err, pinned, reason) => { expect(err).to.not.exist expect(pinned).to.be.true // indirect pin 'reason' is the b58 hash of recursive root pin expect(reason).to.equal( - bs58.encode(objB.multihash()).toString() + bs58.encode(Obj.B.multihash()).toString() ) done() }) @@ -97,14 +105,14 @@ describe('pinner', function () { it('rejects direct pin if already recursively pinned', (done) => { // recursive pin B which has children A and C - pinner.pin(objB, (err) => { + pinner.pin(Obj.B, (err) => { expect(err).to.not.exist // direct pin B should fail - pinner.pin(objB, false, (err) => { - expect(err).to.equal(bs58.encode(objB.multihash()).toString() + + pinner.pin(Obj.B, false, (err) => { + expect(err).to.equal(bs58.encode(Obj.B.multihash()).toString() + ' already pinned recursively') // pinning recursively again should succeed - pinner.pin(objB, (err) => { + pinner.pin(Obj.B, (err) => { expect(err).to.not.exist done() }) @@ -113,19 +121,18 @@ describe('pinner', function () { }) it('rejects recursive pin if child object is not stored', (done) => { - objD = new DAGNode(new Buffer('Node D')) - objE = new DAGNode(new Buffer('Node E')) - var linkToE = new DAGLink('childE', objE.size(), objE.multihash()) - objD.addRawLink(linkToE) - ipfs.object.put(objD, (err) => { + Obj.Y = new DAGNode(new Buffer('Node Y')) + Obj.Z = new DAGNode(new Buffer('Node Z')) + Obj.Y.addRawLink(new DAGLink('Child Z', Obj.Z.size(), Obj.Z.multihash())) + ipfs.object.put(Obj.Y, (err) => { expect(err).to.not.exist - // this should fail because E is not stored - pinner.pin(objD, (err) => { + // this should fail because Z is not stored + pinner.pin(Obj.Y, (err) => { expect(err).to.exist - ipfs.object.put(objE, (err) => { + ipfs.object.put(Obj.Z, (err) => { expect(err).to.not.exist // now it should succeed - pinner.pin(objD, (err) => { + pinner.pin(Obj.Y, (err) => { expect(err).to.not.exist done() }) @@ -137,11 +144,11 @@ describe('pinner', function () { describe('unpin', () => { it('unpins directly pinned object', (done) => { - pinner.pin(objA, false, (err) => { + pinner.pin(Obj.A, false, (err) => { expect(err).to.not.exist - pinner.unpin(objA.multihash(), false, (err) => { + pinner.unpin(Obj.A.multihash(), false, (err) => { expect(err).to.not.exist - pinner.isPinned(objA.multihash(), (err, pinned) => { + pinner.isPinned(Obj.A.multihash(), (err, pinned) => { expect(err).to.not.exist expect(pinned).to.be.false done() @@ -151,23 +158,23 @@ describe('pinner', function () { }) it('unpins recursively by default', (done) => { - const bs58A = bs58.encode(objA.multihash()).toString() - const bs58B = bs58.encode(objB.multihash()).toString() + const bs58A = bs58.encode(Obj.A.multihash()).toString() + const bs58B = bs58.encode(Obj.B.multihash()).toString() // recursive pin B which has children A and C - pinner.pin(objB, (err) => { + pinner.pin(Obj.B, (err) => { expect(err).to.not.exist // indirect pin A should not be unpinnable while B is pinned - pinner.unpin(objA.multihash(), (err) => { + pinner.unpin(Obj.A.multihash(), (err) => { expect(err).to.equal( bs58A + ' is pinned indirectly under ' + bs58B ) // unpinning B should also unpin A - pinner.unpin(objB.multihash(), (err) => { + pinner.unpin(Obj.B.multihash(), (err) => { expect(err).to.not.exist - pinner.isPinned(objB.multihash(), (err, pinned) => { + pinner.isPinned(Obj.B.multihash(), (err, pinned) => { expect(err).to.not.exist expect(pinned).to.be.false - pinner.isPinned(objA.multihash(), (err, pinned) => { + pinner.isPinned(Obj.A.multihash(), (err, pinned) => { expect(err).to.not.exist expect(pinned).to.be.false done() @@ -181,10 +188,10 @@ describe('pinner', function () { describe('hasChild', () => { it('finds if child hash is somewhere in object tree', (done) => { - pinner.hasChild(objB, objC.multihash(), (err, has) => { + pinner.hasChild(Obj.B, Obj.D.multihash(), (err, has) => { expect(err).to.not.exist expect(has).to.be.true - pinner.hasChild(objA, objC.multihash(), (err, has) => { + pinner.hasChild(Obj.B, Obj.E.multihash(), (err, has) => { expect(err).to.not.exist expect(has).to.be.false done()