|
| 1 | +using MongoDB.Bson; |
| 2 | +using NEventStore.Persistence.AcceptanceTests; |
| 3 | +using NEventStore.Persistence.MongoDB.Support; |
| 4 | +using NEventStore.Persistence.AcceptanceTests.BDD; |
| 5 | +using FluentAssertions; |
| 6 | +#if MSTEST |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | +#endif |
| 9 | +#if NUNIT |
| 10 | +#endif |
| 11 | +#if XUNIT |
| 12 | +using Xunit; |
| 13 | +using Xunit.Should; |
| 14 | +#endif |
| 15 | + |
| 16 | +namespace NEventStore.Persistence.MongoDB.Tests.AcceptanceTests.Async |
| 17 | +{ |
| 18 | +#if MSTEST |
| 19 | + [TestClass] |
| 20 | +#endif |
| 21 | + public class Verify_safe_generator_not_create_hole : PersistenceEngineConcern |
| 22 | + { |
| 23 | + private string? _streamId; |
| 24 | + private string? _bucketId; |
| 25 | + private CommitAttempt? _attempt; |
| 26 | + |
| 27 | + public Verify_safe_generator_not_create_hole() |
| 28 | + { |
| 29 | + var options = new MongoPersistenceOptions |
| 30 | + { |
| 31 | + ConcurrencyStrategy = ConcurrencyExceptionStrategy.Continue |
| 32 | + }; |
| 33 | + |
| 34 | + var db = options.ConnectToDatabase(AcceptanceTestMongoPersistenceFactory.GetConnectionString()); |
| 35 | + var collection = db.GetCollection<BsonDocument>("Commits"); |
| 36 | + options.CheckpointGenerator = new AlwaysQueryDbForNextValueCheckpointGenerator(collection); |
| 37 | + |
| 38 | + PersistenceEngineFixture.Options = options; |
| 39 | + |
| 40 | + // workaround for test initialization to have uniform config for all 3 test frameworks |
| 41 | + // we can't use ClassInitialize, TestFixtureSetup or SetFixture |
| 42 | + Fixture!.Initialize(ConfiguredPageSizeForTesting); |
| 43 | + |
| 44 | + // reset this immediately, hopefully will not impact other tests |
| 45 | + PersistenceEngineFixture.Options = null; |
| 46 | + } |
| 47 | + |
| 48 | + protected override async Task ContextAsync() |
| 49 | + { |
| 50 | + _streamId = Guid.NewGuid().ToString(); |
| 51 | + _attempt = _streamId.BuildAttempt(); |
| 52 | + var commit = await Persistence.CommitAsync(_attempt).ConfigureAwait(false); |
| 53 | + _bucketId = commit!.BucketId; |
| 54 | + } |
| 55 | + |
| 56 | + protected override async Task BecauseAsync() |
| 57 | + { |
| 58 | + try |
| 59 | + { |
| 60 | + await Persistence.CommitAsync(_streamId!.BuildAttempt()).ConfigureAwait(false); |
| 61 | + throw new Exception("Previous message should throw concurrency exception"); |
| 62 | + } |
| 63 | + catch (ConcurrencyException) |
| 64 | + { |
| 65 | + //do nothing. |
| 66 | + } |
| 67 | + await Persistence.CommitAsync(_attempt!.BuildNextAttempt()).ConfigureAwait(false); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public async Task Holes_are_presents() |
| 72 | + { |
| 73 | + var observer = new CommitStreamObserver(); |
| 74 | + await Persistence.GetFromAsync(_bucketId!, _streamId!, int.MinValue, int.MaxValue, observer).ConfigureAwait(false); |
| 75 | + var commits = observer.Commits.ToArray(); |
| 76 | + commits.Length.Should().Be(2); |
| 77 | + commits[0].CheckpointToken.Should().Be(1); |
| 78 | + commits[1].CheckpointToken.Should().Be(2); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +#if MSTEST |
| 83 | + [TestClass] |
| 84 | +#endif |
| 85 | + public class Holes_are_filled_after_concurrency_exception : PersistenceEngineConcern |
| 86 | + { |
| 87 | + private string? _streamId; |
| 88 | + private string? _bucketId; |
| 89 | + private CommitAttempt? _attempt; |
| 90 | + |
| 91 | + public Holes_are_filled_after_concurrency_exception() |
| 92 | + { |
| 93 | + var options = new MongoPersistenceOptions |
| 94 | + { |
| 95 | + ConcurrencyStrategy = ConcurrencyExceptionStrategy.FillHole |
| 96 | + }; |
| 97 | + PersistenceEngineFixture.Options = options; |
| 98 | + |
| 99 | + // workaround for test initialization to have uniform config for all 3 test frameworks |
| 100 | + // we can't use ClassInitialize, TestFixtureSetup or SetFixture |
| 101 | + Fixture!.Initialize(ConfiguredPageSizeForTesting); |
| 102 | + |
| 103 | + // reset this immediately, hopefully will not impact other tests |
| 104 | + PersistenceEngineFixture.Options = null; |
| 105 | + } |
| 106 | + |
| 107 | + protected override async Task ContextAsync() |
| 108 | + { |
| 109 | + _streamId = Guid.NewGuid().ToString(); |
| 110 | + var commit = await Persistence.CommitAsync(_attempt = _streamId.BuildAttempt()).ConfigureAwait(false); |
| 111 | + _bucketId = commit!.BucketId; |
| 112 | + } |
| 113 | + |
| 114 | + protected override async Task BecauseAsync() |
| 115 | + { |
| 116 | + try |
| 117 | + { |
| 118 | + await Persistence.CommitAsync(_streamId!.BuildAttempt()).ConfigureAwait(false); |
| 119 | + throw new Exception("Previous message should throw concurrency exception"); |
| 120 | + } |
| 121 | + catch (ConcurrencyException) |
| 122 | + { |
| 123 | + //do nothing. |
| 124 | + } |
| 125 | + await Persistence.CommitAsync(_attempt!.BuildNextAttempt()).ConfigureAwait(false); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public async Task Holes_are_not_presents() |
| 130 | + { |
| 131 | + var observer = new CommitStreamObserver(); |
| 132 | + await Persistence.GetFromAsync(_bucketId!, _streamId!, int.MinValue, int.MaxValue, observer).ConfigureAwait(false); |
| 133 | + var commits = observer.Commits.ToArray(); |
| 134 | + commits.Length.Should().Be(2); |
| 135 | + commits[0].CheckpointToken.Should().Be(1); |
| 136 | + commits[1].CheckpointToken.Should().Be(3); |
| 137 | + |
| 138 | + observer = new CommitStreamObserver(); |
| 139 | + await Persistence.GetFromAsync("system", "system.empty.2", int.MinValue, int.MaxValue, observer).ConfigureAwait(false); |
| 140 | + commits = observer.Commits.ToArray(); |
| 141 | + commits.Length.Should().Be(1); |
| 142 | + commits[0].CheckpointToken.Should().Be(2); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +#if MSTEST |
| 147 | + [TestClass] |
| 148 | +#endif |
| 149 | + public class Holes_are_not_filled_as_default_behavior : PersistenceEngineConcern |
| 150 | + { |
| 151 | + private string? _streamId; |
| 152 | + private string? _bucketId; |
| 153 | + private CommitAttempt? _attempt; |
| 154 | + |
| 155 | + public Holes_are_not_filled_as_default_behavior() |
| 156 | + { |
| 157 | + var options = new MongoPersistenceOptions |
| 158 | + { |
| 159 | + ConcurrencyStrategy = ConcurrencyExceptionStrategy.Continue |
| 160 | + }; |
| 161 | + |
| 162 | + var db = options.ConnectToDatabase(AcceptanceTestMongoPersistenceFactory.GetConnectionString()); |
| 163 | + var collection = db.GetCollection<BsonDocument>("Commits"); |
| 164 | + |
| 165 | + options.CheckpointGenerator = new InMemoryCheckpointGenerator(collection); |
| 166 | + PersistenceEngineFixture.Options = options; |
| 167 | + |
| 168 | + // workaround for test initialization to have uniform config for all 3 test frameworks |
| 169 | + // we can't use ClassInitialize, TestFixtureSetup or SetFixture |
| 170 | + Fixture!.Initialize(ConfiguredPageSizeForTesting); |
| 171 | + |
| 172 | + // reset this immediately, hopefully will not impact other tests |
| 173 | + PersistenceEngineFixture.Options = null; |
| 174 | + } |
| 175 | + |
| 176 | + protected override async Task ContextAsync() |
| 177 | + { |
| 178 | + _streamId = Guid.NewGuid().ToString(); |
| 179 | + var commit = await Persistence.CommitAsync(_attempt = _streamId.BuildAttempt()).ConfigureAwait(false); |
| 180 | + _bucketId = commit!.BucketId; |
| 181 | + } |
| 182 | + |
| 183 | + protected override async Task BecauseAsync() |
| 184 | + { |
| 185 | + try |
| 186 | + { |
| 187 | + await Persistence.CommitAsync(_streamId!.BuildAttempt()).ConfigureAwait(false); |
| 188 | + throw new Exception("Previous message should throw concurrency exception"); |
| 189 | + } |
| 190 | + catch (ConcurrencyException) |
| 191 | + { |
| 192 | + //do nothing. |
| 193 | + } |
| 194 | + await Persistence.CommitAsync(_attempt!.BuildNextAttempt()).ConfigureAwait(false); |
| 195 | + } |
| 196 | + |
| 197 | + [Fact] |
| 198 | + public async Task Holes_are_presents() |
| 199 | + { |
| 200 | + var observer = new CommitStreamObserver(); |
| 201 | + await Persistence.GetFromAsync(_bucketId!, _streamId!, int.MinValue, int.MaxValue, observer).ConfigureAwait(false); |
| 202 | + var commits = observer.Commits.ToArray(); |
| 203 | + commits.Length.Should().Be(2); |
| 204 | + commits[0].CheckpointToken.Should().Be(1); |
| 205 | + commits[1].CheckpointToken.Should().Be(3); |
| 206 | + |
| 207 | + observer = new CommitStreamObserver(); |
| 208 | + await Persistence.GetFromAsync("system", "system.2", int.MinValue, int.MaxValue, observer).ConfigureAwait(false); |
| 209 | + commits = observer.Commits.ToArray(); |
| 210 | + commits.Length.Should().Be(0); |
| 211 | + } |
| 212 | + } |
| 213 | + |
| 214 | +#if MSTEST |
| 215 | + [TestClass] |
| 216 | +#endif |
| 217 | + public class Default_behavior_after_concurrency_exception : PersistenceEngineConcern |
| 218 | + { |
| 219 | + private string? _streamId; |
| 220 | + private string? _bucketId; |
| 221 | + private CommitAttempt? _attempt; |
| 222 | + |
| 223 | + protected override async Task ContextAsync() |
| 224 | + { |
| 225 | + _streamId = Guid.NewGuid().ToString(); |
| 226 | + var commit = await Persistence.CommitAsync(_attempt = _streamId.BuildAttempt()).ConfigureAwait(false); |
| 227 | + _bucketId = commit!.BucketId; |
| 228 | + } |
| 229 | + |
| 230 | + protected override async Task BecauseAsync() |
| 231 | + { |
| 232 | + try |
| 233 | + { |
| 234 | + await Persistence.CommitAsync(_streamId!.BuildAttempt()).ConfigureAwait(false); |
| 235 | + throw new Exception("Previous message should throw concurrency exception"); |
| 236 | + } |
| 237 | + catch (ConcurrencyException) |
| 238 | + { |
| 239 | + //do nothing. |
| 240 | + } |
| 241 | + await Persistence.CommitAsync(_attempt!.BuildNextAttempt()).ConfigureAwait(false); |
| 242 | + } |
| 243 | + |
| 244 | + [Fact] |
| 245 | + public async Task Holes_are_not_presents() |
| 246 | + { |
| 247 | + var observer = new CommitStreamObserver(); |
| 248 | + await Persistence.GetFromAsync(_bucketId!, _streamId!, int.MinValue, int.MaxValue, observer).ConfigureAwait(false); |
| 249 | + var commits = observer.Commits.ToArray(); |
| 250 | + commits.Length.Should().Be(2); |
| 251 | + commits[0].CheckpointToken.Should().Be(1); |
| 252 | + commits[1].CheckpointToken.Should().Be(2); |
| 253 | + |
| 254 | + observer = new CommitStreamObserver(); |
| 255 | + await Persistence.GetFromAsync("system", "system.2", int.MinValue, int.MaxValue, observer).ConfigureAwait(false); |
| 256 | + commits = observer.Commits.ToArray(); |
| 257 | + commits.Length.Should().Be(0); |
| 258 | + } |
| 259 | + } |
| 260 | +} |
0 commit comments