public async Task ReceiveAsync_WhenReaderIsEmpty_FreesSession(
            [Frozen] Mock<IEventStreamConsumingSession> sessionMock,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();

            sessionMock.Verify(self => self.FreeAsync(), Times.Once());
        }
        public async Task ReceiveAsync_WhenReaderIsNotEmpty_ReadEventsFromReader(
            [Frozen] Mock<IEventStreamReader> readerMock,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();

            readerMock.Verify(self => self.ReadEventsAsync(), Times.Once());
        }
        public async Task EnumerateEvents_WhenReceiveMethodWasCalled_ReturnsEventsFromReader(
            [Frozen] IReadOnlyList<JournaledEvent> events,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();

            var receivedEvents = consumer.EnumerateEvents();

            Assert.Equal(events, receivedEvents);
        }
 public async Task ReceiveAsync_WhenReaderWasNotBeenPromotedToLeader_ReturnsFalse(
     EventStreamConsumer consumer)
 {
     Assert.False(await consumer.ReceiveEventsAsync());
 }
 public async Task ReceiveAsync_WhenReaderIsNotEmpty_ReturnsTrue(
     EventStreamConsumer consumer)
 {
     Assert.True(await consumer.ReceiveEventsAsync());
 }
        public async Task RememberConsumedStreamVersionAsync_WhenLatestManuallyCommitedVersionEqualsToStreamCurrent_SkipCommitOnRecevieAsync(
            [Frozen] StreamVersion version,
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            [Frozen] Mock<IEventStreamReader> readerMock,
            [Frozen] JournaledEvent[] events,
            EventStreamConsumer consumer)
        {
            var streamVersion = version.Increment(events.Length);

            readerMock
                .Setup(self => self.CurrentStreamVersion)
                .Returns(streamVersion);

            await consumer.ReceiveEventsAsync();

            var handledEvents = 0;
            foreach (var e in consumer.EnumerateEvents())
            {
                handledEvents++;
                await consumer.CommitProcessedStreamVersionAsync();
            }

            await consumer.ReceiveEventsAsync();

            Assert.Equal(handledEvents, commitStreamVersionMock.CallsCount);
            Assert.Equal(streamVersion, commitStreamVersionMock.CommitedVersion);
        }
        public async Task CloseAsync_WhenReaderHasUnprocessedEventsAndNoMessagesWereConsumed_DoesNotThrow(
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();
            await consumer.CloseAsync();

            Assert.Equal(0, commitStreamVersionMock.CallsCount);
        }
        public async Task ReceiveEventsAsync_WhenReaderCompleted_ContinueReading(
            [Frozen] Mock<IEventStreamReader> readerMock,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();

            readerMock.Verify(self => self.ContinueAsync(), Times.Once());
        }
        public async Task RememberConsumedStreamVersionAsync_WhenEventWasConsumed_CommitsConsumedVersion(
            [Frozen] StreamVersion version,
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();

            var handledEvents = 0;
            foreach (var e in consumer.EnumerateEvents())
            {
                handledEvents++;
                await consumer.CommitProcessedStreamVersionAsync();
                break;
            }


            Assert.Equal(1, commitStreamVersionMock.CallsCount);
            Assert.Equal(version.Increment(handledEvents), commitStreamVersionMock.CommitedVersion);
        }
        public async Task CloseAsync_WhenReaderInCompletedStateAndHasNoUnprocessedEvents_CommitsConsumedVersion(
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            [Frozen] IEventStreamReader reader,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();
            consumer.EnumerateEvents().ToList(); // drains events from reader;

            await consumer.CloseAsync();

            Assert.Equal(1, commitStreamVersionMock.CallsCount);
            Assert.Equal(reader.CurrentStreamVersion, commitStreamVersionMock.CommitedVersion);
        }
 public async Task CloseAsync_WhenReaderHasNoUnprocessedEvents_NotThrows(
     EventStreamConsumer consumer)
 {
     await consumer.CloseAsync();
 }
        public async Task CloseAsync_WhenReaderHasUnprocessedEventsAndCurrentEventHasBeenCommited_SkipCommit(
            [Frozen] StreamVersion version,
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();

            foreach (var e in consumer.EnumerateEvents())
            {
                await consumer.CommitProcessedStreamVersionAsync();
                break;
            }

            await consumer.CloseAsync();

            Assert.Equal(1, commitStreamVersionMock.CallsCount);
            Assert.Equal(version.Increment(), commitStreamVersionMock.CommitedVersion);
        }
        public async Task CloseAsync_WhenReaderHasUnprocessedEventsAndOnMessagesWasConsumed_CommitsConsumedVersion(
            [Frozen] StreamVersion version,
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();

            var handledEventCount = 0;
            foreach (var e in consumer.EnumerateEvents())
            {
                if (handledEventCount >= 1)
                {
                    break;
                }

                handledEventCount++;
            }

            await consumer.CloseAsync();

            Assert.Equal(1, commitStreamVersionMock.CallsCount);
            Assert.Equal(version.Increment(), commitStreamVersionMock.CommitedVersion);
        }
        public async Task CloseAsync_FreesConsumingSessions(
            [Frozen] Mock<IEventStreamConsumingSession> sessionMock,
            EventStreamConsumer consumer)
        {
            await consumer.CloseAsync();

            sessionMock.Verify(self => self.FreeAsync(), Times.Once());
        }
        public async Task RememberConsumedStreamVersionAsync_WhenSkipCurrentFlagIsTrue_CommitsOnlyProcessedEvents(
            [Frozen] StreamVersion version,
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();

            var handledEvents = 0;
            foreach (var e in consumer.EnumerateEvents())
            {
                handledEvents++;
                await consumer.CommitProcessedStreamVersionAsync(skipCurrent: true);
                break;
            }

            Assert.Equal(0, commitStreamVersionMock.CallsCount);
        }
 public void EnumerateEvents_WhenReceiveMethodWasNotCalled_Throws(
     EventStreamConsumer consumer)
 {
     Assert.Throws<InvalidOperationException>(() => consumer.EnumerateEvents().ToList());
 }
        public async Task RememberConsumedStreamVersionAsync_WhenSkipCurrentFlagIsTrue_CommitsOnlyProcessedEvents(
            [Frozen] StreamVersion version,
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            [Frozen] Mock<IEventStreamReader> readerMock,
            [Frozen] JournaledEvent[] events,
            EventStreamConsumer consumer)
        {
            var streamVersion = version.Increment(events.Length);

            readerMock
                .Setup(self => self.CurrentStreamVersion)
                .Returns(streamVersion);

            await consumer.ReceiveEventsAsync();

            var handledEvents = 0;
            foreach (var e in consumer.EnumerateEvents())
            {
                handledEvents++;
                await consumer.CommitProcessedStreamVersionAsync(skipCurrent: true);
            }

            Assert.Equal(handledEvents - 1, commitStreamVersionMock.CallsCount);
            Assert.Equal(streamVersion.Decrement(), commitStreamVersionMock.CommitedVersion);
        }
        public async Task ReceiveEventsAsync_WhenReceivingWasStarted_CommitsReaderVersion(
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            [Frozen] IEventStreamReader reader,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync(); // starts receiving
            await consumer.ReceiveEventsAsync(); // continues receiving

            Assert.Equal(1, commitStreamVersionMock.CallsCount);
            Assert.Equal(reader.CurrentStreamVersion, commitStreamVersionMock.CommitedVersion);
        }
        public async Task ReceiveEventsAsync_WhenReceivingWasNotStartedBefore_CommitsReaderVersion(
            [Frozen] CommitStreamVersionFMock commitStreamVersionMock,
            [Frozen] IEventStreamReader reader,
            EventStreamConsumer consumer)
        {
            await consumer.ReceiveEventsAsync();

            Assert.Equal(0, commitStreamVersionMock.CallsCount);
            Assert.Equal(StreamVersion.Unknown, commitStreamVersionMock.CommitedVersion);
        }