Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// -----------------------------------------------------------------------
// <copyright file="BaselineJournalBuilderSpec.cs" company="Akka.NET Project">
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using Akka.Actor;
using Akka.Event;
using Akka.Hosting;
using Akka.Persistence.Hosting;
using Akka.Persistence.Query;
using Akka.Persistence.Sql.Query;
using Akka.Persistence.Sql.Tests.Common.Containers;
using Akka.Persistence.TCK.Query;
using Akka.Streams;
using Akka.Streams.TestKit;
using FluentAssertions;
using FluentAssertions.Extensions;
using LinqToDB;
using Xunit;
using Xunit.Abstractions;

namespace Akka.Persistence.Sql.Hosting.Tests
{
/// <summary>
/// Baseline test to validate current journalBuilder functionality before refactoring
/// </summary>
public class BaselineJournalBuilderSpec : Akka.Hosting.TestKit.TestKit, IClassFixture<SqliteContainer>
{
private const string PId = "baseline-test";
private readonly SqliteContainer _fixture;

public BaselineJournalBuilderSpec(ITestOutputHelper output, SqliteContainer fixture)
: base(nameof(BaselineJournalBuilderSpec), output)
{
_fixture = fixture;

if (!_fixture.InitializeDbAsync().Wait(10.Seconds()))
throw new Exception("Failed to clean up database in 10 seconds");
}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
// Test the refactored pattern to ensure basic persistence works
builder.WithSqlPersistence(
connectionString: _fixture.ConnectionString,
providerName: _fixture.ProviderName);

builder.StartActors((system, registry) =>
{
var actor = system.ActorOf(Props.Create(() => new TestPersistentActor(PId)));
registry.Register<TestPersistentActor>(actor);
});
}

[Fact]
public async Task Refactored_hosting_should_support_basic_persistence()
{
// Arrange
var actor = ActorRegistry.Get<TestPersistentActor>();

// Act - persist an event
actor.Tell("test-event");
await ExpectMsgAsync<string>("ACK", 3.Seconds());

// Verify the event was persisted
var readJournal = Sys.ReadJournalFor<SqlReadJournal>("akka.persistence.query.journal.sql");
var source = readJournal.CurrentEventsByPersistenceId(PId, 0, long.MaxValue);
var probe = source.RunWith(this.SinkProbe<EventEnvelope>(), Sys.Materializer());

probe.Request(1);
var envelope = await probe.ExpectNextAsync(3.Seconds());
envelope.PersistenceId.Should().Be(PId);
envelope.Event.Should().Be("test-event");
await probe.ExpectCompleteAsync();
}

private class TestPersistentActor : ReceivePersistentActor
{
public TestPersistentActor(string persistenceId)
{
PersistenceId = persistenceId;

Command<string>(str =>
{
var sender = Sender;
Persist(str, _ => sender.Tell("ACK"));
});
}

public override string PersistenceId { get; }
}
}
}
101 changes: 101 additions & 0 deletions src/Akka.Persistence.Sql.Hosting.Tests/HealthCheckSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// -----------------------------------------------------------------------
// <copyright file="HealthCheckSpec.cs" company="Akka.NET Project">
// Copyright (C) 2013-2023 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Akka.Hosting;
using Akka.Hosting.HealthChecks;
using Akka.Persistence.Sql.Tests.Common.Containers;
using FluentAssertions;
using FluentAssertions.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Xunit;
using Xunit.Abstractions;

namespace Akka.Persistence.Sql.Hosting.Tests
{
/// <summary>
/// Validates that health checks are properly registered after the refactoring
/// </summary>
public class HealthCheckSpec : Akka.Hosting.TestKit.TestKit, IClassFixture<SqliteContainer>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validates that health checks actually get registered and run

{
private readonly SqliteContainer _fixture;

public HealthCheckSpec(ITestOutputHelper output, SqliteContainer fixture)
: base(nameof(HealthCheckSpec), output)
{
_fixture = fixture;

if (!_fixture.InitializeDbAsync().Wait(10.Seconds()))
throw new Exception("Failed to clean up database in 10 seconds");
}

protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services)
{
base.ConfigureServices(context, services);
services.AddHealthChecks();
}

protected override void ConfigureAkka(AkkaConfigurationBuilder builder, IServiceProvider provider)
{
// Use the refactored WithSqlPersistence with health check registration
builder.WithSqlPersistence(
connectionString: _fixture.ConnectionString,
providerName: _fixture.ProviderName,
journalBuilder: journal =>
{
journal.WithHealthCheck(HealthStatus.Degraded);
},
snapshotBuilder: snapshot =>
{
snapshot.WithHealthCheck(HealthStatus.Degraded);
});
}

[Fact]
public async Task Health_checks_should_be_registered_and_healthy()
{
// Arrange
var healthCheckService = Host.Services.GetRequiredService<HealthCheckService>();

// Act - run all health checks
var healthReport = await healthCheckService.CheckHealthAsync(CancellationToken.None);

// Assert - verify that health checks are registered and healthy
healthReport.Entries.Should().NotBeEmpty("health checks should be registered");

// Debug: print all registered health checks
Output?.WriteLine($"Total health checks registered: {healthReport.Entries.Count}");
foreach (var entry in healthReport.Entries)
{
Output?.WriteLine($" - {entry.Key}: {entry.Value.Status}");
}

// We should have at least 1 health check for SQL persistence
var sqlHealthChecks = healthReport.Entries
.Where(e => e.Key.Contains("sql", StringComparison.OrdinalIgnoreCase))
.ToList();

sqlHealthChecks.Should().HaveCountGreaterOrEqualTo(1,
"because we registered health checks for SQL persistence");

// Verify all SQL health checks are healthy
foreach (var healthCheck in sqlHealthChecks)
{
healthCheck.Value.Status.Should().Be(HealthStatus.Healthy,
$"because {healthCheck.Key} should be properly initialized");
}

// Verify overall health status
healthReport.Status.Should().Be(HealthStatus.Healthy,
"because all health checks should pass");
}
}
}
Loading
Loading