-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathEventPublisherDecorator.cs
More file actions
80 lines (60 loc) · 3.85 KB
/
Copy pathEventPublisherDecorator.cs
File metadata and controls
80 lines (60 loc) · 3.85 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
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>
/// <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.</remarks>
public class EventPublisherDecorator(string key, TestSharedState testSharedState, IEventPublisher innerEventPublisher) : IEventPublisher
{
private readonly TestSharedState _sharedState = testSharedState.ThrowIfNull();
private readonly IEventPublisher _innerEventPublisher = innerEventPublisher.ThrowIfNull();
/// <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();
// 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);
}
}