Files
riojoy/tests/RioJoy.Core.Tests/Serial/FakeTransport.cs
T
CydandClaude Opus 4.8 fe87c79f55 net48 port (test branch): retarget all projects to .NET Framework 4.8
Retargets RioJoy.Core/Overlay/Tray + tests from net8.0-windows to net48 so
the app can be tested as a framework-dependent build (relies on the in-box
.NET Framework 4.8 on Windows 10/11). Builds clean; all 241 tests pass.

Polyfills (no behavior change):
- PolySharp source generator for init/records/Index/Range/required members.
- System.Memory, System.Text.Json, Microsoft.Bcl.HashCode,
  System.Threading.Channels (tests) NuGet packages.
- Compat/Net48Polyfills.cs: GetValueOrDefault, KeyValuePair.Deconstruct,
  Math.Clamp; tests/TestPolyfills.cs: Task.WaitAsync.

Source adjustments for APIs absent on net48:
- ArgumentNullException/ArgumentException.ThrowIf* inlined to manual guards.
- Convert.ToHexString, Encoding.Latin1, Environment.ProcessPath,
  ApplicationConfiguration.Initialize, Enum.GetNames<T>/GetValues<T>,
  string.StartsWith(char), string.Split(char, opts), TextBox.PlaceholderText,
  PeriodicTimer, Memory-based Stream Read/WriteAsync, array range-slicing.
- Dropped [SupportedOSPlatform] hints (net48 is single-platform).

deploy/build-package.ps1: publish framework-dependent (no self-contained).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 12:34:47 -05:00

55 lines
2.0 KiB
C#

using System.Threading.Channels;
using RioJoy.Core.Serial;
namespace RioJoy.Core.Tests.Serial;
/// <summary>
/// In-memory <see cref="IRioTransport"/> for driving <see cref="RioSerialLink"/>
/// in tests: enqueue inbound chunks, inspect outbound writes.
/// </summary>
internal sealed class FakeTransport : IRioTransport
{
private readonly Channel<byte[]> _incoming = Channel.CreateUnbounded<byte[]>();
private readonly Channel<byte[]> _writes = Channel.CreateUnbounded<byte[]>();
public string Description => "fake";
/// <summary>Outbound writes, in order. Each call to WriteAsync yields one item.</summary>
public ChannelReader<byte[]> Writes => _writes.Reader;
/// <summary>Queue an inbound chunk for the receive loop to read.</summary>
public void Enqueue(params byte[] data) => _incoming.Writer.TryWrite(data);
/// <summary>Signal that no more inbound data will arrive (transport closed).</summary>
public void CompleteIncoming() => _incoming.Writer.TryComplete();
public async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken)
{
while (await _incoming.Reader.WaitToReadAsync(cancellationToken).ConfigureAwait(false))
{
if (_incoming.Reader.TryRead(out byte[]? chunk))
{
chunk.AsSpan().CopyTo(buffer.Span);
return chunk.Length;
}
}
return 0; // completed
}
public ValueTask WriteAsync(ReadOnlyMemory<byte> data, CancellationToken cancellationToken)
{
_writes.Writer.TryWrite(data.ToArray());
return default; // net48 has no ValueTask.CompletedTask; default(ValueTask) is the completed task
}
/// <summary>Read the next outbound write, failing if none arrives in time.</summary>
public async Task<byte[]> NextWriteAsync(TimeSpan? timeout = null)
{
using var cts = new CancellationTokenSource(timeout ?? TimeSpan.FromSeconds(5));
return await _writes.Reader.ReadAsync(cts.Token).ConfigureAwait(false);
}
public void Dispose() { }
}