Skip to content

Commit

Permalink
Merge pull request #66 from orbitdb/new-refactor-2-rename
Browse files Browse the repository at this point in the history
New refactor 2 rename
  • Loading branch information
haydenyoung authored Nov 21, 2024
2 parents 5eee2d5 + 71723a5 commit 550e55c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 109 deletions.
3 changes: 3 additions & 0 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { config as libp2pConfig } from './utils/libp2p-config.js'
import { rpc as rpcId, appPath, rpcPath, app, orbiter as orbiterId, orbiterPath } from './utils/id.js'
import { saveConfig } from './utils/config-manager.js'
import { logger, enable } from '@libp2p/logger'

export default async ({ options }) => {
options = options || {}

Expand Down Expand Up @@ -102,4 +103,6 @@ export default async ({ options }) => {
await orbiter.stop()
process.exit(0)
})

console.log('started')
}
18 changes: 7 additions & 11 deletions test/commands/auth.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
import { rpc as rpcId, rpcPath } from '../../src/utils/id.js'
import { Identities, KeyStore } from '@orbitdb/core'
import { exec, execSync } from 'node:child_process'
import { spawn, execSync } from 'node:child_process'
import { strictEqual } from 'assert'
import { renameSync } from 'fs'
import { rimraf } from 'rimraf'
import waitForDaemonStarted from '../utils/wait-for-daemon-start.js'

describe('auth', function () {
let daemon

before(async function () {
daemon = exec('./src/bin/cli.js daemon')

// TODO: Probably a better way to establish if daemon is running.
// Maybe ping it?
await new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 500)
})
daemon = spawn('./src/bin/cli.js', ['daemon'])
await waitForDaemonStarted(daemon)
})

after(async function () {
daemon.kill()
if (daemon) {
daemon.kill()
}
await rimraf('voyager')
})

Expand Down
25 changes: 7 additions & 18 deletions test/commands/daemon.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import { appPath, orbiterPath } from '../../src/utils/id.js'
import { exec } from 'node:child_process'
import { spawn } from 'node:child_process'
import { strictEqual } from 'assert'
import { existsSync } from 'fs'
import { rimraf } from 'rimraf'
import { join } from 'path'
import waitForDaemonStarted from '../utils/wait-for-daemon-start.js'

