|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using EventStore.Common.Utils; |
| 5 | +using EventStore.Core.Services.Storage.ReaderIndex; |
| 6 | +using EventStore.Core.Services.Transport.Grpc; |
| 7 | +using EventStore.Core.Tests.Helpers; |
| 8 | +using NUnit.Framework; |
| 9 | + |
| 10 | +namespace EventStore.Core.Tests.Services.Transport.Grpc; |
| 11 | + |
| 12 | +[TestFixture] |
| 13 | +public class EnumeratorsTests { |
| 14 | + |
| 15 | + [TestFixture(typeof(LogFormat.V2), typeof(string))] |
| 16 | + [TestFixture(typeof(LogFormat.V3), typeof(uint))] |
| 17 | + public class subscribe_all_from_start<TLogFormat, TStreamId> : TestFixtureWithExistingEvents<TLogFormat, TStreamId> { |
| 18 | + |
| 19 | + private readonly List<Guid> _eventIds = new(); |
| 20 | + |
| 21 | + protected override void Given() { |
| 22 | + EnableReadAll(); |
| 23 | + _eventIds.Add(WriteEvent("test-stream", "type1", "{}", "{Data: 1}").Item1.EventId); |
| 24 | + _eventIds.Add(WriteEvent("test-stream", "type2", "{}", "{Data: 2}").Item1.EventId); |
| 25 | + _eventIds.Add(WriteEvent("test-stream", "type3", "{}", "{Data: 3}").Item1.EventId); |
| 26 | + } |
| 27 | + |
| 28 | + [Test] |
| 29 | + public async Task should_receive_live_caught_up_message_after_reading_existing_events() { |
| 30 | + var enumerator = new TestEnumerators.AllSubscription<TStreamId>(new TestEnumerators.SubscriptionRequest(_publisher, Position.Start)); |
| 31 | + |
| 32 | + TestEnumerators.SubscriptionResponse response = await enumerator.GetNext(); |
| 33 | + Assert.True(response is TestEnumerators.SubscriptionConfirmation); |
| 34 | + while ((response = await enumerator.GetNext()) != TestEnumerators.SubscriptionResponse.None) { |
| 35 | + if (_eventIds.IsNotEmpty()) { |
| 36 | + Assert.AreEqual(Uuid.FromGuid(_eventIds[0]), ((TestEnumerators.Event)response).RecordedEvent.Id); |
| 37 | + _eventIds.RemoveAt(0); |
| 38 | + } else { |
| 39 | + Assert.True(response is TestEnumerators.CaughtUp); |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + [TestFixture(typeof(LogFormat.V2), typeof(string))] |
| 47 | + [TestFixture(typeof(LogFormat.V3), typeof(uint))] |
| 48 | + public class subscribe_all_from_end<TLogFormat, TStreamId> : TestFixtureWithExistingEvents<TLogFormat, TStreamId> { |
| 49 | + |
| 50 | + protected override void Given() { |
| 51 | + EnableReadAll(); |
| 52 | + WriteEvent("test-stream", "type1", "{}", "{Data: 1}"); |
| 53 | + WriteEvent("test-stream", "type2", "{}", "{Data: 2}"); |
| 54 | + WriteEvent("test-stream", "type3", "{}", "{Data: 3}"); |
| 55 | + } |
| 56 | + |
| 57 | + [Test] |
| 58 | + public async Task should_receive_live_caught_up_message_immediately() { |
| 59 | + var enumerator = new TestEnumerators.AllSubscription<TStreamId>(new TestEnumerators.SubscriptionRequest(_publisher, Position.End)); |
| 60 | + |
| 61 | + Assert.True(await enumerator.GetNext() is TestEnumerators.SubscriptionConfirmation); |
| 62 | + Assert.True(await enumerator.GetNext() is TestEnumerators.CaughtUp); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + [TestFixture(typeof(LogFormat.V2), typeof(string))] |
| 67 | + [TestFixture(typeof(LogFormat.V3), typeof(uint))] |
| 68 | + public class subscribe_filtered_all_from_start_<TLogFormat, TStreamId> : TestFixtureWithExistingEvents<TLogFormat, TStreamId> { |
| 69 | + |
| 70 | + private readonly List<Guid> _eventIds = new(); |
| 71 | + |
| 72 | + protected override void Given() { |
| 73 | + EnableReadAll(); |
| 74 | + _eventIds.Add(WriteEvent("test-stream", "type1", "{}", "{Data: 1}").Item1.EventId); |
| 75 | + WriteEvent("test-stream", "type2", "{}", "{Data: 2}"); |
| 76 | + WriteEvent("test-stream", "type3", "{}", "{Data: 3}"); |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public async Task should_receive_live_caught_up_message_after_reading_existing_events() { |
| 81 | + var enumerator = new TestEnumerators.AllSubscriptionFiltered<TStreamId>(new TestEnumerators.SubscriptionRequest(_publisher, Position.Start, EventFilter: EventFilter.EventType.Prefixes(false, "type1"))); |
| 82 | + |
| 83 | + TestEnumerators.SubscriptionResponse response = await enumerator.GetNext(); |
| 84 | + Assert.True(response is TestEnumerators.SubscriptionConfirmation); |
| 85 | + while ((response = await enumerator.GetNext()) != TestEnumerators.SubscriptionResponse.None) { |
| 86 | + if (_eventIds.IsNotEmpty()) { |
| 87 | + Assert.AreEqual(Uuid.FromGuid(_eventIds[0]), ((TestEnumerators.Event)response).RecordedEvent.Id); |
| 88 | + _eventIds.RemoveAt(0); |
| 89 | + } else { |
| 90 | + Assert.True(response is TestEnumerators.CaughtUp); |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + [TestFixture(typeof(LogFormat.V2), typeof(string))] |
| 98 | + [TestFixture(typeof(LogFormat.V3), typeof(uint))] |
| 99 | + public class subscribe_filtered_all_from_end_<TLogFormat, TStreamId> : TestFixtureWithExistingEvents<TLogFormat, TStreamId> { |
| 100 | + |
| 101 | + protected override void Given() { |
| 102 | + EnableReadAll(); |
| 103 | + WriteEvent("test-stream", "type1", "{}", "{Data: 1}"); |
| 104 | + WriteEvent("test-stream", "type2", "{}", "{Data: 2}"); |
| 105 | + WriteEvent("test-stream", "type3", "{}", "{Data: 3}"); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public async Task should_receive_live_caught_up_message_immediately() { |
| 110 | + var enumerator = new TestEnumerators.AllSubscriptionFiltered<TStreamId>(new TestEnumerators.SubscriptionRequest(_publisher, Position.End, EventFilter: EventFilter.EventType.Prefixes(false, "type1"))); |
| 111 | + |
| 112 | + Assert.True(await enumerator.GetNext() is TestEnumerators.SubscriptionConfirmation); |
| 113 | + Assert.True(await enumerator.GetNext() is TestEnumerators.CaughtUp); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + [TestFixture(typeof(LogFormat.V2), typeof(string))] |
| 118 | + [TestFixture(typeof(LogFormat.V3), typeof(uint))] |
| 119 | + public class subscribe_stream_from_start_<TLogFormat, TStreamId> : TestFixtureWithExistingEvents<TLogFormat, TStreamId> { |
| 120 | + |
| 121 | + private readonly List<Guid> _eventIds = new(); |
| 122 | + |
| 123 | + protected override void Given() { |
| 124 | + EnableReadAll(); |
| 125 | + _eventIds.Add(WriteEvent("test-stream1", "type1", "{}", "{Data: 1}").Item1.EventId); |
| 126 | + WriteEvent("test-stream2", "type2", "{}", "{Data: 2}"); |
| 127 | + WriteEvent("test-stream3", "type3", "{}", "{Data: 3}"); |
| 128 | + } |
| 129 | + |
| 130 | + [Test] |
| 131 | + public async Task should_receive_live_caught_up_message_after_reading_existing_events() { |
| 132 | + var enumerator = new TestEnumerators.StreamSubscription<TStreamId>(new TestEnumerators.SubscriptionRequest(_publisher, StreamName: "test-stream1")); |
| 133 | + |
| 134 | + TestEnumerators.SubscriptionResponse response = await enumerator.GetNext(); |
| 135 | + Assert.True(response is TestEnumerators.SubscriptionConfirmation); |
| 136 | + while ((response = await enumerator.GetNext()) != TestEnumerators.SubscriptionResponse.None) { |
| 137 | + if (_eventIds.IsNotEmpty()) { |
| 138 | + Assert.AreEqual(Uuid.FromGuid(_eventIds[0]), ((TestEnumerators.Event)response).RecordedEvent.Id); |
| 139 | + _eventIds.RemoveAt(0); |
| 140 | + } else { |
| 141 | + Assert.True(response is TestEnumerators.CaughtUp); |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + [TestFixture(typeof(LogFormat.V2), typeof(string))] |
| 149 | + [TestFixture(typeof(LogFormat.V3), typeof(uint))] |
| 150 | + public class subscribe_stream_from_end<TLogFormat, TStreamId> : TestFixtureWithExistingEvents<TLogFormat, TStreamId> { |
| 151 | + |
| 152 | + private readonly List<Guid> _eventIds = new(); |
| 153 | + |
| 154 | + protected override void Given() { |
| 155 | + EnableReadAll(); |
| 156 | + _eventIds.Add(WriteEvent("test-stream1", "type1", "{}", "{Data: 1}").Item1.EventId); |
| 157 | + WriteEvent("test-stream2", "type2", "{}", "{Data: 2}"); |
| 158 | + WriteEvent("test-stream3", "type3", "{}", "{Data: 3}"); |
| 159 | + } |
| 160 | + |
| 161 | + [Test] |
| 162 | + public async Task should_receive_live_caught_up_message_immediately() { |
| 163 | + var enumerator = new TestEnumerators.StreamSubscription<TStreamId>(new TestEnumerators.SubscriptionRequest(_publisher, StreamName: "test-stream1", StartRevision: StreamRevision.End)); |
| 164 | + |
| 165 | + Assert.True(await enumerator.GetNext() is TestEnumerators.SubscriptionConfirmation); |
| 166 | + Assert.True(await enumerator.GetNext() is TestEnumerators.CaughtUp); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments