Path: redb-identity/tests/redb.Identity.Tests/Routes/UniqueViolationMetricCallSitesTests.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/Routes/UniqueViolationMetricCallSitesTests.cs ---- using System.Diagnostics.Metrics; using AwesomeAssertions; using Microsoft.Extensions.DependencyInjection; using redb.Identity.Core.Metrics; using redb.Identity.Core.Routes.Processors; using redb.Route.Core; using Xunit; namespace redb.Identity.Tests.Routes; /// /// The processors that catch a unique violation themselves and answer the caller with a conflict /// count it on identity.unique_violation as the builder-level handlers do. The list below is /// the whole of them; a processor added to it or taken from it changes what the metric means. /// public class UniqueViolationMetricCallSitesTests { private static readonly string ProcessorsFolder = LocateProcessorsFolder(); public static IEnumerable ProcessorsThatAnswerAConflictThemselves() => new[] { new object[] { "ApplicationManagementProcessor.cs" }, new object[] { "ScopeManagementProcessor.cs" }, new object[] { "ClaimScopeManagementProcessor.cs" }, new object[] { "BootstrapAdminProcessor.cs" }, new object[] { "AccountRegisterProcessor.cs" }, new object[] { "ScimUserProcessor.cs" }, }; [Theory] [MemberData(nameof(ProcessorsThatAnswerAConflictThemselves))] public void TheProcessorCountsTheViolationItCatches(string file) { var source = File.ReadAllText(Path.Combine(ProcessorsFolder, file)); source.Should().Contain("IdentityProcessorHelpers.CountUniqueViolation(_context, exchange,", $"{file} answers a unique violation itself, so it has to count it itself"); } [Fact] public async Task TheProcessorSideCount_ReachesTheMeterOfTheHost() { var seen = new List>(); using var listener = new MeterListener(); listener.InstrumentPublished = (instrument, l) => { if (instrument.Meter.Name == IdentityMetrics.MeterName && instrument.Name == "identity.unique_violation") l.EnableMeasurementEvents(instrument); }; listener.SetMeasurementEventCallback((_, _, tags, _) => { var map = new Dictionary(); foreach (var tag in tags) map[tag.Key] = tag.Value?.ToString(); lock (seen) seen.Add(map); }); var services = new ServiceCollection(); services.AddSingleton(); await using var provider = services.BuildServiceProvider(); listener.Start(); await using var context = new RouteContext(provider, "unique-violation-call-site"); var exchange = new Exchange(new Message()); IdentityProcessorHelpers.CountUniqueViolation(context, exchange, new InvalidOperationException("UNIQUE constraint failed: _users._email")); seen.Should().ContainSingle(); seen[0]["scheme"].Should().Be("_users"); seen[0]["column"].Should().Be("_email"); } private static string LocateProcessorsFolder() { for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent) { foreach (var root in new[] { dir.FullName, Path.Combine(dir.FullName, "redb.Identity") }) { var candidate = Path.Combine(root, "src", "redb.Identity.Core", "Routes", "Processors"); if (Directory.Exists(candidate)) return candidate; } } throw new InvalidOperationException("Could not locate redb.Identity.Core/Routes/Processors from the test base directory."); } }