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

Commit

Permalink
Use less trivial tree for pinner tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamStone committed Apr 10, 2016
1 parent 3018983 commit 579d4c3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/core/pinner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 || {}

Expand Down
103 changes: 55 additions & 48 deletions test/core-tests/test-pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
})
}
})
})
})
Expand All @@ -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)
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
})
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 579d4c3

Please sign in to comment.