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

Commit

Permalink
fix: use unidirectional streams (#105)
Browse files Browse the repository at this point in the history
* fix: use unidirectional streams

* chore: use libp2p-pubsub@0.5.2
  • Loading branch information
vasco-santos authored Jun 4, 2020
1 parent 70dd732 commit 96d3265
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"debug": "^4.1.1",
"it-length-prefixed": "^3.0.0",
"it-pipe": "^1.0.1",
"libp2p-pubsub": "^0.5.0",
"libp2p-pubsub": "~0.5.2",
"p-map": "^4.0.0",
"peer-id": "~0.13.3",
"protons": "^1.0.1",
Expand Down
28 changes: 28 additions & 0 deletions test/2-nodes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ describe('basics between 2 nodes', () => {
// Connect floodsub nodes
before(async () => {
const onConnectA = registrarRecordA[multicodec].onConnect
const onConnectB = registrarRecordB[multicodec].onConnect
const handleA = registrarRecordA[multicodec].handler
const handleB = registrarRecordB[multicodec].handler

// Notice peers of connection
const [c0, c1] = ConnectionPair()
await onConnectA(peerIdB, c0)
await onConnectB(peerIdA, c1)

await handleB({
protocol: multicodec,
Expand All @@ -72,6 +75,14 @@ describe('basics between 2 nodes', () => {
}
})

await handleA({
protocol: multicodec,
stream: c0.stream,
connection: {
remotePeer: peerIdB
}
})

expect(fsA.peers.size).to.be.eql(1)
expect(fsB.peers.size).to.be.eql(1)
})
Expand Down Expand Up @@ -275,11 +286,28 @@ describe('basics between 2 nodes', () => {
const dial = async () => {
const onConnectA = registrarRecordA[multicodec].onConnect
const onConnectB = registrarRecordB[multicodec].onConnect
const handleA = registrarRecordA[multicodec].handler
const handleB = registrarRecordB[multicodec].handler

// Notice peers of connection
const [c0, c1] = ConnectionPair()
await onConnectA(peerIdB, c0)
await handleB({
protocol: multicodec,
stream: c1.stream,
connection: {
remotePeer: peerIdA
}
})

await onConnectB(peerIdA, c1)
await handleA({
protocol: multicodec,
stream: c0.stream,
connection: {
remotePeer: peerIdB
}
})
}

await Promise.all([
Expand Down
94 changes: 93 additions & 1 deletion test/multiple-nodes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,46 @@ describe('multiple nodes (more than 2)', () => {
const onConnectA = registrarRecordA[multicodec].onConnect
const onConnectB = registrarRecordB[multicodec].onConnect
const onConnectC = registrarRecordC[multicodec].onConnect
const handleA = registrarRecordA[multicodec].handler
const handleB = registrarRecordB[multicodec].handler
const handleC = registrarRecordC[multicodec].handler

// Notice peers of connection
const [d0, d1] = ConnectionPair()
await onConnectA(peerIdB, d0)
await handleB({
protocol: multicodec,
stream: d1.stream,
connection: {
remotePeer: peerIdA
}
})
await onConnectB(peerIdA, d1)
await handleA({
protocol: multicodec,
stream: d0.stream,
connection: {
remotePeer: peerIdB
}
})

const [d2, d3] = ConnectionPair()
await onConnectB(peerIdC, d2)
await handleC({
protocol: multicodec,
stream: d3.stream,
connection: {
remotePeer: peerIdB
}
})
await onConnectC(peerIdB, d3)
await handleB({
protocol: multicodec,
stream: d2.stream,
connection: {
remotePeer: peerIdC
}
})
})

after(() => Promise.all([
Expand Down Expand Up @@ -267,23 +298,84 @@ describe('multiple nodes (more than 2)', () => {
const onConnectC = registrarRecordC[multicodec].onConnect
const onConnectD = registrarRecordD[multicodec].onConnect
const onConnectE = registrarRecordE[multicodec].onConnect
const handleA = registrarRecordA[multicodec].handler
const handleB = registrarRecordB[multicodec].handler
const handleC = registrarRecordC[multicodec].handler
const handleD = registrarRecordD[multicodec].handler
const handleE = registrarRecordE[multicodec].handler

// Notice peers of connection
const [d0, d1] = ConnectionPair() // A <-> B
await onConnectA(peerIdB, d0)
await handleB({
protocol: multicodec,
stream: d1.stream,
connection: {
remotePeer: peerIdA
}
})
await onConnectB(peerIdA, d1)
await handleA({
protocol: multicodec,
stream: d0.stream,
connection: {
remotePeer: peerIdB
}
})

const [d2, d3] = ConnectionPair() // B <-> C
await onConnectB(peerIdC, d2)
await handleC({
protocol: multicodec,
stream: d3.stream,
connection: {
remotePeer: peerIdB
}
})
await onConnectC(peerIdB, d3)
await handleB({
protocol: multicodec,
stream: d2.stream,
connection: {
remotePeer: peerIdC
}
})

const [d4, d5] = ConnectionPair() // C <-> D
await onConnectC(peerIdD, d4)
await handleD({
protocol: multicodec,
stream: d5.stream,
connection: {
remotePeer: peerIdC
}
})
await onConnectD(peerIdC, d5)
await handleC({
protocol: multicodec,
stream: d4.stream,
connection: {
remotePeer: peerIdD
}
})

const [d6, d7] = ConnectionPair() // C <-> D
const [d6, d7] = ConnectionPair() // D <-> E
await onConnectD(peerIdE, d6)
await handleE({
protocol: multicodec,
stream: d7.stream,
connection: {
remotePeer: peerIdD
}
})
await onConnectE(peerIdD, d7)
await handleD({
protocol: multicodec,
stream: d6.stream,
connection: {
remotePeer: peerIdE
}
})
})

after(() => Promise.all([
Expand Down

0 comments on commit 96d3265

Please sign in to comment.