describe('daemon', function () {
describe('defaults', function () {
it('starts daemon in default directory', async function () {
const daemon = exec('./src/bin/cli.js daemon')
const daemon = spawn('./src/bin/cli.js', ['daemon'])

// TODO: Probably a better way to establish if daemon is running.
// Maybe ping it?
await new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 500)
})
await waitForDaemonStarted(daemon)

strictEqual(existsSync(appPath()), true)
strictEqual(existsSync(orbiterPath()), true)
Expand All @@ -29,15 +24,9 @@ describe('daemon', function () {
describe('custom settings', function () {
it('starts daemon in custom directory', async function () {
const directory = 'alternative-directory'
const daemon = exec(`./src/bin/cli.js daemon -d ${directory}`)

// TODO: Probably a better way to establish if daemon is running.
// Maybe ping it?
await new Promise((resolve) => {
setTimeout(() => {
resolve()
}, 500)
})
const daemon = spawn('./src/bin/cli.js', ['daemon', '-d', directory])

await waitForDaemonStarted(daemon)

strictEqual(existsSync(directory), true)
strictEqual(existsSync(join(directory, appPath())), true)
Expand Down
131 changes: 51 additions & 80 deletions test/stress-test.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,79 @@
// import { multiaddr } from '@multiformats/multiaddr'
import { strictEqual, deepStrictEqual } from 'assert'
import { rimraf } from 'rimraf'
import { launchLander } from './utils/launch-lander.js'
import { launchOrbiter } from './utils/launch-orbiter.js'
import waitFor from './utils/wait-for.js'
// import connectPeers from './utils/connect-nodes-via-relay.js'

// const isBrowser = () => typeof window !== 'undefined'
describe('Stress test', function () {
this.timeout(10000000)

describe('End-to-End Browser Tests', function () {
describe('Orbiter in the browser', function () {
this.timeout(10000000)
let orbiter
let lander1
let lander2

// const orbiterAddress1 = isBrowser()
// ? multiaddr('/ip4/127.0.0.1/tcp/55441/ws/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')
// : multiaddr('/ip4/127.0.0.1/tcp/54321/p2p/16Uiu2HAmBzKcgCfpJ4j4wJSLkKLbCVvnNBWPnhexrnJWJf1fDu5y')

// const orbiterAddress2 = isBrowser()
// ? multiaddr('/ip4/127.0.0.1/tcp/55442/ws/p2p/16Uiu2HAmATMovCwY46yyJib7bGZF2f2XLRar7d7R3NJCSJtuyQLt')
// : multiaddr('/ip4/127.0.0.1/tcp/54322/p2p/16Uiu2HAmATMovCwY46yyJib7bGZF2f2XLRar7d7R3NJCSJtuyQLt')

let orbiter
let lander1
let lander2

before(async function () {
orbiter = await launchOrbiter({ directory: 'orbiter3' })
})

after(async function () {
if (orbiter) {
await orbiter.shutdown()
}
await rimraf('./orbiter3')
})

it('add and replicate a database - lander1->orbiter1(nodejs)->orbiter2(nodejs)->lander2', async function () {
const rounds = 50
const entryAmount = 100
const addr = orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().shift()
before(async function () {
orbiter = await launchOrbiter({ directory: 'orbiter3' })
})

console.log('start', addr)
for (let k = 1; k <= rounds; k++) {
let replicated = false
after(async function () {
if (orbiter) {
await orbiter.shutdown()
}
await rimraf('./orbiter3')
})

// lander1 = await launchLander({ orbiterAddress: orbiterAddress1, directory: 'lander4' })
lander1 = await launchLander({ orbiterAddress: addr, directory: 'lander4' })
await orbiter.auth.add(lander1.orbitdb.identity.id)
it('add and replicate a database - lander1->orbiter1->lander2', async function () {
const rounds = 50
const entryAmount = 100
const addr = orbiter.orbitdb.ipfs.libp2p.getMultiaddrs().shift()

const db1 = await lander1.orbitdb.open('my-db3')
for (let k = 1; k <= rounds; k++) {
let replicated = false

// console.log("--", lander1.orbitdb.ipfs.libp2p.peerId.toString(), db1.address)
lander1 = await launchLander({ orbiterAddress: addr, directory: 'lander4' })
await orbiter.auth.add(lander1.orbitdb.identity.id)

for (let i = 0; i < entryAmount; i++) {
await db1.add('hello world ' + i)
}
const db1 = await lander1.orbitdb.open('my-db3')

const expected = await db1.all()
for (let i = 0; i < entryAmount; i++) {
await db1.add('hello world ' + i)
}

// console.time('pin')
await lander1.add(db1.address)
// console.timeEnd('pin')
const expected = await db1.all()

await lander1.shutdown()
await lander1.add(db1.address)

lander2 = await launchLander({ orbiterAddress: addr, directory: 'lander5' })
await orbiter.auth.add(lander2.orbitdb.identity.id)
// lander2 = await launchLander({ orbiterAddress: orbiterAddress1, directory: 'lander5' })
await lander1.shutdown()

// console.time('pin2')
// await lander2.pin(db1.address)
// console.timeEnd('pin2')
lander2 = await launchLander({ orbiterAddress: addr, directory: 'lander5' })
await orbiter.auth.add(lander2.orbitdb.identity.id)

// console.time('replicate')
// console.log("open", db1.address)
console.time('round ' + k + '/' + rounds)
const db2 = await lander2.orbitdb.open(db1.address)
console.time('round ' + k + '/' + rounds)
const db2 = await lander2.orbitdb.open(db1.address)

const onConnected = (peerId, heads) => {
replicated = true
}
const onConnected = (peerId, heads) => {
replicated = true
}

db2.events.on('join', onConnected)
db2.events.on('join', onConnected)

await waitFor(() => replicated, () => true)
// console.timeEnd('replicate')
console.timeEnd('round ' + k + '/' + rounds)
await waitFor(() => replicated, () => true)
console.timeEnd('round ' + k + '/' + rounds)

const res = await db2.all()
const res = await db2.all()

strictEqual(expected.length, entryAmount)
strictEqual(res.length, entryAmount)
deepStrictEqual(expected, res)
// console.log('done ' + k + "/" + rounds)
strictEqual(expected.length, entryAmount)
strictEqual(res.length, entryAmount)
deepStrictEqual(expected, res)

if (lander1) {
await lander1.shutdown()
}
if (lander2) {
await lander2.shutdown()
}
await rimraf('./lander4')
await rimraf('./lander5')
if (lander1) {
await lander1.shutdown()
}
if (lander2) {
await lander2.shutdown()
}
})
await rimraf('./lander4')
await rimraf('./lander5')
}
})
})
9 changes: 9 additions & 0 deletions test/utils/wait-for-daemon-start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const waitForDaemonStarted = async (daemon) => {
return new Promise((resolve) => {
// Wait for the daemon to start which is when the daemon has printed "started" in stdout
const onStdout = (data) => (data.toString().trimRight() === 'started') ? resolve() : {}
daemon.stdout.on('data', onStdout)
})
}

export default waitForDaemonStarted

0 comments on commit 550e55c

Please sign in to comment.