Skip to content
This repository was archived by the owner on Sep 30, 2023. It is now read-only.

Commit a2b080e

Browse files
committed
adding replciator tests
adding nyc for code coverage adding replicator tests linting Fix replications tests, add more debug output Fix linter fixing ipfs config in tests fix: linting
1 parent 71f2cc3 commit a2b080e

File tree

9 files changed

+1097
-396
lines changed

9 files changed

+1097
-396
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ node_modules/
33
test/browser/bundle.js
44
test/browser/bundle.js.map
55
dist/orbit-db-store.min.js.map
6+
.nyc_output
7+
orbitdb/

package-lock.json

Lines changed: 960 additions & 390 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "src/Store.js",
66
"scripts": {
77
"test": "npm run test:node && npm run test:browser",
8-
"test:node": "TEST=all mocha",
8+
"test:node": "TEST=go mocha; TEST=js nyc mocha",
99
"test:browser": "npm run build:tests && mocha-headless-chrome -f ./test/browser/index.html -a no-sandbox",
1010
"build": "npm run build:dist",
1111
"build:dist": "webpack --config ./conf/webpack.config.js --display-modules --sort-modules-by size --mode production",
@@ -49,11 +49,12 @@
4949
"markdown-toc": "^1.2.0",
5050
"mocha": "^8.1.1",
5151
"mocha-headless-chrome": "^3.1.0",
52+
"nyc": "^15.1.0",
5253
"orbit-db-cache": "~0.3.0",
5354
"orbit-db-identity-provider": "~0.3.1",
5455
"orbit-db-keystore": "~0.3.5",
5556
"orbit-db-storage-adapter": "^0.5.3",
56-
"orbit-db-test-utils": "^0.11.0",
57+
"orbit-db-test-utils": "^0.11.1",
5758
"rimraf": "^3.0.0",
5859
"standard": "^14.0.2",
5960
"webpack": "^4.44.1",

src/Replicator.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,13 @@ class Replicator extends EventEmitter {
165165
this._stats.tasksStarted += 1
166166

167167
const exclude = []
168+
// console.log('>', hash)
168169
const log = await Log.fromEntryHash(this._store._ipfs, this._store.identity, hash, { logId: this._store._oplog.id, access: this._store.access, length: batchSize, exclude })
169170
this._buffer.push(log)
170171

171172
const latest = log.values[0]
172173
delete this._queue[hash]
174+
// console.log('>>', latest.payload)
173175

174176
// Mark this task as processed
175177
this._stats.tasksProcessed += 1

test/add.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
3838
const cache = new Cache(cacheStore)
3939

4040
testIdentity = await IdentityProvider.createIdentity({ id: 'userA', keystore })
41-
ipfsd = await startIpfs(IPFS, ipfsConfig)
41+
ipfsd = await startIpfs(IPFS, ipfsConfig.daemon1)
4242
ipfs = ipfsd.api
4343

4444
const address = 'test-address'

test/constructor.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
3838
const cache = new Cache(cacheStore)
3939

4040
testIdentity = await IdentityProvider.createIdentity({ id: 'userA', keystore })
41-
ipfs = await startIpfs(IPFS, ipfsConfig)
41+
ipfs = await startIpfs(IPFS, ipfsConfig.daemon1)
4242

4343
const address = 'test-address'
4444
store = new Store(ipfs, testIdentity, address, DefaultOptions)

test/events.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
5050
const cache = new Cache(cacheStore)
5151

5252
testIdentity = await IdentityProvider.createIdentity({ id: 'userA', keystore })
53-
ipfsd = await startIpfs(IPFS, ipfsConfig)
53+
ipfsd = await startIpfs(IPFS, ipfsConfig.daemon1)
5454
ipfs = ipfsd.api
5555

5656
const address = 'test-address'

test/replicator.spec.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
})

test/snapshot.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Object.keys(testAPIs).forEach((IPFS) => {
3838
const cache = new Cache(cacheStore)
3939

4040
testIdentity = await IdentityProvider.createIdentity({ id: 'userA', keystore })
41-
ipfsd = await startIpfs(IPFS, ipfsConfig)
41+
ipfsd = await startIpfs(IPFS, ipfsConfig.daemon1)
4242
ipfs = ipfsd.api
4343

4444
const address = 'test-address'

0 commit comments

Comments
 (0)