Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,8 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

# JetBrains

.idea/
67 changes: 41 additions & 26 deletions Monero.Test/TestMoneroConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using Monero.Common;
using Monero.Test.Utils;
using Monero.Wallet;
using Xunit.Sdk;

namespace Monero.Test;

public class TestMoneroConnectionManager
{


public TestMoneroConnectionManager() { }

[Fact]
public void TestConnectionManager()
{
List<MoneroWalletRpc> walletRpcs = [];
MoneroConnectionManager? connectionManager = null;

Exception? ex = null;
try
{
// start monero-wallet-rpc instances as test server connections (can also use monerod servers)
Expand All @@ -33,7 +33,8 @@ public void TestConnectionManager()
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
connectionManager.AddConnection(
new MoneroRpcConnection(walletRpcs[1].GetRpcConnection().GetUri())); // test unauthenticated

// test connections and order
List<MoneroRpcConnection> orderedConnections = connectionManager.GetRpcConnections();
Expand All @@ -42,16 +43,17 @@ public void TestConnectionManager()
Assert.True(orderedConnections[2] == walletRpcs[3].GetRpcConnection());
Assert.True(orderedConnections[3] == walletRpcs[0].GetRpcConnection());
Assert.True(orderedConnections[4].GetUri() == walletRpcs[1].GetRpcConnection().GetUri());
foreach (MoneroRpcConnection c in orderedConnections) Assert.NotNull(c.IsOnline());
foreach (MoneroRpcConnection c in orderedConnections) Assert.Null(c.IsOnline());

// test getting connection by uri
Assert.True(connectionManager.HasConnection(walletRpcs[0].GetRpcConnection().GetUri()));
Assert.True(connectionManager.GetConnection(walletRpcs[0].GetRpcConnection().GetUri()) == walletRpcs[0].GetRpcConnection());
Assert.True(connectionManager.GetConnection(walletRpcs[0].GetRpcConnection().GetUri()) ==
walletRpcs[0].GetRpcConnection());

// test unknown connection
int numExpectedChanges = 0;
connectionManager.SetConnection(orderedConnections[0]);
Assert.Null( connectionManager.IsConnected());
Assert.Null(connectionManager.IsConnected());
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);

// auto connect to best available connection
Expand All @@ -60,6 +62,7 @@ public void TestConnectionManager()
Assert.True(connectionManager.IsConnected());
MoneroRpcConnection? connection = connectionManager.GetConnection();
Assert.True(connection.IsOnline() == true);
// TODO manager doesn't seem to check all connections in time
Assert.True(connection == walletRpcs[4].GetRpcConnection());
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);
Assert.True(listener.ChangedConnections[listener.ChangedConnections.Count - 1] == connection);
Expand Down Expand Up @@ -94,12 +97,13 @@ public void TestConnectionManager()
TestUtils.StopWalletRpcProcess(walletRpcs[2]);
TestUtils.StopWalletRpcProcess(walletRpcs[3]);
TestUtils.StopWalletRpcProcess(walletRpcs[4]);
Thread.Sleep(TestUtils.SYNC_PERIOD_IN_MS + 100); // allow time to poll
GenUtils.WaitFor(TestUtils.SYNC_PERIOD_IN_MS + 100); // allow time to poll
Assert.False(connectionManager.IsConnected());
Assert.False(connectionManager.GetConnection().IsOnline());
Assert.Null(connectionManager.GetConnection().IsAuthenticated());
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);
Assert.True(listener.ChangedConnections[listener.ChangedConnections.Count - 1] == connectionManager.GetConnection());
Assert.True(listener.ChangedConnections[listener.ChangedConnections.Count - 1] ==
connectionManager.GetConnection());

// test connection order
orderedConnections = connectionManager.GetRpcConnections();
Expand All @@ -123,20 +127,21 @@ public void TestConnectionManager()
// test online and authentication status
for (int i = 0; i < orderedConnections.Count; i++)
{
bool? IsOnline = orderedConnections[i].IsOnline();
bool? IsAuthenticated = orderedConnections[i].IsAuthenticated();
if (i == 1 || i == 2) Assert.True(IsOnline);
else Assert.False(IsOnline);
if (i == 1) Assert.True(IsAuthenticated);
else if (i == 2) Assert.False(IsAuthenticated);
else Assert.Null(IsAuthenticated);
bool? isOnline = orderedConnections[i].IsOnline();
bool? isAuthenticated = orderedConnections[i].IsAuthenticated();
if (i == 1 || i == 2) Assert.True(isOnline);
else Assert.False(isOnline);
if (i == 1) Assert.True(isAuthenticated);
else if (i == 2) Assert.False(isAuthenticated);
else Assert.Null(isAuthenticated);
}

