orbitdb/test/replicate-and-load.test.js
Mark Henderson 39a00060d6 parent 32cc9a9b009d0d8543f81a6bab96ad9bcccd3088
author Mark Henderson <mark@mrh.io> 1598051057 -0400
committer Mark Henderson <mark@mrh.io> 1598229380 -0400

parent 32cc9a9b009d0d8543f81a6bab96ad9bcccd3088
author Mark Henderson <mark@mrh.io> 1598051057 -0400
committer Mark Henderson <mark@mrh.io> 1598229291 -0400

package updates and ipfs repo migration

validate-maintainers and orbit-db-test-utils

moving from 3 tabs to 2

wait workaround

wait 1000

standard --fix

redoing the replication test changes for some reason

validate-maintainers and orbit-db-test-utils

moving from 3 tabs to 2

wait workaround

wait 1000

standard --fix

fix create-open tests

fixing eventlog tests

fixing feed tests

fixing kvstore tests

fixing set identity tests

skipping replication tests for now

repo path based on API variable

js-ipfs.zip

go-ipfs.zip

skipping replicate tests as well

repo path based on API variable

js-ipfs.zip

go-ipfs.zip

package-lock

finalizing rebase
2020-08-23 21:52:10 -04:00

160 lines
5.0 KiB
JavaScript

'use strict'
const assert = require('assert')
const mapSeries = require('p-each-series')
const rmrf = require('rimraf')
const OrbitDB = require('../src/OrbitDB')
// Include test utilities
const {
config,
startIpfs,
stopIpfs,
testAPIs,
connectPeers,
waitForPeers,
} = require('orbit-db-test-utils')
const dbPath1 = './orbitdb/tests/replicate-and-load/1'
const dbPath2 = './orbitdb/tests/replicate-and-load/2'
const ipfsPath1 = './orbitdb/tests/replicate-and-load/1/ipfs'
const ipfsPath2 = './orbitdb/tests/replicate-and-load/2/ipfs'
Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - Replicate and Load (${API})`, function() {
this.timeout(config.timeout * 2)
let ipfsd1, ipfsd2, ipfs1, ipfs2
let orbitdb1, orbitdb2
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)
ipfsd1 = await startIpfs(API, config.daemon1)
ipfsd2 = await startIpfs(API, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
orbitdb1 = await OrbitDB.createInstance(ipfs1, { directory: dbPath1 })
orbitdb2 = await OrbitDB.createInstance(ipfs2, { directory: dbPath2 })
// Connect the peers manually to speed up test times
await connectPeers(ipfs1, ipfs2)
})
after(async () => {
if(orbitdb1)
await orbitdb1.stop()
if(orbitdb2)
await orbitdb2.stop()
if (ipfsd1)
await stopIpfs(ipfsd1)
if (ipfsd2)
await stopIpfs(ipfsd2)
})
describe('two peers', function() {
let db1, db2
const openDatabases = async (options) => {
// Set write access for both clients
options.write = [
orbitdb1.identity.publicKey,
orbitdb2.identity.publicKey
],
options = Object.assign({}, options, { path: dbPath1, create: true })
db1 = await orbitdb1.eventlog('tests', options)
// Set 'localOnly' flag on and it'll error if the database doesn't exist locally
options = Object.assign({}, options, { path: dbPath2 })
db2 = await orbitdb2.eventlog(db1.address.toString(), options)
}
before(async () => {
await openDatabases({ sync: true })
assert.equal(db1.address.toString(), db2.address.toString())
console.log("Waiting for peers...")
await waitForPeers(ipfs1, [orbitdb2.id], db1.address.toString())
await waitForPeers(ipfs2, [orbitdb1.id], db1.address.toString())
console.log("Found peers")
})
after(async () => {
await db1.drop()
await db2.drop()
})
it('replicates database of 100 entries and loads it from the disk', async () => {
const entryCount = 100
const entryArr = []
let timer
for (let i = 0; i < entryCount; i ++)
entryArr.push(i)
console.log("Writing to database...")
await mapSeries(entryArr, (i) => db1.add('hello' + i))
console.log("Done")
return new Promise((resolve, reject) => {
timer = setInterval(async () => {
if (db2._oplog.length === entryCount) {
clearInterval(timer)
const items = db2.iterator({ limit: -1 }).collect()
assert.equal(items.length, entryCount)
assert.equal(items[0].payload.value, 'hello0')
assert.equal(items[items.length - 1].payload.value, 'hello99')
try {
// Set write access for both clients
let options = {
accessController: {
write: [
orbitdb1.identity.id,
orbitdb2.identity.id
]
}
}
// Get the previous address to make sure nothing mutates it
const addr = db1.address.toString()
// Open the database again (this time from the disk)
options = Object.assign({}, options, { path: dbPath1, create: false })
const db3 = await orbitdb1.eventlog(addr, options)
// Set 'localOnly' flag on and it'll error if the database doesn't exist locally
options = Object.assign({}, options, { path: dbPath2, localOnly: true })
const db4 = await orbitdb2.eventlog(addr, options)
await db3.load()
await db4.load()
// Make sure we have all the entries in the databases
const result1 = db3.iterator({ limit: -1 }).collect()
const result2 = db4.iterator({ limit: -1 }).collect()
assert.equal(result1.length, entryCount)
assert.equal(result2.length, entryCount)
await db3.drop()
await db4.drop()
} catch (e) {
reject(e)
}
resolve()
}
}, 100)
})
})
})
})
})