Path: redb-route/tests/redb.Route.Tests.GenericFile/TestFileHarness.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.GenericFile/TestFileHarness.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.GenericFile;
namespace redb.Route.Tests.GenericFile;
/// Options for the in-memory test transport.
public sealed class TestFileEndpointOptions : GenericFileEndpointOptions
{
///
public override void Validate() => ValidateCommon();
}
/// Component for the in-memory test transport. Scheme: "testfile".
public sealed class TestFileComponent : ComponentBase
{
private readonly FakeFileOperations _ops;
/// Filter bean the component hands to the options, the way the real components resolve filter=#name from the registry.
public IGenericFileFilter? Filter { get; set; }
public TestFileComponent(FakeFileOperations ops) => _ops = ops;
///
public override string Scheme => "testfile";
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
var options = new TestFileEndpointOptions();
options.BindFromUri(uri.RawParameters);
options.FilterInstance = Filter;
options.Validate();
return new TestFileEndpoint(uri, this, options, _ops);
}
}
/// Endpoint for the in-memory test transport.
public sealed class TestFileEndpoint : EndpointBase
{
private readonly FakeFileOperations _ops;
public TestFileEndpoint(EndpointUri uri, TestFileComponent component, TestFileEndpointOptions options, FakeFileOperations ops)
: base(uri, component, options)
=> _ops = ops;
/// Base directory this endpoint polls or writes to.
public string DirectoryPath => Uri.Path.Length == 0 ? "/" : Uri.Path;
/// Read-lock outcome injected by tests (null = strategy grants the lock).
public Func? ReadLockGate { get; set; }
///
public override IProducer CreateProducer() => new TestFileProducer(this, Options, _ops);
///
public override IConsumer CreateConsumer(IProcessor processor)
=> new TestFileConsumer(this, processor, Options, _ops);
}
/// Consumer over , exercising the shared poll loop.
public sealed class TestFileConsumer : GenericFileConsumer
{
private readonly TestFileEndpoint _endpoint;
///
protected override string ConsumerName => $"testfile:{_endpoint.DirectoryPath}";
///
protected override string BasePath => _endpoint.DirectoryPath;
public TestFileConsumer(TestFileEndpoint endpoint, IProcessor processor, TestFileEndpointOptions options, FakeFileOperations ops)
: base(endpoint, processor, options, ops)
=> _endpoint = endpoint;
/// Runs exactly one poll cycle, returning its tally (created / failed exchanges).
public Task PollOnceAsync(CancellationToken ct = default) => PollDirectory(ct);
///
protected override void SetExchangeHeaders(IMessage message, GenericFileInfo file, string workPath)
{
message.Headers["testFile.Name"] = file.Name;
message.Headers["testFile.AbsolutePath"] = workPath;
message.Headers["testFile.Length"] = file.Length;
}
///
protected override Task AcquireReadLockAsync(GenericFileInfo file, CancellationToken ct)
=> Task.FromResult(_endpoint.ReadLockGate?.Invoke(file) ?? true);
}
/// Producer over , exercising the shared write flow.
public sealed class TestFileProducer : GenericFileProducer
{
private readonly TestFileEndpoint _endpoint;
///
protected override string BasePath => _endpoint.DirectoryPath;
///
protected override string FileNameProducedHeader => "testFile.NameProduced";
public TestFileProducer(TestFileEndpoint endpoint, TestFileEndpointOptions options, FakeFileOperations ops)
: base(endpoint, options, ops)
=> _endpoint = endpoint;
}
/// Shared scaffolding for the tests in this project.
public abstract class GenericFileTestBase
{
protected const string BaseDir = "/in";
protected FakeFileOperations Ops { get; } = new();
protected GenericFileTestBase() => Ops.AddDirectory(BaseDir);
protected TestFileEndpoint Endpoint(Dictionary? parameters = null, string? dir = null, IGenericFileFilter? filter = null)
{
var component = new TestFileComponent(Ops) { Filter = filter };
var path = dir ?? BaseDir;
var uri = new EndpointUri("testfile", path, "testfile://" + path, parameters ?? new Dictionary());
return (TestFileEndpoint)component.CreateEndpoint(uri);
}
/// Collecting processor: records every exchange body as UTF-8 text.
protected static (IProcessor Processor, List Bodies, List Exchanges) Collector(
Action? onProcess = null)
{
var bodies = new List();
var exchanges = new List();
var processor = Substitute.For();
processor.Process(Arg.Any(), Arg.Any())
.Returns(Task.CompletedTask)
.AndDoes(call =>
{
var exchange = call.Arg();
exchanges.Add(exchange);
bodies.Add(exchange.In.Body switch
{
byte[] b => System.Text.Encoding.UTF8.GetString(b),
null => "",
var other => other.ToString() ?? ""
});
onProcess?.Invoke(exchange);
});
return (processor, bodies, exchanges);
}
}