Path: redb-identity/tests/redb.Identity.Web.Tests/RevokedSidsPollHostedServiceTests.cs
Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity
Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Web.Tests/RevokedSidsPollHostedServiceTests.cs
----
using System.Net;
using System.Net.Sockets;
using AwesomeAssertions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NSubstitute;
using redb.Identity.Client.Backchannel;
using redb.Identity.Contracts.Sessions;
using redb.Identity.Web.Configuration;
using redb.Identity.Web.Services;
using Xunit;
namespace redb.Identity.Web.Tests;
///
/// The revoked-SIDs poll while the Identity host is still starting: the full image starts the host and
/// the web server together, and the first poll used to land before the host answered, a warning on every
/// start and a minute until the next try.
///
public sealed class RevokedSidsPollHostedServiceTests
{
[Fact]
public async Task A_host_still_starting_is_retried_soon_and_logged_without_a_warning()
{
var client = Substitute.For();
var calls = 0;
var answered = new TaskCompletionSource();
client.GetRevokedSidsSinceAsync(Arg.Any(), Arg.Any())
.Returns(_ =>
{
if (Interlocked.Increment(ref calls) == 1)
throw new HttpRequestException("Connection refused", new SocketException((int)SocketError.ConnectionRefused));
answered.TrySetResult();
return Task.FromResult(new RevokedSidsSinceResponse());
});
var log = new ListLogger();
var service = new RevokedSidsPollHostedService(client, new RevokedSidsCache(), Options(TimeSpan.FromMinutes(5)), log);
await service.StartAsync(CancellationToken.None);
var finished = await Task.WhenAny(answered.Task, Task.Delay(TimeSpan.FromSeconds(15)));
await service.StopAsync(CancellationToken.None);
finished.Should().Be(answered.Task, "the second try comes after the short bootstrap retry, not after the 5 minute interval");
log.Entries.Should().NotContain(e => e.Level >= LogLevel.Warning);
log.Entries.Should().Contain(e => e.Level == LogLevel.Information && e.Message.Contains("not reachable yet"));
}
[Fact]
public void Only_a_failure_without_an_HTTP_answer_counts_as_unreachable()
{
RevokedSidsPollHostedService.IsUnreachable(new HttpRequestException("refused", new SocketException()))
.Should().BeTrue();
RevokedSidsPollHostedService.IsUnreachable(new HttpRequestException("denied", null, HttpStatusCode.Unauthorized))
.Should().BeFalse("a refused secret is an answer and stays a warning");
RevokedSidsPollHostedService.IsUnreachable(new InvalidOperationException("no token"))
.Should().BeFalse();
}
private static IOptions Options(TimeSpan interval)
{
var options = new IdentityWebOptions();
options.BackchannelClient.ClientSecret = "backchannel-secret-for-the-test";
options.RevokedSids.PollInterval = interval;
return Microsoft.Extensions.Options.Options.Create(options);
}
private sealed class ListLogger : ILogger
{
public List<(LogLevel Level, string Message)> Entries { get; } = [];
public IDisposable? BeginScope(TState state) where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => true;
public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter)
{
lock (Entries)
Entries.Add((logLevel, formatter(state, exception)));
}
}
}