public void Stop_WhenSubscriptionHasNotBeenStarted_Throws(
     [Frozen] Mock <INotificationListener> listenerMock,
     NotificationListenerSubscription subscription,
     INotification notification)
 {
     Assert.Throws <InvalidOperationException>(() => subscription.Stop());
 }
 public async Task CreateSubscriptionConsumerAsync_WhenSubscriptionWasNotStarted_Throws(
     [Frozen] EventStreamReaderId consumerId,
     NotificationListenerSubscription subscription,
     string streamName)
 {
     await Assert.ThrowsAsync <InvalidOperationException>(
         () => subscription.CreateSubscriptionConsumerAsync(streamName));
 }
        public void Start_NotifyListener(
            [Frozen] Mock <INotificationListener> listenerMock,
            IEventStoreConnection connection,
            NotificationListenerSubscription subscription)
        {
            subscription.Start(connection);

            listenerMock.Verify(self => self.OnSubscriptionStarted(subscription), Times.Once());
        }
        public async Task HandleNotificationAsync_WhenSubscriptionDidNotStart_NotPropagatesNotificationToTheListener(
            [Frozen] Mock <INotificationListener> listenerMock,
            NotificationListenerSubscription subscription,
            EventStreamUpdated notification)
        {
            await subscription.HandleNotificationAsync(notification);

            listenerMock.Verify(
                self => self.On(notification),
                Times.Never());
        }
        public void CreateSubscriptionConsumerAsync_WhenSubscriptionWasStarted_UseConnection(
            [Frozen] EventStreamReaderId consumerId,
            Mock <IEventStoreConnection> connectionMock,
            NotificationListenerSubscription subscription,
            string streamName)
        {
            subscription.Start(connectionMock.Object);

            subscription.CreateSubscriptionConsumerAsync(streamName);

            connectionMock.Verify(self => self.CreateStreamConsumerAsync(
                                      It.IsAny <Action <IEventStreamConsumerConfiguration> >()));
        }
        public async Task HandleNotificationAsync_WhenSubscriptionStarted_PropagatesNotificationToTheListener(
            [Frozen] Mock <INotificationListener> listenerMock,
            IEventStoreConnection connection,
            NotificationListenerSubscription subscription,
            EventStreamUpdated notification)
        {
            subscription.Start(connection);

            await subscription.HandleNotificationAsync(notification);

            listenerMock.Verify(
                self => self.On(notification),
                Times.Once());
        }
        public async Task HandleNotificationAsync_WhenNotificationNotAddressedToConsumer_PropagatesNotificationToTheListener(
            [Frozen] Mock <INotificationListener> listenerMock,
            IEventStoreConnection connection,
            NotificationListenerSubscription subscription,
            EventStreamUpdated notification,
            EventStreamReaderId consumerId)
        {
            subscription.Start(connection);

            await subscription.HandleNotificationAsync(notification.SendTo(listenerMock.Object));

            listenerMock.Verify(
                self => self.On(notification),
                Times.Never());
        }
        public async Task DefferNotificationAsync_SendAddressedNotification(
            [Frozen] Mock <INotificationsChannel> channelMock,
            [Frozen] Mock <INotification> receivedNotificationMock,
            INotification deferredNotification,
            IEventStoreConnection connection,
            INotificationListener listener,
            NotificationListenerSubscription subscription)
        {
            receivedNotificationMock
            .Setup(self => self.SendTo(listener))
            .Returns(deferredNotification);

            subscription.Start(connection);

            await subscription.RetryNotificationProcessingAsync(receivedNotificationMock.Object);

            channelMock
            .Verify(self => self.SendAsync(
                        deferredNotification,
                        It.Is <TimeSpan>(v => TimeSpan.FromSeconds(deferredNotification.DeliveryCount * 2) == v)));
        }