0.16.0-beta.6

Add replication test
Use latest ipfs-daemon
Cleanups
This commit is contained in:
haad 2017-01-16 23:23:11 +01:00
parent e2b51113a6
commit c79a1a125f
7 changed files with 138 additions and 18 deletions

4
dist/orbitdb.js vendored
View File

@ -25700,10 +25700,10 @@ var OrbitDB = function () {
}
}, {
key: '_onData',
value: function _onData(dbname, item) {
value: function _onData(dbname, items) {
// 'New database entry...', after a new entry was added to the database
// console.log(".SYNCED", dbname, items.length)
this.events.emit('data', dbname, item);
this.events.emit('data', dbname, items);
}
}, {
key: '_onClose',

2
dist/orbitdb.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,6 @@
{
"name": "orbit-db",
"version": "0.16.0-beta.5",
"version": "0.16.0-beta.6",
"description": "Distributed p2p database on IPFS",
"author": "Haad",
"license": "MIT",
@ -36,7 +36,7 @@
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.18.0",
"bluebird": "^3.4.6",
"ipfs-daemon": "^0.3.0-beta.14",
"ipfs-daemon": "^0.3.0-beta.16",
"json-loader": "^0.5.4",
"lodash": "^4.16.4",
"mocha": "^3.1.2",

View File

@ -105,10 +105,10 @@ class OrbitDB {
Cache.set(dbname, hash)
}
_onData(dbname, item) {
_onData(dbname, items) {
// 'New database entry...', after a new entry was added to the database
// console.log(".SYNCED", dbname, items.length)
this.events.emit('data', dbname, item)
this.events.emit('data', dbname, items)
}
_onClose(dbname) {

View File

@ -31,6 +31,7 @@ const hasIpfsApiWithPubsub = (ipfs) => {
}
[IpfsNativeDaemon, IpfsNodeDaemon].forEach((IpfsDaemon) => {
// [IpfsNodeDaemon].forEach((IpfsDaemon) => {
// IpfsApis.forEach(function(ipfsApi) {
describe('orbit-db client', function() {
@ -50,14 +51,6 @@ const hasIpfsApiWithPubsub = (ipfs) => {
client2 = new OrbitDB(ipfs, username + '2')
done()
})
// ipfsApi.start({ IpfsDataDir: '/tmp/orbit-db-tests' })
// .then((res) => {
// ipfs = res
// client = new OrbitDB(ipfs, username)
// client2 = new OrbitDB(ipfs, username + '2')
// done()
// })
// .catch(done)
})
after(() => {

View File

@ -6,7 +6,7 @@ module.exports = {
Swarm: ['/ip4/0.0.0.0/tcp/0'],
Gateway: '/ip4/0.0.0.0/tcp/0'
},
Bootstrap: []
// Bootstrap: []
},
daemon2: {
IpfsDataDir: '/tmp/orbit-db-tests-2',
@ -15,6 +15,6 @@ module.exports = {
Swarm: ['/ip4/0.0.0.0/tcp/0'],
Gateway: '/ip4/0.0.0.0/tcp/0'
},
Bootstrap: []
// Bootstrap: []
},
}

127
test/replicate.test.js Normal file
View File

@ -0,0 +1,127 @@
'use strict'
const _ = require('lodash')
const fs = require('fs')
const path = require('path')
const assert = require('assert')
const async = require('asyncawait/async')
const await = require('asyncawait/await')
const OrbitDB = require('../src/OrbitDB')
const rmrf = require('rimraf')
const IpfsNodeDaemon = require('ipfs-daemon/src/ipfs-node-daemon')
const IpfsNativeDaemon = require('ipfs-daemon/src/ipfs-native-daemon')
if (typeof window !== 'undefined')
window.LOG = 'ERROR'
// Data directories
const defaultIpfsDirectory = './ipfs'
const defaultOrbitDBDirectory = './orbit-db'
// Daemon settings
const daemonsConf = require('./ipfs-daemons.conf.js')
const databaseName = 'oribt-db-tests'
const hasIpfsApiWithPubsub = (ipfs) => {
return ipfs.object.get !== undefined
&& ipfs.object.put !== undefined
&& ipfs.pubsub.publish !== undefined
&& ipfs.pubsub.subscribe !== undefined
}
const waitForPeers = (ipfs, channel) => {
return new Promise((resolve) => {
console.log("Waiting for peers...")
const interval = setInterval(() => {
ipfs.pubsub.peers(channel)
.then((peers) => {
if (peers.length > 0) {
clearInterval(interval)
resolve()
}
})
}, 1000)
})
}
// [IpfsNativeDaemon, IpfsNodeDaemon].forEach((IpfsDaemon) => {
[IpfsNativeDaemon].forEach((IpfsDaemon) => {
describe('orbit-db replication', function() {
this.timeout(40000)
let ipfs1, ipfs2, client1, client2, db1, db2
const removeDirectories= () => {
rmrf.sync(daemonsConf.daemon1.IpfsDataDir)
rmrf.sync(daemonsConf.daemon2.IpfsDataDir)
rmrf.sync(defaultIpfsDirectory)
rmrf.sync(defaultOrbitDBDirectory)
}
before(function (done) {
removeDirectories()
ipfs1 = new IpfsDaemon(daemonsConf.daemon1)
ipfs1.on('error', done)
ipfs1.on('ready', () => {
assert.equal(hasIpfsApiWithPubsub(ipfs1), true)
ipfs2 = new IpfsDaemon(daemonsConf.daemon2)
ipfs2.on('error', done)
ipfs2.on('ready', () => {
assert.equal(hasIpfsApiWithPubsub(ipfs2), true)
client1 = new OrbitDB(ipfs1, databaseName)
client2 = new OrbitDB(ipfs2, databaseName + '2')
done()
})
})
})
after(() => {
ipfs1.stop()
ipfs2.stop()
removeDirectories()
})
describe('two peers', function() {
beforeEach(() => {
db1 = client1.eventlog(databaseName, { maxHistory: 0 })
db2 = client2.eventlog(databaseName, { maxHistory: 0 })
})
it('replicates database of 1 entry', (done) => {
waitForPeers(ipfs1, databaseName + '2')
.then(async(() => {
db2.events.on('history', (db, data) => {
const items = db2.iterator().collect()
assert.equal(items.length, 1)
assert.equal(items[0].payload.value, 'hello')
done()
})
db1.add('hello')
}))
})
it('replicates database of 100 entries', (done) => {
const entryCount = 100
waitForPeers(ipfs1, databaseName + '2')
.then(async(() => {
let count = 0
db2.events.on('history', (db, data) => {
count ++
if (count === entryCount) {
const items = db2.iterator({ limit: 100 }).collect()
assert.equal(items.length, entryCount)
assert.equal(items[0].payload.value, 'hello0')
assert.equal(_.last(items).payload.value, 'hello99')
done()
}
})
for(let i = 0; i < entryCount; i ++)
await(db1.add('hello' + i))
}))
})
})
})
})