Path: redb/redb.Tests.Integration/Tests/Base/DeleteSubtreeCountTestsBase.cs Snapshot: redb, 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 Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/Base/DeleteSubtreeCountTestsBase.cs ---- using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using redb.Core; using redb.Core.Extensions; using redb.Core.Models.Entities; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.Base; /// /// DeleteSubtreeAsync answers how many objects of the subtree it deleted, the start object included. It collects the /// subtree first and deletes it after; it used to answer the size of what it collected, so an object another session /// deleted in between was counted too. The delete is put exactly after the collection through the test context. /// public abstract class DeleteSubtreeCountTestsBase { private readonly SqlRecorder _recorder = new(); protected abstract void UseProvider(RedbOptionsBuilder options); protected virtual void Register(IServiceCollection services, Action configure) => services.AddRedb(configure); private ServiceProvider Build(bool decorate) { var services = new ServiceCollection(); services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning)); Register(services, options => { UseProvider(options); options.Configure(c => c.CacheDomain = $"subtree-count-{GetType().Name}-{Guid.NewGuid():N}"); }); if (decorate) RecordingRedbContext.Decorate(services, _recorder); return services.BuildServiceProvider(); } /// The statement that collects the subtree (Tree_SelectSubtreeRows). private static bool CollectsSubtree(string sql) => sql.Contains("AS IdParent, _id_scheme AS IdScheme, depth AS Depth", StringComparison.Ordinal); /// A root, three children, a grandchild under the first child: five objects. private static async Task<(long Root, long Grandchild)> SeedAsync(IRedbService redb) { await redb.SyncSchemeAsync(); var tag = "subc-" + Guid.NewGuid().ToString("N")[..8]; var root = new TreeRedbObject { name = $"{tag}-root", Props = new SimpleProps { Title = tag } }; root.id = await redb.SaveAsync(root); long firstChild = 0; for (var i = 0; i < 3; i++) { var child = new TreeRedbObject { name = $"{tag}-c{i}", Props = new SimpleProps { Title = tag, Count = i } }; child.id = await redb.CreateChildAsync(child, root); if (i == 0) firstChild = child.id; } var grandchild = new TreeRedbObject { name = $"{tag}-g", Props = new SimpleProps { Title = tag, Count = 9 } }; var parent = new TreeRedbObject { id = firstChild }; grandchild.id = await redb.CreateChildAsync(grandchild, parent); return (root.id, grandchild.id); } [Fact] public async Task DeleteSubtree_CountsEveryObjectOfTheSubtree() { await using var host = Build(decorate: false); var redb = host.GetRequiredService(); await redb.InitializeAsync(ensureCreated: true); var (root, _) = await SeedAsync(redb); var deleted = await redb.DeleteSubtreeAsync(new RedbObject { id = root }); deleted.Should().Be(5); (await redb.LoadAsync(root)).Should().BeNull(); } [Fact] public async Task ObjectDeletedAfterTheSubtreeWasCollected_IsNotCounted() { await using var writerHost = Build(decorate: false); var writer = writerHost.GetRequiredService(); await writer.InitializeAsync(ensureCreated: true); var (root, grandchild) = await SeedAsync(writer); await using var deleterHost = Build(decorate: true); var deleter = deleterHost.GetRequiredService(); await deleter.InitializeAsync(ensureCreated: true); var interleaved = 0; _recorder.AfterQuery = async sql => { if (!CollectsSubtree(sql) || Interlocked.Exchange(ref interleaved, 1) == 1) return; (await writer.DeleteAsync(grandchild)).Should().BeTrue("precondition: the other session deletes the grandchild"); }; int deleted; try { deleted = await deleter.DeleteSubtreeAsync(new RedbObject { id = root }); } finally { _recorder.AfterQuery = null; } interleaved.Should().Be(1, "precondition: the delete ran between the collection and the delete of the subtree"); deleted.Should().Be(4, "the grandchild was deleted by the other session, not by this call"); (await writer.LoadAsync(root)).Should().BeNull(); } }