// test auto switch when disconnected
connectionManager.SetAutoSwitch(true);
Thread.Sleep(TestUtils.SYNC_PERIOD_IN_MS + 100);
GenUtils.WaitFor(TestUtils.SYNC_PERIOD_IN_MS + 100);
Assert.True(connectionManager.IsConnected());
connection = connectionManager.GetConnection();
Assert.NotNull(connection);
Assert.True(connection.IsOnline());
Assert.True(connection == walletRpcs[0].GetRpcConnection());
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);
Expand Down Expand Up @@ -175,7 +180,9 @@ public void TestConnectionManager()
Assert.True(orderedConnections[2] == walletRpcs[4].GetRpcConnection());
Assert.True(orderedConnections[3] == walletRpcs[2].GetRpcConnection());
Assert.True(orderedConnections[4] == walletRpcs[3].GetRpcConnection());
for (int i = 0; i < orderedConnections.Count - 1; i++) Assert.True(i <= 1 ? orderedConnections[i].IsOnline() == true : orderedConnections[i].IsOnline() != true);
for (int i = 0; i < orderedConnections.Count - 1; i++)
Assert.True(
i <= 1 ? orderedConnections[i].IsOnline() == true : orderedConnections[i].IsOnline() != true);
Assert.False(orderedConnections[4].IsOnline());

// set connection to existing uri
Expand All @@ -194,7 +201,8 @@ public void TestConnectionManager()
}

Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);
Assert.True(listener.ChangedConnections[listener.ChangedConnections.Count - 1] == walletRpcs[0].GetRpcConnection());
Assert.True(listener.ChangedConnections[listener.ChangedConnections.Count - 1] ==
walletRpcs[0].GetRpcConnection());

// set connection to new uri
connectionManager.StopPolling();
Expand All @@ -206,7 +214,7 @@ public void TestConnectionManager()

// set connection to empty string
connectionManager.SetConnection("");
Assert.Null( connectionManager.GetConnection());
Assert.Null(connectionManager.GetConnection());
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);

// check all connections and test auto switch
Expand All @@ -226,35 +234,37 @@ public void TestConnectionManager()
connectionManager.SetConnection(null);
Assert.False(connectionManager.IsConnected());
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);
connectionManager.StartPolling((ulong)TestUtils.SYNC_PERIOD_IN_MS, null, null, MoneroConnectionManager.PollType.CURRENT, null);
connectionManager.StartPolling((ulong)TestUtils.SYNC_PERIOD_IN_MS, null, null,
MoneroConnectionManager.PollType.Current, null);
Thread.Sleep(TestUtils.AUTO_CONNECT_TIMEOUT_MS);
Assert.True(connectionManager.IsConnected());
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);

// test polling all connections
connectionManager.SetConnection(null);
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);
connectionManager.StartPolling((ulong)TestUtils.SYNC_PERIOD_IN_MS, null, null, MoneroConnectionManager.PollType.ALL, null);
connectionManager.StartPolling((ulong)TestUtils.SYNC_PERIOD_IN_MS, null, null,
MoneroConnectionManager.PollType.All, null);
Thread.Sleep(TestUtils.AUTO_CONNECT_TIMEOUT_MS);
Assert.True(connectionManager.IsConnected());
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);

// shut down all connections
connection = connectionManager.GetConnection();
foreach (MoneroWalletRpc walletRpc in walletRpcs) TestUtils.StopWalletRpcProcess(walletRpc);
Thread.Sleep(TestUtils.SYNC_PERIOD_IN_MS + 100);
GenUtils.WaitFor(TestUtils.SYNC_PERIOD_IN_MS + 100);
Assert.False(connection.IsOnline());
Assert.True(++numExpectedChanges == listener.ChangedConnections.Count);
Assert.True(listener.ChangedConnections[listener.ChangedConnections.Count - 1] == connection);

// reset
connectionManager.Reset();
Assert.True(0 == connectionManager.GetConnections().Count);
Assert.Null( connectionManager.GetConnection());
Assert.Null(connectionManager.GetConnection());
}
catch (Exception ex)
catch (Exception exception)
{
Assert.Fail($"An exception occurred: {ex.Message}");
ex = exception;
}
finally
{
Expand All @@ -269,9 +279,14 @@ public void TestConnectionManager()
{
TestUtils.StopWalletRpcProcess(wallet);
}
catch { }
catch (Exception e)
{
Console.WriteLine("An error occurred while stopping RPC wallet: " + e.Message);
}
}
}

if (ex != null) Assert.Fail($"An exception occurred: {ex.Message}, {ex.StackTrace}");
}
}

Expand Down
6 changes: 6 additions & 0 deletions Monero.Test/TestMoneroDaemonModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Monero.Test;

public class TestMoneroDaemonModel
{

}
Loading