Path: redb/redb.Tests.Integration/Tests/SqlitePro/SqliteProReferenceCycleSaveTests.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/SqlitePro/SqliteProReferenceCycleSaveTests.cs
----
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Core;
using redb.Core.Attributes;
using redb.Core.Models.Entities;
using redb.Core.Pro.Extensions;
using redb.SQLite.Pro.Extensions;
namespace redb.Tests.Integration.Tests.SqlitePro;
public class ReferenceCycleNode
{
public string? Name { get; set; }
public ReferenceCycleNode? Next { get; set; }
}
[RedbScheme("Save: reference cycle probe", Name = "reference_cycle_probe")]
public class ReferenceCycleProps
{
public string? Title { get; set; }
public ReferenceCycleNode? Head { get; set; }
}
///
/// A Props graph with a back-reference through plain class properties: the save refuses it with an error naming the
/// cycle and writes nothing, instead of overflowing the stack in the object hash and killing the process.
///
public class SqliteProReferenceCycleSaveTests
{
private const string Cs = "Data Source=redb_tests_reference_cycle_pro.db";
[Fact]
public async Task SaveOfAGraphWithABackReference_IsRefusedAndWritesNothing()
{
Fixtures.SqliteTestSupport.DeleteDbFiles(Cs);
var services = new ServiceCollection();
services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning));
services.AddRedbPro(options => options.UseSqlite(Cs));
await using var provider = services.BuildServiceProvider();
var redb = provider.GetRequiredService();
await redb.InitializeAsync(ensureCreated: true);
await redb.SyncSchemeAsync();
var first = new ReferenceCycleNode { Name = "first" };
first.Next = new ReferenceCycleNode { Name = "second", Next = first };
var act = () => redb.SaveAsync(new RedbObject
{ name = "cycle", Props = new ReferenceCycleProps { Title = "t", Head = first } });
await act.Should().ThrowAsync()
.WithMessage($"*reference cycle*{nameof(ReferenceCycleNode)}*");
(await redb.Query().CountAsync()).Should().Be(0, "nothing of the refused save is written");
}
}