Files
ExecuteR/tests/Shared.Tests/DeviceFileStreamTests.cs
Alphaeus Mote 1e1491ebd6 feat(files): contracts and paced frames for a device's file browser
Requests answer on core NATS. Transfers move in 256 KB frames with at most
2 MB unacknowledged, so a slow browser slows the device and nothing queues.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 04:17:50 -05:00

163 lines
6.3 KiB
C#

using System.Security.Cryptography;
using System.Threading.Channels;
using Shared.Contracts.Messages;
using Xunit;
namespace Shared.Tests;
/// <summary>A file transfer's frames, paced by acknowledgements, over in-memory channels standing in for the bus.</summary>
public class DeviceFileStreamTests
{
private static readonly TimeSpan Idle = TimeSpan.FromSeconds(10);
private static ValueTask To(Channel<byte[]> channel, byte[] frame)
{
channel.Writer.TryWrite(frame);
return ValueTask.CompletedTask;
}
[Fact]
public async Task EveryByteArrivesInOrder()
{
var data = RandomNumberGenerator.GetBytes(5 * 1024 * 1024 + 123);
var toReceiver = Channel.CreateUnbounded<byte[]>();
var toSender = Channel.CreateUnbounded<byte[]>();
var ct = TestContext.Current.CancellationToken;
using var received = new MemoryStream();
var reading = DeviceFileStreamReader.ReadAsync(
toReceiver.Reader,
(bytes, token) => received.WriteAsync(bytes, token),
(frame, _) => To(toSender, frame),
Idle,
ct);
await using var writer = new DeviceFileStreamWriter((frame, _) => To(toReceiver, frame), toSender.Reader, Idle);
await writer.WriteAsync(data, ct);
await writer.CompleteAsync(ct);
Assert.Equal(data.Length, await reading);
Assert.Equal(data, received.ToArray());
}
[Fact]
public async Task ASenderStopsAtTheWindowUntilItIsAcknowledged()
{
var published = new List<byte[]>();
var acks = Channel.CreateUnbounded<byte[]>();
var ct = TestContext.Current.CancellationToken;
await using var writer = new DeviceFileStreamWriter(
(frame, _) =>
{
lock (published)
{
published.Add(frame);
}
return ValueTask.CompletedTask;
},
acks.Reader,
Idle,
frameBytes: 100,
windowBytes: 300);
var writing = writer.WriteAsync(new byte[1000], ct).AsTask();
await Task.Delay(100, ct);
Assert.False(writing.IsCompleted);
lock (published)
{
Assert.Equal(3, published.Count); // The window's worth, and no more.
}
acks.Writer.TryWrite(DeviceFileFrame.Create(DeviceFileFrame.Ack, 1000));
await writing;
lock (published)
{
Assert.Equal(10, published.Count);
}
}
[Fact]
public async Task ASenderThatGetsNoAcknowledgementGivesUp()
{
var ct = TestContext.Current.CancellationToken;
await using var writer = new DeviceFileStreamWriter(
(_, _) => ValueTask.CompletedTask,
Channel.CreateUnbounded<byte[]>().Reader,
TimeSpan.FromMilliseconds(50),
frameBytes: 100,
windowBytes: 100);
await Assert.ThrowsAsync<DeviceFileStreamException>(async () => await writer.WriteAsync(new byte[500], ct));
}
[Fact]
public async Task AReceiverAbortStopsTheSender()
{
var incoming = Channel.CreateUnbounded<byte[]>();
var ct = TestContext.Current.CancellationToken;
await using var writer = new DeviceFileStreamWriter((_, _) => ValueTask.CompletedTask, incoming.Reader, Idle, frameBytes: 100, windowBytes: 10_000);
incoming.Writer.TryWrite(DeviceFileFrame.CreateAbort("The disk is full."));
var problem = await Assert.ThrowsAsync<DeviceFileStreamException>(async () => await writer.WriteAsync(new byte[100], ct));
Assert.Equal("The disk is full.", problem.Message);
}
[Fact]
public async Task ASenderAbortReachesTheReceiverWithItsReason()
{
var incoming = Channel.CreateUnbounded<byte[]>();
incoming.Writer.TryWrite(DeviceFileFrame.Create(DeviceFileFrame.Data, 0, new byte[10]));
incoming.Writer.TryWrite(DeviceFileFrame.CreateAbort("Access denied."));
var problem = await Assert.ThrowsAsync<DeviceFileStreamException>(() => DeviceFileStreamReader.ReadAsync(
incoming.Reader, (_, _) => ValueTask.CompletedTask, (_, _) => ValueTask.CompletedTask, Idle, TestContext.Current.CancellationToken));
Assert.Equal("Access denied.", problem.Message);
}
[Theory]
[InlineData(5, 10)] // A frame is missing.
[InlineData(0, 11)] // The end claims more than came.
public async Task AGapIsNotPassedOffAsAFile(long secondOffset, long endTotal)
{
var incoming = Channel.CreateUnbounded<byte[]>();
incoming.Writer.TryWrite(DeviceFileFrame.Create(DeviceFileFrame.Data, 0, new byte[5]));
if (secondOffset != 0)
{
incoming.Writer.TryWrite(DeviceFileFrame.Create(DeviceFileFrame.Data, secondOffset + 5, new byte[5]));
}
incoming.Writer.TryWrite(DeviceFileFrame.Create(DeviceFileFrame.End, endTotal));
await Assert.ThrowsAsync<DeviceFileStreamException>(() => DeviceFileStreamReader.ReadAsync(
incoming.Reader, (_, _) => ValueTask.CompletedTask, (_, _) => ValueTask.CompletedTask, Idle, TestContext.Current.CancellationToken));
}
[Fact]
public async Task ASilentSenderTimesOutAndAClosedLinkEnds()
{
var ct = TestContext.Current.CancellationToken;
var silent = Channel.CreateUnbounded<byte[]>();
await Assert.ThrowsAsync<DeviceFileStreamException>(() => DeviceFileStreamReader.ReadAsync(
silent.Reader, (_, _) => ValueTask.CompletedTask, (_, _) => ValueTask.CompletedTask, TimeSpan.FromMilliseconds(50), ct));
var closed = Channel.CreateUnbounded<byte[]>();
closed.Writer.TryComplete();
await Assert.ThrowsAsync<DeviceFileStreamException>(() => DeviceFileStreamReader.ReadAsync(
closed.Reader, (_, _) => ValueTask.CompletedTask, (_, _) => ValueTask.CompletedTask, Idle, ct));
}
[Fact]
public async Task AResultIsFoundBehindLateAcknowledgements()
{
var incoming = Channel.CreateUnbounded<byte[]>();
incoming.Writer.TryWrite(DeviceFileFrame.Create(DeviceFileFrame.Ack, 10));
incoming.Writer.TryWrite(DeviceFileFrame.CreateResult(new DeviceFileReply(DeviceFileStatuses.Ok, Path: "/tmp/a")));
var reply = await DeviceFileStreamReader.ReadResultAsync(incoming.Reader, Idle, TestContext.Current.CancellationToken);
Assert.Equal("/tmp/a", reply.Path);
}
}