#region ========================================================================= USING ===================================================================================== using Lumina.Application.Common.DomainEvents; using Lumina.Domain.Common.Events; using Microsoft.AspNetCore.Http; using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; #endregion namespace Lumina.Presentation.Api.IntegrationTests.Common.Setup; /// /// Domain events queue that exposes its enqueuing and dequeuing to a . /// [ExcludeFromCodeCoverage] public sealed class ProbingDomainEventsQueue : IDomainEventsQueue { private readonly ConcurrentQueue _queue = new(); private readonly DomainEventsQueueProbe _probe; private readonly IHttpContextAccessor _httpContextAccessor; /// /// Initializes a new instance of the class. /// /// The probe that controls and observes the queue. /// Injected accessor used to identify the request that dequeues the queue. public ProbingDomainEventsQueue(DomainEventsQueueProbe probe, IHttpContextAccessor httpContextAccessor) { _probe = probe; _httpContextAccessor = httpContextAccessor; } /// /// Adds a domain event to the queue, before reporting it to the probe. /// /// The domain event to enqueue. public void Enqueue(IDomainEvent domainEvent) { _queue.Enqueue(domainEvent); _probe.RecordEnqueue(); } /// /// Attempts to remove and return the domain event at the beginning of the queue, reporting the attempt to the probe. /// /// When this method returns, contains the domain event removed from the queue, or the default value of if the queue is empty. /// /// if a domain event was successfully dequeued, otherwise. public bool TryDequeue(out IDomainEvent domainEvent) { bool wasDequeued = _queue.TryDequeue(out domainEvent!); string path = _httpContextAccessor.HttpContext?.Request.Path.Value ?? string.Empty; _probe.RecordDequeue(path, wasDequeued); return wasDequeued; } }