Skip to content

Commit

Permalink
add or replace connection on connectionManager.setConnection()
Browse files Browse the repository at this point in the history
  • Loading branch information
woodser committed May 26, 2023
1 parent 127e614 commit 8ae3fe8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
42 changes: 20 additions & 22 deletions src/main/js/common/MoneroConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class MoneroConnectionManager {
async removeConnection(uri) {
let connection = this.getConnectionByUri(uri);
if (!connection) throw new MoneroError("No connection exists with URI: " + uri);
GenUtils.remove(connections, connection);
GenUtils.remove(this._connections, connection);
if (connection === this._currentConnection) {
this._currentConnection = undefined;
this._onConnectionChanged(this._currentConnection);
Expand All @@ -139,10 +139,11 @@ class MoneroConnectionManager {
/**
* Indicates if the connection manager is connected to a node.
*
* @return {boolean} true if the current connection is set, online, and not unauthenticated. false otherwise
* @return {boolean|undefined} true if the current connection is set, online, and not unauthenticated, undefined if unknown, false otherwise
*/
isConnected() {
return this._currentConnection && this._currentConnection.isConnected();
if (!this._currentConnection) return false;
return this._currentConnection.isConnected();
}

/**
Expand Down Expand Up @@ -213,7 +214,7 @@ class MoneroConnectionManager {
/**
* Set the current connection.
* Provide a URI to select an existing connection without updating its credentials.
* Provide a MoneroRpcConnection to add new connection or update credentials of existing connection with same URI.
* Provide a MoneroRpcConnection to add new connection or replace existing connection with the same URI.
* Notify if current connection changes.
* Does not check the connection.
*
Expand Down Expand Up @@ -242,25 +243,13 @@ class MoneroConnectionManager {
// validate connection
if (!(connection instanceof MoneroRpcConnection)) throw new MoneroError("Must provide string or MoneroRpcConnection to set connection");
if (!connection.getUri()) throw new MoneroError("Connection is missing URI");
// check if adding new connection

// add or replace connection
let prevConnection = this.getConnectionByUri(connection.getUri());
if (!prevConnection) {
this.addConnection(connection);
this._currentConnection = connection;
if (this._proxyToWorker !== undefined) connection.setProxyToWorker(this._proxyToWorker);
this._onConnectionChanged(this._currentConnection);
return this;
}

// check if updating current connection
if (prevConnection !== this._currentConnection || prevConnection.getUsername() !== connection.getUsername() || prevConnection.getPassword() !== connection.getPassword() || prevConnection.getPriority() !== connection.getPriority()) {
prevConnection.setCredentials(connection.getUsername(), connection.getPassword());
prevConnection.setPriority(connection.getPriority());
this._currentConnection = prevConnection;
if (this._proxyToWorker !== undefined) connection.setProxyToWorker(this._proxyToWorker);
this._onConnectionChanged(this._currentConnection);
}
if (prevConnection) GenUtils.remove(this._connections, prevConnection);
this.addConnection(connection);
this._currentConnection = connection;
this._onConnectionChanged(this._currentConnection);

return this;
}
Expand Down Expand Up @@ -457,6 +446,15 @@ class MoneroConnectionManager {
this._autoSwitch = false;
return this;
}

/**
* Get all listeners.
*
* @return {MoneroConnectionManagerListener[]} all listeners
*/
getListeners() {
return this._listeners
}

// ------------------------------ PRIVATE HELPERS ---------------------------

Expand Down
40 changes: 27 additions & 13 deletions src/test/TestMoneroConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class TestMoneroConnectionManager {
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);
Expand All @@ -51,12 +57,12 @@ class TestMoneroConnectionManager {
let connection = connectionManager.getConnection();
assert(connection.isOnline());
assert(connection === walletRpcs[4].getRpcConnection());
assert.equal(1, listener.changedConnections.length);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);
connectionManager.setAutoSwitch(false);
connectionManager.stopCheckingConnection();
connectionManager.disconnect();
assert.equal(2, listener.changedConnections.length);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === undefined);

// start periodically checking connection
Expand All @@ -68,7 +74,7 @@ class TestMoneroConnectionManager {
assert(connection === walletRpcs[4].getRpcConnection());
assert(connection.isOnline());
assert(connection.isAuthenticated());
assert.equal(3, listener.changedConnections.length);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);

// test connections and order
Expand All @@ -88,7 +94,7 @@ class TestMoneroConnectionManager {
assert.equal(false, connectionManager.isConnected());
assert.equal(false, connectionManager.getConnection().isOnline());
assert.equal(undefined, connectionManager.getConnection().isAuthenticated());
assert.equal(4, listener.changedConnections.length);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connectionManager.getConnection());

// test connection order
Expand Down Expand Up @@ -128,7 +134,7 @@ class TestMoneroConnectionManager {
connection = connectionManager.getConnection();
assert(connection.isOnline());
assert(connection === walletRpcs[0].getRpcConnection());
assert.equal(5, listener.changedConnections.length);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);

// test connection order
Expand All @@ -145,7 +151,7 @@ class TestMoneroConnectionManager {
assert.equal(false, connection.isAuthenticated());
connectionManager.setConnection(connection);
assert.equal(false, connectionManager.isConnected());
assert.equal(6, listener.changedConnections.length);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);

// connect to specific endpoint with authentication
connectionManager.setAutoSwitch(false);
Expand All @@ -154,7 +160,7 @@ class TestMoneroConnectionManager {
assert.equal(connection.getUri(), walletRpcs[1].getRpcConnection().getUri());
assert(connection.isOnline());
assert(connection.isAuthenticated());
assert.equal(7, listener.changedConnections.length);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);

// test connection order
Expand All @@ -173,28 +179,36 @@ class TestMoneroConnectionManager {
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(8, listener.changedConnections.length);
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(9, listener.changedConnections.length);
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(10, listener.changedConnections.length);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);

// check all connections and test auto switch
connectionManager.setAutoSwitch(true);
await connectionManager.checkConnections();
assert.equal(11, listener.changedConnections.length);
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());

Expand All @@ -204,7 +218,7 @@ class TestMoneroConnectionManager {
for (let connection of orderedConnections) connection._setFakeDisconnected(true);
await GenUtils.waitFor(TestUtils.SYNC_PERIOD_IN_MS + 100);
assert.equal(false, connection.isOnline());
assert.equal(12, listener.changedConnections.length);
assert.equal(listener.changedConnections.length, ++numExpectedChanges);
assert(listener.changedConnections[listener.changedConnections.length - 1] === connection);

// reset
Expand Down

0 comments on commit 8ae3fe8

Please sign in to comment.