|
| 1 | +const assert = require('assert') |
| 2 | +const Log = require('ipfs-log') |
| 3 | + |
| 4 | +const Keystore = require('orbit-db-keystore') |
| 5 | +const IdentityProvider = require('orbit-db-identity-provider') |
| 6 | +const Replicator = require('../src/Replicator') |
| 7 | + |
| 8 | +const { |
| 9 | + connectPeers, |
| 10 | + config, |
| 11 | + testAPIs, |
| 12 | + startIpfs, |
| 13 | + stopIpfs |
| 14 | +} = require('orbit-db-test-utils') |
| 15 | + |
| 16 | +// Tests timeout |
| 17 | +const timeout = 30000 |
| 18 | + |
| 19 | +class DummyStore { |
| 20 | + constructor (log, ipfs, identity) { |
| 21 | + this._oplog = log |
| 22 | + this._ipfs = ipfs |
| 23 | + this.identity = identity |
| 24 | + } |
| 25 | + |
| 26 | + async close () {} |
| 27 | +} |
| 28 | + |
| 29 | +Object.keys(testAPIs).forEach((IPFS) => { |
| 30 | + describe(`Replicator, ${IPFS}`, function () { |
| 31 | + this.timeout(timeout) |
| 32 | + |
| 33 | + let log, ipfsd, ipfs, replicator, store, keystore, signingKeystore |
| 34 | + |
| 35 | + const { identityKeysPath } = config |
| 36 | + |
| 37 | + before(async () => { |
| 38 | + keystore = new Keystore(identityKeysPath) |
| 39 | + |
| 40 | + ipfsd = await startIpfs(IPFS, config.daemon1) |
| 41 | + ipfs = ipfsd.api |
| 42 | + const id = await ipfsd.api.id() |
| 43 | + |
| 44 | + const testIdentity = await IdentityProvider.createIdentity({ id, keystore }) |
| 45 | + log = new Log(ipfs, testIdentity) |
| 46 | + |
| 47 | + store = new DummyStore(log, ipfs, testIdentity) |
| 48 | + replicator = new Replicator(store, 123) |
| 49 | + }) |
| 50 | + |
| 51 | + after(async () => { |
| 52 | + await store.close() |
| 53 | + await stopIpfs(ipfsd) |
| 54 | + await replicator.stop() |
| 55 | + await keystore.close() |
| 56 | + }) |
| 57 | + |
| 58 | + it('default options', async () => { |
| 59 | + assert.deepStrictEqual(replicator._buffer, []) |
| 60 | + }) |
| 61 | + |
| 62 | + describe('concurrency = 1', function () { |
| 63 | + let ipfsd2, ipfs2, log2, store2 |
| 64 | + |
| 65 | + this.timeout(timeout) |
| 66 | + |
| 67 | + const logLength = 100 |
| 68 | + |
| 69 | + before(async () => { |
| 70 | + ipfsd2 = await startIpfs(IPFS, config.daemon2) |
| 71 | + ipfs2 = ipfsd2.api |
| 72 | + await connectPeers(ipfs, ipfs2) |
| 73 | + |
| 74 | + const testIdentity = await IdentityProvider.createIdentity({ id: 'userB', keystore, signingKeystore }) |
| 75 | + log2 = new Log(ipfs2, testIdentity, { logId: log.id }) |
| 76 | + |
| 77 | + console.log(`writing ${logLength} entries to the log`) |
| 78 | + for (let i = 0; i < logLength; i++) { |
| 79 | + await log2.append(`entry${i}`) |
| 80 | + } |
| 81 | + assert(log2.values.length, logLength) |
| 82 | + |
| 83 | + store2 = new DummyStore(log2, ipfs2, testIdentity) |
| 84 | + }) |
| 85 | + |
| 86 | + after(async () => { |
| 87 | + await store2.close() |
| 88 | + await stopIpfs(ipfsd2) |
| 89 | + }) |
| 90 | + |
| 91 | + it('loads', (done) => { |
| 92 | + let replicated = 0 |
| 93 | + |
| 94 | + assert.strictEqual(log.id, log2.id) |
| 95 | + |
| 96 | + replicator.load(log2.heads) |
| 97 | + |
| 98 | + assert.strictEqual(replicator._buffer.length, 0) |
| 99 | + assert.deepStrictEqual(replicator.getQueue()[0], log2.heads[0]) |
| 100 | + assert.strictEqual(replicator.tasksQueued, 1) |
| 101 | + assert.strictEqual(replicator.tasksRequested, 1) |
| 102 | + assert.strictEqual(replicator.tasksStarted, 0) // ?? |
| 103 | + |
| 104 | + replicator.on('load.end', async (replicatedLogs) => { |
| 105 | + replicated++ |
| 106 | + assert.strictEqual(replicator.tasksStarted, replicated) // ?? |
| 107 | + assert.strictEqual(replicator.tasksQueued, 0) |
| 108 | + assert.strictEqual(replicator.tasksFinished, replicated) |
| 109 | + // console.log(replicatedLogs.length) |
| 110 | + for (const replicatedLog of replicatedLogs) { |
| 111 | + // console.log(replicatedLog.values.length, log.values.length, replicatedLog.values[0]) |
| 112 | + await log.join(replicatedLog) |
| 113 | + } |
| 114 | + // console.log(log.values.length) |
| 115 | + // console.log(log.values[0].payload) |
| 116 | + // console.log(log.values.map(e => e.payload).join('\n')) |
| 117 | + |
| 118 | + if (log.values.length === logLength) { |
| 119 | + assert.deepStrictEqual(log.values, log2.values) |
| 120 | + done() |
| 121 | + } |
| 122 | + }) |
| 123 | + }) |
| 124 | + }) |
| 125 | + }) |
| 126 | +}) |
0 commit comments