save progress

This commit is contained in:
StellaOps Bot
2026-01-06 09:42:02 +02:00
parent 94d68bee8b
commit 37e11918e0
443 changed files with 85863 additions and 897 deletions

View File

@@ -0,0 +1,168 @@
// <copyright file="InMemoryHlcStateStoreTests.cs" company="StellaOps">
// Copyright (c) StellaOps. Licensed under AGPL-3.0-or-later.
// </copyright>
using FluentAssertions;
using Xunit;
namespace StellaOps.HybridLogicalClock.Tests;
/// <summary>
/// Unit tests for <see cref="InMemoryHlcStateStore"/>.
/// </summary>
[Trait("Category", "Unit")]
public sealed class InMemoryHlcStateStoreTests
{
[Fact]
public async Task LoadAsync_NoState_ReturnsNull()
{
// Arrange
var store = new InMemoryHlcStateStore();
var ct = TestContext.Current.CancellationToken;
// Act
var result = await store.LoadAsync("node1", ct);
// Assert
result.Should().BeNull();
}
[Fact]
public async Task SaveAsync_ThenLoadAsync_ReturnsState()
{
// Arrange
var store = new InMemoryHlcStateStore();
var ct = TestContext.Current.CancellationToken;
var timestamp = new HlcTimestamp
{
PhysicalTime = 1000,
NodeId = "node1",
LogicalCounter = 5
};
// Act
await store.SaveAsync(timestamp, ct);
var result = await store.LoadAsync("node1", ct);
// Assert
result.Should().Be(timestamp);
}
[Fact]
public async Task SaveAsync_GreaterTimestamp_Updates()
{
// Arrange
var store = new InMemoryHlcStateStore();
var ct = TestContext.Current.CancellationToken;
var first = new HlcTimestamp
{
PhysicalTime = 1000,
NodeId = "node1",
LogicalCounter = 5
};
var second = new HlcTimestamp
{
PhysicalTime = 1000,
NodeId = "node1",
LogicalCounter = 10
};
// Act
await store.SaveAsync(first, ct);
await store.SaveAsync(second, ct);
var result = await store.LoadAsync("node1", ct);
// Assert
result.Should().Be(second);
}
[Fact]
public async Task SaveAsync_SmallerTimestamp_DoesNotUpdate()
{
// Arrange
var store = new InMemoryHlcStateStore();
var ct = TestContext.Current.CancellationToken;
var first = new HlcTimestamp
{
PhysicalTime = 1000,
NodeId = "node1",
LogicalCounter = 10
};
var second = new HlcTimestamp
{
PhysicalTime = 1000,
NodeId = "node1",
LogicalCounter = 5
};
// Act
await store.SaveAsync(first, ct);
await store.SaveAsync(second, ct);
var result = await store.LoadAsync("node1", ct);
// Assert
result.Should().Be(first);
}
[Fact]
public async Task SaveAsync_MultipleNodes_Isolated()
{
// Arrange
var store = new InMemoryHlcStateStore();
var ct = TestContext.Current.CancellationToken;
var node1State = new HlcTimestamp
{
PhysicalTime = 1000,
NodeId = "node1",
LogicalCounter = 1
};
var node2State = new HlcTimestamp
{
PhysicalTime = 2000,
NodeId = "node2",
LogicalCounter = 2
};
// Act
await store.SaveAsync(node1State, ct);
await store.SaveAsync(node2State, ct);
// Assert
var loaded1 = await store.LoadAsync("node1", ct);
var loaded2 = await store.LoadAsync("node2", ct);
loaded1.Should().Be(node1State);
loaded2.Should().Be(node2State);
store.Count.Should().Be(2);
}
[Fact]
public async Task Clear_RemovesAllState()
{
// Arrange
var store = new InMemoryHlcStateStore();
var ct = TestContext.Current.CancellationToken;
await store.SaveAsync(new HlcTimestamp { PhysicalTime = 1, NodeId = "n1", LogicalCounter = 0 }, ct);
await store.SaveAsync(new HlcTimestamp { PhysicalTime = 2, NodeId = "n2", LogicalCounter = 0 }, ct);
// Act
store.Clear();
// Assert
store.Count.Should().Be(0);
}
[Fact]
public async Task LoadAsync_NullNodeId_ThrowsArgumentNullException()
{
// Arrange
var store = new InMemoryHlcStateStore();
var ct = TestContext.Current.CancellationToken;
// Act
var act = () => store.LoadAsync(null!, ct);
// Assert
await act.Should().ThrowAsync<ArgumentNullException>();
}
}