Path: redb-route/tests/redb.Route.Tests.Llm/Storage/NoExchangeConcurrencyTests.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.Llm/Storage/NoExchangeConcurrencyTests.cs
----
using NSubstitute;
using redb.Route.Abstractions;
using redb.Route.Llm.Abstractions.Tools;
using redb.Route.Llm.Engine.Governance;
using redb.Route.Llm.Engine.Storage;
using redb.Route.Llm.Providers;
using redb.Route.Llm.Storage.Redb;
namespace redb.Route.Tests.Llm.Storage;
///
/// Every redb store AddRedbLlmStorage registers, called concurrently without an exchange: each call opens a
/// scope of its own, so none of them shares the context's single instance (which refuses a second command at once).
///
[Collection("StoragePro")]
public sealed class NoExchangeConcurrencyTests
{
private const int Parallel = 8;
private readonly StorageProFixture _fx;
public NoExchangeConcurrencyTests(StorageProFixture fx) => _fx = fx;
private static async Task AllAtOnce(Func> call)
{
using var start = new ManualResetEventSlim(false);
var tasks = Enumerable.Range(0, Parallel).Select(i => Task.Run(async () =>
{
start.Wait();
return await call(i);
})).ToList();
start.Set();
return await Task.WhenAll(tasks);
}
[Fact]
public async Task ApprovalStore()
{
var store = new RedbApprovalStore(_fx.RouteContext);
var prefix = $"appr-{Guid.NewGuid():N}";
ApprovalRequest Request(int i) => new()
{
ConversationId = $"{prefix}-c{i}",
ToolUseId = "tu-1",
InputJson = "{}",
Exchange = Substitute.For(),
Tool = new LlmToolCapability { Name = "delete_user", Description = "test tool", InputSchema = "{}" }
};
await AllAtOnce(async i => { await store.RecordAsync(Request(i), ApprovalDecision.Approve($"{prefix}-{i}")); return i; });
var found = await AllAtOnce(i => store.FindAsync($"{prefix}-{i}"));
found.Should().OnlyContain(r => r != null && r.Approved);
}
[Fact]
public async Task BatchStore()
{
var store = new RedbBatchStore(_fx.RouteContext);
var prefix = $"batch-{Guid.NewGuid():N}";
await AllAtOnce(async i =>
{
await store.RegisterAsync(new BatchJobRecord { BatchId = $"{prefix}-{i}", ProviderId = "anthropic", Status = "submitted" });
return i;
});
var found = await AllAtOnce(i => store.GetAsync($"{prefix}-{i}"));
found.Should().OnlyContain(r => r != null && r.Status == "submitted");
}
[Fact]
public async Task ConversationStore()
{
var store = new RedbConversationStore(_fx.RouteContext);
var prefix = $"conv-{Guid.NewGuid():N}";
await AllAtOnce(i => store.AppendAsync($"{prefix}-{i}", null, LlmMessage.User($"q{i}"),
new ConversationMessageMeta { CreatedAtUtc = DateTime.UtcNow, Iteration = 0, Usage = new LlmUsage(1, 1) }));
var paths = await AllAtOnce(i => store.LoadPathAsync($"{prefix}-{i}"));
paths.Should().OnlyContain(p => p.Count == 1);
}
[Fact]
public async Task KnowledgeStore()
{
var store = new RedbKnowledgeStore(_fx.RouteContext);
var collection = $"kn-{Guid.NewGuid():N}";
await AllAtOnce(async i =>
{
await store.UpsertAsync(new KnowledgeChunk
{
Id = $"{collection}-{i}", Text = $"chunk {i}", Embedding = new[] { 1f, i }, Collection = collection
});
return i;
});
var hits = await AllAtOnce(_ => store.SearchAsync(new[] { 1f, 0f }, topK: Parallel, collection));
hits.Should().OnlyContain(h => h.Count == Parallel);
}
[Fact]
public async Task PromptTemplateRegistry()
{
var store = new RedbPromptTemplateRegistry(_fx.RouteContext);
var prefix = $"tpl-{Guid.NewGuid():N}";
await AllAtOnce(async i => { await store.SetAsync(new PromptTemplate { Name = $"{prefix}-{i}", Version = "1.0", Body = $"b{i}" }); return i; });
var found = await AllAtOnce(i => store.GetAsync($"{prefix}-{i}"));
found.Select(t => t?.Body).Should().Equal(Enumerable.Range(0, Parallel).Select(i => $"b{i}"));
}
[Fact]
public async Task ToolResultCache()
{
var store = new RedbToolResultCache(_fx.RouteContext);
var prefix = $"cache-{Guid.NewGuid():N}";
await AllAtOnce(async i => { await store.SetAsync($"{prefix}-{i}", $$"""{"v":{{i}}}"""); return i; });
var found = await AllAtOnce(async i => await store.GetAsync($"{prefix}-{i}"));
found.Should().Equal(Enumerable.Range(0, Parallel).Select(i => $$"""{"v":{{i}}}"""));
}
}