-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathTestMoneroConnectionManager.ts
283 lines (243 loc) · 15.3 KB
/
TestMoneroConnectionManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import assert from "assert";
import TestUtils from "./utils/TestUtils";
import {
GenUtils,
MoneroWalletRpc,
MoneroConnectionManager,
MoneroConnectionManagerListener,
MoneroRpcConnection
} from "../../index";
/**
* Test the Monero RPC connection manager.
*/
class TestMoneroConnectionManager {
runTests() {
describe("Test connection manager", function() {
it("Can manage connections", async function() {
let err;
let walletRpcs: MoneroWalletRpc[] = [];
let connectionManager: MoneroConnectionManager;
try {
// start monero-wallet-rpc instances as test server connections (can also use monerod servers)
for (let i = 0; i < 5; i++) walletRpcs.push(await TestUtils.startWalletRpcProcess());
// create connection manager
connectionManager = new MoneroConnectionManager();
// listen for changes
let listener = new ConnectionChangeCollector();
connectionManager.addListener(listener);
// add prioritized connections
await connectionManager.addConnection(walletRpcs[4].getRpcConnection()!.setPriority(1));
await connectionManager.addConnection(walletRpcs[2].getRpcConnection()!.setPriority(2));
await connectionManager.addConnection(walletRpcs[3].getRpcConnection()!.setPriority(2));
await connectionManager.addConnection(walletRpcs[0].getRpcConnection()); // default priority is lowest
await connectionManager.addConnection(new MoneroRpcConnection(walletRpcs[1].getRpcConnection()!.getUri())); // test unauthenticated
// test connections and order
let orderedConnections = connectionManager.getConnections();
assert(orderedConnections[0] === walletRpcs[4].getRpcConnection());
assert(orderedConnections[1] === walletRpcs[2].getRpcConnection());
assert(orderedConnections[2] === walletRpcs[3].getRpcConnection());
assert(orderedConnections[3] === walletRpcs[0].getRpcConnection());
assert.equal(orderedConnections[4].getUri(), (walletRpcs[1].getRpcConnection()!).getUri());
for (let connection of orderedConnections) assert.equal(undefined, connection.getIsOnline());
// test getting connection by uri
assert(connectionManager.hasConnection(walletRpcs[0].getRpcConnection()!.getUri()));
assert(connectionManager.getConnectionByUri(walletRpcs[0].getRpcConnection()!.getUri()) === walletRpcs[0].getRpcConnection());
// test unknown connection
let numExpectedChanges = 0;
await connectionManager.setConnection(orderedConnections[0]);
assert.equal(connectionManager.isConnected(), undefined);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
// auto connect to best available connection
connectionManager.startPolling(TestUtils.SYNC_PERIOD_IN_MS);
await GenUtils.waitFor(TestUtils.AUTO_CONNECT_TIMEOUT_MS);
assert(connectionManager.isConnected());
let connection = connectionManager.getConnection();
assert(connection.getIsOnline());
assert(connection === walletRpcs[4].getRpcConnection());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);
connectionManager.setAutoSwitch(false);
connectionManager.stopPolling();
await connectionManager.disconnect();
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === undefined);
// start periodically checking connection without auto switch
await connectionManager.startPolling(TestUtils.SYNC_PERIOD_IN_MS, false);
// connect to best available connection in order of priority and response time
connection = await connectionManager.getBestAvailableConnection();
await connectionManager.setConnection(connection);
assert(connection === walletRpcs[4].getRpcConnection());
assert(connection.getIsOnline());
assert(connection.getIsAuthenticated());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);
// test connections and order
orderedConnections = connectionManager.getConnections();
assert(orderedConnections[0] === walletRpcs[4].getRpcConnection());
assert(orderedConnections[1] === walletRpcs[2].getRpcConnection());
assert(orderedConnections[2] === walletRpcs[3].getRpcConnection());
assert(orderedConnections[3] === walletRpcs[0].getRpcConnection());
assert.equal(orderedConnections[4].getUri(), walletRpcs[1].getRpcConnection()!.getUri());
for (let i = 1; i < orderedConnections.length; i++) assert.equal(undefined, orderedConnections[i].getIsOnline());
// shut down prioritized servers
walletRpcs[2].getRpcConnection()!.setFakeDisconnected(true); // browser does not start or stop instances
walletRpcs[3].getRpcConnection()!.setFakeDisconnected(true);
walletRpcs[4].getRpcConnection()!.setFakeDisconnected(true);
await GenUtils.waitFor(TestUtils.SYNC_PERIOD_IN_MS + 100);
assert.equal(false, connectionManager.isConnected());
assert.equal(false, connectionManager.getConnection().getIsOnline());
assert.equal(undefined, connectionManager.getConnection().getIsAuthenticated());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connectionManager.getConnection());
// test connection order
orderedConnections = connectionManager.getConnections();
assert(orderedConnections[0] === walletRpcs[4].getRpcConnection());
assert(orderedConnections[1] === walletRpcs[0].getRpcConnection());
assert.equal(orderedConnections[2].getUri(), walletRpcs[1].getRpcConnection()!.getUri());
assert(orderedConnections[3] === walletRpcs[2].getRpcConnection());
assert(orderedConnections[4] === walletRpcs[3].getRpcConnection());
// check all connections
await connectionManager.checkConnections();
// test connection order
orderedConnections = connectionManager.getConnections();
assert(orderedConnections[0] === walletRpcs[4].getRpcConnection());
assert(orderedConnections[1] === walletRpcs[0].getRpcConnection());
assert(orderedConnections[2].getUri() === walletRpcs[1].getRpcConnection()!.getUri());
assert(orderedConnections[3] === walletRpcs[2].getRpcConnection());
assert(orderedConnections[4] === walletRpcs[3].getRpcConnection());
// test online and authentication status
for (let i = 0; i < orderedConnections.length; i++) {
let isOnline = orderedConnections[i].getIsOnline();
let isAuthenticated = orderedConnections[i].getIsAuthenticated();
if (i === 1 || i === 2) assert.equal(true, isOnline);
else assert.equal(false, isOnline);
if (i === 1) assert.equal(true, isAuthenticated);
else if (i === 2) assert.equal(false, isAuthenticated);
else assert.equal(undefined, isAuthenticated);
}
// test auto switch when disconnected
connectionManager.setAutoSwitch(true);
await GenUtils.waitFor(TestUtils.SYNC_PERIOD_IN_MS + 100); // allow time to poll
assert(connectionManager.isConnected());
connection = connectionManager.getConnection();
assert(connection.getIsOnline());
assert(connection === walletRpcs[0].getRpcConnection());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);
// test connection order
orderedConnections = connectionManager.getConnections();
assert(orderedConnections[0] === connection);
assert(orderedConnections[0] === walletRpcs[0].getRpcConnection());
assert(orderedConnections[1].getUri() === walletRpcs[1].getRpcConnection()!.getUri());
assert(orderedConnections[2] === walletRpcs[4].getRpcConnection());
assert(orderedConnections[3] === walletRpcs[2].getRpcConnection());
assert(orderedConnections[4] === walletRpcs[3].getRpcConnection());
// connect to specific endpoint without authentication
connection = orderedConnections[1];
assert.equal(false, connection.getIsAuthenticated());
await connectionManager.setConnection(connection);
assert.equal(false, connectionManager.isConnected());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
// connect to specific endpoint with authentication
orderedConnections[1].setCredentials("rpc_user", "abc123");
await connectionManager.checkConnection();
assert.equal(connectionManager.getConnection().getUri(), walletRpcs[1].getRpcConnection()!.getUri());
assert.equal(connection.getUri(), walletRpcs[1].getRpcConnection()!.getUri());
assert(connection.getIsOnline());
assert(connection.getIsAuthenticated());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);
// test connection order
orderedConnections = connectionManager.getConnections();
assert(orderedConnections[0] === connectionManager.getConnection());
assert.equal(orderedConnections[0].getUri(), walletRpcs[1].getRpcConnection()!.getUri());
assert(orderedConnections[1] === walletRpcs[0].getRpcConnection());
assert(orderedConnections[2] === walletRpcs[4].getRpcConnection());
assert(orderedConnections[3] === walletRpcs[2].getRpcConnection());
assert(orderedConnections[4] === walletRpcs[3].getRpcConnection());
for (let i = 0; i < orderedConnections.length; i++) assert(i <= 1 ? orderedConnections[i].getIsOnline() : !orderedConnections[i].getIsOnline());
// set connection to existing uri
await connectionManager.setConnection(walletRpcs[0].getRpcConnection()!.getUri());
assert(connectionManager.isConnected());
assert(walletRpcs[0].getRpcConnection() === connectionManager.getConnection());
assert.equal(TestUtils.WALLET_RPC_CONFIG.username, connectionManager.getConnection().getUsername());
assert.equal(TestUtils.WALLET_RPC_CONFIG.password, connectionManager.getConnection().getPassword());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === walletRpcs[0].getRpcConnection());
// set connection to new uri
connectionManager.stopPolling();
let uri = "http://localhost:49999";
await connectionManager.setConnection(uri);
assert.equal(connectionManager.getConnection().getUri(), uri);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert.equal(uri, listener.changedConnections[listener.changedConnections.length - 1].getUri());
// set connection to empty string
await connectionManager.setConnection("");
assert.equal(undefined, connectionManager.getConnection());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
// check all connections and test auto switch
await connectionManager.checkConnections();
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(connectionManager.isConnected());
// remove current connection and test auto switch
await connectionManager.removeConnection(connectionManager.getConnection().getUri());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert.equal(connectionManager.isConnected(), false);
await connectionManager.checkConnections();
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(connectionManager.isConnected());
// check connection promises
await Promise.all(connectionManager.checkConnectionPromises());
// test polling current connection
await connectionManager.setConnection();
assert.equal(connectionManager.isConnected(), false);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
connectionManager.startPolling(TestUtils.SYNC_PERIOD_IN_MS, undefined, undefined, MoneroConnectionManager.PollType.CURRENT, undefined);
await GenUtils.waitFor(TestUtils.AUTO_CONNECT_TIMEOUT_MS);
assert(connectionManager.isConnected());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
// test polling all connections
await connectionManager.setConnection();
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
connectionManager.startPolling(TestUtils.SYNC_PERIOD_IN_MS, undefined, undefined, MoneroConnectionManager.PollType.ALL, undefined);
await GenUtils.waitFor(TestUtils.AUTO_CONNECT_TIMEOUT_MS);
assert(connectionManager.isConnected());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
// shut down all connections
connection = connectionManager.getConnection();
for (let connection of orderedConnections) connection.setFakeDisconnected(true);
await GenUtils.waitFor(TestUtils.SYNC_PERIOD_IN_MS + 100);
assert.equal(false, connection.getIsOnline());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);
// reset
connectionManager.reset();
assert.equal(connectionManager.getConnections().length, 0);
assert.equal(connectionManager.getConnection(), undefined);
} catch(err2) {
err = err2;
}
// stop connection manager
if (connectionManager) connectionManager.reset();
// stop monero-wallet-rpc instances
for (let walletRpc of walletRpcs) {
try { await TestUtils.stopWalletRpcProcess(walletRpc); }
catch (err2) { }
}
// throw error if applicable
if (err) throw err;
});
});
}
}
class ConnectionChangeCollector extends MoneroConnectionManagerListener {
changedConnections: MoneroRpcConnection[] = [];
constructor() {
super();
this.changedConnections = [];
}
async onConnectionChanged(connection) {
this.changedConnections.push(connection);
}
}
export default TestMoneroConnectionManager;