orbitdb/test/browser/concurrent.spec.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

131 lines
3.7 KiB
JavaScript

'use strict'
import assert from 'assert'
import puppeteer from 'puppeteer-core'
import chromium from 'chromium'
import path from 'path'
import mapSeries from 'p-map-series'
import pMap from 'p-map'
import { config } from 'orbit-db-test-utils'
const clicksPerTab = 20
const numTabs = 3
const wait = async (milliseconds) => {
return new Promise((resolve, reject) => {
console.log('waiting...')
setTimeout(resolve, milliseconds)
})
}
describe('orbit-db - browser concurrent writes', function () {
this.timeout(numTabs * config.timeout)
let browser
const options = {
ignoreHTTPSErrors: true,
dumpio: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
executablePath: chromium.path
}
before(async () => {
browser = await puppeteer.launch(options)
})
after(async () => {
await browser.close()
})
describe('Write concurrently', function () {
const tabs = []
before(async () => {
const createTab = async () => {
const page = await browser.newPage()
await page.goto(`file://${path.resolve('test/browser/index.html')}`)
page.on('dialog', dialog => dialog.dismiss())
page.on('pageerror', err => console.error(err))
page.on('console', message => console.log(message))
await wait(1000)
return page
}
// open several tabs
for (let i = 0; i < numTabs; i++) {
const tab = await createTab()
tabs.push(tab)
}
const addDataButton = 'button#addData'
await pMap(tabs, async (page) => {
await page.waitForFunction(
'document.querySelector("#waitForOpenDB").innerText.includes("orbitdb")'
)
const addDataToLog = (maxClicks, maxWaitTime) => {
let count = 0
const repeat = () => new Promise((resolve, reject) => {
setTimeout(async () => {
await page.click(addDataButton)
if (++count < maxClicks) {
await repeat()
}
resolve()
}, Math.random() * maxWaitTime + 300) // ensure waiting at least ~300ms
})
return repeat()
}
return addDataToLog(clicksPerTab, 1000)
})
})
it('syncLocal option - Multiple tabs converge to same log', async () => {
return new Promise((resolve, reject) => {
let polls = 0
const interval = setInterval(async () => {
const logHashes = []
await mapSeries(tabs, async (page) => {
await page.evaluate(() => loadConsistentLog())
const hash = await page.evaluate(async () => await getConsistentLogHash())
logHashes.push(hash)
})
try {
const hashes = Array.from(new Set(logHashes))
// ensure log hashes are equal
assert.strictEqual(hashes.length, 1)
clearInterval(interval)
resolve()
} catch (e) {
console.log('Repolling...')
if (++polls > 5) {
reject(e)
}
}
}, 3000)
})
})
it('no syncLocal option - Multiple tabs do not converge to same log', async () => {
return new Promise((resolve, reject) => {
const interval = setInterval(async () => {
const logHashes = []
await mapSeries(tabs, async (page) => {
const hash = await page.evaluate(async () => await getInconsistentLogHash())
logHashes.push(hash)
})
try {
const hashes = Array.from(new Set(logHashes))
// logs hash different hashes
assert.strictEqual(hashes.length, numTabs)
clearInterval(interval)
resolve()
} catch (e) {
reject(e)
}
}, 3000)
})
})
})
})