Skip to content

Commit f3f51df

Browse files
test: reproduce bulk app traffic delaying discovery
1 parent b9b544c commit f3f51df

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

test/replicate.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const test = require('brittle')
22
const b4a = require('b4a')
3+
const compact = require('compact-encoding')
4+
const DebuggingStream = require('debugging-stream')
35
const NoiseSecretStream = require('@hyperswarm/secret-stream')
6+
const Protomux = require('protomux')
47
const {
58
create,
69
createStored,
@@ -757,6 +760,61 @@ test('multiplexing with createProtocolStream (ondiscoverykey is called)', async
757760
s2.destroy()
758761
})
759762

763+
test('custom bulk channel does not delay hypercore discovery', async function (t) {
764+
const advertised = await create(t)
765+
766+
const n1 = new NoiseSecretStream(true)
767+
const n2 = new NoiseSecretStream(false)
768+
769+
const slowIncoming = new DebuggingStream(n2.rawStream, {
770+
writeSpeed: 512 * 1024,
771+
readSpeed: Infinity
772+
})
773+
774+
t.teardown(function () {
775+
n1.destroy()
776+
n2.destroy()
777+
slowIncoming.destroy()
778+
})
779+
780+
n1.rawStream.pipe(slowIncoming).pipe(n1.rawStream)
781+
782+
const mux1 = Protomux.from(n1)
783+
const mux2 = Protomux.from(n2)
784+
785+
const app1 = mux1.createChannel({ protocol: 'app/bulk' })
786+
const app2 = mux2.createChannel({ protocol: 'app/bulk' })
787+
const bulk = app1.addMessage({ encoding: compact.buffer })
788+
app2.addMessage({ encoding: compact.buffer })
789+
790+
app1.open()
791+
app2.open()
792+
793+
await Promise.all([app1.fullyOpened(), app2.fullyOpened()])
794+
795+
// Model a user-defined high-throughput app channel sharing the same
796+
// Hyperswarm connection as Hypercore. It writes 64 * 64 KiB = 4 MiB at
797+
// 512 KiB/s before Hypercore opens its replication channel, so the
798+
// receiver can take ~8s to discover the core behind those app bytes.
799+
bulk.send(Buffer.alloc(64 * 64 * 1024))
800+
801+
const discovery = new Promise((resolve) => {
802+
Hypercore.createProtocolStream(n2, {
803+
ondiscoverykey(discoveryKey) {
804+
if (b4a.equals(discoveryKey, advertised.discoveryKey)) resolve('discovered')
805+
}
806+
})
807+
})
808+
809+
const stream = Hypercore.createProtocolStream(n1)
810+
const replication = advertised.replicate(stream, { keepAlive: false })
811+
812+
t.teardown(() => replication.destroy())
813+
814+
const result = await Promise.race([discovery, sleep(5000).then(() => 'timeout')])
815+
t.is(result, 'discovered', 'hypercore discovery should not wait behind app bulk data')
816+
})
817+
760818
test('seeking while replicating', async function (t) {
761819
const a = await create(t)
762820
const b = await create(t, a.key)
@@ -2958,4 +3016,8 @@ async function waitForRequestBlock(core) {
29583016
}
29593017
}
29603018

3019+
function sleep(ms) {
3020+
return new Promise((resolve) => setTimeout(resolve, ms))
3021+
}
3022+
29613023
function noop() {}

0 commit comments

Comments
 (0)