Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

fix: enable and fix browser tests #352

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pids
lib-cov

# Coverage directory used by tools like istanbul
coverage
.coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@
"lint": "aegir lint",
"build": "aegir build",
"generate": "protons ./src/message/dht.proto",
"test": "aegir test -t node",
"test:node": "aegir test -t node",
"test": "aegir test",
"test:node": "aegir test -t node --cov",
"test:chrome": "aegir test -t browser --cov",
"test:chrome-webworker": "aegir test -t webworker",
"test:firefox": "aegir test -t browser -- --browser firefox",
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
"dep-check": "aegir dep-check",
"release": "aegir release"
},
Expand All @@ -159,6 +163,7 @@
"any-signal": "^3.0.0",
"datastore-core": "^8.0.1",
"err-code": "^3.0.1",
"events": "^3.3.0",
"hashlru": "^2.3.0",
"interface-datastore": "^7.0.0",
"it-all": "^1.0.6",
Expand Down Expand Up @@ -191,7 +196,7 @@
"@types/lodash.range": "^3.2.6",
"@types/varint": "^6.0.0",
"@types/which": "^2.0.1",
"aegir": "^37.0.8",
"aegir": "^37.3.0",
"datastore-level": "^9.0.0",
"delay": "^5.0.0",
"execa": "^6.0.0",
Expand Down
31 changes: 31 additions & 0 deletions test/message.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import range from 'lodash.range'
import fs from 'fs'
import path from 'path'
import { Message } from '../src/message/index.js'
import { isPeerId } from '@libp2p/interface-peer-id'

describe('Message', () => {
it('go-interop', () => {
range(1, 9).forEach((i) => {
const raw = fs.readFileSync(
path.join(process.cwd(), 'test', 'fixtures', `msg-${i}`)
)

const msg = Message.deserialize(raw)

expect(msg.clusterLevel).to.gte(0)
if (msg.record != null) {
expect(msg.record.key).to.be.a('Uint8Array')
}

if (msg.providerPeers.length > 0) {
msg.providerPeers.forEach((p) => {
expect(isPeerId(p.id)).to.be.true()
})
}
})
})
})
25 changes: 0 additions & 25 deletions test/message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import { expect } from 'aegir/chai'
import { multiaddr } from '@multiformats/multiaddr'
import range from 'lodash.range'
import random from 'lodash.random'
import { Libp2pRecord } from '@libp2p/record'
import fs from 'fs'
import path from 'path'
import { Message, MESSAGE_TYPE } from '../src/message/index.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { isPeerId } from '@libp2p/interface-peer-id'

describe('Message', () => {
it('create', () => {
Expand Down Expand Up @@ -88,25 +84,4 @@ describe('Message', () => {
msg.clusterLevel = 10
expect(msg.clusterLevel).to.eql(9)
})

it('go-interop', () => {
range(1, 9).forEach((i) => {
const raw = fs.readFileSync(
path.join(process.cwd(), 'test', 'fixtures', `msg-${i}`)
)

const msg = Message.deserialize(raw)

expect(msg.clusterLevel).to.gte(0)
if (msg.record != null) {
expect(msg.record.key).to.be.a('Uint8Array')
}

if (msg.providerPeers.length > 0) {
msg.providerPeers.forEach((p) => {
expect(isPeerId(p.id)).to.be.true()
})
}
})
})
})
63 changes: 63 additions & 0 deletions test/providers.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-env mocha */

import { LevelDatastore } from 'datastore-level'
import path from 'path'
import os from 'os'
import { Providers } from '../src/providers.js'
import { createValues } from './utils/create-values.js'
import { createPeerIds } from './utils/create-peer-id.js'
import { MemoryDatastore } from 'datastore-core/memory'

describe('Providers', () => {
let providers: Providers

before(async function () {
this.timeout(10 * 1000)
})

afterEach(async () => {
await providers?.stop()
})

// slooow so only run when you need to
it.skip('many', async function () {
const p = path.join(
os.tmpdir(), (Math.random() * 100).toString()
)
const store = new LevelDatastore(p)
await store.open()
providers = new Providers({
datastore: new MemoryDatastore()
}, {
cacheSize: 10
})

console.log('starting') // eslint-disable-line no-console
const [createdValues, createdPeers] = await Promise.all([
createValues(100),
createPeerIds(600)
])

console.log('got values and peers') // eslint-disable-line no-console
const total = Date.now()

for (const v of createdValues) {
for (const p of createdPeers) {
await providers.addProvider(v.cid, p)
}
}

console.log('addProvider %s peers %s cids in %sms', createdPeers.length, createdValues.length, Date.now() - total) // eslint-disable-line no-console
console.log('starting profile with %s peers and %s cids', createdPeers.length, createdValues.length) // eslint-disable-line no-console

for (let i = 0; i < 3; i++) {
const start = Date.now()
for (const v of createdValues) {
await providers.getProviders(v.cid)
console.log('query %sms', (Date.now() - start)) // eslint-disable-line no-console
}
}

await store.close()
})
})
46 changes: 0 additions & 46 deletions test/providers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import { expect } from 'aegir/chai'
import { MemoryDatastore } from 'datastore-core/memory'
import { CID } from 'multiformats/cid'
import { sha256 } from 'multiformats/hashes/sha2'
import { LevelDatastore } from 'datastore-level'
import path from 'path'
import os from 'os'
import { Providers } from '../src/providers.js'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { createValues } from './utils/create-values.js'
import { createPeerIds } from './utils/create-peer-id.js'
import type { PeerId } from '@libp2p/interface-peer-id'
import delay from 'delay'
Expand Down Expand Up @@ -114,46 +110,4 @@ describe('Providers', () => {
expect(provsAfter).to.have.length(0)
await providers.stop()
})

// slooow so only run when you need to
it.skip('many', async function () {
const p = path.join(
os.tmpdir(), (Math.random() * 100).toString()
)
const store = new LevelDatastore(p)
await store.open()
providers = new Providers({
datastore: new MemoryDatastore()
}, {
cacheSize: 10
})

console.log('starting') // eslint-disable-line no-console
const [createdValues, createdPeers] = await Promise.all([
createValues(100),
createPeerIds(600)
])

console.log('got values and peers') // eslint-disable-line no-console
const total = Date.now()

for (const v of createdValues) {
for (const p of createdPeers) {
await providers.addProvider(v.cid, p)
}
}

console.log('addProvider %s peers %s cids in %sms', createdPeers.length, createdValues.length, Date.now() - total) // eslint-disable-line no-console
console.log('starting profile with %s peers and %s cids', createdPeers.length, createdValues.length) // eslint-disable-line no-console

for (let i = 0; i < 3; i++) {
const start = Date.now()
for (const v of createdValues) {
await providers.getProviders(v.cid)
console.log('query %sms', (Date.now() - start)) // eslint-disable-line no-console
}
}

await store.close()
})
})
4 changes: 2 additions & 2 deletions test/rpc/index.spec.ts → test/rpc/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ describe('rpc', () => {
}
}

await rpc.onIncomingStream({
rpc.onIncomingStream({
stream: mockStream(duplexStream),
connection: stubInterface<Connection>()
})

return await defer.promise
await defer.promise
})
})