Path: redb-identity/tests/redb.Identity.Tests/Webhooks/WebhookSubscriptionCacheTests.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.Tests/Webhooks/WebhookSubscriptionCacheTests.cs
----
using AwesomeAssertions;
using redb.Core.Models.Entities;
using redb.Identity.Core.Events;
using redb.Identity.Core.Models;
using Xunit;
namespace redb.Identity.Tests.Webhooks;
/// The subscription cache never keeps a list that a change dropped while it was being read.
public class WebhookSubscriptionCacheTests
{
private static readonly IReadOnlyList> Before = [new RedbObject()];
private static readonly IReadOnlyList> After =
[new RedbObject(), new RedbObject()];
[Fact]
public async Task AReadRunningWhenTheListIsDropped_AnswersItsEvent_ButIsNotKept()
{
var cache = new WebhookSubscriptionCache(TimeProvider.System, TimeSpan.FromMinutes(5));
var reading = new TaskCompletionSource>>(
TaskCreationOptions.RunContinuationsAsynchronously);
// An event starts a read; a subscription is created before that read returns.
var first = cache.GetEnabledAsync(_ => reading.Task);
cache.Invalidate();
reading.SetResult(Before);
(await first).Should().BeSameAs(Before);
// The next event reads again and sees the new subscription.
var next = await cache.GetEnabledAsync(_ => Task.FromResult(After));
next.Should().BeSameAs(After);
cache.Loads.Should().Be(2);
// That read was not overtaken by a change, so it is kept.
(await cache.GetEnabledAsync(_ => throw new InvalidOperationException("the cached list should answer")))
.Should().BeSameAs(After);
}
[Fact]
public async Task TwoEventsThatFindNoList_ShareOneRead()
{
var cache = new WebhookSubscriptionCache(TimeProvider.System, TimeSpan.FromMinutes(5));
var reading = new TaskCompletionSource>>(
TaskCreationOptions.RunContinuationsAsynchronously);
var reads = 0;
Task>> Load(CancellationToken _)
{
Interlocked.Increment(ref reads);
return reading.Task;
}
var first = cache.GetEnabledAsync(Load);
var second = cache.GetEnabledAsync(Load);
reads.Should().Be(1, "the second event waits for the read the first one started");
reading.SetResult(Before);
(await first).Should().BeSameAs(Before);
(await second).Should().BeSameAs(Before);
cache.Loads.Should().Be(1);
}
[Fact]
public async Task AFailedRead_AnswersItsWaitersWithTheError_AndIsNotKept()
{
var cache = new WebhookSubscriptionCache(TimeProvider.System, TimeSpan.FromMinutes(5));
var reading = new TaskCompletionSource>>(
TaskCreationOptions.RunContinuationsAsynchronously);
var first = cache.GetEnabledAsync(_ => reading.Task);
var second = cache.GetEnabledAsync(_ => throw new InvalidOperationException("the running read should answer"));
reading.SetException(new TimeoutException("database"));
await FluentActions.Awaiting(() => first).Should().ThrowAsync();
await FluentActions.Awaiting(() => second).Should().ThrowAsync();
// The next event reads again and that read is kept.
(await cache.GetEnabledAsync(_ => Task.FromResult(After))).Should().BeSameAs(After);
(await cache.GetEnabledAsync(_ => throw new InvalidOperationException("the cached list should answer")))
.Should().BeSameAs(After);
cache.Loads.Should().Be(2, "the failed read counts as a read");
}
[Fact]
public async Task AnEventAfterTheListIsDropped_DoesNotJoinTheReadAlreadyRunning()
{
var cache = new WebhookSubscriptionCache(TimeProvider.System, TimeSpan.FromMinutes(5));
var older = new TaskCompletionSource>>(
TaskCreationOptions.RunContinuationsAsynchronously);
var newer = new TaskCompletionSource>>(
TaskCreationOptions.RunContinuationsAsynchronously);
var first = cache.GetEnabledAsync(_ => older.Task);
cache.Invalidate();
var second = cache.GetEnabledAsync(_ => newer.Task);
newer.SetResult(After);
older.SetResult(Before);
(await first).Should().BeSameAs(Before);
(await second).Should().BeSameAs(After);
cache.Loads.Should().Be(2);
(await cache.GetEnabledAsync(_ => throw new InvalidOperationException("the cached list should answer")))
.Should().BeSameAs(After);
}
[Fact]
public async Task AReadIsCountedWhenItStarts_SoAReadFromBeforeACountDoesNotLandInIt()
{
var cache = new WebhookSubscriptionCache(TimeProvider.System, TimeSpan.FromMinutes(5));
var older = new TaskCompletionSource>>(
TaskCreationOptions.RunContinuationsAsynchronously);
// An earlier event's read is still running when a change drops the list and the count is taken.
var earlier = cache.GetEnabledAsync(_ => older.Task);
cache.Invalidate();
var loads = cache.Loads;
older.SetResult(Before);
await earlier;
(await cache.GetEnabledAsync(_ => Task.FromResult(After))).Should().BeSameAs(After);
(cache.Loads - loads).Should().Be(1, "only the read started after the count");
}
}