2018-03-30 12:46:53 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const assert = require('assert')
|
|
|
|
const mapSeries = require('p-map-series')
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const rmrf = require('rimraf')
|
|
|
|
const levelup = require('levelup')
|
|
|
|
const leveldown = require('leveldown')
|
|
|
|
const OrbitDB = require('../src/OrbitDB')
|
|
|
|
const OrbitDBAddress = require('../src/orbit-db-address')
|
|
|
|
|
|
|
|
// Include test utilities
|
|
|
|
const {
|
|
|
|
config,
|
|
|
|
startIpfs,
|
|
|
|
stopIpfs,
|
|
|
|
testAPIs,
|
|
|
|
} = require('./utils')
|
|
|
|
|
|
|
|
const dbPath1 = './orbitdb/tests/create-open/1'
|
|
|
|
const dbPath2 = './orbitdb/tests/create-open/2'
|
|
|
|
const ipfsPath = './orbitdb/tests/create-open/ipfs'
|
|
|
|
|
|
|
|
Object.keys(testAPIs).forEach(API => {
|
2018-03-31 12:35:29 +02:00
|
|
|
describe(`orbit-db - Replication Status (${API})`, function() {
|
2018-03-30 12:46:53 +02:00
|
|
|
this.timeout(config.timeout)
|
|
|
|
|
|
|
|
let ipfsd, ipfs, orbitdb1, orbitdb2, db, address
|
|
|
|
let localDataPath
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
config.daemon1.repo = ipfsPath
|
|
|
|
rmrf.sync(config.daemon1.repo)
|
|
|
|
rmrf.sync(dbPath1)
|
|
|
|
rmrf.sync(dbPath2)
|
|
|
|
ipfsd = await startIpfs(API, config.daemon1)
|
|
|
|
ipfs = ipfsd.api
|
2019-02-20 13:18:03 +00:00
|
|
|
orbitdb1 = await OrbitDB.createInstance(ipfs, { directory: dbPath1 })
|
|
|
|
orbitdb2 = await OrbitDB.createInstance(ipfs, { directory: dbPath2 })
|
2018-03-31 12:35:29 +02:00
|
|
|
db = await orbitdb1.log('replication status tests')
|
2018-03-30 12:46:53 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
after(async () => {
|
2019-02-20 13:18:03 +00:00
|
|
|
if(orbitdb1)
|
2018-03-30 12:46:53 +02:00
|
|
|
await orbitdb1.stop()
|
|
|
|
|
2019-02-20 13:18:03 +00:00
|
|
|
if(orbitdb2)
|
2018-03-30 12:46:53 +02:00
|
|
|
await orbitdb2.stop()
|
|
|
|
|
|
|
|
if (ipfsd)
|
|
|
|
await stopIpfs(ipfsd)
|
|
|
|
})
|
|
|
|
|
2018-03-31 12:35:29 +02:00
|
|
|
it('has correct initial state', async () => {
|
|
|
|
assert.deepEqual(db.replicationStatus, { buffered: 0, queued: 0, progress: 0, max: 0 })
|
|
|
|
})
|
2018-03-30 12:46:53 +02:00
|
|
|
|
2018-03-31 12:35:29 +02:00
|
|
|
it('has correct replication info after load', async () => {
|
|
|
|
await db.add('hello')
|
|
|
|
await db.close()
|
|
|
|
await db.load()
|
|
|
|
assert.deepEqual(db.replicationStatus, { buffered: 0, queued: 0, progress: 1, max: 1 })
|
|
|
|
})
|
2018-03-30 12:46:53 +02:00
|
|
|
|
2018-03-31 12:35:29 +02:00
|
|
|
it('has correct replication info after close', async () => {
|
|
|
|
await db.close()
|
|
|
|
assert.deepEqual(db.replicationStatus, { buffered: 0, queued: 0, progress: 0, max: 0 })
|
|
|
|
})
|
2018-03-30 12:46:53 +02:00
|
|
|
|
2018-03-31 12:35:29 +02:00
|
|
|
it('has correct replication info after sync', async () => {
|
|
|
|
await db.load()
|
|
|
|
await db.add('hello2')
|
|
|
|
assert.deepEqual(db.replicationStatus, { buffered: 0, queued: 0, progress: 2, max: 2 })
|
|
|
|
|
|
|
|
const db2 = await orbitdb2.log(db.address.toString(), { create: false, sync: false })
|
|
|
|
await db2.sync(db._oplog.heads)
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
|
|
|
assert.deepEqual(db2.replicationStatus, { buffered: 0, queued: 0, progress: 2, max: 2 })
|
|
|
|
resolve()
|
|
|
|
} catch (e) {
|
|
|
|
reject(e)
|
|
|
|
}
|
|
|
|
}, 100)
|
2018-03-30 12:46:53 +02:00
|
|
|
})
|
2018-03-31 12:35:29 +02:00
|
|
|
})
|
2018-03-30 12:46:53 +02:00
|
|
|
|
2018-03-31 12:35:29 +02:00
|
|
|
it('has correct replication info after loading from snapshot', async () => {
|
|
|
|
await db.saveSnapshot()
|
|
|
|
await db.close()
|
|
|
|
await db.loadFromSnapshot()
|
|
|
|
assert.deepEqual(db.replicationStatus, { buffered: 0, queued: 0, progress: 2, max: 2 })
|
2018-03-30 12:46:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|