@streamr/dht 100.2.5-beta.0 → 101.0.0-beta.0
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/package.json +7 -7
- package/dist/src/connection/ConnectionLockRpcLocal.d.ts +3 -3
- package/dist/src/connection/ConnectionLockRpcLocal.js +8 -8
- package/dist/src/connection/ConnectionLockRpcLocal.js.map +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js +1 -1
- package/dist/src/connection/ConnectionLockRpcRemote.js.map +1 -1
- package/dist/src/connection/ConnectionManager.d.ts +4 -6
- package/dist/src/connection/ConnectionManager.js +128 -103
- package/dist/src/connection/ConnectionManager.js.map +1 -1
- package/dist/src/connection/ConnectorFacade.d.ts +15 -14
- package/dist/src/connection/ConnectorFacade.js +70 -52
- package/dist/src/connection/ConnectorFacade.js.map +1 -1
- package/dist/src/connection/Handshaker.d.ts +9 -2
- package/dist/src/connection/Handshaker.js +117 -27
- package/dist/src/connection/Handshaker.js.map +1 -1
- package/dist/src/connection/ManagedConnection.d.ts +13 -38
- package/dist/src/connection/ManagedConnection.js +31 -252
- package/dist/src/connection/ManagedConnection.js.map +1 -1
- package/dist/src/connection/OutputBuffer.d.ts +9 -0
- package/dist/src/connection/OutputBuffer.js +26 -0
- package/dist/src/connection/OutputBuffer.js.map +1 -0
- package/dist/src/connection/PendingConnection.d.ts +19 -0
- package/dist/src/connection/PendingConnection.js +59 -0
- package/dist/src/connection/PendingConnection.js.map +1 -0
- package/dist/src/connection/connectivityChecker.js +3 -3
- package/dist/src/connection/connectivityChecker.js.map +1 -1
- package/dist/src/connection/connectivityRequestHandler.js +2 -2
- package/dist/src/connection/connectivityRequestHandler.js.map +1 -1
- package/dist/src/connection/simulator/Simulator.d.ts +1 -3
- package/dist/src/connection/simulator/Simulator.js +1 -4
- package/dist/src/connection/simulator/Simulator.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnection.js +1 -2
- package/dist/src/connection/simulator/SimulatorConnection.js.map +1 -1
- package/dist/src/connection/simulator/SimulatorConnector.d.ts +3 -3
- package/dist/src/connection/simulator/SimulatorConnector.js +28 -21
- package/dist/src/connection/simulator/SimulatorConnector.js.map +1 -1
- package/dist/src/connection/webrtc/NodeWebrtcConnection.d.ts +1 -6
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js +3 -20
- package/dist/src/connection/webrtc/NodeWebrtcConnection.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnector.d.ts +11 -6
- package/dist/src/connection/webrtc/WebrtcConnector.js +57 -42
- package/dist/src/connection/webrtc/WebrtcConnector.js.map +1 -1
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.d.ts +8 -10
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js +21 -44
- package/dist/src/connection/webrtc/WebrtcConnectorRpcLocal.js.map +1 -1
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js +8 -2
- package/dist/src/connection/websocket/AbstractWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/AutoCertifierClientFacade.d.ts +3 -3
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js +8 -8
- package/dist/src/connection/websocket/AutoCertifierClientFacade.js.map +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js +1 -1
- package/dist/src/connection/websocket/NodeWebsocketClientConnection.js.map +1 -1
- package/dist/src/connection/websocket/WebsocketClientConnector.d.ts +26 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js +86 -0
- package/dist/src/connection/websocket/WebsocketClientConnector.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.d.ts +19 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js +23 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcLocal.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.d.ts +5 -0
- package/dist/src/connection/websocket/{WebsocketConnectorRpcRemote.js → WebsocketClientConnectorRpcRemote.js} +4 -4
- package/dist/src/connection/websocket/WebsocketClientConnectorRpcRemote.js.map +1 -0
- package/dist/src/connection/websocket/WebsocketServer.d.ts +8 -5
- package/dist/src/connection/websocket/WebsocketServer.js +11 -11
- package/dist/src/connection/websocket/WebsocketServer.js.map +1 -1
- package/dist/src/connection/websocket/{WebsocketConnector.d.ts → WebsocketServerConnector.d.ts} +16 -21
- package/dist/src/connection/websocket/{WebsocketConnector.js → WebsocketServerConnector.js} +112 -160
- package/dist/src/connection/websocket/WebsocketServerConnector.js.map +1 -0
- package/dist/src/dht/DhtNode.d.ts +4 -4
- package/dist/src/dht/DhtNode.js +85 -84
- package/dist/src/dht/DhtNode.js.map +1 -1
- package/dist/src/dht/DhtNodeRpcLocal.d.ts +3 -3
- package/dist/src/dht/DhtNodeRpcLocal.js +9 -9
- package/dist/src/dht/DhtNodeRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcLocal.d.ts +3 -3
- package/dist/src/dht/ExternalApiRpcLocal.js +5 -5
- package/dist/src/dht/ExternalApiRpcLocal.js.map +1 -1
- package/dist/src/dht/ExternalApiRpcRemote.js +2 -2
- package/dist/src/dht/ExternalApiRpcRemote.js.map +1 -1
- package/dist/src/dht/PeerManager.d.ts +4 -4
- package/dist/src/dht/PeerManager.js +22 -22
- package/dist/src/dht/PeerManager.js.map +1 -1
- package/dist/src/dht/contact/SortedContactList.d.ts +3 -3
- package/dist/src/dht/contact/SortedContactList.js +9 -9
- package/dist/src/dht/contact/SortedContactList.js.map +1 -1
- package/dist/src/dht/discovery/DiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/DiscoverySession.js +21 -21
- package/dist/src/dht/discovery/DiscoverySession.js.map +1 -1
- package/dist/src/dht/discovery/PeerDiscovery.d.ts +3 -3
- package/dist/src/dht/discovery/PeerDiscovery.js +46 -44
- package/dist/src/dht/discovery/PeerDiscovery.js.map +1 -1
- package/dist/src/dht/discovery/RingDiscoverySession.d.ts +3 -3
- package/dist/src/dht/discovery/RingDiscoverySession.js +19 -19
- package/dist/src/dht/discovery/RingDiscoverySession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js +33 -33
- package/dist/src/dht/recursive-operation/RecursiveOperationManager.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.d.ts +3 -3
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js +8 -8
- package/dist/src/dht/recursive-operation/RecursiveOperationRpcLocal.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js +24 -24
- package/dist/src/dht/recursive-operation/RecursiveOperationSession.js.map +1 -1
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.d.ts +4 -4
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js +5 -5
- package/dist/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/Router.d.ts +3 -3
- package/dist/src/dht/routing/Router.js +20 -20
- package/dist/src/dht/routing/Router.js.map +1 -1
- package/dist/src/dht/routing/RouterRpcLocal.d.ts +3 -3
- package/dist/src/dht/routing/RouterRpcLocal.js +16 -16
- package/dist/src/dht/routing/RouterRpcLocal.js.map +1 -1
- package/dist/src/dht/routing/RoutingSession.d.ts +3 -3
- package/dist/src/dht/routing/RoutingSession.js +24 -24
- package/dist/src/dht/routing/RoutingSession.js.map +1 -1
- package/dist/src/dht/store/StoreManager.d.ts +3 -3
- package/dist/src/dht/store/StoreManager.js +25 -25
- package/dist/src/dht/store/StoreManager.js.map +1 -1
- package/dist/src/dht/store/StoreRpcLocal.d.ts +3 -3
- package/dist/src/dht/store/StoreRpcLocal.js +12 -12
- package/dist/src/dht/store/StoreRpcLocal.js.map +1 -1
- package/dist/src/exports.d.ts +3 -0
- package/dist/src/exports.js +5 -1
- package/dist/src/exports.js.map +1 -1
- package/dist/src/proto/google/protobuf/any.d.ts +5 -8
- package/dist/src/proto/google/protobuf/any.js.map +1 -1
- package/dist/src/proto/google/protobuf/empty.d.ts +1 -0
- package/dist/src/proto/google/protobuf/empty.js.map +1 -1
- package/dist/src/proto/google/protobuf/timestamp.d.ts +1 -10
- package/dist/src/proto/google/protobuf/timestamp.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.d.ts +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js +8 -8
- package/dist/src/proto/packages/dht/protos/DhtRpc.client.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.d.ts +3 -3
- package/dist/src/proto/packages/dht/protos/DhtRpc.js +4 -4
- package/dist/src/proto/packages/dht/protos/DhtRpc.js.map +1 -1
- package/dist/src/proto/packages/dht/protos/DhtRpc.server.d.ts +2 -2
- package/dist/src/proto/packages/proto-rpc/protos/ProtoRpc.js +1 -1
- package/dist/src/transport/ListeningRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js +2 -2
- package/dist/src/transport/ListeningRpcCommunicator.js.map +1 -1
- package/dist/src/transport/RoutingRpcCommunicator.d.ts +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js +2 -2
- package/dist/src/transport/RoutingRpcCommunicator.js.map +1 -1
- package/package.json +7 -7
- package/protos/DhtRpc.proto +1 -1
- package/src/connection/ConnectionLockRpcLocal.ts +9 -9
- package/src/connection/ConnectionLockRpcRemote.ts +1 -1
- package/src/connection/ConnectionManager.ts +153 -111
- package/src/connection/ConnectorFacade.ts +84 -61
- package/src/connection/Handshaker.ts +131 -27
- package/src/connection/ManagedConnection.ts +41 -304
- package/src/connection/OutputBuffer.ts +28 -0
- package/src/connection/PendingConnection.ts +68 -0
- package/src/connection/connectivityChecker.ts +2 -2
- package/src/connection/connectivityRequestHandler.ts +1 -1
- package/src/connection/simulator/Simulator.ts +1 -5
- package/src/connection/simulator/SimulatorConnection.ts +1 -2
- package/src/connection/simulator/SimulatorConnector.ts +34 -33
- package/src/connection/webrtc/BrowserWebrtcConnection.ts +0 -6
- package/src/connection/webrtc/NodeWebrtcConnection.ts +3 -24
- package/src/connection/webrtc/WebrtcConnector.ts +73 -62
- package/src/connection/webrtc/WebrtcConnectorRpcLocal.ts +26 -56
- package/src/connection/websocket/AbstractWebsocketClientConnection.ts +8 -2
- package/src/connection/websocket/AutoCertifierClientFacade.ts +11 -11
- package/src/connection/websocket/NodeWebsocketClientConnection.ts +1 -1
- package/src/connection/websocket/WebsocketClientConnector.ts +119 -0
- package/src/connection/websocket/WebsocketClientConnectorRpcLocal.ts +39 -0
- package/src/connection/websocket/{WebsocketConnectorRpcRemote.ts → WebsocketClientConnectorRpcRemote.ts} +2 -2
- package/src/connection/websocket/WebsocketServer.ts +18 -14
- package/src/connection/websocket/{WebsocketConnector.ts → WebsocketServerConnector.ts} +128 -205
- package/src/dht/DhtNode.ts +90 -89
- package/src/dht/DhtNodeRpcLocal.ts +11 -11
- package/src/dht/ExternalApiRpcLocal.ts +6 -6
- package/src/dht/ExternalApiRpcRemote.ts +2 -2
- package/src/dht/PeerManager.ts +24 -24
- package/src/dht/contact/SortedContactList.ts +10 -10
- package/src/dht/discovery/DiscoverySession.ts +24 -24
- package/src/dht/discovery/PeerDiscovery.ts +47 -45
- package/src/dht/discovery/RingDiscoverySession.ts +23 -23
- package/src/dht/recursive-operation/RecursiveOperationManager.ts +36 -36
- package/src/dht/recursive-operation/RecursiveOperationRpcLocal.ts +9 -9
- package/src/dht/recursive-operation/RecursiveOperationSession.ts +25 -25
- package/src/dht/recursive-operation/RecursiveOperationSessionRpcLocal.ts +7 -7
- package/src/dht/routing/Router.ts +21 -21
- package/src/dht/routing/RouterRpcLocal.ts +17 -17
- package/src/dht/routing/RoutingSession.ts +26 -26
- package/src/dht/store/StoreManager.ts +27 -27
- package/src/dht/store/StoreRpcLocal.ts +13 -13
- package/src/exports.ts +3 -0
- package/src/proto/google/protobuf/any.ts +6 -9
- package/src/proto/google/protobuf/empty.ts +2 -1
- package/src/proto/google/protobuf/timestamp.ts +2 -11
- package/src/proto/packages/dht/protos/DhtRpc.client.ts +9 -9
- package/src/proto/packages/dht/protos/DhtRpc.server.ts +3 -3
- package/src/proto/packages/dht/protos/DhtRpc.ts +4 -4
- package/src/proto/packages/proto-rpc/protos/ProtoRpc.ts +1 -1
- package/src/transport/ListeningRpcCommunicator.ts +3 -3
- package/src/transport/RoutingRpcCommunicator.ts +3 -3
- package/test/end-to-end/Layer0Webrtc.test.ts +0 -10
- package/test/integration/ConnectionManager.test.ts +3 -2
- package/test/integration/GeoIpConnectivityChecking.test.ts +1 -1
- package/test/integration/SimultaneousConnections.test.ts +2 -2
- package/test/integration/WebrtcConnectionManagement.test.ts +2 -10
- package/test/integration/{WebsocketConnectorRpc.test.ts → WebsocketClientConnectorRpc.test.ts} +9 -9
- package/test/integration/WebsocketConnectionManagement.test.ts +11 -29
- package/test/unit/ConnectionManager.test.ts +64 -0
- package/test/unit/DiscoverySession.test.ts +1 -1
- package/test/unit/Handshaker.test.ts +169 -0
- package/test/unit/ManagedConnection.test.ts +58 -0
- package/test/unit/PendingConnection.test.ts +57 -0
- package/test/unit/WebrtcConnector.test.ts +56 -0
- package/test/unit/{WebsocketConnector.test.ts → WebsocketClientConnector.test.ts} +56 -11
- package/test/unit/WebsocketServerConnector.test.ts +102 -0
- package/test/utils/FakeConnectorFacade.ts +41 -0
- package/test/utils/mock/MockConnection.ts +26 -0
- package/test/utils/utils.ts +2 -2
- package/dist/src/connection/IConnectionSource.d.ts +0 -4
- package/dist/src/connection/IConnectionSource.js +0 -3
- package/dist/src/connection/IConnectionSource.js.map +0 -1
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.d.ts +0 -7
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js +0 -20
- package/dist/src/connection/webrtc/ManagedWebrtcConnection.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnector.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.d.ts +0 -19
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js +0 -23
- package/dist/src/connection/websocket/WebsocketConnectorRpcLocal.js.map +0 -1
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.d.ts +0 -5
- package/dist/src/connection/websocket/WebsocketConnectorRpcRemote.js.map +0 -1
- package/src/connection/IConnectionSource.ts +0 -6
- package/src/connection/webrtc/ManagedWebrtcConnection.ts +0 -27
- package/src/connection/websocket/WebsocketConnectorRpcLocal.ts +0 -39
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Message, PeerDescriptor } from '../proto/packages/dht/protos/DhtRpc'
|
|
2
2
|
import { v4 } from 'uuid'
|
|
3
|
-
import { RpcCommunicator,
|
|
3
|
+
import { RpcCommunicator, RpcCommunicatorOptions } from '@streamr/proto-rpc'
|
|
4
4
|
import { DhtCallContext } from '../rpc-protocol/DhtCallContext'
|
|
5
5
|
import { RpcMessage } from '../proto/packages/proto-rpc/protos/ProtoRpc'
|
|
6
6
|
import { ServiceID } from '../types/ServiceID'
|
|
@@ -13,9 +13,9 @@ export class RoutingRpcCommunicator extends RpcCommunicator<DhtCallContext> {
|
|
|
13
13
|
constructor(
|
|
14
14
|
ownServiceId: ServiceID,
|
|
15
15
|
sendFn: (msg: Message, opts: SendOptions) => Promise<void>,
|
|
16
|
-
|
|
16
|
+
options?: RpcCommunicatorOptions
|
|
17
17
|
) {
|
|
18
|
-
super(
|
|
18
|
+
super(options)
|
|
19
19
|
this.ownServiceId = ownServiceId
|
|
20
20
|
this.sendFn = sendFn
|
|
21
21
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { areEqualBinaries, waitForEvent3 } from '@streamr/utils'
|
|
2
2
|
import { ConnectionManager } from '../../src/connection/ConnectionManager'
|
|
3
|
-
import { ConnectionType } from '../../src/connection/IConnection'
|
|
4
3
|
import { DhtNode } from '../../src/dht/DhtNode'
|
|
5
4
|
import { PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
6
5
|
import { createMockPeerDescriptor } from '../utils/utils'
|
|
@@ -69,11 +68,6 @@ describe('Layer0 with WebRTC connections', () => {
|
|
|
69
68
|
const nodeId2 = getNodeIdFromPeerDescriptor(node2.getLocalPeerDescriptor())
|
|
70
69
|
expect((node1.getTransport() as ConnectionManager).hasConnection(nodeId2)).toEqual(true)
|
|
71
70
|
expect((node2.getTransport() as ConnectionManager).hasConnection(nodeId1)).toEqual(true)
|
|
72
|
-
expect((node1.getTransport() as ConnectionManager).getConnection(nodeId2)!.connectionType)
|
|
73
|
-
.toEqual(ConnectionType.WEBRTC)
|
|
74
|
-
expect((node2.getTransport() as ConnectionManager).getConnection(nodeId1)!.connectionType)
|
|
75
|
-
.toEqual(ConnectionType.WEBRTC)
|
|
76
|
-
|
|
77
71
|
}, 60000)
|
|
78
72
|
|
|
79
73
|
it('Happy path simultaneous joins', async () => {
|
|
@@ -87,9 +81,5 @@ describe('Layer0 with WebRTC connections', () => {
|
|
|
87
81
|
const nodeId2 = getNodeIdFromPeerDescriptor(node2.getLocalPeerDescriptor())
|
|
88
82
|
expect((node1.getTransport() as ConnectionManager).hasConnection(nodeId2)).toEqual(true)
|
|
89
83
|
expect((node2.getTransport() as ConnectionManager).hasConnection(nodeId1)).toEqual(true)
|
|
90
|
-
expect((node1.getTransport() as ConnectionManager).getConnection(nodeId2)!.connectionType)
|
|
91
|
-
.toEqual(ConnectionType.WEBRTC)
|
|
92
|
-
expect((node2.getTransport() as ConnectionManager).getConnection(nodeId1)!.connectionType)
|
|
93
|
-
.toEqual(ConnectionType.WEBRTC)
|
|
94
84
|
})
|
|
95
85
|
})
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Logger, MetricsContext, waitForEvent3 } from '@streamr/utils'
|
|
2
2
|
import { MarkOptional } from 'ts-essentials'
|
|
3
3
|
import { ConnectionManager } from '../../src/connection/ConnectionManager'
|
|
4
|
-
import { DefaultConnectorFacade,
|
|
4
|
+
import { DefaultConnectorFacade, DefaultConnectorFacadeOptions } from '../../src/connection/ConnectorFacade'
|
|
5
5
|
import { Simulator } from '../../src/connection/simulator/Simulator'
|
|
6
6
|
import { SimulatorTransport } from '../../src/connection/simulator/SimulatorTransport'
|
|
7
7
|
import { createPeerDescriptor } from '../../src/helpers/createPeerDescriptor'
|
|
@@ -30,7 +30,7 @@ describe('ConnectionManager', () => {
|
|
|
30
30
|
const mockConnectorTransport2 = new SimulatorTransport(mockPeerDescriptor2, simulator)
|
|
31
31
|
let createLocalPeerDescriptor: jest.Mock<PeerDescriptor, [ConnectivityResponse]>
|
|
32
32
|
|
|
33
|
-
const createConnectionManager = (opts: MarkOptional<
|
|
33
|
+
const createConnectionManager = (opts: MarkOptional<DefaultConnectorFacadeOptions, 'createLocalPeerDescriptor'>) => {
|
|
34
34
|
return new ConnectionManager({
|
|
35
35
|
createConnectorFacade: () => new DefaultConnectorFacade({
|
|
36
36
|
createLocalPeerDescriptor: async (response) => createLocalPeerDescriptor(response),
|
|
@@ -304,6 +304,7 @@ describe('ConnectionManager', () => {
|
|
|
304
304
|
})
|
|
305
305
|
msg.targetDescriptor = mockPeerDescriptor4
|
|
306
306
|
connectionManager3.send(msg)
|
|
307
|
+
|
|
307
308
|
await Promise.all([dataPromise, connectedPromise1, connectedPromise2])
|
|
308
309
|
|
|
309
310
|
// @ts-expect-error private field
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { MetricsContext, waitForCondition } from '@streamr/utils'
|
|
2
2
|
import { ConnectionManager } from '../../src/connection/ConnectionManager'
|
|
3
|
-
import { DefaultConnectorFacade,
|
|
3
|
+
import { DefaultConnectorFacade, DefaultConnectorFacadeOptions } from '../../src/connection/ConnectorFacade'
|
|
4
4
|
import { LatencyType, Simulator } from '../../src/connection/simulator/Simulator'
|
|
5
5
|
import { SimulatorTransport } from '../../src/connection/simulator/SimulatorTransport'
|
|
6
6
|
import { Message, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
@@ -19,7 +19,7 @@ const BASE_MESSAGE: Message = {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
const createConnectionManager = (localPeerDescriptor: PeerDescriptor, opts: Omit<
|
|
22
|
+
const createConnectionManager = (localPeerDescriptor: PeerDescriptor, opts: Omit<DefaultConnectorFacadeOptions, 'createLocalPeerDescriptor'>) => {
|
|
23
23
|
return new ConnectionManager({
|
|
24
24
|
createConnectorFacade: () => new DefaultConnectorFacade({
|
|
25
25
|
createLocalPeerDescriptor: async () => localPeerDescriptor,
|
|
@@ -2,14 +2,12 @@ import { ConnectionManager } from '../../src/connection/ConnectionManager'
|
|
|
2
2
|
import { LatencyType, Simulator } from '../../src/connection/simulator/Simulator'
|
|
3
3
|
import { Message, NodeType, PeerDescriptor } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
4
4
|
import { RpcMessage } from '../../src/proto/packages/proto-rpc/protos/ProtoRpc'
|
|
5
|
-
import { ConnectionType } from '../../src/connection/IConnection'
|
|
6
5
|
import { ITransport } from '../../src/transport/ITransport'
|
|
7
6
|
import * as Err from '../../src/helpers/errors'
|
|
8
7
|
import { SimulatorTransport } from '../../src/connection/simulator/SimulatorTransport'
|
|
9
8
|
import { DefaultConnectorFacade } from '../../src/connection/ConnectorFacade'
|
|
10
9
|
import { MetricsContext } from '@streamr/utils'
|
|
11
10
|
import { createMockPeerDescriptor } from '../utils/utils'
|
|
12
|
-
import { getNodeIdFromPeerDescriptor } from '../../src/identifiers'
|
|
13
11
|
|
|
14
12
|
const createConnectionManager = (localPeerDescriptor: PeerDescriptor, transport: ITransport) => {
|
|
15
13
|
return new ConnectionManager({
|
|
@@ -32,7 +30,7 @@ describe('WebRTC Connection Management', () => {
|
|
|
32
30
|
let connectorTransport2: SimulatorTransport
|
|
33
31
|
|
|
34
32
|
beforeEach(async () => {
|
|
35
|
-
simulator = new Simulator(LatencyType.FIXED,
|
|
33
|
+
simulator = new Simulator(LatencyType.FIXED, 20)
|
|
36
34
|
connectorTransport1 = new SimulatorTransport(peerDescriptor1, simulator)
|
|
37
35
|
await connectorTransport1.start()
|
|
38
36
|
manager1 = createConnectionManager(peerDescriptor1, connectorTransport1)
|
|
@@ -68,8 +66,6 @@ describe('WebRTC Connection Management', () => {
|
|
|
68
66
|
|
|
69
67
|
manager2.on('message', (message: Message) => {
|
|
70
68
|
expect(message.messageId).toEqual('mockerer')
|
|
71
|
-
expect(manager1.getConnection(getNodeIdFromPeerDescriptor(peerDescriptor2))!.connectionType).toEqual(ConnectionType.WEBRTC)
|
|
72
|
-
expect(manager2.getConnection(getNodeIdFromPeerDescriptor(peerDescriptor1))!.connectionType).toEqual(ConnectionType.WEBRTC)
|
|
73
69
|
|
|
74
70
|
done()
|
|
75
71
|
})
|
|
@@ -77,7 +73,7 @@ describe('WebRTC Connection Management', () => {
|
|
|
77
73
|
manager1.send(dummyMessage).catch((e) => {
|
|
78
74
|
throw e
|
|
79
75
|
})
|
|
80
|
-
},
|
|
76
|
+
}, 15000)
|
|
81
77
|
|
|
82
78
|
it('Peer2 can open WebRTC Datachannel', (done) => {
|
|
83
79
|
const dummyMessage: Message = {
|
|
@@ -90,9 +86,6 @@ describe('WebRTC Connection Management', () => {
|
|
|
90
86
|
}
|
|
91
87
|
manager1.on('message', (message: Message) => {
|
|
92
88
|
expect(message.messageId).toEqual('mockerer')
|
|
93
|
-
expect(manager1.getConnection(getNodeIdFromPeerDescriptor(peerDescriptor2))!.connectionType).toEqual(ConnectionType.WEBRTC)
|
|
94
|
-
expect(manager2.getConnection(getNodeIdFromPeerDescriptor(peerDescriptor1))!.connectionType).toEqual(ConnectionType.WEBRTC)
|
|
95
|
-
|
|
96
89
|
done()
|
|
97
90
|
})
|
|
98
91
|
dummyMessage.targetDescriptor = peerDescriptor1
|
|
@@ -221,6 +214,5 @@ describe('WebRTC Connection Management', () => {
|
|
|
221
214
|
manager1.send(msg),
|
|
222
215
|
disconnectedPromise1
|
|
223
216
|
])
|
|
224
|
-
expect(manager1.getConnection(getNodeIdFromPeerDescriptor(msg.targetDescriptor))).toBeUndefined()
|
|
225
217
|
}, 20000)
|
|
226
218
|
})
|
package/test/integration/{WebsocketConnectorRpc.test.ts → WebsocketClientConnectorRpc.test.ts}
RENAMED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { ProtoRpcClient, RpcCommunicator, toProtoRpcClient } from '@streamr/proto-rpc'
|
|
2
|
-
import {
|
|
2
|
+
import { WebsocketClientConnectorRpcClient } from '../../src/proto/packages/dht/protos/DhtRpc.client'
|
|
3
3
|
import { createMockPeerDescriptor } from '../utils/utils'
|
|
4
4
|
import {
|
|
5
5
|
WebsocketConnectionRequest
|
|
6
6
|
} from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
7
|
-
import {
|
|
7
|
+
import { mockWebsocketClientConnectorRpc } from '../utils/utils'
|
|
8
8
|
import { RpcMessage } from '../../src/proto/packages/proto-rpc/protos/ProtoRpc'
|
|
9
9
|
import { Empty } from '../../src/proto/google/protobuf/empty'
|
|
10
10
|
import { DhtCallContext } from '../../src/rpc-protocol/DhtCallContext'
|
|
11
11
|
|
|
12
|
-
describe('
|
|
12
|
+
describe('WebsocketClientConnectorRpc', () => {
|
|
13
13
|
|
|
14
14
|
let rpcCommunicator1: RpcCommunicator<DhtCallContext>
|
|
15
15
|
let rpcCommunicator2: RpcCommunicator<DhtCallContext>
|
|
16
|
-
let client1: ProtoRpcClient<
|
|
17
|
-
let client2: ProtoRpcClient<
|
|
16
|
+
let client1: ProtoRpcClient<WebsocketClientConnectorRpcClient>
|
|
17
|
+
let client2: ProtoRpcClient<WebsocketClientConnectorRpcClient>
|
|
18
18
|
const peerDescriptor1 = createMockPeerDescriptor()
|
|
19
19
|
const peerDescriptor2 = createMockPeerDescriptor()
|
|
20
20
|
|
|
@@ -24,7 +24,7 @@ describe('WebsocketConnectorRpc', () => {
|
|
|
24
24
|
WebsocketConnectionRequest,
|
|
25
25
|
Empty,
|
|
26
26
|
'requestConnection',
|
|
27
|
-
|
|
27
|
+
mockWebsocketClientConnectorRpc.requestConnection
|
|
28
28
|
)
|
|
29
29
|
|
|
30
30
|
rpcCommunicator2 = new RpcCommunicator()
|
|
@@ -32,7 +32,7 @@ describe('WebsocketConnectorRpc', () => {
|
|
|
32
32
|
WebsocketConnectionRequest,
|
|
33
33
|
Empty,
|
|
34
34
|
'requestConnection',
|
|
35
|
-
|
|
35
|
+
mockWebsocketClientConnectorRpc.requestConnection
|
|
36
36
|
)
|
|
37
37
|
|
|
38
38
|
rpcCommunicator1.on('outgoingMessage', (message: RpcMessage) => {
|
|
@@ -43,8 +43,8 @@ describe('WebsocketConnectorRpc', () => {
|
|
|
43
43
|
rpcCommunicator1.handleIncomingMessage(message)
|
|
44
44
|
})
|
|
45
45
|
|
|
46
|
-
client1 = toProtoRpcClient(new
|
|
47
|
-
client2 = toProtoRpcClient(new
|
|
46
|
+
client1 = toProtoRpcClient(new WebsocketClientConnectorRpcClient(rpcCommunicator1.getRpcClientTransport()))
|
|
47
|
+
client2 = toProtoRpcClient(new WebsocketClientConnectorRpcClient(rpcCommunicator2.getRpcClientTransport()))
|
|
48
48
|
})
|
|
49
49
|
|
|
50
50
|
afterEach(async () => {
|
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { MetricsContext, waitForCondition, waitForEvent3 } from '@streamr/utils'
|
|
4
4
|
import { ConnectionManager } from '../../src/connection/ConnectionManager'
|
|
5
|
-
import { DefaultConnectorFacade,
|
|
6
|
-
import { ConnectionType } from '../../src/connection/IConnection'
|
|
5
|
+
import { DefaultConnectorFacade, DefaultConnectorFacadeOptions } from '../../src/connection/ConnectorFacade'
|
|
7
6
|
import { Simulator } from '../../src/connection/simulator/Simulator'
|
|
8
7
|
import { SimulatorTransport } from '../../src/connection/simulator/SimulatorTransport'
|
|
9
8
|
import * as Err from '../../src/helpers/errors'
|
|
@@ -14,7 +13,7 @@ import { getNodeIdFromPeerDescriptor } from '../../src/identifiers'
|
|
|
14
13
|
|
|
15
14
|
const SERVICE_ID = 'test'
|
|
16
15
|
|
|
17
|
-
const
|
|
16
|
+
const createOptions = (localPeerDescriptor: PeerDescriptor, opts: Omit<DefaultConnectorFacadeOptions, 'createLocalPeerDescriptor'>) => {
|
|
18
17
|
return {
|
|
19
18
|
createConnectorFacade: () => new DefaultConnectorFacade({
|
|
20
19
|
createLocalPeerDescriptor: async () => localPeerDescriptor,
|
|
@@ -61,21 +60,21 @@ describe('Websocket Connection Management', () => {
|
|
|
61
60
|
connectorTransport3 = new SimulatorTransport(biggerNoWsServerConnectorPeerDescriptor, simulator)
|
|
62
61
|
await connectorTransport3.start()
|
|
63
62
|
|
|
64
|
-
const
|
|
63
|
+
const options1 = createOptions(wsServerConnectorPeerDescriptor, {
|
|
65
64
|
transport: connectorTransport1,
|
|
66
65
|
websocketHost: '127.0.0.1',
|
|
67
66
|
websocketPortRange: { min: 12223, max: 12223 }
|
|
68
67
|
})
|
|
69
|
-
const
|
|
68
|
+
const options2 = createOptions(noWsServerConnectorPeerDescriptor, {
|
|
70
69
|
transport: connectorTransport2
|
|
71
70
|
})
|
|
72
|
-
const
|
|
71
|
+
const options3 = createOptions(biggerNoWsServerConnectorPeerDescriptor, {
|
|
73
72
|
transport: connectorTransport3
|
|
74
73
|
})
|
|
75
74
|
|
|
76
|
-
wsServerManager = new ConnectionManager(
|
|
77
|
-
noWsServerManager = new ConnectionManager(
|
|
78
|
-
biggerNoWsServerManager = new ConnectionManager(
|
|
75
|
+
wsServerManager = new ConnectionManager(options1)
|
|
76
|
+
noWsServerManager = new ConnectionManager(options2)
|
|
77
|
+
biggerNoWsServerManager = new ConnectionManager(options3)
|
|
79
78
|
|
|
80
79
|
await wsServerManager.start()
|
|
81
80
|
await noWsServerManager.start()
|
|
@@ -103,12 +102,6 @@ describe('Websocket Connection Management', () => {
|
|
|
103
102
|
}
|
|
104
103
|
noWsServerManager.on('message', (message: Message) => {
|
|
105
104
|
expect(message.messageId).toEqual('mockerer')
|
|
106
|
-
expect(wsServerManager.getConnection(getNodeIdFromPeerDescriptor(noWsServerConnectorPeerDescriptor))!.connectionType).toEqual(
|
|
107
|
-
ConnectionType.WEBSOCKET_SERVER
|
|
108
|
-
)
|
|
109
|
-
expect(noWsServerManager.getConnection(getNodeIdFromPeerDescriptor(wsServerConnectorPeerDescriptor))!.connectionType).toEqual(
|
|
110
|
-
ConnectionType.WEBSOCKET_CLIENT
|
|
111
|
-
)
|
|
112
105
|
|
|
113
106
|
done()
|
|
114
107
|
})
|
|
@@ -128,13 +121,6 @@ describe('Websocket Connection Management', () => {
|
|
|
128
121
|
}
|
|
129
122
|
biggerNoWsServerManager.on('message', (message: Message) => {
|
|
130
123
|
expect(message.messageId).toEqual('mockerer')
|
|
131
|
-
expect(wsServerManager.getConnection(getNodeIdFromPeerDescriptor(biggerNoWsServerConnectorPeerDescriptor))!.connectionType).toEqual(
|
|
132
|
-
ConnectionType.WEBSOCKET_SERVER
|
|
133
|
-
)
|
|
134
|
-
expect(biggerNoWsServerManager.getConnection(getNodeIdFromPeerDescriptor(wsServerConnectorPeerDescriptor))!.connectionType).toEqual(
|
|
135
|
-
ConnectionType.WEBSOCKET_CLIENT
|
|
136
|
-
)
|
|
137
|
-
|
|
138
124
|
done()
|
|
139
125
|
})
|
|
140
126
|
|
|
@@ -159,7 +145,7 @@ describe('Websocket Connection Management', () => {
|
|
|
159
145
|
waitForEvent3<TransportEvents>(wsServerManager, 'disconnected', 15000),
|
|
160
146
|
wsServerManager.send(dummyMessage)
|
|
161
147
|
])
|
|
162
|
-
expect(wsServerManager.
|
|
148
|
+
expect(wsServerManager.hasConnection(getNodeIdFromPeerDescriptor(dummyMessage.targetDescriptor!))).toBeFalse()
|
|
163
149
|
}, 20000)
|
|
164
150
|
|
|
165
151
|
it('Can open connections to peer with server', async () => {
|
|
@@ -176,15 +162,11 @@ describe('Websocket Connection Management', () => {
|
|
|
176
162
|
await waitForCondition(
|
|
177
163
|
() => {
|
|
178
164
|
const nodeId = getNodeIdFromPeerDescriptor(noWsServerConnectorPeerDescriptor)
|
|
179
|
-
return
|
|
180
|
-
&& wsServerManager.getConnection(nodeId)!.connectionType === ConnectionType.WEBSOCKET_SERVER)
|
|
165
|
+
return wsServerManager.hasConnection(nodeId)
|
|
181
166
|
}
|
|
182
167
|
)
|
|
183
168
|
await waitForCondition(
|
|
184
|
-
() =>
|
|
185
|
-
const connection = noWsServerManager.getConnection(getNodeIdFromPeerDescriptor(wsServerConnectorPeerDescriptor))!
|
|
186
|
-
return connection.connectionType === ConnectionType.WEBSOCKET_CLIENT
|
|
187
|
-
}
|
|
169
|
+
() => noWsServerManager.hasConnection(getNodeIdFromPeerDescriptor(wsServerConnectorPeerDescriptor))
|
|
188
170
|
)
|
|
189
171
|
})
|
|
190
172
|
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { MetricsContext } from '@streamr/utils'
|
|
2
|
+
import { ConnectionManager } from '../../src/connection/ConnectionManager'
|
|
3
|
+
import { getNodeIdFromPeerDescriptor, PendingConnection } from '../../src/exports'
|
|
4
|
+
import { FakeConnectorFacade } from '../utils/FakeConnectorFacade'
|
|
5
|
+
import { MockConnection } from '../utils/mock/MockConnection'
|
|
6
|
+
import { createMockPeerDescriptor } from '../utils/utils'
|
|
7
|
+
import { getOfferer } from '../../src/helpers/offering'
|
|
8
|
+
|
|
9
|
+
describe('ConnetionManager', () => {
|
|
10
|
+
|
|
11
|
+
let connectionManager: ConnectionManager
|
|
12
|
+
let fakeConnectorFacade: FakeConnectorFacade
|
|
13
|
+
const localPeerDescriptor = createMockPeerDescriptor()
|
|
14
|
+
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
connectionManager = new ConnectionManager({
|
|
17
|
+
metricsContext: new MetricsContext(),
|
|
18
|
+
createConnectorFacade: () => {
|
|
19
|
+
fakeConnectorFacade = new FakeConnectorFacade(localPeerDescriptor)
|
|
20
|
+
return fakeConnectorFacade
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
await connectionManager.start()
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
await connectionManager.stop()
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('should replace a duplicate connecting connection', () => {
|
|
31
|
+
const remotePeerDescriptor = createMockPeerDescriptor()
|
|
32
|
+
const pendingConnection1 = new PendingConnection(remotePeerDescriptor)
|
|
33
|
+
const offerer = getOfferer(getNodeIdFromPeerDescriptor(localPeerDescriptor), getNodeIdFromPeerDescriptor(remotePeerDescriptor))
|
|
34
|
+
const accepted1 = fakeConnectorFacade.callOnNewConnection(pendingConnection1)
|
|
35
|
+
expect(accepted1).toBeTrue()
|
|
36
|
+
const pendingConnection2 = new PendingConnection(remotePeerDescriptor)
|
|
37
|
+
const accepted2 = fakeConnectorFacade.callOnNewConnection(pendingConnection2)
|
|
38
|
+
|
|
39
|
+
expect(accepted2).toBe(offerer === 'remote')
|
|
40
|
+
|
|
41
|
+
pendingConnection1.close(true)
|
|
42
|
+
pendingConnection2.close(true)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should replace a duplicate connected connection', () => {
|
|
46
|
+
const remotePeerDescriptor = createMockPeerDescriptor()
|
|
47
|
+
const pendingConnection1 = new PendingConnection(remotePeerDescriptor)
|
|
48
|
+
const offerer = getOfferer(getNodeIdFromPeerDescriptor(localPeerDescriptor), getNodeIdFromPeerDescriptor(remotePeerDescriptor))
|
|
49
|
+
const accepted1 = fakeConnectorFacade.callOnNewConnection(pendingConnection1)
|
|
50
|
+
expect(accepted1).toBeTrue()
|
|
51
|
+
pendingConnection1.onHandshakeCompleted(new MockConnection())
|
|
52
|
+
const pendingConnection2 = new PendingConnection(remotePeerDescriptor)
|
|
53
|
+
const accepted2 = fakeConnectorFacade.callOnNewConnection(pendingConnection2)
|
|
54
|
+
if (accepted2) {
|
|
55
|
+
pendingConnection1.onHandshakeCompleted(new MockConnection())
|
|
56
|
+
}
|
|
57
|
+
expect(accepted2).toBe(offerer === 'remote')
|
|
58
|
+
|
|
59
|
+
pendingConnection1.close(true)
|
|
60
|
+
pendingConnection2.close(true)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
})
|
|
64
|
+
|
|
@@ -12,7 +12,7 @@ const NODE_COUNT = 40
|
|
|
12
12
|
const MIN_NEIGHBOR_COUNT = 2 // nodes can get more neighbors when we merge network partitions
|
|
13
13
|
const PARALLELISM = 1
|
|
14
14
|
const NO_PROGRESS_LIMIT = 1
|
|
15
|
-
const QUERY_BATCH_SIZE = 5 // the default value in DhtNode's
|
|
15
|
+
const QUERY_BATCH_SIZE = 5 // the default value in DhtNode's options, not relevant in this test
|
|
16
16
|
|
|
17
17
|
const createPeerDescriptor = (nodeId: DhtAddress): PeerDescriptor => {
|
|
18
18
|
return {
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import EventEmitter from 'eventemitter3'
|
|
2
|
+
import {
|
|
3
|
+
acceptHandshake,
|
|
4
|
+
createHandshakeRequest,
|
|
5
|
+
createHandshakeResponse,
|
|
6
|
+
createIncomingHandshaker,
|
|
7
|
+
createOutgoingHandshaker,
|
|
8
|
+
Handshaker,
|
|
9
|
+
rejectHandshake
|
|
10
|
+
} from '../../src/connection/Handshaker'
|
|
11
|
+
import { ConnectionEvents, IConnection } from '../../src/connection/IConnection'
|
|
12
|
+
import { createMockPeerDescriptor } from '../utils/utils'
|
|
13
|
+
import { HandshakeError, Message } from '../../src/proto/packages/dht/protos/DhtRpc'
|
|
14
|
+
import { PendingConnection } from '../../src/connection/PendingConnection'
|
|
15
|
+
|
|
16
|
+
describe('Handshaker', () => {
|
|
17
|
+
|
|
18
|
+
let handshaker: Handshaker
|
|
19
|
+
let pendingConnection: PendingConnection
|
|
20
|
+
let connection: IConnection
|
|
21
|
+
|
|
22
|
+
let mockOnHandshakeCompleted: () => void
|
|
23
|
+
let mockSend: () => void
|
|
24
|
+
let mockConnectionClose: () => void
|
|
25
|
+
let mockPendingConnectionClose: () => void
|
|
26
|
+
let mockPendingConnectionDestroy: () => void
|
|
27
|
+
let mockConnectionDestroy: () => void
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
mockOnHandshakeCompleted = jest.fn()
|
|
31
|
+
mockPendingConnectionClose = jest.fn()
|
|
32
|
+
mockPendingConnectionDestroy = jest.fn()
|
|
33
|
+
mockConnectionDestroy = jest.fn()
|
|
34
|
+
pendingConnection = new class extends EventEmitter {
|
|
35
|
+
// eslint-disable-next-line class-methods-use-this
|
|
36
|
+
attachConnection() {
|
|
37
|
+
mockOnHandshakeCompleted()
|
|
38
|
+
}
|
|
39
|
+
// eslint-disable-next-line class-methods-use-this
|
|
40
|
+
close() {
|
|
41
|
+
mockPendingConnectionClose()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// eslint-disable-next-line class-methods-use-this
|
|
45
|
+
destroy() {
|
|
46
|
+
mockPendingConnectionDestroy()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// eslint-disable-next-line class-methods-use-this
|
|
50
|
+
onHandshakeCompleted(_connection: IConnection) {
|
|
51
|
+
mockOnHandshakeCompleted()
|
|
52
|
+
}
|
|
53
|
+
} as any
|
|
54
|
+
|
|
55
|
+
mockSend = jest.fn()
|
|
56
|
+
mockConnectionClose = jest.fn()
|
|
57
|
+
connection = new class extends EventEmitter<ConnectionEvents> {
|
|
58
|
+
// eslint-disable-next-line class-methods-use-this
|
|
59
|
+
send(_message: any) {
|
|
60
|
+
mockSend()
|
|
61
|
+
}
|
|
62
|
+
// eslint-disable-next-line class-methods-use-this
|
|
63
|
+
close() {
|
|
64
|
+
mockConnectionClose()
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// eslint-disable-next-line class-methods-use-this
|
|
68
|
+
destroy() {
|
|
69
|
+
mockConnectionDestroy()
|
|
70
|
+
}
|
|
71
|
+
} as any
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
describe('Outgoing', () => {
|
|
75
|
+
|
|
76
|
+
beforeEach(() => {
|
|
77
|
+
handshaker = createOutgoingHandshaker(
|
|
78
|
+
createMockPeerDescriptor(),
|
|
79
|
+
pendingConnection,
|
|
80
|
+
connection,
|
|
81
|
+
createMockPeerDescriptor()
|
|
82
|
+
)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
afterEach(() => {
|
|
86
|
+
handshaker.stop()
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('sends request after connected', () => {
|
|
90
|
+
(connection as any).emit('connected')
|
|
91
|
+
expect(mockSend).toHaveBeenCalledTimes(1)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('onHandshakeCompleted', () => {
|
|
95
|
+
const message = createHandshakeResponse(createMockPeerDescriptor());
|
|
96
|
+
(connection as any).emit('data', Message.toBinary(message))
|
|
97
|
+
handshaker.emit('handshakeCompleted', createMockPeerDescriptor())
|
|
98
|
+
expect(mockOnHandshakeCompleted).toHaveBeenCalledTimes(1)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('onHandshakeFailed invalid PeerDescriptor', () => {
|
|
102
|
+
handshaker.emit('handshakeFailed', HandshakeError.INVALID_TARGET_PEER_DESCRIPTOR)
|
|
103
|
+
expect(mockOnHandshakeCompleted).not.toHaveBeenCalled()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('onHandshakeFailed unsupported version', () => {
|
|
107
|
+
handshaker.emit('handshakeFailed', HandshakeError.UNSUPPORTED_VERSION)
|
|
108
|
+
expect(mockOnHandshakeCompleted).not.toHaveBeenCalled()
|
|
109
|
+
expect(mockPendingConnectionClose).toHaveBeenCalledTimes(1)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it('onHandShakeFailed ', () => {
|
|
113
|
+
handshaker.emit('handshakeFailed', HandshakeError.DUPLICATE_CONNECTION)
|
|
114
|
+
expect(mockPendingConnectionDestroy).not.toHaveBeenCalled()
|
|
115
|
+
expect(mockOnHandshakeCompleted).not.toHaveBeenCalled()
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('calls pending connection close if connection closes', () => {
|
|
119
|
+
(connection as any).emit('disconnected')
|
|
120
|
+
expect(mockPendingConnectionClose).toHaveBeenCalledTimes(1)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('closes connection if managed connection closes', () => {
|
|
124
|
+
(pendingConnection as any).emit('disconnected')
|
|
125
|
+
expect(mockConnectionClose).toHaveBeenCalledTimes(1)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
describe('Incoming', () => {
|
|
131
|
+
|
|
132
|
+
beforeEach(() => {
|
|
133
|
+
handshaker = createIncomingHandshaker(createMockPeerDescriptor(), pendingConnection, connection)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
afterEach(() => {
|
|
137
|
+
handshaker.stop()
|
|
138
|
+
})
|
|
139
|
+
|
|
140
|
+
it('onHandshakeRequest', () => {
|
|
141
|
+
const message = createHandshakeRequest(createMockPeerDescriptor(), createMockPeerDescriptor());
|
|
142
|
+
(connection as any).emit('data', Message.toBinary(message))
|
|
143
|
+
handshaker.emit('handshakeRequest', createMockPeerDescriptor(), '1.0')
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
it('calls pending connection onDisconnected if connection closes', () => {
|
|
147
|
+
(connection as any).emit('disconnected')
|
|
148
|
+
expect(mockPendingConnectionClose).toHaveBeenCalledTimes(1)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('closes connection if managed connection closes', () => {
|
|
152
|
+
(pendingConnection as any).emit('disconnected')
|
|
153
|
+
expect(mockConnectionClose).toHaveBeenCalledTimes(1)
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('destroys connection if handshake is rejected', () => {
|
|
157
|
+
rejectHandshake(pendingConnection, connection, handshaker, HandshakeError.DUPLICATE_CONNECTION)
|
|
158
|
+
expect(mockPendingConnectionDestroy).toHaveBeenCalled()
|
|
159
|
+
expect(mockConnectionDestroy).toHaveBeenCalled()
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
it('calls onHandshakeCompleted if handshake is accepted', () => {
|
|
163
|
+
acceptHandshake(handshaker, pendingConnection, connection)
|
|
164
|
+
expect(mockOnHandshakeCompleted).toHaveBeenCalled()
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
})
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { wait } from '@streamr/utils'
|
|
2
|
+
import { ManagedConnection } from '../../src/exports'
|
|
3
|
+
import { MockConnection } from '../utils/mock/MockConnection'
|
|
4
|
+
import { createMockPeerDescriptor } from '../utils/utils'
|
|
5
|
+
|
|
6
|
+
describe('ManagedConnection', () => {
|
|
7
|
+
|
|
8
|
+
let managedConnection: ManagedConnection
|
|
9
|
+
let connection: MockConnection
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
connection = new MockConnection()
|
|
12
|
+
managedConnection = new ManagedConnection(createMockPeerDescriptor(), connection)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
afterEach(() => {
|
|
16
|
+
managedConnection.close(false)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('emits disconnected after close', (done) => {
|
|
20
|
+
managedConnection.once('disconnected', (graceful) => {
|
|
21
|
+
expect(graceful).toBe(true)
|
|
22
|
+
done()
|
|
23
|
+
})
|
|
24
|
+
managedConnection.close(true)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('sends data', () => {
|
|
28
|
+
const data = new Uint8Array([1, 2, 3])
|
|
29
|
+
managedConnection.send(data)
|
|
30
|
+
expect(connection.sentData[0]).toEqual(data)
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('emits data', (done) => {
|
|
34
|
+
const data = new Uint8Array([1, 2, 3])
|
|
35
|
+
managedConnection.on('managedData', (data) => {
|
|
36
|
+
expect(data).toEqual(data)
|
|
37
|
+
done()
|
|
38
|
+
})
|
|
39
|
+
connection.emitData(data)
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('sets lastUsedTimestamp on send', async () => {
|
|
43
|
+
const createdTimestamp = managedConnection.getLastUsedTimestamp()
|
|
44
|
+
await wait(5)
|
|
45
|
+
managedConnection.send(new Uint8Array([1, 2, 3]))
|
|
46
|
+
expect(managedConnection.getLastUsedTimestamp()).toBeGreaterThan(createdTimestamp)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it('replace as duplicate', async () => {
|
|
50
|
+
managedConnection.once('disconnected', () => {
|
|
51
|
+
throw new Error('disconnected')
|
|
52
|
+
})
|
|
53
|
+
managedConnection.replaceAsDuplicate()
|
|
54
|
+
managedConnection.close(true)
|
|
55
|
+
await wait(50)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
})
|