cs-codex-dist-tests/Tests/CodexTests/BasicTests/PyramidTests.cs

89 lines
2.4 KiB
C#
Raw Normal View History

2024-08-21 08:53:38 +00:00
using CodexPlugin;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;
namespace CodexTests.BasicTests
{
[TestFixture]
public class PyramidTests : CodexDistTest
{
[Test]
[CreateTranscript(nameof(PyramidTest))]
public void PyramidTest()
{
var size = 5.MB();
var numberOfLayers = 3;
2024-08-21 08:53:38 +00:00
var bottomLayer = StartLayers(numberOfLayers);
var cids = UploadFiles(bottomLayer, size);
DownloadAllFilesFromEachNodeInLayer(bottomLayer, cids);
}
private List<ICodexNode> StartLayers(int numberOfLayers)
{
var layer = new List<ICodexNode>();
layer.Add(StartCodex(s => s.WithName("Top")));
for (var i = 0; i < numberOfLayers; i++)
{
var newLayer = new List<ICodexNode>();
foreach (var node in layer)
{
newLayer.AddRange(StartCodex(2, s => s.WithBootstrapNode(node).WithName("Layer[" + i + "]")));
}
layer.Clear();
layer.AddRange(newLayer);
}
return layer;
}
private ContentId[] UploadFiles(List<ICodexNode> layer, ByteSize size)
{
var uploadTasks = new List<Task<ContentId>>();
foreach (var node in layer)
{
uploadTasks.Add(Task.Run<ContentId>(() =>
{
var file = GenerateTestFile(size);
return node.UploadFile(file);
}));
}
var cids = uploadTasks.Select(t =>
{
t.Wait();
return t.Result;
}).ToArray();
return cids;
}
private void DownloadAllFilesFromEachNodeInLayer(List<ICodexNode> layer, ContentId[] cids)
{
var downloadTasks = new List<Task>();
foreach (var node in layer)
{
downloadTasks.Add(Task.Run(() =>
{
var dlCids = RandomUtils.Shuffled(cids);
foreach (var cid in dlCids)
{
node.DownloadContent(cid);
}
}));
}
Task.WaitAll(downloadTasks.ToArray());
}
}
}