Merge pull request #259 from orbitdb/feat/multiple-db-test

Add tests for using multiple databases at the same time
This commit is contained in:
Haad 2017-11-30 17:31:28 +01:00 committed by GitHub
commit 5f5479825d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 254 additions and 39 deletions

View File

@ -326,7 +326,7 @@ const ipfs1 = new IPFS({ repo: './ipfs1' })
ipfs1.on('ready', async () => {
// Create the database
const orbitdb1 = new OrbitDB(ipfs1, './orbitdb1')
const db1 = await orbitdb.log('events')
const db1 = await orbitdb1.log('events')
// Create the second peer
const ipfs2 = new IPFS({ repo: './ipfs2' })
@ -338,7 +338,7 @@ ipfs1.on('ready', async () => {
// When the second database replicated new heads, query the database
db2.events.on('replicated', () => {
const result = db2.iterator({ limit: -1 }).collect()
const result = db2.iterator({ limit: -1 }).collect().map(e => e.payload.value)
console.log(result.join('\n'))
})

2
dist/orbitdb.min.js vendored

File diff suppressed because one or more lines are too long

102
package-lock.json generated
View File

@ -954,8 +954,7 @@
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/bintrees/-/bintrees-1.0.1.tgz",
"integrity": "sha1-DmVcm5wkNeqraL9AJyJtK1WjRSQ=",
"dev": true,
"optional": true
"dev": true
},
"bip66": {
"version": "1.1.5",
@ -2167,6 +2166,15 @@
"tapable": "0.2.8"
}
},
"epimetheus": {
"version": "1.0.55",
"resolved": "https://registry.npmjs.org/epimetheus/-/epimetheus-1.0.55.tgz",
"integrity": "sha1-j0dAiy1oCxIm/9IF1QH499XikgY=",
"dev": true,
"requires": {
"prom-client": "10.2.2"
}
},
"errno": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/errno/-/errno-0.1.4.tgz",
@ -4994,6 +5002,20 @@
"integrity": "sha1-gtwzbSMrkGIXnQWrMpOmYFn9Q10=",
"dev": true
},
"inert": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/inert/-/inert-4.2.1.tgz",
"integrity": "sha512-qmbbZYPSzU/eOUOStPQvSjrU9IR1Q3uDtsEsVwnBQeZG43xu7Nrj6yuUrX3ice/03rv5dj/KiKB+NGCbiqH+aQ==",
"dev": true,
"requires": {
"ammo": "2.0.4",
"boom": "5.2.0",
"hoek": "4.2.0",
"items": "2.1.1",
"joi": "10.6.0",
"lru-cache": "4.1.1"
}
},
"inflight": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz",
@ -5118,7 +5140,7 @@
"libp2p-railing": "0.7.1",
"libp2p-secio": "0.8.1",
"libp2p-tcp": "0.11.1",
"libp2p-webrtc-star": "0.13.2",
"libp2p-webrtc-star": "0.13.3",
"libp2p-websockets": "0.10.4",
"lodash.flatmap": "4.5.0",
"lodash.get": "4.4.2",
@ -5923,7 +5945,7 @@
"dev": true,
"requires": {
"global-dirs": "0.1.1",
"is-path-inside": "1.0.0"
"is-path-inside": "1.0.1"
}
},
"is-ipfs": {
@ -5959,9 +5981,9 @@
"dev": true
},
"is-path-inside": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-1.0.0.tgz",
"integrity": "sha1-/AbloWg/vaE95mev9xe7wQpI838=",
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/is-path-inside/-/is-path-inside-1.0.1.tgz",
"integrity": "sha1-jvW33lBDej/cprToZe96pVy0gDY=",
"dev": true,
"requires": {
"path-is-inside": "1.0.2"
@ -6619,15 +6641,17 @@
}
},
"libp2p-webrtc-star": {
"version": "0.13.2",
"resolved": "https://registry.npmjs.org/libp2p-webrtc-star/-/libp2p-webrtc-star-0.13.2.tgz",
"integrity": "sha512-iYtB6sce9AHFujI9QeV0O7cTFkNFV5jAEPnk/pQENhQEpICahcMFNd9YbH/DdKTmF4DzjtNhRGdmfL0R+JB4FQ==",
"version": "0.13.3",
"resolved": "https://registry.npmjs.org/libp2p-webrtc-star/-/libp2p-webrtc-star-0.13.3.tgz",
"integrity": "sha512-UD3SAJwKcpM5DL5EtxEXFHAJFl/YHGIMxBcx9zdIxnwJOiwuZ5Jo8vhIA14gBUgOKjgr/dHOQPzWEdzT3yifSw==",
"dev": true,
"requires": {
"async": "2.6.0",
"debug": "3.0.1",
"debug": "3.1.0",
"detect-node": "2.0.3",
"epimetheus": "1.0.55",
"hapi": "16.6.2",
"inert": "4.2.1",
"interface-connection": "0.3.2",
"mafmt": "3.0.2",
"minimist": "1.2.0",
@ -6643,6 +6667,15 @@
"webrtcsupport": "github:ipfs/webrtcsupport#0669f576582c53a3a42aa5ac014fcc5966809615"
},
"dependencies": {
"debug": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
"dev": true,
"requires": {
"ms": "2.0.0"
}
},
"minimist": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
@ -7087,9 +7120,9 @@
}
},
"mime-db": {
"version": "1.31.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.31.0.tgz",
"integrity": "sha512-oB3w9lx50CMd6nfonoV5rBRUbJtjMifUHaFb5MfzjC8ksAIfVjT0BsX46SjjqBz7n9JGTrTX3paIeLSK+rS5fQ==",
"version": "1.32.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.32.0.tgz",
"integrity": "sha512-+ZWo/xZN40Tt6S+HyakUxnSOgff+JEdaneLWIm0Z6LmpCn5DMcZntLyUY5c/rTDog28LhXLKOUZKoTxTCAdBVw==",
"dev": true
},
"mime-types": {
@ -7122,7 +7155,7 @@
"dev": true,
"requires": {
"hoek": "4.2.0",
"mime-db": "1.31.0"
"mime-db": "1.32.0"
}
},
"minimalistic-assert": {
@ -7632,12 +7665,12 @@
}
},
"orbit-db-counterstore": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/orbit-db-counterstore/-/orbit-db-counterstore-1.0.0.tgz",
"integrity": "sha512-IyL8w8MKm45Wp0iIXBYJgH/trz9/umBFU7LAAETIbxD20PnWNy87cCQka6uLKxcw8axw1tHn7GIYUGSqysBu7A==",
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/orbit-db-counterstore/-/orbit-db-counterstore-1.0.1.tgz",
"integrity": "sha512-DiPoIojzwy2qHvvhp+jgY6FNwul9hjLd2EhYgmSJcimCIVCbArNxCgJlOiZhUClVRcDXP14ZvpJjDGc/JNXrxA==",
"requires": {
"crdts": "0.0.1",
"orbit-db-store": "2.0.2"
"orbit-db-store": "2.0.3"
}
},
"orbit-db-docstore": {
@ -7645,7 +7678,7 @@
"resolved": "https://registry.npmjs.org/orbit-db-docstore/-/orbit-db-docstore-1.0.1.tgz",
"integrity": "sha512-fxXkqtmGs2VSawayoPckzePQ7HNlrvomQFGBX2jCKeSeKkBtei8gTUHNjrK5rPg2V9Nn5hgoO6YLHSS4YkLvwg==",
"requires": {
"orbit-db-store": "2.0.2",
"orbit-db-store": "2.0.3",
"p-map": "1.2.0"
}
},
@ -7654,7 +7687,7 @@
"resolved": "https://registry.npmjs.org/orbit-db-eventstore/-/orbit-db-eventstore-1.0.0.tgz",
"integrity": "sha512-wvDPLKoqxnj1Oz+710n2CjyGNzpoYcT0ldYam2EzYzNkMBxmFOqYKiVddew6RslskzivTTV0KALiCLnjlMmaoA==",
"requires": {
"orbit-db-store": "2.0.2"
"orbit-db-store": "2.0.3"
}
},
"orbit-db-feedstore": {
@ -7680,7 +7713,7 @@
"resolved": "https://registry.npmjs.org/orbit-db-kvstore/-/orbit-db-kvstore-1.0.0.tgz",
"integrity": "sha512-c0CfFb/2l2h5CCKSD646ofv/xfK2vUoQtvQYRzbJc6brj+p4n1aOCzjoXLkpXg3/5/VZho1sYvFsqYtNSGeiuw==",
"requires": {
"orbit-db-store": "2.0.2"
"orbit-db-store": "2.0.3"
}
},
"orbit-db-pubsub": {
@ -7693,13 +7726,14 @@
}
},
"orbit-db-store": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/orbit-db-store/-/orbit-db-store-2.0.2.tgz",
"integrity": "sha512-ZBrAmeJI3XZ+JeHlwGqchVNaT73llY9yDlTQEocqng8SarLbHngg4n+O+rBY0SyjLuThYdDl4qV4ng2Y6bf0rA==",
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/orbit-db-store/-/orbit-db-store-2.0.3.tgz",
"integrity": "sha512-uEOYkeGDruUEbTOTXeWRdSmmDZ2lvOOGxd5lGmYJSgs+llzHjYu7oRIhFLLDosUS1wdLVBO79oyWUUQeJuKQAA==",
"requires": {
"ipfs-log": "4.0.4",
"orbit-db-cache": "0.0.7",
"orbit-db-keystore": "0.0.2",
"p-each-series": "1.0.0",
"readable-stream": "2.3.3"
}
},
@ -7736,7 +7770,6 @@
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-each-series/-/p-each-series-1.0.0.tgz",
"integrity": "sha1-kw89Et0fUOdDRFeiLNbwSsatf3E=",
"dev": true,
"requires": {
"p-reduce": "1.0.0"
}
@ -7779,8 +7812,7 @@
"p-reduce": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/p-reduce/-/p-reduce-1.0.0.tgz",
"integrity": "sha1-GMKw3ZNqRpClKfgjH1ig/bakffo=",
"dev": true
"integrity": "sha1-GMKw3ZNqRpClKfgjH1ig/bakffo="
},
"p-whilst": {
"version": "1.0.0",
@ -8112,7 +8144,6 @@
"resolved": "https://registry.npmjs.org/prom-client/-/prom-client-10.2.2.tgz",
"integrity": "sha512-d3qCBK41qZx00/WVzWOX4tau9FinCztqaECZiGuMI5vGYD//5VSdKMOZPRQKjVh5RkI4Ex98DI0YPsoFnEo1QQ==",
"dev": true,
"optional": true,
"requires": {
"tdigest": "0.1.1"
}
@ -9541,7 +9572,6 @@
"resolved": "https://registry.npmjs.org/tdigest/-/tdigest-0.1.1.tgz",
"integrity": "sha1-Ljyyw56kSeVdHmzZEReszKRYgCE=",
"dev": true,
"optional": true,
"requires": {
"bintrees": "1.0.1"
}
@ -9733,9 +9763,9 @@
"optional": true
},
"uglifyjs-webpack-plugin": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/uglifyjs-webpack-plugin/-/uglifyjs-webpack-plugin-1.1.1.tgz",
"integrity": "sha512-JPs2UFQxIbaPd7iOvWx1beA7My7YMo3tjTLTAmxuKFoKHQkt6fB70Jm6nm25ponWp4+gu/7U4eamelgDlu0Y3g==",
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/uglifyjs-webpack-plugin/-/uglifyjs-webpack-plugin-1.1.2.tgz",
"integrity": "sha512-k07cmJTj+8vZMSc3BaQ9uW7qVl2MqDts4ti4KaNACXEcXSw2vQM2S8olSk/CODxvcSFGvUHzNSqA8JQlhgUJPw==",
"dev": true,
"requires": {
"cacache": "10.0.1",
@ -9962,9 +9992,9 @@
"dev": true
},
"webpack": {
"version": "3.8.1",
"resolved": "https://registry.npmjs.org/webpack/-/webpack-3.8.1.tgz",
"integrity": "sha512-5ZXLWWsMqHKFr5y0N3Eo5IIisxeEeRAajNq4mELb/WELOR7srdbQk2N5XiyNy2A/AgvlR3AmeBCZJW8lHrolbw==",
"version": "3.9.0",
"resolved": "https://registry.npmjs.org/webpack/-/webpack-3.9.0.tgz",
"integrity": "sha512-SlBO3yUIhSohW7uCA5c0v03V32DsaXU3vDyUtHB8rubgTgfwl1nv+I+BQIScuQ6exu74wWT6brF/GDXxGLStuA==",
"dev": true,
"requires": {
"acorn": "5.2.1",

View File

@ -0,0 +1,185 @@
'use strict'
const assert = require('assert')
const mapSeries = require('p-each-series')
const rmrf = require('rimraf')
const OrbitDB = require('../src/OrbitDB')
const config = require('./utils/config')
const startIpfs = require('./utils/start-ipfs')
const stopIpfs = require('./utils/stop-ipfs')
const waitForPeers = require('./utils/wait-for-peers')
const dbPath1 = './orbitdb/tests/multiple-databases/1'
const dbPath2 = './orbitdb/tests/multiple-databases/2'
const ipfsPath1 = './orbitdb/tests/multiple-databases/1/ipfs'
const ipfsPath2 = './orbitdb/tests/multiple-databases/2/ipfs'
const databaseInterfaces = [
{
name: 'logdb',
open: async (orbitdb, address, options) => await orbitdb.log(address, options),
write: async (db, index) => await db.add('hello' + index),
query: (db) => db.iterator({ limit: -1 }).collect().length,
},
{
name: 'feed',
open: async (orbitdb, address, options) => await orbitdb.feed(address, options),
write: async (db, index) => await db.add('hello' + index),
query: (db) => db.iterator({ limit: -1 }).collect().length,
},
{
name: 'key-value',
open: async (orbitdb, address, options) => await orbitdb.keyvalue(address, options),
write: async (db, index) => await db.put('hello', index),
query: (db) => db.get('hello'),
},
{
name: 'counterdb',
open: async (orbitdb, address, options) => await orbitdb.counter(address, options),
write: async (db, index) => await db.inc(1),
query: (db) => db.value,
},
{
name: 'documents',
open: async (orbitdb, address, options) => await orbitdb.docs(address, options),
write: async (db, index) => await db.put({ _id: 'hello', testing: index }),
query: (db) => {
const docs = db.get('hello')
return docs ? docs[0].testing : 0
},
},
]
describe('orbit-db - Multiple Databases', function() {
this.timeout(config.timeout)
let ipfs1, ipfs2, orbitdb1, orbitdb2, db1, db2, db3, db4
let localDatabases = []
let remoteDatabases = []
// Create two IPFS instances and two OrbitDB instaces (2 nodes/peers)
before(async () => {
config.daemon1.repo = ipfsPath1
config.daemon2.repo = ipfsPath2
rmrf.sync(config.daemon1.repo)
rmrf.sync(config.daemon2.repo)
rmrf.sync(dbPath1)
rmrf.sync(dbPath2)
ipfs1 = await startIpfs(config.daemon1)
ipfs2 = await startIpfs(config.daemon2)
orbitdb1 = new OrbitDB(ipfs1, dbPath1)
orbitdb2 = new OrbitDB(ipfs2, dbPath2)
})
after(async () => {
if(orbitdb1)
await orbitdb1.stop()
if(orbitdb2)
await orbitdb2.stop()
if (ipfs1)
await stopIpfs(ipfs1)
if (ipfs2)
await stopIpfs(ipfs2)
})
beforeEach(async () => {
let options = {}
// Set write access for both clients
options.write = [
orbitdb1.key.getPublic('hex'),
orbitdb2.key.getPublic('hex')
],
console.log("Creating databases and waiting for peers to connect")
// Open the databases on the first node
options = Object.assign({}, options, { create: true })
// Open the databases on the first node
for (let dbInterface of databaseInterfaces) {
const db = await dbInterface.open(orbitdb1, dbInterface.name, options)
localDatabases.push(db)
}
// Open the databases on the second node, set 'sync' flag so that
// the second peer fetches the db manifest from the network
options = Object.assign({}, options, { sync: true })
for (let [index, dbInterface] of databaseInterfaces.entries()) {
const address = localDatabases[index].address.toString()
const db = await dbInterface.open(orbitdb2, address, options)
remoteDatabases.push(db)
}
// Wait for the peers to connect
await waitForPeers(ipfs1, [orbitdb2.id], localDatabases[0].address.toString())
await waitForPeers(ipfs1, [orbitdb2.id], localDatabases[0].address.toString())
console.log("Peers connected")
})
afterEach(async () => {
for (let db of remoteDatabases)
await db.drop()
for (let db of localDatabases)
await db.drop()
})
it('replicates multiple open databases', async () => {
const entryCount = 100
const entryArr = []
// Create an array that we use to create the db entries
for (let i = 1; i < entryCount + 1; i ++)
entryArr.push(i)
// Result state,
// we count how many times 'replicated' event was fired per db
let replicated = {}
localDatabases.forEach(db => {
replicated[db.address.toString()] = 0
})
// Listen for the updates from remote peers
remoteDatabases.forEach(db => {
db.events.on('replicated', (address) => {
replicated[address] += 1
})
})
// Write entries to each database
console.log("Writing to databases")
databaseInterfaces.forEach((dbInterface, index) => {
const db = localDatabases[index]
mapSeries(entryArr, val => dbInterface.write(db, val))
})
// Function to check if all databases have been replicated,
// we calculate this by checking number of 'replicated' events fired
const allReplicated = () => {
return remoteDatabases.every(db => db._oplog.length === entryCount)
}
console.log("Waiting for replication to finish")
return new Promise((resolve, reject) => {
const interval = setInterval(() => {
if (allReplicated()) {
clearInterval(interval)
// Verify that the databases contain all the right entries
databaseInterfaces.forEach((dbInterface, index) => {
const db = remoteDatabases[index]
const result = dbInterface.query(db)
assert.equal(result, entryCount)
assert.equal(db._oplog.length, entryCount)
})
resolve()
}
}, 500)
})
})
})