public async Task ItShouldRollbackTheExpectedMessagesWhenAnExceptionIsThrown(
            [Frozen] IStorageProvider <long> storageProvider,
            NotificationTransaction expectedTransaction,
            INotification <long>[] expectedNotifications,
            INotificationChannel <long> channel,
            AbstractMessageProcessor <long> sut)
        {
            // Arrange
            A.CallTo(() => storageProvider.Store(
                         A <IStorageMessageSerializer> ._,
                         A <INotification <long>[]> ._))
            .Throws <Exception>();

            // Act
            await sut.GetMessages(CancellationToken.None);

            // Assert
            A.CallTo(() => channel.Commit(A <INotification <long>[]> ._, A <NotificationTransaction> ._)).MustNotHaveHappened();
            A.CallTo(() => channel.Rollback(expectedNotifications, expectedTransaction)).MustHaveHappened(Repeated.Exactly.Once);
        }
Exemple #2
0
        public async Task <MessageEnvelope> GetMessages(CancellationToken cancellation)
        {
            // very lame representation of what this method could look like
            var notifications = await _notificationChannel.Receive(cancellation);

            if (!notifications.Any())
            {
                return(new MessageEnvelope());
            }

            var transaction = _transactionTracker.CreateTransaction();

            try
            {
                _storageProvider.Store(_messageSerializer, notifications);
                _notificationChannel.Commit(notifications, transaction);
                return(new MessageEnvelope("Notifications Commited", transaction));
            }
            catch (Exception ex)
            {
                _notificationChannel.Rollback(notifications, transaction);
                return(new MessageEnvelope(ex.Message, transaction));
            }
        }