Files
duplicati/Duplicati/UnitTest/DiskImage/UnitTests/RawDiskSectorAlignedTests.cs
T
Kenneth Skovhede 179d1ceb47 Make controller Async
This PR has a large blast radius because it takes the final step and bumps up the Controller to be fully async.

We have historically done a piece-by-piece update, so all operations were already async but the controller interface was kept synchronous.

With this update, the controller is now fully async and all tests are updated.

Most places where the new C# compiler warns about function names not ending in `Async` were also adressed, giving a massive refactor change.

Functionally, no changes are done.
2026-05-13 15:04:13 +02:00

84 lines
3.8 KiB
C#

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Assert = NUnit.Framework.Legacy.ClassicAssert;
namespace Duplicati.UnitTest.DiskImage.UnitTests;
public partial class DiskImageUnitTests : BasicSetupHelper
{
[Test]
public async Task Test_RawDisk_ReadSector_ReturnsNonEmptyData_Async()
{
var sectorSize = s_gptRawDisk!.SectorSize;
using var stream = await s_gptRawDisk.ReadSectorsAsync(0, 1, CancellationToken.None);
var buffer = new byte[sectorSize];
var bytesRead = await stream.ReadAsync(buffer.AsMemory(), CancellationToken.None);
Assert.AreEqual(sectorSize, bytesRead, "Should have read 1 sector.");
bool hasData = buffer.Any(x => x != 0);
Assert.IsTrue(hasData, "Sector 0 should contain data.");
}
[Test]
public async Task Test_RawDisk_ReadBytes_ReturnsData_Async()
{
var sectorSize = s_gptRawDisk!.SectorSize;
using var stream = await s_gptRawDisk.ReadBytesAsync(sectorSize, sectorSize, CancellationToken.None);
Assert.AreEqual(sectorSize, stream.Length, "Should have read the correct amount of bytes.");
}
[Test]
public async Task Test_RawDisk_ReadBytesAsync_CallerProvidedBuffer_Async()
{
var sectorSize = s_gptRawDisk!.SectorSize;
var buffer = new byte[sectorSize];
var bytesRead = await s_gptRawDisk.ReadBytesAsync(0, buffer.AsMemory(), CancellationToken.None);
Assert.AreEqual(sectorSize, bytesRead, "Should have read the correct amount of bytes.");
}
[Test]
public async Task Test_RawDisk_WriteSectors_DataMatches_Async()
{
var sectorSize = _writableRawDisk.SectorSize;
var writeBuffer = new byte[sectorSize];
new Random().NextBytes(writeBuffer);
await _writableRawDisk.WriteSectorsAsync(1, writeBuffer, CancellationToken.None);
using var readStream = await _writableRawDisk.ReadSectorsAsync(1, 1, CancellationToken.None);
var readBuffer = new byte[sectorSize];
var bytesRead = await readStream.ReadAsync(readBuffer.AsMemory(), CancellationToken.None);
Assert.AreEqual(sectorSize, bytesRead, "Should have read the correct amount of bytes.");
Assert.AreEqual(writeBuffer, readBuffer, "Data should match.");
}
[Test]
public async Task Test_RawDisk_WriteBytes_DataMatches_Async()
{
var sectorSize = _writableRawDisk.SectorSize;
var writeBuffer = new byte[sectorSize];
new Random().NextBytes(writeBuffer);
await _writableRawDisk.WriteBytesAsync(sectorSize, writeBuffer, CancellationToken.None);
using var readStream = await _writableRawDisk.ReadBytesAsync(sectorSize, sectorSize, CancellationToken.None);
var readBuffer = new byte[sectorSize];
var bytesRead = await readStream.ReadAsync(readBuffer.AsMemory(), CancellationToken.None);
Assert.AreEqual(sectorSize, bytesRead, "Should have read the correct amount of bytes.");
Assert.AreEqual(writeBuffer, readBuffer, "Data should match.");
}
[Test]
public async Task Test_RawDisk_WriteBytes_Memory_Async()
{
var sectorSize = _writableRawDisk.SectorSize;
var writeBuffer = new byte[sectorSize];
new Random().NextBytes(writeBuffer);
await _writableRawDisk.WriteBytesAsync(sectorSize, writeBuffer.AsMemory(), CancellationToken.None);
using var readStream = await _writableRawDisk.ReadBytesAsync(sectorSize, sectorSize, CancellationToken.None);
var readBuffer = new byte[sectorSize];
var bytesRead = await readStream.ReadAsync(readBuffer.AsMemory(), CancellationToken.None);
Assert.AreEqual(sectorSize, bytesRead, "Should have read the correct amount of bytes.");
Assert.AreEqual(writeBuffer, readBuffer, "Data should match.");
}
}