|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2015-2025 Rasmus Mikkelsen |
| 4 | +// https://github.com/eventflow/EventFlow |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 7 | +// this software and associated documentation files (the "Software"), to deal in |
| 8 | +// the Software without restriction, including without limitation the rights to |
| 9 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 10 | +// the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | +// subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 18 | +// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | +// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 20 | +// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | +// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | + |
| 23 | +using System; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.Threading.Tasks; |
| 26 | +using System.Threading; |
| 27 | +using EventFlow.Aggregates; |
| 28 | +using EventFlow.Core; |
| 29 | +using EventFlow.EventStores; |
| 30 | +using EventFlow.Extensions; |
| 31 | +using EventFlow.ReadStores; |
| 32 | +using Microsoft.Extensions.DependencyInjection; |
| 33 | +using NUnit.Framework; |
| 34 | + |
| 35 | +namespace EventFlow.Tests.Exploration |
| 36 | +{ |
| 37 | + // Related https://github.com/eventflow/EventFlow/issues/1083 |
| 38 | + public class ReadModelRepopulateExplorationTest |
| 39 | + { |
| 40 | + private IServiceProvider _serviceProvider; |
| 41 | + |
| 42 | + [SetUp] |
| 43 | + public void SetUp() |
| 44 | + { |
| 45 | + _serviceProvider = EventFlowOptions.New() |
| 46 | + .AddEvents(new[] { typeof(EventV1), typeof(EventV2) }) |
| 47 | + .AddEventUpgraders(typeof(BrokenUpgradeV1ToV2)) |
| 48 | + .UseInMemoryReadStoreFor<UpgradeReadModel>() |
| 49 | + .ServiceCollection.BuildServiceProvider(); |
| 50 | + } |
| 51 | + |
| 52 | + [TearDown] |
| 53 | + public void TearDown() |
| 54 | + { |
| 55 | + (_serviceProvider as IDisposable)?.Dispose(); |
| 56 | + } |
| 57 | + |
| 58 | + [Test] |
| 59 | + public async Task ActuallyStops() |
| 60 | + { |
| 61 | + // Arrange |
| 62 | + var id = BrokenId.New; |
| 63 | + var aggregateStore = _serviceProvider.GetRequiredService<IAggregateStore>(); |
| 64 | + var readModelPopulator = _serviceProvider.GetRequiredService<IReadModelPopulator>(); |
| 65 | + await aggregateStore.UpdateAsync<BrokenAggregate, BrokenId>( |
| 66 | + id, |
| 67 | + SourceId.New, |
| 68 | + (a, c) => |
| 69 | + { |
| 70 | + a.EmitUpgradeEventV1(); |
| 71 | + return Task.CompletedTask; |
| 72 | + }, |
| 73 | + CancellationToken.None); |
| 74 | + |
| 75 | + // Act and Assert |
| 76 | + using var timeoutSource = new CancellationTokenSource(TimeSpan.FromSeconds(2)); |
| 77 | + Assert.ThrowsAsync<Exception>(() => readModelPopulator.PopulateAsync(typeof(UpgradeReadModel), timeoutSource.Token)); |
| 78 | + } |
| 79 | + |
| 80 | + public class UpgradeReadModel : IReadModel, |
| 81 | + IAmReadModelFor<BrokenAggregate, BrokenId, EventV1>, |
| 82 | + IAmReadModelFor<BrokenAggregate, BrokenId, EventV2> |
| 83 | + { |
| 84 | + public Task ApplyAsync( |
| 85 | + IReadModelContext context, |
| 86 | + IDomainEvent<BrokenAggregate, BrokenId, EventV1> domainEvent, |
| 87 | + CancellationToken cancellationToken) |
| 88 | + { |
| 89 | + return Task.CompletedTask; |
| 90 | + } |
| 91 | + |
| 92 | + public Task ApplyAsync( |
| 93 | + IReadModelContext context, |
| 94 | + IDomainEvent<BrokenAggregate, BrokenId, EventV2> domainEvent, |
| 95 | + CancellationToken cancellationToken) |
| 96 | + { |
| 97 | + return Task.CompletedTask; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public class BrokenId : Identity<BrokenId> |
| 102 | + { |
| 103 | + public BrokenId(string value) : base(value) { } |
| 104 | + } |
| 105 | + |
| 106 | + public class BrokenAggregate : AggregateRoot<BrokenAggregate, BrokenId>, |
| 107 | + IEmit<EventV1>, |
| 108 | + IEmit<EventV2> |
| 109 | + { |
| 110 | + public BrokenAggregate(BrokenId id) : base(id) { } |
| 111 | + |
| 112 | + public bool V1Applied { get; private set; } |
| 113 | + public bool V2Applied { get; private set; } |
| 114 | + |
| 115 | + public void EmitUpgradeEventV1() |
| 116 | + { |
| 117 | + Emit(new EventV1()); |
| 118 | + } |
| 119 | + |
| 120 | + public void Apply(EventV1 aggregateEvent) |
| 121 | + { |
| 122 | + V1Applied = true; |
| 123 | + } |
| 124 | + |
| 125 | + public void Apply(EventV2 aggregateEvent) |
| 126 | + { |
| 127 | + V2Applied = true; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + public class EventV1 : IAggregateEvent<BrokenAggregate, BrokenId> |
| 132 | + { |
| 133 | + } |
| 134 | + |
| 135 | + public class EventV2 : IAggregateEvent<BrokenAggregate, BrokenId> |
| 136 | + { |
| 137 | + } |
| 138 | + |
| 139 | + public class BrokenUpgradeV1ToV2 : EventUpgraderNonAsync<BrokenAggregate, BrokenId> |
| 140 | + { |
| 141 | + protected override IEnumerable<IDomainEvent<BrokenAggregate, BrokenId>> Upgrade( |
| 142 | + IDomainEvent<BrokenAggregate, BrokenId> domainEvent) |
| 143 | + { |
| 144 | + throw new Exception("Always broken!"); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments