Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Aug 30, 2018
1 parent 1528185 commit 248c4a2
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 208 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"bs58": "^4.0.1",
"buffer-loader": "~0.0.1",
"chai": "^4.1.2",
"cross-env": "^5.2.0",
"cids": "~0.5.3",
"cross-env": "^5.2.0",
"detect-node": "^2.0.3",
"dir-compare": "^1.4.0",
"dirty-chai": "^2.0.1",
Expand All @@ -51,7 +51,7 @@
"form-data": "^2.3.2",
"go-ipfs-dep": "~0.4.17",
"hat": "0.0.3",
"ipfs": "~0.31.6",
"ipfs": "github:ipfs/js-ipfs",
"ipfs-api": "^24.0.0",
"ipfsd-ctl": "~0.39.1",
"ipfs-unixfs": "~0.1.15",
Expand Down
271 changes: 85 additions & 186 deletions test/ipns.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,219 +13,118 @@ const path = require('path')
const hat = require('hat')

const DaemonFactory = require('ipfsd-ctl')
const goDf = DaemonFactory.create()
const jsDf = DaemonFactory.create({ type: 'js' })

const spawnJsDaemon = (dir, callback) => {
DaemonFactory.create({ type: 'js' })
.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 512 }
}, callback)
}

const spawnGoDaemon = (dir, callback) => {
DaemonFactory.create()
.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 1024 },
args: ['--offline']
}, callback)
}

const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'

const publishAndResolve = (publisherDaemon, resolverDaemon, callback) => {
let nodeId
let sameDaemon = false

if (typeof resolverDaemon === 'function') {
callback = resolverDaemon
resolverDaemon = publisherDaemon
sameDaemon = true
}

const stopAndStartSecondDaemon = (callback) => {
if (sameDaemon) {
return callback()
}
series([
(cb) => publisherDaemon.stop(cb),
(cb) => setTimeout(cb, 2000),
(cb) => resolverDaemon.start(cb),
], callback)
}

series([
(cb) => publisherDaemon.init(cb),
(cb) => publisherDaemon.start(cb),
(cb) => publisherDaemon.api.id((err, res) => {
expect(err).to.not.exist()
nodeId = res.id
cb()
}),
(cb) => publisherDaemon.api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => stopAndStartSecondDaemon(cb),
(cb) => {
resolverDaemon.api.name.resolve(nodeId, { local: true }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.equal(ipfsRef)
cb()
})
},
(cb) => resolverDaemon.stop(cb),
(cb) => setTimeout(cb, 2000),
(cb) => resolverDaemon.cleanup(cb)
], callback)
}

describe('ipns', () => {
it('should publish an ipns record to a js daemon and resolve it using the same js daemon', function (done) {
this.timeout(100 * 1000)

const dir = path.join(os.tmpdir(), hat())
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'

let jsDaemon
let jsId

series([
(cb) => jsDf.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 512 }
}, (err, node) => {
expect(err).to.not.exist()
jsDaemon = node
jsDaemon.init(cb)
}),
(cb) => jsDaemon.start(cb),
(cb) => jsDaemon.api.id((err, res) => {
expect(err).to.not.exist()
jsId = res.id
cb()
}),
(cb) => jsDaemon.api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => {
jsDaemon.api.name.resolve(jsId, (err, res) => {
expect(err).to.not.exist()
expect(res).to.equal(ipfsRef)
cb()
})
},
(cb) => jsDaemon.stop(cb)
// TODO cleanup repo
], done)
spawnJsDaemon(dir, (err, jsDaemon) => {
expect(err).to.not.exist()
publishAndResolve(jsDaemon, done)
})
})

it('should publish an ipns record to a go daemon and resolve it using the same go daemon', function (done) {
this.timeout(100 * 1000)

this.timeout(120 * 1000)
const dir = path.join(os.tmpdir(), hat())
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'

let goDaemon1
let goDaemon2
let goId1

parallel([
(cb) => goDf.spawn({
repoPath: dir,
initOptions: { bits: 1024 }
}, cb),
(cb) => goDf.spawn({ initOptions: { bits: 1024 } }, cb)
], (err, nodes) => {
spawnGoDaemon(dir, (err, goDaemon) => {
expect(err).to.not.exist()

goDaemon1 = nodes[0]
goDaemon2 = nodes[1]

parallel([
(cb) => goDaemon1.start(cb),
(cb) => goDaemon2.start(cb)
], (err, nodes) => {
expect(err).to.not.exist()

series([
(cb) => goDaemon1.api.id((err, res) => {
expect(err).to.not.exist()
goId1 = res
cb()
}),
(cb) => goDaemon1.api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => {
goDaemon1.api.name.resolve(goId1, { resolve: false }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.equal(ipfsRef)
cb()
})
},
(cb) => goDaemon1.stop(cb),
(cb) => goDaemon2.stop(cb)
// TODO cleanup repo
// TODO check need for 2 daemons
], done)
})
publishAndResolve(goDaemon, done)
})
})

it('should publish an ipns record to a js daemon and resolve it using a go daemon through the reuse of the same repo', function (done) {
this.timeout(1000 * 1000)

this.timeout(100 * 1000)
const dir = path.join(os.tmpdir(), hat())
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'

let goDaemon1
let goDaemon2
let jsDaemon
let peerId

series([
(cb) => jsDf.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 512 }
}, (err, node) => {
expect(err).to.not.exist()

jsDaemon = node
jsDaemon.init(cb)
}),
(cb) => jsDaemon.start(cb),
(cb) => jsDaemon.api.id((err, res) => {
expect(err).to.not.exist()
peerId = res.id
parallel([
(cb) => spawnJsDaemon(dir, cb),
(cb) => spawnGoDaemon(dir, cb)
], (err, daemons) => {
expect(err).to.not.exist()

cb()
}),
(cb) => jsDaemon.api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => jsDaemon.stop(cb),
(cb) => setTimeout(cb, 2000),
(cb) => goDf.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 1024 }
}, (err, node) => {
expect(err).to.not.exist()
goDaemon1 = node
cb()
}),
(cb) => goDf.spawn({ initOptions: { bits: 1024 } }, (err, node) => {
expect(err).to.not.exist()
goDaemon2 = node
cb()
}),
(cb) => goDaemon1.start(cb),
(cb) => goDaemon2.start(cb),
(cb) => {
goDaemon1.api.name.resolve(peerId, { resolve: false, local: true }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.equal(ipfsRef)
cb()
})
},
(cb) => goDaemon1.stop(cb),
(cb) => goDaemon2.stop(cb)
// TODO cleanup repo
], done)
publishAndResolve(daemons[0], daemons[1], done)
})
})

it('should publish an ipns record to a go daemon and resolve it using a js daemon through the reuse of the same repo', function (done) {
this.timeout(1000 * 1000)

this.timeout(120 * 1000)
const dir = path.join(os.tmpdir(), hat())
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'

let goDaemon1
let goDaemon2
let jsDaemon
let peerId

parallel([
(cb) => goDf.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 1024 }
}, cb),
(cb) => goDf.spawn({ initOptions: { bits: 1024 } }, cb)
], (err, nodes) => {
(cb) => spawnGoDaemon(dir, cb),
(cb) => spawnJsDaemon(dir, cb)
], (err, daemons) => {
expect(err).to.not.exist()

goDaemon1 = nodes[0]
goDaemon2 = nodes[1]

series([
(cb) => goDaemon1.init(cb),
(cb) => goDaemon1.start(cb),
(cb) => goDaemon2.start(cb),
(cb) => goDaemon1.api.id((err, res) => {
expect(err).to.not.exist()

peerId = res.id
cb()
}),
(cb) => goDaemon1.api.name.publish(ipfsRef, { resolve: false }, cb),
(cb) => goDaemon1.stop(cb),
(cb) => goDaemon2.stop(cb),
(cb) => jsDf.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 512 }
}, (err, node) => {
expect(err).to.not.exist()

jsDaemon = node
cb()
}),
(cb) => jsDaemon.start(cb),
(cb) => {
jsDaemon.api.name.resolve(peerId, (err, res) => {
expect(err).to.not.exist()
expect(res).to.equal(ipfsRef)
cb()
})
},
(cb) => jsDaemon.stop(cb),
(cb) => setTimeout(cb, 2000)
// TODO cleanup repo
], done)
publishAndResolve(daemons[0], daemons[1], done)
})
})
})
31 changes: 11 additions & 20 deletions test/repo.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ describe.skip('repo', () => {
], done)
})

// This was last due to an update on go-ipfs that changed how datastore is
// configured
it('read repo: js -> go', function (done) {
this.timeout(80 * 1000)
this.timeout(50 * 1000)
const dir = path.join(os.tmpdir(), hat())
const data = crypto.randomBytes(1024 * 5)

Expand All @@ -93,33 +91,26 @@ describe.skip('repo', () => {
series([
(cb) => jsDf.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 512 }
}, (err, node) => {
expect(err).to.not.exist()

}, cb),
(node, cb) => {
jsDaemon = node
jsDaemon.init(cb)
}),
(cb) => jsDaemon.start(cb),
(cb) => jsDaemon.api.add(data, (err, res) => {
expect(err).to.not.exist()

cb()
},
(cb) => jsDaemon.api.add(data, cb),
(res, cb) => {
hash = res[0].hash
catAndCheck(jsDaemon.api, hash, data, cb)
}),
},
(cb) => jsDaemon.stop(cb),
(cb) => goDf.spawn({
repoPath: dir,
disposable: false,
initOptions: { bits: 1024 }
}, (err, node) => {
expect(err).to.not.exist()

}, cb),
(node, cb) => {
goDaemon = node
cb()
}),
(cb) => goDaemon.start(cb),
},
(cb) => catAndCheck(goDaemon.api, hash, data, cb),
(cb) => goDaemon.stop(cb)
], done)
Expand Down

0 comments on commit 248c4a2

Please sign in to comment.