-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandleBadCasesTest.cs
97 lines (80 loc) · 3.04 KB
/
HandleBadCasesTest.cs
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
95
96
97
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using EventSourcing.Infrastructure;
using FluentAssertions;
namespace EventSourcing.Test;
[TestClass]
public class HandleBadCasesTest
{
[TestMethod]
public async Task SkipCorruptedEvent()
{
var test = await TestHelper.SetupEventSourcing<TestService>();
var receivedEvents = await test.SendAndWait(new IEventPayload[]
{
new UnreadableEntryAdded("1", "Next entry"),
new EntryAdded("1", "Hallo diary")
}, 1);
receivedEvents[0].Payload.Should().BeOfType<EntryAdded>();
}
[TestMethod]
public async Task MapToErrorEvent()
{
var test = await TestHelper.SetupEventSourcing<TestService>(options => options
.WithCorruptedEventsHandler<MapToErrorPayloadHandler>()
);
var receivedEvents = await test
.SendAndWait(new IEventPayload[]
{
new EntryAdded("1", "Hallo diary"),
new UnreadableEntryAdded("1", "Next entry")
});
receivedEvents[1].Payload.Should().BeOfType<MapToErrorPayloadHandler.CorruptedEvent>();
}
class StreamIds
{
public static StreamId Journal(string id) => new("Journal", id);
}
[SerializableEventPayload("EntryAdded")]
record EntryAdded : EventPayload
{
public EntryAdded(string journalId, string text) : base(StreamIds.Journal(journalId), "EntryAdded")
{
JournalId = journalId;
Text = text;
}
public string JournalId { get; }
public string Text { get; }
}
[SerializableEventPayload("UnreadableEntryAdded")]
record UnreadableEntryAdded : EventPayload //this cannot be deserialized because there is no matching property for constructor parameter journalId
{
public UnreadableEntryAdded(string journalId, string text) : base(StreamIds.Journal(journalId), "UnreadableEntryAdded") => Text = text;
public string Text { get; }
}
}
class TestService
{
readonly EventStream<Event> _eventStream;
readonly IEventStore _eventStore;
public TestService(EventStream<Event> eventStream, IEventStore eventStore)
{
_eventStream = eventStream;
_eventStore = eventStore;
}
public async Task<IList<Event>> SendAndWait(IReadOnlyCollection<IEventPayload> payloads, int? numberOfExpectedEvents = null)
{
await _eventStore.WriteEvents(payloads);
return await _eventStream.Take(numberOfExpectedEvents ?? payloads.Count)
.ToList()
.ToTask()
.WaitAsync(TimeSpan.FromSeconds(1));
}
}
public class MapToErrorPayloadHandler : ICorruptedEventHandler
{
public IEventPayload OnDeserializeOrMappingError(Exception error, long eventPosition, string eventType,
DateTimeOffset timestamp, object serializedPayload) =>
new CorruptedEvent(eventType, serializedPayload);
public record CorruptedEvent(string EventType, object SerializedPayload) : EventPayload(new("Corrupted", "Events"), EventType);
}