Skip to content

Commit

Permalink
Limit the number of connections (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx authored Sep 6, 2024
1 parent 4992e40 commit cf92173
Show file tree
Hide file tree
Showing 24 changed files with 2,080 additions and 1,011 deletions.
4 changes: 2 additions & 2 deletions dkgNodeLibrary/Services/DkgNodeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public async Task Register(HttpClient httpClient)

try
{
response = await httpClient.PostAsync($"{ServiceNodeUrl}/api/nodes/register", httpContent);
response = await httpClient.PostAsync($"{ServiceNodeUrl}/api/ops/register", httpContent);
}
catch (Exception e)
{
Expand Down Expand Up @@ -369,7 +369,7 @@ public async Task<StatusResponse> ReportStatus(HttpClient httpClient, string[]?
var httpContent = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
try
{
httpResponse = await httpClient.PostAsync($"{ServiceNodeUrl}/api/nodes/status", httpContent);
httpResponse = await httpClient.PostAsync($"{ServiceNodeUrl}/api/ops/status", httpContent);
if (httpResponse == null)
{
Logger.LogError("Node '{Name}' failed to report {r} to '{ServiceNodeUrl}', no response received, resetting node",
Expand Down
62 changes: 46 additions & 16 deletions dkgNodesTests/NodeComparer.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,95 @@
// POSSIBILITY OF SUCH DAMAGE.

using dkgServiceNode.Models;
using dkgServiceNode.Services.Cache;
using dkgServiceNode.Services.NodeComparer;

namespace dkgNodesTests
{
[TestFixture]
public class NodeComparerTests
{
private readonly NodesRoundHistoryCache nodesRoundHistoryCache = new();

[Test]
public void Compare_BothNodesNull_ReturnsZero()
{
var comparer = new NodeComparer(0, 0);
var comparer = new NodeComparer(0, 1, nodesRoundHistoryCache);
var result = comparer.Compare(null, null);
Assert.That(result, Is.EqualTo(0));
}

[Test]
public void Compare_FirstNodeNull_ReturnsOne()
{
var comparer = new NodeComparer(0, 0);
var node = new Node { NodesRoundHistory = new List<NodesRoundHistory>() };
// Arrange
var comparer = new NodeComparer(0, 1, nodesRoundHistoryCache);
var node = new Node() { Id = 100181 };

var history1 = new NodesRoundHistory { NodeId = 100181, RoundId = 1, NodeRandom = 5 };
var history2 = new NodesRoundHistory { NodeId = 100182, RoundId = 1, NodeRandom = 3 };
nodesRoundHistoryCache.LoadNodesRoundHistoryToCache(history1);
nodesRoundHistoryCache.LoadNodesRoundHistoryToCache(history2);

var result = comparer.Compare(null, node);
Assert.That(result, Is.EqualTo(0));
Assert.That(result, Is.EqualTo(1));
}

[Test]
public void Compare_SecondNodeNull_ReturnsMinusOne()
{
var comparer = new NodeComparer(0, 0);
var node = new Node { NodesRoundHistory = new List<NodesRoundHistory>() };
var comparer = new NodeComparer(0, 1, nodesRoundHistoryCache);
var node = new Node { Id = 1001810 };
var result = comparer.Compare(node, null);
Assert.That(result, Is.EqualTo(0));
}

[Test]
public void Compare_BothNodesHaveSameNodeRandom_ReturnsZero()
{
var comparer = new NodeComparer(0, 1);
var node1 = new Node { NodesRoundHistory = new List<NodesRoundHistory> { new NodesRoundHistory { RoundId = 1, NodeRandom = 5 } } };
var node2 = new Node { NodesRoundHistory = new List<NodesRoundHistory> { new NodesRoundHistory { RoundId = 1, NodeRandom = 5 } } };
var comparer = new NodeComparer(0, 1,nodesRoundHistoryCache);
var node1 = new Node { Id = 1001811 };
var node2 = new Node { Id = 1001812 };

var history1 = new NodesRoundHistory { NodeId = 1001811, RoundId = 1, NodeRandom = 5 };
var history2 = new NodesRoundHistory { NodeId = 1001812, RoundId = 1, NodeRandom = 5 };

nodesRoundHistoryCache.LoadNodesRoundHistoryToCache(history1);
nodesRoundHistoryCache.LoadNodesRoundHistoryToCache(history2);

var result = comparer.Compare(node1, node2);
Assert.That(result, Is.EqualTo(0));
}

[Test]
public void Compare_FirstNodeHasSmallerNodeRandom_ReturnsMinusOne()
{
var comparer = new NodeComparer(0, 1);
var node1 = new Node { NodesRoundHistory = new List<NodesRoundHistory> { new NodesRoundHistory { RoundId = 1, NodeRandom = 3 } } };
var node2 = new Node { NodesRoundHistory = new List<NodesRoundHistory> { new NodesRoundHistory { RoundId = 1, NodeRandom = 5 } } };
var comparer = new NodeComparer(0, 1, nodesRoundHistoryCache);
var node1 = new Node { Id = 1001821 };
var node2 = new Node { Id = 1001822 };

var history1 = new NodesRoundHistory { NodeId = 1001821, RoundId = 1, NodeRandom = 3 };
var history2 = new NodesRoundHistory { NodeId = 1001822, RoundId = 1, NodeRandom = 5 };

nodesRoundHistoryCache.LoadNodesRoundHistoryToCache(history1);
nodesRoundHistoryCache.LoadNodesRoundHistoryToCache(history2);

var result = comparer.Compare(node1, node2);
Assert.That(result, Is.EqualTo(-1));
}

[Test]
public void Compare_SecondNodeHasSmallerNodeRandom_ReturnsOne()
{
var comparer = new NodeComparer(0, 1);
var node1 = new Node { NodesRoundHistory = new List<NodesRoundHistory> { new NodesRoundHistory { RoundId = 1, NodeRandom = 5 } } };
var node2 = new Node { NodesRoundHistory = new List<NodesRoundHistory> { new NodesRoundHistory { RoundId = 1, NodeRandom = 3 } } };
var comparer = new NodeComparer(0, 1, nodesRoundHistoryCache);
var node1 = new Node { Id = 1001831 };
var node2 = new Node { Id = 1001832 };

var history1 = new NodesRoundHistory { NodeId = 1001831, RoundId = 1, NodeRandom = 5 };
var history2 = new NodesRoundHistory { NodeId = 1001832, RoundId = 1, NodeRandom = 3 };

nodesRoundHistoryCache.LoadNodesRoundHistoryToCache(history1);
nodesRoundHistoryCache.LoadNodesRoundHistoryToCache(history2);

var result = comparer.Compare(node1, node2);
Assert.That(result, Is.EqualTo(1));
}
Expand Down
159 changes: 159 additions & 0 deletions dkgNodesTests/NodesCache.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// Copyright (C) 2024 Maxim [maxirmx] Samsonov (www.sw.consulting)
// All rights reserved.
// This file is a part of dkg service node
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
using dkgServiceNode.Models;
using dkgServiceNode.Services.Cache;

namespace dkgNodesTests
{
[TestFixture]
public class NodeCacheTests
{
private readonly NodesCache nodesCache = new();
[SetUp]
public void SetUp()
{
for (int i = 1; i <= 2; i++)
{
var node = nodesCache.GetNodeById(i);
if (node != null)
{
nodesCache.DeleteNodeFromCache(node);
}
}
}

[Test]
public void LoadNodesToCache_ShouldLoadNodes()
{

var nodes = new List<Node>
{
new Node { Id = 1, Name = "Node1" },
new Node { Id = 2, Name = "Node2" }
};


nodesCache.LoadNodesToCache(nodes);


nodes = nodesCache.GetAllNodes();
Assert.Multiple(() =>
{
Assert.That(nodes.Count, Is.EqualTo(2));
Assert.That(nodes.First(n => n.Id == 1).Name, Is.EqualTo("Node1"));
Assert.That(nodes.First(n => n.Id == 2).Name, Is.EqualTo("Node2"));
});
}

[Test]
public void GetNodeById_ShouldReturnCorrectNode()
{

var node = new Node { Id = 1, Name = "Node1" };
nodesCache.LoadNodeToCache(node);


var result = nodesCache.GetNodeById(1);


Assert.That(result, Is.Not.Null);
Assert.That(result.Name, Is.EqualTo("Node1"));
}

[Test]
public void AddNodeToCache_ShouldAddNode()
{

var node = new Node { Id = 1, Name = "Node1" };


nodesCache.LoadNodeToCache(node);


var result = nodesCache.GetNodeById(1);
Assert.That(result, Is.Not.Null);
Assert.That(result.Name, Is.EqualTo("Node1"));
}

[Test]
public void UpdateNodeInCache_ShouldUpdateNode()
{

var node = new Node { Id = 1, Name = "Node1" };
nodesCache.LoadNodeToCache(node);


node.Name = "UpdatedNode1";
nodesCache.UpdateNodeInCache(node);


var result = nodesCache.GetNodeById(1);
Assert.That(result, Is.Not.Null);
Assert.That(result.Name, Is.EqualTo("UpdatedNode1"));
}

[Test]
public void DeleteNodeFromCache_ShouldRemoveNode()
{

var node = new Node { Id = 1, Name = "Node1" };
nodesCache.LoadNodeToCache(node);


node = nodesCache.GetNodeById(1);
Assert.That(node, Is.Not.Null);
nodesCache.DeleteNodeFromCache(node);


var result = nodesCache.GetNodeById(1);
Assert.That(result, Is.Null);
}

[Test]
public void NodeExists_ShouldReturnTrueIfExists()
{

var node = new Node { Id = 1, Name = "Node1" };
nodesCache.LoadNodeToCache(node);


var exists = nodesCache.GetNodeById(1) != null;


Assert.That(exists, Is.True);
}

[Test]
public void NodeExists_ShouldReturnFalseIfNotExists()
{

var exists = nodesCache.GetNodeById(1) != null;


Assert.That(exists, Is.False);
}
}

}
Loading

0 comments on commit cf92173

Please sign in to comment.