orbitdb/test/orbitdb-replication.test.js
haad a063b3fb4a Refactor OrbitDB
Fix sync
Fix linter
Fix tests
Clean up
Set default references count to 0
Fix sync
Use address instead of databaseId
Sync protocol
Keep references to open databases in OrbitDB
Fix append benchmark
Initial version of heads exchange
Remove Feed
Fix KeyValuePersisted iterator
Refactor OrbitDBAddress a bit more
Add rest of the database types
Refactor OrbitDB addresses
Initial version for the full circle
Initial structure and tests for new OrbitDB
Make sure KeyStore is open when a Database is created
Re-organize OrbitDB
Use new databases and Log
More clean up
Add 'drop' event to Database
Clean up OrbitDB
Remove id from OrbitDB
Use new KeyStore and Identities
Remove storage from OrbitDB
Remove migrations from OrbitDB
Remove caches from OrbitDB
Remove pubsub from OrbitDB
2023-03-01 16:21:07 +02:00

127 lines
3.3 KiB
JavaScript

import { strictEqual, deepStrictEqual } from 'assert'
import rmrf from 'rimraf'
import { OrbitDB } from '../src/index.js'
import { config, testAPIs, startIpfs, stopIpfs } from 'orbit-db-test-utils'
import connectPeers from './utils/connect-nodes.js'
import waitFor from './utils/wait-for.js'
Object.keys(testAPIs).forEach((IPFS) => {
describe('Replicating databases (' + IPFS + ')', function () {
this.timeout(config.timeout * 2)
let ipfsd1, ipfsd2
let ipfs1, ipfs2
let orbitdb1, orbitdb2
before(async () => {
ipfsd1 = await startIpfs(IPFS, config.daemon1)
ipfsd2 = await startIpfs(IPFS, config.daemon2)
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api
await connectPeers(ipfs1, ipfs2)
})
after(async () => {
if (ipfsd1) {
await stopIpfs(ipfsd1)
}
if (ipfsd2) {
await stopIpfs(ipfsd2)
}
await rmrf('./orbitdb1')
await rmrf('./orbitdb2')
})
describe('replicating a database', () => {
let db1, db2
const amount = 128 + 1 // Same amount as in oplog replication test
before(async () => {
orbitdb1 = await OrbitDB({ ipfs: ipfs1, id: 'user1', directory: './orbitdb1' })
orbitdb2 = await OrbitDB({ ipfs: ipfs2, id: 'user2', directory: './orbitdb2' })
db1 = await orbitdb1.open('helloworld')
for (let i = 0; i < amount; i++) {
await db1.add('hello' + i)
}
})
after(async () => {
if (db1) {
await db1.close()
}
if (db2) {
await db2.close()
}
if (orbitdb1) {
await orbitdb1.stop()
}
if (orbitdb2) {
await orbitdb2.stop()
}
await rmrf('./orbitdb1')
await rmrf('./orbitdb2')
})
it('returns all entries in the replicated database', async () => {
console.time('replicate2')
let replicated = false
const onConnected = async (peerId) => {
const head = (await db2.log.heads())[0]
if (head && head.clock.time === amount) {
replicated = true
}
}
const onUpdated = (entry) => {
if (entry.clock.time === amount) {
replicated = true
}
}
const onError = (err) => {
console.error(err)
}
db1.events.on('error', onError)
db2 = await orbitdb2.open(db1.address)
db2.events.on('join', onConnected)
db2.events.on('update', onUpdated)
db2.events.on('error', onError)
await waitFor(() => replicated, () => true)
strictEqual(db1.address, db2.address)
strictEqual(db1.name, db2.name)
strictEqual(db1.type, db2.type)
const all2 = []
console.time('all2')
for await (const event of db2.iterator()) {
all2.unshift(event)
}
console.timeEnd('all2')
console.timeEnd('replicate2')
const expected = []
for (let i = 0; i < amount; i++) {
expected.push('hello' + i)
}
deepStrictEqual(all2, expected)
const all1 = []
console.time('all1')
for await (const event of db1.iterator()) {
all1.unshift(event)
}
console.timeEnd('all1')
deepStrictEqual(all1, expected)
})
})
})
})