- 
                Notifications
    You must be signed in to change notification settings 
- Fork 14
Fix health check registration in SQL persistence hosting extensions #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
341844f
              09a0d12
              743dec7
              604b9d4
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; } | ||
| } | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // ----------------------------------------------------------------------- | ||
| // <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.Journal; | ||
| 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> | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 (ALL of them, not just SQL) | ||
| 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 exactly 2 health checks: journal and snapshot | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate that BOTH health checks (journal + snapshot store) run: this is what helped me find akkadotnet/Akka.Hosting#666 | ||
| // Look for any Akka.Persistence-related health checks | ||
| var persistenceHealthChecks = healthReport.Entries | ||
| .Where(e => e.Key.Contains("Akka.Persistence", StringComparison.OrdinalIgnoreCase)) | ||
| .ToList(); | ||
|  | ||
| persistenceHealthChecks.Should().HaveCount(2, | ||
| "because we registered health checks for both journal and snapshot store"); | ||
|  | ||
| // Verify journal health check exists and is healthy | ||
| var journalHealthCheck = persistenceHealthChecks | ||
| .FirstOrDefault(e => e.Key.Contains("journal", StringComparison.OrdinalIgnoreCase)); | ||
|  | ||
| journalHealthCheck.Should().NotBeNull("journal health check should be registered"); | ||
| journalHealthCheck.Value.Status.Should().Be(HealthStatus.Healthy, | ||
| "SQL journal should be properly initialized"); | ||
|  | ||
| // Verify snapshot health check exists and is healthy | ||
| var snapshotHealthCheck = persistenceHealthChecks | ||
| .FirstOrDefault(e => e.Key.Contains("snapshot", StringComparison.OrdinalIgnoreCase)); | ||
|  | ||
| snapshotHealthCheck.Should().NotBeNull("snapshot health check should be registered"); | ||
| snapshotHealthCheck.Value.Status.Should().Be(HealthStatus.Healthy, | ||
| "SQL snapshot store should be properly initialized"); | ||
|  | ||
| // Verify overall health status | ||
| healthReport.Status.Should().Be(HealthStatus.Healthy, | ||
| "because all health checks should pass"); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleaned up the documentation some