예제 #1
0
        public async Task return_events_in_correct_order_compared_to_written()
        {
            var events = await _fixture.Client.ReadAllForwardsAsync(Position.Start, _fixture.Events.Length * 2, false)
                         .ToArrayAsync();

            Assert.True(EventDataComparer.Equal(_fixture.Events, events.AsResolvedTestEvents().ToArray()));
        }
        public async Task return_events_in_reversed_order_compared_to_written()
        {
            var events = await _fixture.Client.ReadAllBackwardsAsync(Position.End, _fixture.Events.Length, false)
                         .ToArrayAsync();

            Assert.True(EventDataComparer.Equal(
                            _fixture.Events.Reverse().ToArray(),
                            events.AsResolvedTestEvents().ToArray()));
        }
예제 #3
0
        public async Task returns_all_events_including_tombstone()
        {
            var events = await _fixture.Client.ReadAllForwardsAsync(Position.Start, _fixture.Events.Length * 2, false)
                         .ToArrayAsync();

            var tombstone = events.Last();

            Assert.Equal(Stream, tombstone.Event.EventStreamId);
            Assert.Equal(SystemEventTypes.StreamDeleted, tombstone.Event.EventType);

            Assert.True(EventDataComparer.Equal(_fixture.Events, events.AsResolvedTestEvents().ToArray()));
        }
        public async Task reads_all_existing_events_and_keep_listening_to_new_ones()
        {
            var appeared       = new TaskCompletionSource <bool>();
            var dropped        = new TaskCompletionSource <(SubscriptionDroppedReason, Exception)>();
            var appearedEvents = new List <EventRecord>();
            var beforeEvents   = _fixture.CreateTestEvents(10).ToArray();
            var afterEvents    = _fixture.CreateTestEvents(10).ToArray();

            foreach (var @event in beforeEvents)
            {
                await _fixture.Client.AppendToStreamAsync($"stream-{@event.EventId:n}", AnyStreamRevision.NoStream,
                                                          new[] { @event });
            }

            using var subscription = _fixture.Client.SubscribeToAll(EventAppeared, false, SubscriptionDropped);

            foreach (var @event in afterEvents)
            {
                await _fixture.Client.AppendToStreamAsync($"stream-{@event.EventId:n}", AnyStreamRevision.NoStream,
                                                          new[] { @event });
            }

            await appeared.Task.WithTimeout();

            Assert.True(EventDataComparer.Equal(beforeEvents.Concat(afterEvents).ToArray(), appearedEvents.ToArray()));

            Assert.False(dropped.Task.IsCompleted);

            subscription.Dispose();

            var(reason, ex) = await dropped.Task.WithTimeout();

            Assert.Equal(SubscriptionDroppedReason.Disposed, reason);
            Assert.Null(ex);

            Task EventAppeared(StreamSubscription s, ResolvedEvent e, CancellationToken ct)
            {
                if (!SystemStreams.IsSystemStream(e.OriginalStreamId))
                {
                    appearedEvents.Add(e.Event);

                    if (appearedEvents.Count >= beforeEvents.Length + afterEvents.Length)
                    {
                        appeared.TrySetResult(true);
                    }
                }

                return(Task.CompletedTask);
            }

            void SubscriptionDropped(StreamSubscription s, SubscriptionDroppedReason reason, Exception ex) =>
            dropped.SetResult((reason, ex));
        }
예제 #5
0
        public async Task be_able_to_read_from_arbitrary_position()
        {
            var stream = _fixture.GetStreamName();

            var events = _fixture.CreateTestEvents(10).ToArray();

            await _fixture.Client.AppendToStreamAsync(stream, AnyStreamRevision.NoStream, events);

            var actual = await _fixture.Client.ReadStreamForwardsAsync(stream, new StreamRevision(3), 2)
                         .Select(x => x.Event)
                         .ToArrayAsync();

            Assert.True(EventDataComparer.Equal(events.Skip(3).Take(2).ToArray(), actual));
        }
예제 #6
0
        public async Task returns_events_in_order()
        {
            var stream = _fixture.GetStreamName();

            var expected = _fixture.CreateTestEvents(10).ToArray();

            await _fixture.Client.AppendToStreamAsync(stream, AnyStreamRevision.NoStream, expected);

            var actual = await _fixture.Client
                         .ReadStreamForwardsAsync(stream, StreamRevision.Start, expected.Length, false)
                         .Select(x => x.Event).ToArrayAsync();

            Assert.True(EventDataComparer.Equal(expected, actual));
        }
예제 #7
0
        public async Task be_able_to_read_first_event()
        {
            var stream = _fixture.GetStreamName();

            var testEvents = _fixture.CreateTestEvents(10).ToArray();

            await _fixture.Client.AppendToStreamAsync(stream, AnyStreamRevision.NoStream, testEvents);

            var events = await _fixture.Client.ReadStreamForwardsAsync(stream, StreamRevision.Start, 1)
                         .Select(x => x.Event)
                         .ToArrayAsync();

            Assert.Single(events);
            Assert.True(EventDataComparer.Equal(testEvents[0], events[0]));
        }
