-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathEventPublisherDecorator.cs
More file actions
94 lines (72 loc) · 5.12 KB
/
Copy pathEventPublisherDecorator.cs
File metadata and controls
94 lines (72 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
namespace CoreEx.UnitTesting.Events;
/// <summary>
/// Provides a decorator for an event publisher that integrates with <see cref="TestSharedState"/> enabling additional test-related behaviors while delegating event publishing operations to the actual underlying publisher.
/// </summary>
/// <param name="key">The key used to reference the published events in the shared state.</param>
/// <param name="testSharedState">The shared test state used to coordinate or track event publishing during tests.</param>
/// <param name="innerEventPublisher">The underlying event publisher to which all event publishing operations are delegated.</param>
/// <param name="logger">The optional logger.</param>
/// <remarks>This decorator is typically used in testing scenarios to augment and observe event publishing without modifying the core event publisher implementation. All publishing operations are forwarded
/// to the specified inner event publisher.
/// <para>Where a <paramref name="logger"/> is provided, the full event payload for each event about to be published is logged at <see cref="LogLevel.Debug"/> prior to publishing; this is deliberately
/// <b>not</b> available on the production <see cref="CoreEx.Events.Publishing.EventPublisherBase"/> as it could result in sensitive payload data being logged in a deployed environment. As this decorator
/// only exists within the test pipeline, the same visibility here carries no such risk.</para></remarks>
public class EventPublisherDecorator(string key, TestSharedState testSharedState, IEventPublisher innerEventPublisher, ILogger<EventPublisherDecorator>? logger = null) : IEventPublisher
{
private static readonly JsonSerializerOptions _debugJsonSerializerOptions = new() { WriteIndented = true };
private readonly TestSharedState _sharedState = testSharedState.ThrowIfNull();
private readonly IEventPublisher _innerEventPublisher = innerEventPublisher.ThrowIfNull();
private readonly ILogger<EventPublisherDecorator>? _logger = logger;
/// <summary>
/// Gets the key used to reference the published events in the shared state.
/// </summary>
/// <remarks>This key is typically the same as used to register the underlying service itself.</remarks>
public string Key { get; } = key.ThrowIfNullOrEmpty();
/// <inheritdoc/>
public bool HasBeenPublished => _innerEventPublisher.HasBeenPublished;
/// <inheritdoc/>
public bool IsEmpty => _innerEventPublisher.IsEmpty;
/// <inheritdoc/>
public int Count => _innerEventPublisher.Count;
/// <inheritdoc/>
public void Add(IEnumerable<EventData> events) => _innerEventPublisher.Add(events);
/// <inheritdoc/>
public void Add(string destination, IEnumerable<EventData> events) => _innerEventPublisher.Add(destination, events);
/// <inheritdoc/>
public void Add(string destination, IEnumerable<CloudEvent> events) => _innerEventPublisher.Add(destination, events);
/// <inheritdoc/>
public void Add(params EventData[] events) => _innerEventPublisher.Add(events);
/// <inheritdoc/>
public void Add(string destination, params EventData[] events) => _innerEventPublisher.Add(destination, events);
/// <inheritdoc/>
public void Add(string destination, params CloudEvent[] events) => _innerEventPublisher.Add(destination, events);
/// <inheritdoc/>
public void Add(IEnumerable<DestinationEvent> events) => _innerEventPublisher.Add(events);
/// <inheritdoc/>
public void Clear() => _innerEventPublisher.Clear();
/// <inheritdoc/>
public void Reset() => _innerEventPublisher.Reset();
/// <inheritdoc/>
public void Rollback(int count) => _innerEventPublisher.Rollback(count);
/// <inheritdoc/>
public DestinationEvent[] GetEvents() => _innerEventPublisher.GetEvents();
/// <inheritdoc/>
public async Task PublishAsync(CancellationToken cancellationToken = default)
{
var events = GetEvents();
var requestId = _sharedState.GetHttpRequestId();
// Where an action is registered in the shared state for the current request, invoke it; this allows for test-specific behaviors to be executed just prior to the actual publishing of events.
if (_sharedState.RequestStateData(requestId).TryGetValue($"_{nameof(EventPublisherDecorator)}_{key}", out var val) && val is Action publishAction)
publishAction();
// Log contents of the events to be published at debug level, if a logger is provided and debug logging is enabled.
if (_logger?.IsEnabled(LogLevel.Debug) ?? false)
{
var list = events.Select(de => new { destination = de.Destination, @event = de.Event.EncodeToJsonElement() });
_logger.LogDebug("Preparing to send {Length} event(s):{NewLine}{Json}", events.Length, Environment.NewLine, JsonSerializer.Serialize(list, _debugJsonSerializerOptions));
}
// Publish the events using the underlying publisher.
await _innerEventPublisher.PublishAsync(cancellationToken).ConfigureAwait(false);
// Forward the published events appending to the shared state.
_sharedState.RequestStateData(requestId).AddOrUpdate(Key, events, (_, __) => events);
}
}