42885b20a4
Use latest store modules from npm Update README Update docs Update examples Update benchmarks Update dependencies Add Getting Started guide Add new a screenshot Add a new live demo Add persistency tests for snapshot saving/loading and events Add network stress tests (but skip them by default as they're very heavy and lengthy) Add browser benchmarks Add log() alias for eventlog() database Add possibility to create database if it doesn't exist yet Add support for orbitdb addresses Add a test for starting replication when peers connect Add debug build Use IPFS nodeID as default user id Use ipfs-pubsub-room Handle closing of databases properly Handle cache errors Clean up tests, re-organize code files Clean up code style Support for CLI Remove obsolete scripts
113 lines
3.2 KiB
JavaScript
113 lines
3.2 KiB
JavaScript
'use strict'
|
|
|
|
const path = require('path')
|
|
const assert = require('assert')
|
|
const rmrf = require('rimraf')
|
|
const mapSeries = require('p-each-series')
|
|
const OrbitDB = require('../src/OrbitDB')
|
|
const config = require('./utils/config')
|
|
const startIpfs = require('./utils/start-ipfs')
|
|
const waitForPeers = require('./utils/wait-for-peers')
|
|
|
|
const dbPath1 = './orbitdb/tests/counters/peer1'
|
|
const dbPath2 = './orbitdb/tests/counters/peer2'
|
|
const ipfsPath1 = './orbitdb/tests/counters/peer1/ipfs'
|
|
const ipfsPath2 = './orbitdb/tests/counters/peer2/ipfs'
|
|
|
|
describe('CounterStore', function() {
|
|
this.timeout(config.timeout)
|
|
|
|
let orbitdb1, orbitdb2
|
|
let ipfs1, ipfs2
|
|
|
|
before(async () => {
|
|
rmrf.sync(dbPath1)
|
|
rmrf.sync(dbPath2)
|
|
config.daemon1.repo = ipfsPath1
|
|
config.daemon2.repo = ipfsPath2
|
|
ipfs1 = await startIpfs(config.daemon1)
|
|
ipfs2 = await startIpfs(config.daemon2)
|
|
})
|
|
|
|
after(async () => {
|
|
if (orbitdb1)
|
|
orbitdb1.stop()
|
|
|
|
if (orbitdb2)
|
|
orbitdb2.stop()
|
|
|
|
if (ipfs1)
|
|
await ipfs1.stop()
|
|
|
|
if (ipfs2)
|
|
await ipfs2.stop()
|
|
})
|
|
|
|
beforeEach(() => {
|
|
orbitdb1 = new OrbitDB(ipfs1, './orbitdb/1')
|
|
orbitdb2 = new OrbitDB(ipfs2, './orbitdb/2')
|
|
})
|
|
|
|
afterEach(() => {
|
|
if (orbitdb1)
|
|
orbitdb1.stop()
|
|
|
|
if (orbitdb2)
|
|
orbitdb2.stop()
|
|
})
|
|
|
|
describe('counters', function() {
|
|
let address
|
|
|
|
it('increases a counter value', async () => {
|
|
const counter = await orbitdb1.counter('counter test', { path: dbPath1 })
|
|
address = counter.address.toString()
|
|
await mapSeries([13, 1], (f) => counter.inc(f))
|
|
assert.equal(counter.value, 14)
|
|
await counter.close()
|
|
})
|
|
|
|
it('opens a saved counter', async () => {
|
|
const counter = await orbitdb1.counter(address, { path: dbPath1 })
|
|
await counter.load()
|
|
assert.equal(counter.value, 14)
|
|
await counter.close()
|
|
})
|
|
|
|
it('syncs counters', async () => {
|
|
let options = {
|
|
// Set write access for both clients
|
|
write: [
|
|
orbitdb1.key.getPublic('hex'),
|
|
orbitdb2.key.getPublic('hex')
|
|
],
|
|
}
|
|
|
|
const numbers = [[13, 10], [2, 5]]
|
|
const increaseCounter = (counterDB, i) => mapSeries(numbers[i], n => counterDB.inc(n))
|
|
|
|
// Create a new counter database in the first client
|
|
options = Object.assign({}, options, { path: dbPath1 })
|
|
const counter1 = await orbitdb1.counter(new Date().getTime().toString(), options)
|
|
// Open the database in the second client
|
|
options = Object.assign({}, options, { path: dbPath2, sync: true })
|
|
const counter2 = await orbitdb2.counter(counter1.address.toString(), options)
|
|
|
|
// Wait for peers to connect first
|
|
await waitForPeers(ipfs1, [orbitdb2.id], counter1.address.toString())
|
|
await waitForPeers(ipfs2, [orbitdb1.id], counter1.address.toString())
|
|
// Increase the counters sequentially
|
|
await mapSeries([counter1, counter2], increaseCounter)
|
|
|
|
return new Promise(resolve => {
|
|
// Wait for a while to make sure db's have been synced
|
|
setTimeout(() => {
|
|
assert.equal(counter1.value, 30)
|
|
assert.equal(counter2.value, 30)
|
|
resolve()
|
|
}, 2000)
|
|
})
|
|
})
|
|
})
|
|
})
|