-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestMoneroConnectionManager.js
More file actions
255 lines (221 loc) · 13.8 KB
/
Copy pathTestMoneroConnectionManager.js
File metadata and controls
255 lines (221 loc) · 13.8 KB
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
const assert = require("assert");
const TestUtils = require("./utils/TestUtils");
const monerojs = require("../../index");
const GenUtils = monerojs.GenUtils;
const MoneroRpcConnection = monerojs.MoneroRpcConnection;
const MoneroConnectionManager = monerojs.MoneroConnectionManager;
const MoneroConnectionManagerListener = monerojs.MoneroConnectionManagerListener;
/**
* Test the Monero RPC connection manager.
*/
class TestMoneroConnectionManager {
runTests() {
describe("Test connection manager", function() {
it("Can manage connections", async function() {
let err;
let walletRpcs = [];
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
let connectionManager = new MoneroConnectionManager();
// listen for changes
let listener = new ConnectionChangeCollector();
connectionManager.addListener(listener);
// add prioritized connections
connectionManager.addConnection(walletRpcs[4].getRpcConnection().setPriority(1));
connectionManager.addConnection(walletRpcs[2].getRpcConnection().setPriority(2));
connectionManager.addConnection(walletRpcs[3].getRpcConnection().setPriority(2));
connectionManager.addConnection(walletRpcs[0].getRpcConnection()); // default priority is lowest
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.isOnline());
// 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.setAutoSwitch(true);
await connectionManager.startCheckingConnection(TestUtils.SYNC_PERIOD_IN_MS);
assert(connectionManager.isConnected());
let connection = connectionManager.getConnection();
assert(connection.isOnline());
assert(connection === walletRpcs[4].getRpcConnection());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);
connectionManager.setAutoSwitch(false);
connectionManager.stopCheckingConnection();
connectionManager.disconnect();
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === undefined);
// start periodically checking connection
await connectionManager.startCheckingConnection(TestUtils.SYNC_PERIOD_IN_MS);
// connect to best available connection in order of priority and response time
connection = await connectionManager.getBestAvailableConnection();
connectionManager.setConnection(connection);
assert(connection === walletRpcs[4].getRpcConnection());
assert(connection.isOnline());
assert(connection.isAuthenticated());
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].isOnline());
// 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().isOnline());
assert.equal(undefined, connectionManager.getConnection().isAuthenticated());
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[2].getRpcConnection());
assert(orderedConnections[2] === walletRpcs[3].getRpcConnection());
assert(orderedConnections[3] === walletRpcs[0].getRpcConnection());
assert.equal(orderedConnections[4].getUri(), walletRpcs[1].getRpcConnection().getUri());
// 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].isOnline();
let isAuthenticated = orderedConnections[i].isAuthenticated();
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.isOnline());
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.isAuthenticated());
connectionManager.setConnection(connection);
assert.equal(false, connectionManager.isConnected());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
// connect to specific endpoint with authentication
connectionManager.setAutoSwitch(false);
orderedConnections[1].setCredentials("rpc_user", "abc123");
await connectionManager.checkConnection();
assert.equal(connection.getUri(), walletRpcs[1].getRpcConnection().getUri());
assert(connection.isOnline());
assert(connection.isAuthenticated());
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].isOnline() : !orderedConnections[i].isOnline());
// set connection to existing uri
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.stopCheckingConnection();
let uri = "http://localhost:49999";
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
connectionManager.setConnection("");
assert.equal(undefined, connectionManager.getConnection());
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
// check all connections and test auto switch
connectionManager.setAutoSwitch(true);
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());
// shut down all connections
connection = connectionManager.getConnection();
await connectionManager.startCheckingConnection(TestUtils.SYNC_PERIOD_IN_MS);
for (let connection of orderedConnections) connection._setFakeDisconnected(true);
await GenUtils.waitFor(TestUtils.SYNC_PERIOD_IN_MS + 100);
assert.equal(false, connection.isOnline());
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 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 {
constructor() {
super();
this.changedConnections = [];
}
async onConnectionChanged(connection) {
this.changedConnections.push(connection);
}
}
module.exports = TestMoneroConnectionManager;