예제 #8
0
        public async Task be_able_to_read_single_event_from_arbitrary_position()
        {
            var stream = _fixture.GetStreamName();

            var events = _fixture.CreateTestEvents(10).ToArray();

            var expected = events[7];

            await _fixture.Client.AppendToStreamAsync(stream, AnyStreamRevision.NoStream, events);

            var actual = await _fixture.Client.ReadStreamForwardsAsync(stream, new StreamRevision(7), 1, false)
                         .Select(x => x.Event)
                         .SingleAsync();

            Assert.True(EventDataComparer.Equal(expected, actual));
        }
        public async Task read_stream_backwards_respects_max_count()
        {
            var stream = _fixture.GetStreamName();

            await _fixture.Client.SetStreamMetadataAsync(stream, AnyStreamRevision.NoStream, new StreamMetadata(3));

            var expected = _fixture.CreateTestEvents(5).ToArray();

            await _fixture.Client.AppendToStreamAsync(stream, AnyStreamRevision.NoStream, expected);

            var actual = await _fixture.Client.ReadStreamBackwardsAsync(stream, StreamRevision.End, 100)
                         .Select(x => x.Event)
                         .ToArrayAsync();

            Assert.Equal(3, actual.Length);
            Assert.True(EventDataComparer.Equal(expected.Skip(2).Reverse().ToArray(), actual));
        }
        public async Task reads_all_existing_events_and_keep_listening_to_new_ones()
        {
            var stream         = _fixture.GetStreamName();
            var appeared       = new TaskCompletionSource <bool>();
            var dropped        = new TaskCompletionSource <(SubscriptionDroppedReason, Exception)>();
            var appearedEvents = new List <EventRecord>();
            var beforeEvents   = _fixture.CreateTestEvents(10).ToArray();
            var afterEvents    = _fixture.CreateTestEvents(10).ToArray();

            await _fixture.Client.AppendToStreamAsync(stream, AnyStreamRevision.Any, beforeEvents);

            using var subscription =
                      _fixture.Client.SubscribeToStream(stream, EventAppeared, false, SubscriptionDropped);

            var writeResult = await _fixture.Client.AppendToStreamAsync(stream, AnyStreamRevision.Any, afterEvents);

            await appeared.Task.WithTimeout(TimeSpan.FromMinutes(10));

            Assert.True(EventDataComparer.Equal(beforeEvents.Concat(afterEvents).ToArray(), appearedEvents.ToArray()));

            Assert.False(dropped.Task.IsCompleted);

            subscription.Dispose();

            var(reason, ex) = await dropped.Task.WithTimeout();

            Assert.Equal(SubscriptionDroppedReason.Disposed, reason);
            Assert.Null(ex);

            Task EventAppeared(StreamSubscription s, ResolvedEvent e, CancellationToken ct)
            {
                appearedEvents.Add(e.Event);

                if (appearedEvents.Count >= beforeEvents.Length + afterEvents.Length)
                {
                    appeared.TrySetResult(true);
                }

                return(Task.CompletedTask);
            }

            void SubscriptionDropped(StreamSubscription s, SubscriptionDroppedReason reason, Exception ex)
            {
                dropped.SetResult((reason, ex));
            }
        }
예제 #11
0
        public async Task returns_all_events_including_tombstone()
        {
            var events = await _fixture.Client.ReadAllForwardsAsync(Position.Start, _fixture.Events.Length * 2, false)
                         .ToArrayAsync();

            var tombstone = events.Last();

            Assert.Equal(SystemStreams.MetastreamOf(Stream), tombstone.Event.EventStreamId);
            Assert.Equal(SystemEventTypes.StreamMetadata, tombstone.Event.EventType);
            var metadata = JsonSerializer.Deserialize <StreamMetadata>(tombstone.Event.Data, new JsonSerializerOptions {
                Converters = { StreamMetadataJsonConverter.Instance }
            });

            Assert.True(metadata.TruncateBefore.HasValue);
            Assert.Equal(StreamRevision.End, metadata.TruncateBefore.Value);

            Assert.True(EventDataComparer.Equal(_fixture.Events, events.AsResolvedTestEvents().ToArray()));
        }
        public async Task after_setting_more_strict_max_count_read_stream_forward_reads_less_events()
        {
            var stream = _fixture.GetStreamName();

            await _fixture.Client.SetStreamMetadataAsync(stream, AnyStreamRevision.NoStream, new StreamMetadata(3));

            var expected = _fixture.CreateTestEvents(5).ToArray();

            await _fixture.Client.AppendToStreamAsync(stream, AnyStreamRevision.NoStream, expected);

            await _fixture.Client.SetStreamMetadataAsync(stream, StreamRevision.Start, new StreamMetadata(2));

            var actual = await _fixture.Client.ReadStreamForwardsAsync(stream, StreamRevision.Start, 100)
                         .Select(x => x.Event)
                         .ToArrayAsync();

            Assert.Equal(2, actual.Length);
            Assert.True(EventDataComparer.Equal(expected.Skip(3).ToArray(), actual));
        }