2
0
mirror of synced 2025-02-11 07:56:58 +00:00

93 lines
2.5 KiB
C#
Raw Normal View History

2024-11-21 10:46:11 +01:00
using CodexPlugin;
using CodexTests;
using FileUtils;
using NUnit.Framework;
using System;
2024-11-21 10:03:09 +01:00
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-11-21 10:46:11 +01:00
using Utils;
2024-11-21 10:03:09 +01:00
namespace CodexReleaseTests.DataTests
{
2024-11-21 10:46:11 +01:00
[TestFixture]
public class SwarmTests : AutoBootstrapDistTest
2024-11-21 10:03:09 +01:00
{
2024-11-21 10:46:11 +01:00
private const int NumberOfNodes = 10;
private const int FileSizeMb = 100;
[Test]
public void SmallSwarm()
{
var nodes = StartCodex(NumberOfNodes);
var files = nodes.Select(UploadUniqueFilePerNode).ToArray();
var tasks = ParallelDownloadEachFile(nodes, files);
Task.WaitAll(tasks);
AssertAllFilesDownloadedCorrectly(files);
}
private SwarmTestNetworkFile UploadUniqueFilePerNode(ICodexNode node)
{
var file = GenerateTestFile(FileSizeMb.MB());
var cid = node.UploadFile(file);
return new SwarmTestNetworkFile(file, cid);
}
private Task[] ParallelDownloadEachFile(ICodexNodeGroup nodes, SwarmTestNetworkFile[] files)
{
var tasks = new List<Task>();
foreach (var node in nodes)
{
foreach (var file in files)
{
tasks.Add(StartDownload(node, file));
}
}
return tasks.ToArray();
}
private Task StartDownload(ICodexNode node, SwarmTestNetworkFile file)
{
return Task.Run(() =>
{
try
{
file.Downloaded = node.DownloadContent(file.Cid);
}
catch (Exception ex)
{
file.Error = ex;
}
});
}
private void AssertAllFilesDownloadedCorrectly(SwarmTestNetworkFile[] files)
{
foreach (var file in files)
{
if (file.Error != null) throw file.Error;
file.Original.AssertIsEqual(file.Downloaded);
}
}
private class SwarmTestNetworkFile
{
public SwarmTestNetworkFile(TrackedFile original, ContentId cid)
{
Original = original;
Cid = cid;
}
public TrackedFile Original { get; }
public ContentId Cid { get; }
public TrackedFile? Downloaded { get; set; }
public Exception? Error { get; set; } = null;
}
2024-11-21 10:03:09 +01:00
}
}