Path: redb/redb.Tests.Integration/Tests/Unit/RedbHashReferenceCycleTests.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/Unit/RedbHashReferenceCycleTests.cs
----
using redb.Core.Models.Entities;
using redb.Core.Utils;
namespace redb.Tests.Integration.Tests.Unit;
///
/// The object hash walks nested classes of the Props by reflection. A graph where a nested object is reached again
/// through its own properties (a back-reference) has no end: the walk must stop with an error that names the cycle,
/// not recurse until the stack overflows and takes the process down. One object reached twice without a cycle (a
/// shared instance) is not a cycle and hashes as before.
///
public sealed class RedbHashReferenceCycleTests
{
public sealed class CycleNode
{
public string? Name { get; set; }
public CycleNode? Next { get; set; }
}
public sealed class CycleHolder
{
public CycleNode? Head { get; set; }
public List? Items { get; set; }
}
[Fact]
public void BackReferenceThroughAClassProperty_IsRefused()
{
var a = new CycleNode { Name = "a" };
var b = new CycleNode { Name = "b", Next = a };
a.Next = b;
var act = () => RedbHash.ComputeFor(new RedbObject { id = 1, Props = new CycleHolder { Head = a } });
act.Should().Throw()
.WithMessage($"*reference cycle*{nameof(CycleNode)}*");
}
[Fact]
public void BackReferenceThroughAListElement_IsRefused()
{
var a = new CycleNode { Name = "a" };
a.Next = a;
var act = () => RedbHash.ComputeFor(new RedbObject { id = 1, Props = new CycleHolder { Items = [a] } });
act.Should().Throw().WithMessage("*reference cycle*");
}
[Fact]
public void SharedInstanceWithoutACycle_HashesLikeTwoEqualCopies()
{
var shared = new CycleNode { Name = "s" };
var sharedHash = RedbHash.ComputeFor(new RedbObject
{ id = 1, Props = new CycleHolder { Head = shared, Items = [shared, shared] } });
var copiesHash = RedbHash.ComputeFor(new RedbObject
{ id = 1, Props = new CycleHolder { Head = new() { Name = "s" }, Items = [new() { Name = "s" }, new() { Name = "s" }] } });
sharedHash.Should().NotBeNull();
sharedHash.Should().Be(copiesHash);
}
}