@libp2p/interface-compliance-tests 2.0.2 → 3.0.1
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/README.md +23 -11
- package/dist/src/{utils/is-valid-tick.d.ts → is-valid-tick.d.ts} +0 -0
- package/dist/src/is-valid-tick.d.ts.map +1 -0
- package/dist/src/{utils/is-valid-tick.js → is-valid-tick.js} +0 -0
- package/dist/src/is-valid-tick.js.map +1 -0
- package/dist/src/{utils/peers.d.ts → peers.d.ts} +0 -0
- package/dist/src/peers.d.ts.map +1 -0
- package/dist/src/{utils/peers.js → peers.js} +0 -0
- package/dist/src/peers.js.map +1 -0
- package/package.json +11 -89
- package/src/{utils/is-valid-tick.ts → is-valid-tick.ts} +0 -0
- package/src/{utils/peers.ts → peers.ts} +0 -0
- package/dist/src/connection/connection.d.ts +0 -5
- package/dist/src/connection/connection.d.ts.map +0 -1
- package/dist/src/connection/connection.js +0 -146
- package/dist/src/connection/connection.js.map +0 -1
- package/dist/src/connection/index.d.ts +0 -5
- package/dist/src/connection/index.d.ts.map +0 -1
- package/dist/src/connection/index.js +0 -5
- package/dist/src/connection/index.js.map +0 -1
- package/dist/src/connection-encrypter/index.d.ts +0 -5
- package/dist/src/connection-encrypter/index.d.ts.map +0 -1
- package/dist/src/connection-encrypter/index.js +0 -77
- package/dist/src/connection-encrypter/index.js.map +0 -1
- package/dist/src/connection-encrypter/utils/index.d.ts +0 -3
- package/dist/src/connection-encrypter/utils/index.d.ts.map +0 -1
- package/dist/src/connection-encrypter/utils/index.js +0 -18
- package/dist/src/connection-encrypter/utils/index.js.map +0 -1
- package/dist/src/mocks/connection-encrypter.d.ts +0 -3
- package/dist/src/mocks/connection-encrypter.d.ts.map +0 -1
- package/dist/src/mocks/connection-encrypter.js +0 -93
- package/dist/src/mocks/connection-encrypter.js.map +0 -1
- package/dist/src/mocks/connection-gater.d.ts +0 -12
- package/dist/src/mocks/connection-gater.d.ts.map +0 -1
- package/dist/src/mocks/connection-gater.js +0 -14
- package/dist/src/mocks/connection-gater.js.map +0 -1
- package/dist/src/mocks/connection-manager.d.ts +0 -28
- package/dist/src/mocks/connection-manager.d.ts.map +0 -1
- package/dist/src/mocks/connection-manager.js +0 -107
- package/dist/src/mocks/connection-manager.js.map +0 -1
- package/dist/src/mocks/connection.d.ts +0 -18
- package/dist/src/mocks/connection.d.ts.map +0 -1
- package/dist/src/mocks/connection.js +0 -138
- package/dist/src/mocks/connection.js.map +0 -1
- package/dist/src/mocks/duplex.d.ts +0 -3
- package/dist/src/mocks/duplex.d.ts.map +0 -1
- package/dist/src/mocks/duplex.js +0 -7
- package/dist/src/mocks/duplex.js.map +0 -1
- package/dist/src/mocks/index.d.ts +0 -10
- package/dist/src/mocks/index.d.ts.map +0 -1
- package/dist/src/mocks/index.js +0 -9
- package/dist/src/mocks/index.js.map +0 -1
- package/dist/src/mocks/multiaddr-connection.d.ts +0 -17
- package/dist/src/mocks/multiaddr-connection.d.ts.map +0 -1
- package/dist/src/mocks/multiaddr-connection.js +0 -51
- package/dist/src/mocks/multiaddr-connection.js.map +0 -1
- package/dist/src/mocks/muxer.d.ts +0 -9
- package/dist/src/mocks/muxer.d.ts.map +0 -1
- package/dist/src/mocks/muxer.js +0 -302
- package/dist/src/mocks/muxer.js.map +0 -1
- package/dist/src/mocks/registrar.d.ts +0 -18
- package/dist/src/mocks/registrar.d.ts.map +0 -1
- package/dist/src/mocks/registrar.js +0 -73
- package/dist/src/mocks/registrar.js.map +0 -1
- package/dist/src/mocks/upgrader.d.ts +0 -15
- package/dist/src/mocks/upgrader.d.ts.map +0 -1
- package/dist/src/mocks/upgrader.js +0 -33
- package/dist/src/mocks/upgrader.js.map +0 -1
- package/dist/src/peer-discovery/index.d.ts +0 -6
- package/dist/src/peer-discovery/index.d.ts.map +0 -1
- package/dist/src/peer-discovery/index.js +0 -64
- package/dist/src/peer-discovery/index.js.map +0 -1
- package/dist/src/pubsub/api.d.ts +0 -6
- package/dist/src/pubsub/api.d.ts.map +0 -1
- package/dist/src/pubsub/api.js +0 -87
- package/dist/src/pubsub/api.js.map +0 -1
- package/dist/src/pubsub/connection-handlers.d.ts +0 -6
- package/dist/src/pubsub/connection-handlers.d.ts.map +0 -1
- package/dist/src/pubsub/connection-handlers.js +0 -329
- package/dist/src/pubsub/connection-handlers.js.map +0 -1
- package/dist/src/pubsub/emit-self.d.ts +0 -6
- package/dist/src/pubsub/emit-self.d.ts.map +0 -1
- package/dist/src/pubsub/emit-self.js +0 -86
- package/dist/src/pubsub/emit-self.js.map +0 -1
- package/dist/src/pubsub/index.d.ts +0 -10
- package/dist/src/pubsub/index.d.ts.map +0 -1
- package/dist/src/pubsub/index.js +0 -17
- package/dist/src/pubsub/index.js.map +0 -1
- package/dist/src/pubsub/messages.d.ts +0 -6
- package/dist/src/pubsub/messages.d.ts.map +0 -1
- package/dist/src/pubsub/messages.js +0 -46
- package/dist/src/pubsub/messages.js.map +0 -1
- package/dist/src/pubsub/multiple-nodes.d.ts +0 -6
- package/dist/src/pubsub/multiple-nodes.d.ts.map +0 -1
- package/dist/src/pubsub/multiple-nodes.js +0 -350
- package/dist/src/pubsub/multiple-nodes.js.map +0 -1
- package/dist/src/pubsub/two-nodes.d.ts +0 -6
- package/dist/src/pubsub/two-nodes.d.ts.map +0 -1
- package/dist/src/pubsub/two-nodes.js +0 -185
- package/dist/src/pubsub/two-nodes.js.map +0 -1
- package/dist/src/pubsub/utils.d.ts +0 -6
- package/dist/src/pubsub/utils.d.ts.map +0 -1
- package/dist/src/pubsub/utils.js +0 -22
- package/dist/src/pubsub/utils.js.map +0 -1
- package/dist/src/record/index.d.ts +0 -5
- package/dist/src/record/index.d.ts.map +0 -1
- package/dist/src/record/index.js +0 -25
- package/dist/src/record/index.js.map +0 -1
- package/dist/src/stream-muxer/base-test.d.ts +0 -5
- package/dist/src/stream-muxer/base-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/base-test.js +0 -147
- package/dist/src/stream-muxer/base-test.js.map +0 -1
- package/dist/src/stream-muxer/close-test.d.ts +0 -5
- package/dist/src/stream-muxer/close-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/close-test.js +0 -180
- package/dist/src/stream-muxer/close-test.js.map +0 -1
- package/dist/src/stream-muxer/index.d.ts +0 -5
- package/dist/src/stream-muxer/index.d.ts.map +0 -1
- package/dist/src/stream-muxer/index.js +0 -13
- package/dist/src/stream-muxer/index.js.map +0 -1
- package/dist/src/stream-muxer/mega-stress-test.d.ts +0 -5
- package/dist/src/stream-muxer/mega-stress-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/mega-stress-test.js +0 -12
- package/dist/src/stream-muxer/mega-stress-test.js.map +0 -1
- package/dist/src/stream-muxer/spawner.d.ts +0 -4
- package/dist/src/stream-muxer/spawner.d.ts.map +0 -1
- package/dist/src/stream-muxer/spawner.js +0 -32
- package/dist/src/stream-muxer/spawner.js.map +0 -1
- package/dist/src/stream-muxer/stress-test.d.ts +0 -5
- package/dist/src/stream-muxer/stress-test.d.ts.map +0 -1
- package/dist/src/stream-muxer/stress-test.js +0 -26
- package/dist/src/stream-muxer/stress-test.js.map +0 -1
- package/dist/src/topology/topology.d.ts +0 -5
- package/dist/src/topology/topology.d.ts.map +0 -1
- package/dist/src/topology/topology.js +0 -20
- package/dist/src/topology/topology.js.map +0 -1
- package/dist/src/transport/dial-test.d.ts +0 -5
- package/dist/src/transport/dial-test.d.ts.map +0 -1
- package/dist/src/transport/dial-test.js +0 -97
- package/dist/src/transport/dial-test.js.map +0 -1
- package/dist/src/transport/filter-test.d.ts +0 -5
- package/dist/src/transport/filter-test.d.ts.map +0 -1
- package/dist/src/transport/filter-test.js +0 -18
- package/dist/src/transport/filter-test.js.map +0 -1
- package/dist/src/transport/index.d.ts +0 -15
- package/dist/src/transport/index.d.ts.map +0 -1
- package/dist/src/transport/index.js +0 -11
- package/dist/src/transport/index.js.map +0 -1
- package/dist/src/transport/listen-test.d.ts +0 -5
- package/dist/src/transport/listen-test.d.ts.map +0 -1
- package/dist/src/transport/listen-test.js +0 -152
- package/dist/src/transport/listen-test.js.map +0 -1
- package/dist/src/utils/is-valid-tick.d.ts.map +0 -1
- package/dist/src/utils/is-valid-tick.js.map +0 -1
- package/dist/src/utils/peers.d.ts.map +0 -1
- package/dist/src/utils/peers.js.map +0 -1
- package/src/connection/README.md +0 -256
- package/src/connection/connection.ts +0 -176
- package/src/connection/index.ts +0 -7
- package/src/connection-encrypter/index.ts +0 -104
- package/src/connection-encrypter/utils/index.ts +0 -23
- package/src/mocks/connection-encrypter.ts +0 -110
- package/src/mocks/connection-gater.ts +0 -14
- package/src/mocks/connection-manager.ts +0 -141
- package/src/mocks/connection.ts +0 -205
- package/src/mocks/duplex.ts +0 -8
- package/src/mocks/index.ts +0 -10
- package/src/mocks/multiaddr-connection.ts +0 -66
- package/src/mocks/muxer.ts +0 -424
- package/src/mocks/registrar.ts +0 -96
- package/src/mocks/upgrader.ts +0 -46
- package/src/peer-discovery/index.ts +0 -89
- package/src/pubsub/api.ts +0 -115
- package/src/pubsub/connection-handlers.ts +0 -413
- package/src/pubsub/emit-self.ts +0 -105
- package/src/pubsub/index.ts +0 -25
- package/src/pubsub/messages.ts +0 -58
- package/src/pubsub/multiple-nodes.ts +0 -429
- package/src/pubsub/two-nodes.ts +0 -235
- package/src/pubsub/utils.ts +0 -28
- package/src/record/index.ts +0 -32
- package/src/stream-muxer/base-test.ts +0 -190
- package/src/stream-muxer/close-test.ts +0 -227
- package/src/stream-muxer/index.ts +0 -15
- package/src/stream-muxer/mega-stress-test.ts +0 -15
- package/src/stream-muxer/spawner.ts +0 -50
- package/src/stream-muxer/stress-test.ts +0 -29
- package/src/topology/topology.ts +0 -25
- package/src/transport/dial-test.ts +0 -123
- package/src/transport/filter-test.ts +0 -25
- package/src/transport/index.ts +0 -25
- package/src/transport/listen-test.ts +0 -191
package/src/mocks/registrar.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { IncomingStreamData, Registrar, StreamHandler } from '@libp2p/interfaces/registrar'
|
|
2
|
-
import type { Connection } from '@libp2p/interfaces/connection'
|
|
3
|
-
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
4
|
-
import type { Topology } from '@libp2p/interfaces/topology'
|
|
5
|
-
|
|
6
|
-
export class MockRegistrar implements Registrar {
|
|
7
|
-
private readonly topologies: Map<string, { topology: Topology, protocols: string[] }> = new Map()
|
|
8
|
-
private readonly handlers: Map<string, StreamHandler> = new Map()
|
|
9
|
-
|
|
10
|
-
getProtocols () {
|
|
11
|
-
return Array.from(this.handlers.keys()).sort()
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async handle (protocols: string | string[], handler: StreamHandler): Promise<void> {
|
|
15
|
-
const protocolList = Array.isArray(protocols) ? protocols : [protocols]
|
|
16
|
-
|
|
17
|
-
for (const protocol of protocolList) {
|
|
18
|
-
if (this.handlers.has(protocol)) {
|
|
19
|
-
throw new Error(`Handler already registered for protocol ${protocol}`)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
this.handlers.set(protocol, handler)
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async unhandle (protocols: string | string[]) {
|
|
27
|
-
const protocolList = Array.isArray(protocols) ? protocols : [protocols]
|
|
28
|
-
|
|
29
|
-
protocolList.forEach(protocol => {
|
|
30
|
-
this.handlers.delete(protocol)
|
|
31
|
-
})
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
getHandler (protocol: string) {
|
|
35
|
-
const handler = this.handlers.get(protocol)
|
|
36
|
-
|
|
37
|
-
if (handler == null) {
|
|
38
|
-
throw new Error(`No handler registered for protocol ${protocol}`)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return handler
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async register (protocols: string | string[], topology: Topology) {
|
|
45
|
-
if (!Array.isArray(protocols)) {
|
|
46
|
-
protocols = [protocols]
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const id = `topology-id-${Math.random()}`
|
|
50
|
-
|
|
51
|
-
this.topologies.set(id, {
|
|
52
|
-
topology,
|
|
53
|
-
protocols
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
return id
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
unregister (id: string | string[]) {
|
|
60
|
-
if (!Array.isArray(id)) {
|
|
61
|
-
id = [id]
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
id.forEach(id => this.topologies.delete(id))
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
getTopologies (protocol: string) {
|
|
68
|
-
const output: Topology[] = []
|
|
69
|
-
|
|
70
|
-
for (const { topology, protocols } of this.topologies.values()) {
|
|
71
|
-
if (protocols.includes(protocol)) {
|
|
72
|
-
output.push(topology)
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (output.length > 0) {
|
|
77
|
-
return output
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return []
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function mockRegistrar () {
|
|
85
|
-
return new MockRegistrar()
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
export async function mockIncomingStreamEvent (protocol: string, conn: Connection, remotePeer: PeerId): Promise<IncomingStreamData> {
|
|
89
|
-
return {
|
|
90
|
-
...await conn.newStream([protocol]),
|
|
91
|
-
// @ts-expect-error incomplete implementation
|
|
92
|
-
connection: {
|
|
93
|
-
remotePeer
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
package/src/mocks/upgrader.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { expect } from 'aegir/chai'
|
|
2
|
-
import { mockConnection } from './connection.js'
|
|
3
|
-
import type { Upgrader, MultiaddrConnection, UpgraderEvents } from '@libp2p/interfaces/transport'
|
|
4
|
-
import type { Registrar } from '@libp2p/interfaces/registrar'
|
|
5
|
-
import { EventEmitter } from '@libp2p/interfaces/events'
|
|
6
|
-
|
|
7
|
-
export interface MockUpgraderInit {
|
|
8
|
-
registrar?: Registrar
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function ensureProps (multiaddrConnection: MultiaddrConnection) {
|
|
12
|
-
['sink', 'source', 'remoteAddr', 'timeline', 'close'].forEach(prop => {
|
|
13
|
-
expect(multiaddrConnection).to.have.property(prop)
|
|
14
|
-
})
|
|
15
|
-
return multiaddrConnection
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
class MockUpgrader extends EventEmitter<UpgraderEvents> implements Upgrader {
|
|
19
|
-
private readonly registrar?: Registrar
|
|
20
|
-
|
|
21
|
-
constructor (init: MockUpgraderInit = {}) {
|
|
22
|
-
super()
|
|
23
|
-
|
|
24
|
-
this.registrar = init.registrar
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async upgradeOutbound (multiaddrConnection: MultiaddrConnection) {
|
|
28
|
-
ensureProps(multiaddrConnection)
|
|
29
|
-
return mockConnection(multiaddrConnection, {
|
|
30
|
-
direction: 'outbound',
|
|
31
|
-
registrar: this.registrar
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async upgradeInbound (multiaddrConnection: MultiaddrConnection) {
|
|
36
|
-
ensureProps(multiaddrConnection)
|
|
37
|
-
return mockConnection(multiaddrConnection, {
|
|
38
|
-
direction: 'inbound',
|
|
39
|
-
registrar: this.registrar
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function mockUpgrader (init: MockUpgraderInit = {}) {
|
|
45
|
-
return new MockUpgrader(init)
|
|
46
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { expect } from 'aegir/chai'
|
|
2
|
-
import { Multiaddr } from '@multiformats/multiaddr'
|
|
3
|
-
import delay from 'delay'
|
|
4
|
-
import pDefer from 'p-defer'
|
|
5
|
-
import { start, stop } from '@libp2p/interfaces/startable'
|
|
6
|
-
import type { TestSetup } from '../index.js'
|
|
7
|
-
import type { PeerDiscovery } from '@libp2p/interfaces/peer-discovery'
|
|
8
|
-
import type { Startable } from '@libp2p/interfaces/startable'
|
|
9
|
-
|
|
10
|
-
export default (common: TestSetup<PeerDiscovery & Startable>) => {
|
|
11
|
-
describe('interface-peer-discovery compliance tests', () => {
|
|
12
|
-
let discovery: PeerDiscovery & Startable
|
|
13
|
-
|
|
14
|
-
beforeEach(async () => {
|
|
15
|
-
discovery = await common.setup()
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
afterEach('ensure discovery was stopped', async () => {
|
|
19
|
-
await stop(discovery)
|
|
20
|
-
|
|
21
|
-
await common.teardown()
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
it('can start the service', async () => {
|
|
25
|
-
await start(discovery)
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
it('can start and stop the service', async () => {
|
|
29
|
-
await start(discovery)
|
|
30
|
-
await stop(discovery)
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('should not fail to stop the service if it was not started', async () => {
|
|
34
|
-
await stop(discovery)
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it('should not fail to start the service if it is already started', async () => {
|
|
38
|
-
await start(discovery)
|
|
39
|
-
await start(discovery)
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
it('should emit a peer event after start', async () => {
|
|
43
|
-
const defer = pDefer()
|
|
44
|
-
|
|
45
|
-
await start(discovery)
|
|
46
|
-
|
|
47
|
-
discovery.addEventListener('peer', (evt) => {
|
|
48
|
-
const { id, multiaddrs } = evt.detail
|
|
49
|
-
expect(id).to.exist()
|
|
50
|
-
expect(id).to.have.property('type').that.is.oneOf(['RSA', 'Ed25519', 'secp256k1'])
|
|
51
|
-
expect(multiaddrs).to.exist()
|
|
52
|
-
|
|
53
|
-
multiaddrs.forEach((m) => expect(Multiaddr.isMultiaddr(m)).to.eql(true))
|
|
54
|
-
|
|
55
|
-
defer.resolve()
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
await defer.promise
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
it('should not receive a peer event before start', async () => {
|
|
62
|
-
discovery.addEventListener('peer', () => {
|
|
63
|
-
throw new Error('should not receive a peer event before start')
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
await delay(2000)
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
it('should not receive a peer event after stop', async () => {
|
|
70
|
-
const deferStart = pDefer()
|
|
71
|
-
|
|
72
|
-
await start(discovery)
|
|
73
|
-
|
|
74
|
-
discovery.addEventListener('peer', () => {
|
|
75
|
-
deferStart.resolve()
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
await deferStart.promise
|
|
79
|
-
|
|
80
|
-
await stop(discovery)
|
|
81
|
-
|
|
82
|
-
discovery.addEventListener('peer', () => {
|
|
83
|
-
throw new Error('should not receive a peer event after stop')
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
await delay(2000)
|
|
87
|
-
})
|
|
88
|
-
})
|
|
89
|
-
}
|
package/src/pubsub/api.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { expect } from 'aegir/chai'
|
|
2
|
-
import sinon from 'sinon'
|
|
3
|
-
import pDefer from 'p-defer'
|
|
4
|
-
import pWaitFor from 'p-wait-for'
|
|
5
|
-
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
6
|
-
import delay from 'delay'
|
|
7
|
-
import type { TestSetup } from '../index.js'
|
|
8
|
-
import type { PubSub } from '@libp2p/interfaces/pubsub'
|
|
9
|
-
import type { PubSubArgs } from './index.js'
|
|
10
|
-
import type { Components } from '@libp2p/interfaces/components'
|
|
11
|
-
import { createComponents } from './utils.js'
|
|
12
|
-
import { isStartable, start, stop } from '@libp2p/interfaces/startable'
|
|
13
|
-
import { mockNetwork } from '../mocks/connection-manager.js'
|
|
14
|
-
|
|
15
|
-
const topic = 'foo'
|
|
16
|
-
const data = uint8ArrayFromString('bar')
|
|
17
|
-
|
|
18
|
-
export default (common: TestSetup<PubSub, PubSubArgs>) => {
|
|
19
|
-
describe('pubsub api', () => {
|
|
20
|
-
let pubsub: PubSub
|
|
21
|
-
let components: Components
|
|
22
|
-
|
|
23
|
-
// Create pubsub router
|
|
24
|
-
beforeEach(async () => {
|
|
25
|
-
mockNetwork.reset()
|
|
26
|
-
components = await createComponents()
|
|
27
|
-
|
|
28
|
-
pubsub = components.setPubSub(await common.setup({
|
|
29
|
-
components,
|
|
30
|
-
init: {
|
|
31
|
-
emitSelf: true
|
|
32
|
-
}
|
|
33
|
-
}))
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
afterEach(async () => {
|
|
37
|
-
sinon.restore()
|
|
38
|
-
await stop(components)
|
|
39
|
-
await common.teardown()
|
|
40
|
-
mockNetwork.reset()
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
it('can start correctly', async () => {
|
|
44
|
-
if (!isStartable(pubsub)) {
|
|
45
|
-
return
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
sinon.spy(components.getRegistrar(), 'register')
|
|
49
|
-
|
|
50
|
-
await start(components)
|
|
51
|
-
|
|
52
|
-
expect(pubsub.isStarted()).to.equal(true)
|
|
53
|
-
expect(components.getRegistrar().register).to.have.property('callCount', 1)
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
it('can stop correctly', async () => {
|
|
57
|
-
if (!isStartable(pubsub)) {
|
|
58
|
-
return
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
sinon.spy(components.getRegistrar(), 'unregister')
|
|
62
|
-
|
|
63
|
-
await start(components)
|
|
64
|
-
await stop(components)
|
|
65
|
-
|
|
66
|
-
expect(pubsub.isStarted()).to.equal(false)
|
|
67
|
-
expect(components.getRegistrar().unregister).to.have.property('callCount', 1)
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it('can subscribe and unsubscribe correctly', async () => {
|
|
71
|
-
const handler = () => {
|
|
72
|
-
throw new Error('a message should not be received')
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
await start(components)
|
|
76
|
-
pubsub.subscribe(topic)
|
|
77
|
-
pubsub.addEventListener('message', handler)
|
|
78
|
-
|
|
79
|
-
await pWaitFor(() => {
|
|
80
|
-
const topics = pubsub.getTopics()
|
|
81
|
-
return topics.length === 1 && topics[0] === topic
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
pubsub.removeEventListener('message', handler)
|
|
85
|
-
pubsub.unsubscribe(topic)
|
|
86
|
-
|
|
87
|
-
await pWaitFor(() => pubsub.getTopics().length === 0)
|
|
88
|
-
|
|
89
|
-
// Publish to guarantee the handler is not called
|
|
90
|
-
await pubsub.publish(topic, data)
|
|
91
|
-
|
|
92
|
-
// handlers are called async
|
|
93
|
-
await delay(100)
|
|
94
|
-
|
|
95
|
-
await stop(components)
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
it('can subscribe and publish correctly', async () => {
|
|
99
|
-
const defer = pDefer()
|
|
100
|
-
|
|
101
|
-
await start(components)
|
|
102
|
-
|
|
103
|
-
pubsub.subscribe(topic)
|
|
104
|
-
pubsub.addEventListener('message', (evt) => {
|
|
105
|
-
expect(evt).to.have.nested.property('detail.topic', topic)
|
|
106
|
-
expect(evt).to.have.deep.nested.property('detail.data', data)
|
|
107
|
-
defer.resolve()
|
|
108
|
-
})
|
|
109
|
-
await pubsub.publish(topic, data)
|
|
110
|
-
await defer.promise
|
|
111
|
-
|
|
112
|
-
await stop(components)
|
|
113
|
-
})
|
|
114
|
-
})
|
|
115
|
-
}
|