2023-05-10 07:55:36 +00:00
|
|
|
|
using DistTestCore.Codex;
|
|
|
|
|
using DistTestCore;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using Utils;
|
|
|
|
|
using Logging;
|
|
|
|
|
|
|
|
|
|
namespace Tests.PeerDiscoveryTests
|
|
|
|
|
{
|
|
|
|
|
public static class PeerTestHelpers
|
|
|
|
|
{
|
|
|
|
|
public static void AssertFullyConnected(IEnumerable<IOnlineCodexNode> nodes, BaseLog? log = null)
|
|
|
|
|
{
|
|
|
|
|
AssertFullyConnected(log, nodes.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void AssertFullyConnected(BaseLog? log = null, params IOnlineCodexNode[] nodes)
|
|
|
|
|
{
|
2023-05-11 11:59:53 +00:00
|
|
|
|
var entries = nodes.Select(n => new Entry(n)).ToArray();
|
2023-05-10 08:47:10 +00:00
|
|
|
|
|
2023-05-11 11:59:53 +00:00
|
|
|
|
var failureMessags = new List<string>();
|
|
|
|
|
for (var x = 0; x < entries.Length; x++)
|
|
|
|
|
{
|
|
|
|
|
for (var y = x + 1; y < entries.Length; y++)
|
2023-05-10 07:55:36 +00:00
|
|
|
|
{
|
2023-05-11 11:59:53 +00:00
|
|
|
|
AssertKnowEachother(failureMessags, entries[x], entries[y], log);
|
2023-05-10 07:55:36 +00:00
|
|
|
|
}
|
2023-05-11 11:59:53 +00:00
|
|
|
|
}
|
2023-05-10 08:47:10 +00:00
|
|
|
|
|
2023-05-11 11:59:53 +00:00
|
|
|
|
CollectionAssert.IsEmpty(failureMessags);
|
2023-05-10 07:55:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 10:44:53 +00:00
|
|
|
|
private static void AssertKnowEachother(List<string> failureMessags, Entry a, Entry b, BaseLog? log)
|
2023-05-10 07:55:36 +00:00
|
|
|
|
{
|
2023-05-10 08:47:10 +00:00
|
|
|
|
AssertKnows(failureMessags, a, b, log);
|
|
|
|
|
AssertKnows(failureMessags, b, a, log);
|
2023-05-10 07:55:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-11 10:44:53 +00:00
|
|
|
|
private static void AssertKnows(List<string> failureMessags, Entry a, Entry b, BaseLog? log)
|
2023-05-10 07:55:36 +00:00
|
|
|
|
{
|
2023-05-11 10:44:53 +00:00
|
|
|
|
var peerId = b.Response.id;
|
2023-05-10 08:47:10 +00:00
|
|
|
|
|
2023-05-11 10:44:53 +00:00
|
|
|
|
try
|
2023-05-10 07:55:36 +00:00
|
|
|
|
{
|
2023-05-11 11:59:53 +00:00
|
|
|
|
var response = GetDebugPeer(a.Node, peerId);
|
2023-05-11 10:44:53 +00:00
|
|
|
|
if (string.IsNullOrEmpty(response.peerId) || !response.addresses.Any())
|
|
|
|
|
{
|
|
|
|
|
failureMessags.Add($"{a.Response.id} did not know peer {peerId}");
|
|
|
|
|
}
|
|
|
|
|
else if (log != null)
|
|
|
|
|
{
|
|
|
|
|
log.Log($"{a.Response.id} knows {peerId}.");
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-11 11:59:53 +00:00
|
|
|
|
catch
|
2023-05-11 10:44:53 +00:00
|
|
|
|
{
|
2023-05-11 11:59:53 +00:00
|
|
|
|
failureMessags.Add($"{a.Response.id} was unable to get 'debug/peer/{peerId}'.");
|
2023-05-10 07:55:36 +00:00
|
|
|
|
}
|
2023-05-11 11:59:53 +00:00
|
|
|
|
}
|
2023-05-10 07:55:36 +00:00
|
|
|
|
|
2023-05-11 11:59:53 +00:00
|
|
|
|
private static CodexDebugPeerResponse GetDebugPeer(IOnlineCodexNode node, string peerId)
|
|
|
|
|
{
|
|
|
|
|
return Time.Retry(() => node.GetDebugPeer(peerId));
|
2023-05-11 10:44:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Entry
|
|
|
|
|
{
|
|
|
|
|
public Entry(IOnlineCodexNode node)
|
2023-05-10 08:47:10 +00:00
|
|
|
|
{
|
2023-05-11 10:44:53 +00:00
|
|
|
|
Node = node;
|
|
|
|
|
Response = node.GetDebugInfo();
|
2023-05-10 08:47:10 +00:00
|
|
|
|
}
|
2023-05-11 10:44:53 +00:00
|
|
|
|
|
|
|
|
|
public IOnlineCodexNode Node { get ; }
|
|
|
|
|
public CodexDebugResponse Response { get; }
|
2023-05-10 07:55:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|