-
Notifications
You must be signed in to change notification settings - Fork 15
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
Merged
Aaronontheweb
merged 4 commits into
akkadotnet:dev
from
Aaronontheweb:fix/hosting-health-checks
Oct 2, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
341844f
Refactor hosting extensions to use unified API and fix health check r…
Aaronontheweb 09a0d12
Upgrade to Akka.Hosting 1.5.51.1 and tighten health check test assert…
Aaronontheweb 743dec7
Update health checks documentation with correct API parameter names
Aaronontheweb 604b9d4
Reference Akka.Persistence.Sql version instead of Akka.Hosting in hea…
Aaronontheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
src/Akka.Persistence.Sql.Hosting.Tests/BaselineJournalBuilderSpec.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
101
src/Akka.Persistence.Sql.Hosting.Tests/HealthCheckSpec.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
| { | ||
| 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"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Validates that health checks actually get registered and run