Files
VolcanicCore/tests/VolcanicCore.Tests/RetentionServiceTests.cs

44 lines
1.6 KiB
C#

using Microsoft.Extensions.Logging.Abstractions;
using VolcanicCore.Api.Engine;
using VolcanicCore.Persistence;
using VolcanicCore.Persistence.Migrations;
using VolcanicCore.Persistence.Repositories;
namespace VolcanicCore.Tests;
public class RetentionServiceTests
{
[Fact]
public void Sweep_prunes_old_rows_and_over_cap_rows()
{
var path = Path.Combine(Path.GetTempPath(), $"vc-{Guid.NewGuid():N}.db");
var f = new SqliteConnectionFactory(path);
using (var conn = f.Open())
new Migrator(new IMigration[]
{
new Migration001Initial(), new Migration002Connections(), new Migration003Workflows(),
new Migration004Executions(), new Migration005NodeExecutions(),
}).Apply(conn);
var er = new ExecutionRepository(f);
// 3 old finished, 5 recent finished (keep=3 → prune 2 of the recent + all 3 old)
for (var i = 0; i < 3; i++)
{
var id = er.Create("wf", "manual", "{}");
er.SetStatus(id, "completed", null, DateTimeOffset.UtcNow.AddDays(-30));
}
for (var i = 0; i < 5; i++)
{
var id = er.Create("wf", "manual", "{}");
er.SetStatus(id, "completed", null, DateTimeOffset.UtcNow.AddSeconds(i));
}
Assert.Equal(8, er.ListForWorkflow("wf", 100).Count);
var svc = new RetentionService(er, retentionDays: 7, keepPerWorkflow: 3,
NullLogger<RetentionService>.Instance);
svc.RunSweep();
Assert.Equal(3, er.ListForWorkflow("wf", 100).Count);
}
}