@libp2p/interface-compliance-tests 1.1.29 → 1.1.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/mocks/connection-manager.d.ts +21 -7
- package/dist/src/mocks/connection-manager.d.ts.map +1 -1
- package/dist/src/mocks/connection-manager.js +97 -9
- package/dist/src/mocks/connection-manager.js.map +1 -1
- package/dist/src/mocks/index.d.ts +1 -1
- package/dist/src/mocks/index.d.ts.map +1 -1
- package/dist/src/mocks/index.js +1 -1
- package/dist/src/mocks/index.js.map +1 -1
- package/dist/src/mocks/registrar.d.ts +0 -2
- package/dist/src/mocks/registrar.d.ts.map +1 -1
- package/dist/src/mocks/registrar.js +1 -12
- package/dist/src/mocks/registrar.js.map +1 -1
- package/dist/src/pubsub/api.d.ts.map +1 -1
- package/dist/src/pubsub/api.js +13 -10
- package/dist/src/pubsub/api.js.map +1 -1
- package/dist/src/pubsub/connection-handlers.d.ts.map +1 -1
- package/dist/src/pubsub/connection-handlers.js +43 -37
- package/dist/src/pubsub/connection-handlers.js.map +1 -1
- package/dist/src/pubsub/emit-self.d.ts.map +1 -1
- package/dist/src/pubsub/emit-self.js +22 -18
- package/dist/src/pubsub/emit-self.js.map +1 -1
- package/dist/src/pubsub/messages.d.ts.map +1 -1
- package/dist/src/pubsub/messages.js +7 -4
- package/dist/src/pubsub/messages.js.map +1 -1
- package/dist/src/pubsub/multiple-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/multiple-nodes.js +33 -33
- package/dist/src/pubsub/multiple-nodes.js.map +1 -1
- package/dist/src/pubsub/two-nodes.d.ts.map +1 -1
- package/dist/src/pubsub/two-nodes.js +10 -8
- package/dist/src/pubsub/two-nodes.js.map +1 -1
- package/dist/src/pubsub/utils.d.ts.map +1 -1
- package/dist/src/pubsub/utils.js +4 -1
- package/dist/src/pubsub/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/mocks/connection-manager.ts +125 -11
- package/src/mocks/index.ts +1 -1
- package/src/mocks/registrar.ts +1 -16
- package/src/pubsub/api.ts +13 -10
- package/src/pubsub/connection-handlers.ts +45 -37
- package/src/pubsub/emit-self.ts +24 -19
- package/src/pubsub/messages.ts +7 -4
- package/src/pubsub/multiple-nodes.ts +35 -35
- package/src/pubsub/two-nodes.ts +11 -10
- package/src/pubsub/utils.ts +6 -1
|
@@ -1,23 +1,137 @@
|
|
|
1
|
-
import { EventEmitter } from '@libp2p/interfaces'
|
|
1
|
+
import { CustomEvent, EventEmitter, Startable } from '@libp2p/interfaces'
|
|
2
2
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
3
3
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
4
|
-
import type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interfaces/
|
|
4
|
+
import type { ConnectionManager, ConnectionManagerEvents } from '@libp2p/interfaces/connection-manager'
|
|
5
|
+
import type { Components, Initializable } from '@libp2p/interfaces/src/components'
|
|
6
|
+
import { connectionPair } from './connection.js'
|
|
7
|
+
import errCode from 'err-code'
|
|
5
8
|
|
|
6
|
-
class
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
class MockNetwork {
|
|
10
|
+
private components: Components[] = []
|
|
11
|
+
|
|
12
|
+
addNode (components: Components): void {
|
|
13
|
+
this.components.push(components)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getNode (peerId: PeerId): Components {
|
|
17
|
+
for (const components of this.components) {
|
|
18
|
+
if (peerId.equals(components.getPeerId())) {
|
|
19
|
+
return components
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
throw errCode(new Error('Peer not found'), 'ERR_PEER_NOT_FOUND')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
reset () {
|
|
27
|
+
this.components = []
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const mockNetwork = new MockNetwork()
|
|
32
|
+
|
|
33
|
+
class MockConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Initializable, Startable {
|
|
34
|
+
private connections: Connection[] = []
|
|
35
|
+
private components?: Components
|
|
36
|
+
private started = false
|
|
37
|
+
|
|
38
|
+
init (components: Components) {
|
|
39
|
+
this.components = components
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
isStarted () {
|
|
43
|
+
return this.started
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async start () {
|
|
47
|
+
this.started = true
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async stop () {
|
|
51
|
+
for (const connection of this.connections) {
|
|
52
|
+
await this.closeConnections(connection.remotePeer)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.started = false
|
|
9
56
|
}
|
|
10
57
|
|
|
11
|
-
|
|
12
|
-
|
|
58
|
+
getConnections (peerId?: PeerId): Connection[] {
|
|
59
|
+
if (peerId != null) {
|
|
60
|
+
return this.connections
|
|
61
|
+
.filter(c => c.remotePeer.toString() === peerId.toString())
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return this.connections
|
|
13
65
|
}
|
|
14
66
|
|
|
15
|
-
|
|
16
|
-
|
|
67
|
+
async openConnection (peerId: PeerId) {
|
|
68
|
+
if (this.components == null) {
|
|
69
|
+
throw errCode(new Error('Not initialized'), 'ERR_NOT_INITIALIZED')
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const existingConnections = this.getConnections(peerId)
|
|
73
|
+
|
|
74
|
+
if (existingConnections.length > 0) {
|
|
75
|
+
return existingConnections[0]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const componentsB = mockNetwork.getNode(peerId)
|
|
79
|
+
|
|
80
|
+
const [aToB, bToA] = connectionPair(this.components, componentsB)
|
|
81
|
+
|
|
82
|
+
// track connections
|
|
83
|
+
this.connections.push(aToB)
|
|
84
|
+
;(componentsB.getConnectionManager() as MockConnectionManager).connections.push(bToA)
|
|
85
|
+
|
|
86
|
+
this.components.getConnectionManager().dispatchEvent(new CustomEvent<Connection>('peer:connect', {
|
|
87
|
+
detail: aToB
|
|
88
|
+
}))
|
|
89
|
+
|
|
90
|
+
for (const protocol of this.components.getRegistrar().getProtocols()) {
|
|
91
|
+
for (const topology of this.components.getRegistrar().getTopologies(protocol)) {
|
|
92
|
+
topology.onConnect(componentsB.getPeerId(), aToB)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
componentsB.getConnectionManager().dispatchEvent(new CustomEvent<Connection>('peer:connect', {
|
|
97
|
+
detail: bToA
|
|
98
|
+
}))
|
|
99
|
+
|
|
100
|
+
for (const protocol of componentsB.getRegistrar().getProtocols()) {
|
|
101
|
+
for (const topology of componentsB.getRegistrar().getTopologies(protocol)) {
|
|
102
|
+
topology.onConnect(this.components.getPeerId(), bToA)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return aToB
|
|
17
107
|
}
|
|
18
108
|
|
|
19
|
-
|
|
20
|
-
|
|
109
|
+
async closeConnections (peerId: PeerId) {
|
|
110
|
+
if (this.components == null) {
|
|
111
|
+
throw errCode(new Error('Not initialized'), 'ERR_NOT_INITIALIZED')
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const connections = this.getConnections(peerId)
|
|
115
|
+
|
|
116
|
+
if (connections.length === 0) {
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const componentsB = mockNetwork.getNode(peerId)
|
|
121
|
+
|
|
122
|
+
for (const protocol of this.components.getRegistrar().getProtocols()) {
|
|
123
|
+
this.components.getRegistrar().getTopologies(protocol).forEach(topology => {
|
|
124
|
+
topology.onDisconnect(componentsB.getPeerId())
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const conn of connections) {
|
|
129
|
+
await conn.close()
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
this.connections = this.connections.filter(c => !c.remotePeer.equals(peerId))
|
|
133
|
+
|
|
134
|
+
await componentsB.getConnectionManager().closeConnections(peerId)
|
|
21
135
|
}
|
|
22
136
|
}
|
|
23
137
|
|
package/src/mocks/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
2
|
export { mockConnectionGater } from './connection-gater.js'
|
|
3
|
-
export { mockConnectionManager } from './connection-manager.js'
|
|
3
|
+
export { mockConnectionManager, mockNetwork } from './connection-manager.js'
|
|
4
4
|
export { mockConnection, mockStream, connectionPair } from './connection.js'
|
|
5
5
|
export { mockMultiaddrConnection, mockMultiaddrConnPair } from './multiaddr-connection.js'
|
|
6
6
|
export { mockMuxer } from './muxer.js'
|
package/src/mocks/registrar.ts
CHANGED
|
@@ -2,8 +2,6 @@ import type { IncomingStreamData, Registrar, StreamHandler } from '@libp2p/inter
|
|
|
2
2
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
3
3
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
4
4
|
import type { Topology } from '@libp2p/interfaces/topology'
|
|
5
|
-
import { connectionPair } from './connection.js'
|
|
6
|
-
import type { Components } from '@libp2p/interfaces/src/components'
|
|
7
5
|
|
|
8
6
|
export class MockRegistrar implements Registrar {
|
|
9
7
|
private readonly topologies: Map<string, { topology: Topology, protocols: string[] }> = new Map()
|
|
@@ -89,7 +87,7 @@ export class MockRegistrar implements Registrar {
|
|
|
89
87
|
return output
|
|
90
88
|
}
|
|
91
89
|
|
|
92
|
-
|
|
90
|
+
return []
|
|
93
91
|
}
|
|
94
92
|
}
|
|
95
93
|
|
|
@@ -106,16 +104,3 @@ export async function mockIncomingStreamEvent (protocol: string, conn: Connectio
|
|
|
106
104
|
}
|
|
107
105
|
}
|
|
108
106
|
}
|
|
109
|
-
|
|
110
|
-
export async function connectPeers (protocol: string, a: Components, b: Components) {
|
|
111
|
-
// Notify peers of connection
|
|
112
|
-
const [aToB, bToA] = connectionPair(a, b)
|
|
113
|
-
|
|
114
|
-
for (const topology of a.getRegistrar().getTopologies(protocol)) {
|
|
115
|
-
await topology.onConnect(b.getPeerId(), aToB)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
for (const topology of b.getRegistrar().getTopologies(protocol)) {
|
|
119
|
-
await topology.onConnect(a.getPeerId(), bToA)
|
|
120
|
-
}
|
|
121
|
-
}
|
package/src/pubsub/api.ts
CHANGED
|
@@ -11,6 +11,7 @@ import type { Components } from '@libp2p/interfaces/components'
|
|
|
11
11
|
import { createComponents } from './utils.js'
|
|
12
12
|
import { start, stop } from '../index.js'
|
|
13
13
|
import { isStartable } from '@libp2p/interfaces'
|
|
14
|
+
import { mockNetwork } from '../mocks/connection-manager.js'
|
|
14
15
|
|
|
15
16
|
const topic = 'foo'
|
|
16
17
|
const data = uint8ArrayFromString('bar')
|
|
@@ -22,20 +23,22 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
22
23
|
|
|
23
24
|
// Create pubsub router
|
|
24
25
|
beforeEach(async () => {
|
|
26
|
+
mockNetwork.reset()
|
|
25
27
|
components = await createComponents()
|
|
26
28
|
|
|
27
|
-
pubsub = await common.setup({
|
|
29
|
+
pubsub = components.setPubSub(await common.setup({
|
|
28
30
|
components,
|
|
29
31
|
init: {
|
|
30
32
|
emitSelf: true
|
|
31
33
|
}
|
|
32
|
-
})
|
|
34
|
+
}))
|
|
33
35
|
})
|
|
34
36
|
|
|
35
37
|
afterEach(async () => {
|
|
36
38
|
sinon.restore()
|
|
37
|
-
await stop(
|
|
39
|
+
await stop(common)
|
|
38
40
|
await common.teardown()
|
|
41
|
+
mockNetwork.reset()
|
|
39
42
|
})
|
|
40
43
|
|
|
41
44
|
it('can start correctly', async () => {
|
|
@@ -45,7 +48,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
45
48
|
|
|
46
49
|
sinon.spy(components.getRegistrar(), 'register')
|
|
47
50
|
|
|
48
|
-
await
|
|
51
|
+
await start(components)
|
|
49
52
|
|
|
50
53
|
expect(pubsub.isStarted()).to.equal(true)
|
|
51
54
|
expect(components.getRegistrar().register).to.have.property('callCount', 1)
|
|
@@ -58,8 +61,8 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
58
61
|
|
|
59
62
|
sinon.spy(components.getRegistrar(), 'unregister')
|
|
60
63
|
|
|
61
|
-
await
|
|
62
|
-
await
|
|
64
|
+
await start(components)
|
|
65
|
+
await stop(components)
|
|
63
66
|
|
|
64
67
|
expect(pubsub.isStarted()).to.equal(false)
|
|
65
68
|
expect(components.getRegistrar().unregister).to.have.property('callCount', 1)
|
|
@@ -70,7 +73,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
70
73
|
throw new Error('a message should not be received')
|
|
71
74
|
}
|
|
72
75
|
|
|
73
|
-
await start(
|
|
76
|
+
await start(components)
|
|
74
77
|
pubsub.subscribe(topic)
|
|
75
78
|
pubsub.addEventListener('message', handler)
|
|
76
79
|
|
|
@@ -90,13 +93,13 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
90
93
|
// handlers are called async
|
|
91
94
|
await delay(100)
|
|
92
95
|
|
|
93
|
-
await stop(
|
|
96
|
+
await stop(components)
|
|
94
97
|
})
|
|
95
98
|
|
|
96
99
|
it('can subscribe and publish correctly', async () => {
|
|
97
100
|
const defer = pDefer()
|
|
98
101
|
|
|
99
|
-
await start(
|
|
102
|
+
await start(components)
|
|
100
103
|
|
|
101
104
|
pubsub.subscribe(topic)
|
|
102
105
|
pubsub.addEventListener('message', (evt) => {
|
|
@@ -107,7 +110,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
107
110
|
await pubsub.publish(topic, data)
|
|
108
111
|
await defer.promise
|
|
109
112
|
|
|
110
|
-
await stop(
|
|
113
|
+
await stop(components)
|
|
111
114
|
})
|
|
112
115
|
})
|
|
113
116
|
}
|
|
@@ -4,7 +4,6 @@ import pDefer from 'p-defer'
|
|
|
4
4
|
import pWaitFor from 'p-wait-for'
|
|
5
5
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
6
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
7
|
-
import { connectPeers } from '../mocks/registrar.js'
|
|
8
7
|
import type { TestSetup } from '../index.js'
|
|
9
8
|
import type { Message, PubSub } from '@libp2p/interfaces/pubsub'
|
|
10
9
|
import type { PubSubArgs } from './index.js'
|
|
@@ -12,6 +11,7 @@ import type { Components } from '@libp2p/interfaces/components'
|
|
|
12
11
|
import { start, stop } from '../index.js'
|
|
13
12
|
import { createComponents } from './utils.js'
|
|
14
13
|
import { pEvent } from 'p-event'
|
|
14
|
+
import { mockNetwork } from '../mocks/connection-manager.js'
|
|
15
15
|
|
|
16
16
|
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
17
17
|
describe('pubsub connection handlers', () => {
|
|
@@ -23,27 +23,28 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
23
23
|
describe('nodes send state on connection', () => {
|
|
24
24
|
// Create pubsub nodes and connect them
|
|
25
25
|
beforeEach(async () => {
|
|
26
|
+
mockNetwork.reset()
|
|
27
|
+
|
|
26
28
|
componentsA = await createComponents()
|
|
27
29
|
componentsB = await createComponents()
|
|
28
30
|
|
|
29
|
-
psA = await common.setup({
|
|
31
|
+
psA = componentsA.setPubSub(await common.setup({
|
|
30
32
|
components: componentsA,
|
|
31
33
|
init: {}
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
+
}))
|
|
35
|
+
|
|
36
|
+
psB = componentsB.setPubSub(await common.setup({
|
|
34
37
|
components: componentsB,
|
|
35
38
|
init: {}
|
|
36
|
-
})
|
|
39
|
+
}))
|
|
37
40
|
|
|
38
41
|
// Start pubsub
|
|
39
|
-
await start(
|
|
42
|
+
await start(componentsA, componentsB)
|
|
40
43
|
|
|
41
44
|
expect(psA.getPeers()).to.be.empty()
|
|
42
45
|
expect(psB.getPeers()).to.be.empty()
|
|
43
|
-
})
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
beforeEach(() => {
|
|
47
|
+
// Make subscriptions prior to nodes connected
|
|
47
48
|
psA.subscribe('Za')
|
|
48
49
|
psB.subscribe('Zb')
|
|
49
50
|
|
|
@@ -55,8 +56,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
55
56
|
|
|
56
57
|
afterEach(async () => {
|
|
57
58
|
sinon.restore()
|
|
58
|
-
await stop(
|
|
59
|
+
await stop(componentsA, componentsB)
|
|
59
60
|
await common.teardown()
|
|
61
|
+
mockNetwork.reset()
|
|
60
62
|
})
|
|
61
63
|
|
|
62
64
|
it('existing subscriptions are sent upon peer connection', async function () {
|
|
@@ -65,7 +67,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
65
67
|
pEvent(psB, 'subscription-change')
|
|
66
68
|
])
|
|
67
69
|
|
|
68
|
-
await
|
|
70
|
+
await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
|
|
69
71
|
|
|
70
72
|
await subscriptionsChanged
|
|
71
73
|
|
|
@@ -88,29 +90,31 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
88
90
|
|
|
89
91
|
// Create pubsub nodes and start them
|
|
90
92
|
beforeEach(async () => {
|
|
93
|
+
mockNetwork.reset()
|
|
91
94
|
componentsA = await createComponents()
|
|
92
95
|
componentsB = await createComponents()
|
|
93
96
|
|
|
94
|
-
psA = await common.setup({
|
|
97
|
+
psA = componentsA.setPubSub(await common.setup({
|
|
95
98
|
components: componentsA,
|
|
96
99
|
init: {}
|
|
97
|
-
})
|
|
98
|
-
psB = await common.setup({
|
|
100
|
+
}))
|
|
101
|
+
psB = componentsB.setPubSub(await common.setup({
|
|
99
102
|
components: componentsB,
|
|
100
103
|
init: {}
|
|
101
|
-
})
|
|
104
|
+
}))
|
|
102
105
|
|
|
103
|
-
await start(
|
|
106
|
+
await start(componentsA, componentsB)
|
|
104
107
|
})
|
|
105
108
|
|
|
106
109
|
afterEach(async () => {
|
|
107
110
|
sinon.restore()
|
|
108
|
-
await stop(
|
|
111
|
+
await stop(componentsA, componentsB)
|
|
109
112
|
await common.teardown()
|
|
113
|
+
mockNetwork.reset()
|
|
110
114
|
})
|
|
111
115
|
|
|
112
116
|
it('should get notified of connected peers on dial', async () => {
|
|
113
|
-
await
|
|
117
|
+
await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
|
|
114
118
|
|
|
115
119
|
return await Promise.all([
|
|
116
120
|
pWaitFor(() => psA.getPeers().length === 1),
|
|
@@ -123,7 +127,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
123
127
|
const topic = 'test-topic'
|
|
124
128
|
const data = uint8ArrayFromString('hey!')
|
|
125
129
|
|
|
126
|
-
await
|
|
130
|
+
await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
|
|
127
131
|
|
|
128
132
|
let subscribedTopics = psA.getTopics()
|
|
129
133
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -160,29 +164,31 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
160
164
|
|
|
161
165
|
// Create pubsub nodes
|
|
162
166
|
beforeEach(async () => {
|
|
167
|
+
mockNetwork.reset()
|
|
163
168
|
componentsA = await createComponents()
|
|
164
169
|
componentsB = await createComponents()
|
|
165
170
|
|
|
166
|
-
psA = await common.setup({
|
|
171
|
+
psA = componentsA.setPubSub(await common.setup({
|
|
167
172
|
components: componentsA,
|
|
168
173
|
init: {}
|
|
169
|
-
})
|
|
170
|
-
psB = await common.setup({
|
|
174
|
+
}))
|
|
175
|
+
psB = componentsB.setPubSub(await common.setup({
|
|
171
176
|
components: componentsB,
|
|
172
177
|
init: {}
|
|
173
|
-
})
|
|
178
|
+
}))
|
|
174
179
|
})
|
|
175
180
|
|
|
176
181
|
afterEach(async () => {
|
|
177
182
|
sinon.restore()
|
|
178
|
-
await stop(
|
|
183
|
+
await stop(componentsA, componentsB)
|
|
179
184
|
await common.teardown()
|
|
185
|
+
mockNetwork.reset()
|
|
180
186
|
})
|
|
181
187
|
|
|
182
188
|
it('should get notified of connected peers after starting', async () => {
|
|
183
|
-
await start(
|
|
189
|
+
await start(componentsA, componentsB)
|
|
184
190
|
|
|
185
|
-
await
|
|
191
|
+
await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
|
|
186
192
|
|
|
187
193
|
return await Promise.all([
|
|
188
194
|
pWaitFor(() => psA.getPeers().length === 1),
|
|
@@ -195,9 +201,9 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
195
201
|
const topic = 'test-topic'
|
|
196
202
|
const data = uint8ArrayFromString('hey!')
|
|
197
203
|
|
|
198
|
-
await start(
|
|
204
|
+
await start(componentsA, componentsB)
|
|
199
205
|
|
|
200
|
-
await
|
|
206
|
+
await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
|
|
201
207
|
|
|
202
208
|
await Promise.all([
|
|
203
209
|
pWaitFor(() => psA.getPeers().length === 1),
|
|
@@ -239,25 +245,27 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
239
245
|
|
|
240
246
|
// Create pubsub nodes and start them
|
|
241
247
|
beforeEach(async () => {
|
|
248
|
+
mockNetwork.reset()
|
|
242
249
|
componentsA = await createComponents()
|
|
243
250
|
componentsB = await createComponents()
|
|
244
251
|
|
|
245
|
-
psA = await common.setup({
|
|
252
|
+
psA = componentsA.setPubSub(await common.setup({
|
|
246
253
|
components: componentsA,
|
|
247
254
|
init: {}
|
|
248
|
-
})
|
|
249
|
-
psB = await common.setup({
|
|
255
|
+
}))
|
|
256
|
+
psB = componentsB.setPubSub(await common.setup({
|
|
250
257
|
components: componentsB,
|
|
251
258
|
init: {}
|
|
252
|
-
})
|
|
259
|
+
}))
|
|
253
260
|
|
|
254
|
-
await start(
|
|
261
|
+
await start(componentsA, componentsB)
|
|
255
262
|
})
|
|
256
263
|
|
|
257
264
|
afterEach(async () => {
|
|
258
265
|
sinon.restore()
|
|
259
|
-
await stop(
|
|
266
|
+
await stop(componentsA, componentsB)
|
|
260
267
|
await common.teardown()
|
|
268
|
+
mockNetwork.reset()
|
|
261
269
|
})
|
|
262
270
|
|
|
263
271
|
it.skip('should receive pubsub messages after a node restart', async function () {
|
|
@@ -268,7 +276,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
268
276
|
const defer1 = pDefer()
|
|
269
277
|
const defer2 = pDefer()
|
|
270
278
|
|
|
271
|
-
await
|
|
279
|
+
await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
|
|
272
280
|
|
|
273
281
|
let subscribedTopics = psA.getTopics()
|
|
274
282
|
expect(subscribedTopics).to.not.include(topic)
|
|
@@ -307,7 +315,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
307
315
|
})
|
|
308
316
|
await start(psB)
|
|
309
317
|
|
|
310
|
-
await
|
|
318
|
+
await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
|
|
311
319
|
|
|
312
320
|
// wait for remoteLibp2p to know about libp2p subscription
|
|
313
321
|
await pWaitFor(() => {
|
|
@@ -371,7 +379,7 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
371
379
|
const originalConnection = await psA._libp2p.dialer.connectToPeer(psB.peerId)
|
|
372
380
|
|
|
373
381
|
// second connection
|
|
374
|
-
await
|
|
382
|
+
await componentsA.getConnectionManager().openConnection(componentsB.getPeerId())
|
|
375
383
|
|
|
376
384
|
// Wait for subscriptions to occur
|
|
377
385
|
await pWaitFor(() => {
|
package/src/pubsub/emit-self.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { Components } from '@libp2p/interfaces/components'
|
|
|
9
9
|
import { start, stop } from '../index.js'
|
|
10
10
|
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
11
11
|
import { createComponents } from './utils.js'
|
|
12
|
+
import { mockNetwork } from '../mocks/connection-manager.js'
|
|
12
13
|
|
|
13
14
|
const topic = 'foo'
|
|
14
15
|
const data = uint8ArrayFromString('bar')
|
|
@@ -16,30 +17,30 @@ const shouldNotHappen = () => expect.fail()
|
|
|
16
17
|
|
|
17
18
|
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
18
19
|
describe('emit self', () => {
|
|
19
|
-
let pubsub: PubSub
|
|
20
|
-
let components: Components
|
|
21
|
-
|
|
22
20
|
describe('enabled', () => {
|
|
21
|
+
let pubsub: PubSub
|
|
22
|
+
let components: Components
|
|
23
|
+
|
|
23
24
|
before(async () => {
|
|
25
|
+
mockNetwork.reset()
|
|
24
26
|
components = await createComponents()
|
|
25
27
|
|
|
26
|
-
pubsub = await common.setup({
|
|
28
|
+
pubsub = components.setPubSub(await common.setup({
|
|
27
29
|
components,
|
|
28
30
|
init: {
|
|
29
31
|
emitSelf: true
|
|
30
32
|
}
|
|
31
|
-
})
|
|
32
|
-
})
|
|
33
|
+
}))
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
await start(pubsub)
|
|
35
|
+
await start(components)
|
|
36
36
|
pubsub.subscribe(topic)
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
after(async () => {
|
|
40
40
|
sinon.restore()
|
|
41
|
-
await stop(
|
|
41
|
+
await stop(components)
|
|
42
42
|
await common.teardown()
|
|
43
|
+
mockNetwork.reset()
|
|
43
44
|
})
|
|
44
45
|
|
|
45
46
|
it('should emit to self on publish', async () => {
|
|
@@ -62,27 +63,31 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
62
63
|
})
|
|
63
64
|
|
|
64
65
|
describe('disabled', () => {
|
|
66
|
+
let pubsub: PubSub
|
|
67
|
+
let components: Components
|
|
68
|
+
|
|
65
69
|
before(async () => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
mockNetwork.reset()
|
|
71
|
+
components = new Components({
|
|
72
|
+
peerId: await createEd25519PeerId(),
|
|
73
|
+
registrar: mockRegistrar()
|
|
74
|
+
})
|
|
75
|
+
pubsub = components.setPubSub(await common.setup({
|
|
76
|
+
components,
|
|
71
77
|
init: {
|
|
72
78
|
emitSelf: false
|
|
73
79
|
}
|
|
74
|
-
})
|
|
75
|
-
})
|
|
80
|
+
}))
|
|
76
81
|
|
|
77
|
-
|
|
78
|
-
await start(pubsub)
|
|
82
|
+
await start(components)
|
|
79
83
|
pubsub.subscribe(topic)
|
|
80
84
|
})
|
|
81
85
|
|
|
82
86
|
after(async () => {
|
|
83
87
|
sinon.restore()
|
|
84
|
-
await stop(
|
|
88
|
+
await stop(components)
|
|
85
89
|
await common.teardown()
|
|
90
|
+
mockNetwork.reset()
|
|
86
91
|
})
|
|
87
92
|
|
|
88
93
|
it('should not emit to self on publish', async () => {
|
package/src/pubsub/messages.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { Components } from '@libp2p/interfaces/components'
|
|
|
8
8
|
import { start, stop } from '../index.js'
|
|
9
9
|
import { pEvent } from 'p-event'
|
|
10
10
|
import { createComponents } from './utils.js'
|
|
11
|
+
import { mockNetwork } from '../mocks/connection-manager.js'
|
|
11
12
|
|
|
12
13
|
const topic = 'foo'
|
|
13
14
|
const data = uint8ArrayFromString('bar')
|
|
@@ -19,21 +20,23 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
|
19
20
|
|
|
20
21
|
// Create pubsub router
|
|
21
22
|
beforeEach(async () => {
|
|
23
|
+
mockNetwork.reset()
|
|
22
24
|
components = await createComponents()
|
|
23
25
|
|
|
24
|
-
pubsub = await common.setup({
|
|
26
|
+
pubsub = components.setPubSub(await common.setup({
|
|
25
27
|
components,
|
|
26
28
|
init: {
|
|
27
29
|
emitSelf: true
|
|
28
30
|
}
|
|
29
|
-
})
|
|
30
|
-
await start(
|
|
31
|
+
}))
|
|
32
|
+
await start(components)
|
|
31
33
|
})
|
|
32
34
|
|
|
33
35
|
afterEach(async () => {
|
|
34
36
|
sinon.restore()
|
|
35
|
-
await stop(
|
|
37
|
+
await stop(components)
|
|
36
38
|
await common.teardown()
|
|
39
|
+
mockNetwork.reset()
|
|
37
40
|
})
|
|
38
41
|
|
|
39
42
|
it('should emit normalized signed messages on publish', async () => {
|