Sets up working peer-discovery tests

This commit is contained in:
benbierens 2023-05-10 08:53:57 +02:00
parent 9c6f00dbce
commit 1d224cf2d3
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 125 additions and 85 deletions

View File

@ -18,6 +18,14 @@
return BringOnline(codexSetup);
}
protected IOnlineCodexNode BootstrapNode
{
get
{
return EnsureBootstapNode();
}
}
private IOnlineCodexNode EnsureBootstapNode()
{
if (bootstrapNode == null)

View File

@ -82,6 +82,20 @@ namespace DistTestCore.Codex
public EnginePeerResponse[] enginePeers { get; set; } = Array.Empty<EnginePeerResponse>();
public SwitchPeerResponse[] switchPeers { get; set; } = Array.Empty<SwitchPeerResponse>();
public CodexDebugVersionResponse codex { get; set; } = new();
public CodexDebugTableResponse table { get; set; } = new();
}
public class CodexDebugTableResponse
{
public CodexDebugTableNodeResponse localNode { get; set; } = new();
public CodexDebugTableNodeResponse[] nodes { get; set; } = Array.Empty<CodexDebugTableNodeResponse>();
}
public class CodexDebugTableNodeResponse
{
public string nodeId { get; set; } = string.Empty;
public string record { get; set; } = string.Empty;
public bool seen { get; set; }
}
public class EnginePeerResponse

View File

@ -1,85 +0,0 @@
using DistTestCore;
using DistTestCore.Codex;
using NUnit.Framework;
namespace Tests.BasicTests
{
[TestFixture]
public class PeerTests : DistTest
{
[Test]
public void TwoNodes()
{
var primary = SetupCodexBootstrapNode();
var secondary = SetupCodexNode(s => s.WithBootstrapNode(primary));
primary.ConnectToPeer(secondary); // TODO REMOVE THIS: This is required for the switchPeers to show up.
// This is required for the enginePeers to show up.
//var file = GenerateTestFile(10.MB());
//var contentId = primary.UploadFile(file);
//var file2 = secondary.DownloadContent(contentId);
//file.AssertIsEqual(file2);
AssertKnowEachother(primary, secondary);
}
[TestCase(2)]
[TestCase(3)]
[TestCase(10)]
public void VariableNodes(int number)
{
var bootstrap = SetupCodexBootstrapNode();
var nodes = SetupCodexNodes(number, s => s.WithBootstrapNode(bootstrap));
var file = GenerateTestFile(10.MB());
var contentId = nodes.First().UploadFile(file);
var file2 = nodes.Last().DownloadContent(contentId);
file.AssertIsEqual(file2);
// <TODO REMOVE THIS>
foreach (var node in nodes) bootstrap.ConnectToPeer(node);
for (var x = 0; x < number; x++)
{
for (var y = x + 1; y < number; y++)
{
nodes[x].ConnectToPeer(nodes[y]);
}
}
// </TODO REMOVE THIS>
foreach (var node in nodes) AssertKnowEachother(node, bootstrap);
for (var x = 0; x < number; x++)
{
for (var y = x + 1; y < number; y++)
{
AssertKnowEachother(nodes[x], nodes[y]);
}
}
}
private void AssertKnowEachother(IOnlineCodexNode a, IOnlineCodexNode b)
{
AssertKnowEachother(a.GetDebugInfo(), b.GetDebugInfo());
}
private void AssertKnowEachother(CodexDebugResponse a, CodexDebugResponse b)
{
AssertKnows(a, b);
AssertKnows(b, a);
}
private void AssertKnows(CodexDebugResponse a, CodexDebugResponse b)
{
var enginePeers = string.Join(",", a.enginePeers.Select(p => p.peerId));
var switchPeers = string.Join(",", a.switchPeers.Select(p => p.peerId));
Debug($"{a.id} is looking for {b.id} in engine-peers [{enginePeers}]");
Debug($"{a.id} is looking for {b.id} in switch-peers [{switchPeers}]");
Assert.That(a.enginePeers.Any(p => p.peerId == b.id), $"{a.id} was looking for '{b.id}' in engine-peers [{enginePeers}] but it was not found.");
Assert.That(a.switchPeers.Any(p => p.peerId == b.id), $"{a.id} was looking for '{b.id}' in switch-peers [{switchPeers}] but it was not found.");
}
}
}

View File

@ -0,0 +1,103 @@
using DistTestCore.Codex;
using DistTestCore;
using NUnit.Framework;
using Utils;
namespace Tests.PeerDiscoveryTests
{
public class PeerDiscoveryTests : AutoBootstrapDistTest
{
[Test]
public void TwoNodes()
{
var node = SetupCodexNode();
AssertKnowEachother(BootstrapNode, node);
}
[TestCase(2)]
[TestCase(3)]
[TestCase(10)]
public void VariableNodes(int number)
{
var nodes = SetupCodexNodes(number);
AssertFullyConnected(nodes);
}
[TestCase(2)]
[TestCase(3)]
[TestCase(10)]
public void VariableNodesInPods(int number)
{
var bootstrap = SetupCodexBootstrapNode();
var nodes = new List<IOnlineCodexNode>();
for (var i = 0; i < number; i++)
{
nodes.Add(SetupCodexNode(s => s.WithBootstrapNode(bootstrap)));
}
AssertFullyConnected(nodes);
}
private void AssertFullyConnected(IEnumerable<IOnlineCodexNode> nodes)
{
Retry(() =>
{
var array = nodes.ToArray();
foreach (var node in array) AssertKnowEachother(node, BootstrapNode);
for (var x = 0; x < array.Length; x++)
{
for (var y = x + 1; y < array.Length; y++)
{
AssertKnowEachother(array[x], array[y]);
}
}
});
}
private static void Retry(Action action)
{
try
{
action();
return;
}
catch
{
Time.Sleep(TimeSpan.FromMinutes(1));
}
action();
}
private void AssertKnowEachother(IOnlineCodexNode a, IOnlineCodexNode b)
{
AssertKnowEachother(a.GetDebugInfo(), b.GetDebugInfo());
}
private void AssertKnowEachother(CodexDebugResponse a, CodexDebugResponse b)
{
AssertKnows(a, b);
AssertKnows(b, a);
}
private void AssertKnows(CodexDebugResponse a, CodexDebugResponse b)
{
//var enginePeers = string.Join(",", a.enginePeers.Select(p => p.peerId));
//var switchPeers = string.Join(",", a.switchPeers.Select(p => p.peerId));
var tableNodes = string.Join(",", a.table.nodes.Select(n => n.nodeId));
//Debug($"{a.id} is looking for {b.id} in engine-peers [{enginePeers}]");
//Debug($"{a.id} is looking for {b.id} in switch-peers [{switchPeers}]");
Debug($"{a.table.localNode.nodeId} is looking for {b.table.localNode.nodeId} in table-nodes [{tableNodes}]");
//Assert.That(a.enginePeers.Any(p => p.peerId == b.id), $"{a.id} was looking for '{b.id}' in engine-peers [{enginePeers}] but it was not found.");
//Assert.That(a.switchPeers.Any(p => p.peerId == b.id), $"{a.id} was looking for '{b.id}' in switch-peers [{switchPeers}] but it was not found.");
Assert.That(a.table.nodes.Any(n => n.nodeId == b.table.localNode.nodeId), $"{a.table.localNode.nodeId} was looking for '{b.table.localNode.nodeId}' in table-nodes [{tableNodes}] but it was not found.");
}
}
}