diff --git a/DistTestCore/AutoBootstrapDistTest.cs b/DistTestCore/AutoBootstrapDistTest.cs index 352e2ec..1ffce5a 100644 --- a/DistTestCore/AutoBootstrapDistTest.cs +++ b/DistTestCore/AutoBootstrapDistTest.cs @@ -18,6 +18,14 @@ return BringOnline(codexSetup); } + protected IOnlineCodexNode BootstrapNode + { + get + { + return EnsureBootstapNode(); + } + } + private IOnlineCodexNode EnsureBootstapNode() { if (bootstrapNode == null) diff --git a/DistTestCore/Codex/CodexAccess.cs b/DistTestCore/Codex/CodexAccess.cs index 8654878..2cee677 100644 --- a/DistTestCore/Codex/CodexAccess.cs +++ b/DistTestCore/Codex/CodexAccess.cs @@ -82,6 +82,20 @@ namespace DistTestCore.Codex public EnginePeerResponse[] enginePeers { get; set; } = Array.Empty(); public SwitchPeerResponse[] switchPeers { get; set; } = Array.Empty(); 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(); + } + + 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 diff --git a/Tests/BasicTests/PeerTests.cs b/Tests/BasicTests/PeerTests.cs deleted file mode 100644 index 09b41ac..0000000 --- a/Tests/BasicTests/PeerTests.cs +++ /dev/null @@ -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); - - // - 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]); - } - } - // - - 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."); - } - } -} diff --git a/Tests/PeerDiscoveryTests/PeerDiscoveryTests.cs b/Tests/PeerDiscoveryTests/PeerDiscoveryTests.cs new file mode 100644 index 0000000..23773f5 --- /dev/null +++ b/Tests/PeerDiscoveryTests/PeerDiscoveryTests.cs @@ -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(); + for (var i = 0; i < number; i++) + { + nodes.Add(SetupCodexNode(s => s.WithBootstrapNode(bootstrap))); + } + + AssertFullyConnected(nodes); + } + + private void AssertFullyConnected(IEnumerable 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."); + } + } +}