public async Task EventStoreContext_ConnectToSubscription_ExisitngSubscription_ConnectionMade()
        {
            EventStorePersistentSubscription result = null;

            EventStoreConnectionSettings eventStoreConnectionSettings = EventStoreConnectionSettings.Create(EventStoreContextTestData.ConnectionString);
            Mock <IEventStoreConnection> connection = new Mock <IEventStoreConnection>();

            connection.Setup(c => c.ConnectToPersistentSubscriptionAsync(It.IsAny <String>(), It.IsAny <String>(),
                                                                         It.IsAny <Func <EventStorePersistentSubscriptionBase, ResolvedEvent, Int32?, Task> >(),
                                                                         It.IsAny <Action <EventStorePersistentSubscriptionBase, SubscriptionDropReason, Exception> >(),
                                                                         It.IsAny <UserCredentials>(), It.IsAny <Int32>(), It.IsAny <Boolean>()))
            .ReturnsAsync(result);
            Mock <ISerialiser> serialiser = new Mock <ISerialiser>();

            Func <EventStoreConnectionSettings, IEventStoreConnection> connectionResolver = settings =>
            {
                return(connection.Object);
            };

            PaymentCentric.Logging.Logger.Initialise(NullLogger.Instance);

            EventStoreContext context = new EventStoreContext(eventStoreConnectionSettings, connectionResolver, serialiser.Object);

            await context.ConnectToSubscription(EventStoreContextTestData.StreamName, EventStoreContextTestData.GroupName,
                                                EventStoreContextTestData.PersistentSubscriptionId,
                                                EventStoreContextTestData.BufferSize);

            connection.Verify(x => x.ConnectToPersistentSubscriptionAsync(It.IsAny <String>(), It.IsAny <String>(), It.IsAny <Func <EventStorePersistentSubscriptionBase, ResolvedEvent, Int32?, Task> >(),
                                                                          It.IsAny <Action <EventStorePersistentSubscriptionBase, SubscriptionDropReason, Exception> >(), It.IsAny <UserCredentials>(), It.IsAny <Int32>(), It.IsAny <Boolean>()), Times.Exactly(1));
        }
        public void EventStoreContext_ConnectToSubscription_ConnectException_ErrorThrown()
        {
            EventStorePersistentSubscription result = null;

            EventStoreConnectionSettings eventStoreConnectionSettings = EventStoreConnectionSettings.Create(EventStoreContextTestData.ConnectionString);
            Mock <IEventStoreConnection> connection = new Mock <IEventStoreConnection>();

            connection.Setup(c => c.ConnectToPersistentSubscriptionAsync(It.IsAny <String>(), It.IsAny <String>(),
                                                                         It.IsAny <Func <EventStorePersistentSubscriptionBase, ResolvedEvent, Int32?, Task> >(),
                                                                         It.IsAny <Action <EventStorePersistentSubscriptionBase, SubscriptionDropReason, Exception> >(),
                                                                         It.IsAny <UserCredentials>(), It.IsAny <Int32>(), It.IsAny <Boolean>()))
            .Throws(new Exception("Error", new Exception("Some nasty error")));

            Mock <ISerialiser> serialiser = new Mock <ISerialiser>();
            Func <EventStoreConnectionSettings, IEventStoreConnection> connectionResolver = settings =>
            {
                return(connection.Object);
            };

            PaymentCentric.Logging.Logger.Initialise(NullLogger.Instance);

            EventStoreContext context = new EventStoreContext(eventStoreConnectionSettings, connectionResolver, serialiser.Object);

            Should.Throw <Exception>(async() =>
            {
                await context.ConnectToSubscription(EventStoreContextTestData.StreamName,
                                                    EventStoreContextTestData.GroupName,
                                                    EventStoreContextTestData.PersistentSubscriptionId,
                                                    EventStoreContextTestData.BufferSize);
            });
        }
        public Task <EventStorePersistentSubscriptionBase> ConnectToPersistentSubscriptionAsync(
            string stream,
            string groupName,
            Action <EventStorePersistentSubscriptionBase, ResolvedEvent> eventAppeared,
            Action <EventStorePersistentSubscriptionBase, SubscriptionDropReason, Exception> subscriptionDropped = null,
            UserCredentials userCredentials = null,
            int bufferSize = 10,
            bool autoAck   = true)
        {
            Ensure.NotNullOrEmpty(groupName, "groupName");
            Ensure.NotNullOrEmpty(stream, "stream");
            Ensure.NotNull(eventAppeared, "eventAppeared");

            var subscription = new EventStorePersistentSubscription(
                groupName, stream, eventAppeared, subscriptionDropped, userCredentials, _settings.Log,
                _settings.VerboseLogging, _settings, _handler, bufferSize, autoAck);

            return(subscription.Start());
        }
        public void EventStoreContext_ConnectToSubscription_ConnectException_InvalidData_ErrorThrown(String streamName, String groupName, Boolean validSubscriptionGroupId, Type exceptionType)
        {
            EventStorePersistentSubscription result = null;

            EventStoreConnectionSettings eventStoreConnectionSettings = EventStoreConnectionSettings.Create(EventStoreContextTestData.ConnectionString);
            Mock <IEventStoreConnection> connection = new Mock <IEventStoreConnection>();
            Mock <ISerialiser>           serialiser = new Mock <ISerialiser>();
            Func <EventStoreConnectionSettings, IEventStoreConnection> connectionResolver = settings =>
            {
                return(connection.Object);
            };

            PaymentCentric.Logging.Logger.Initialise(NullLogger.Instance);

            EventStoreContext context = new EventStoreContext(eventStoreConnectionSettings, connectionResolver, serialiser.Object);

            Should.Throw <Exception>(async() =>
            {
                await context.ConnectToSubscription(streamName, groupName, validSubscriptionGroupId ? EventStoreContextTestData.PersistentSubscriptionId : Guid.Empty,
                                                    EventStoreContextTestData.BufferSize);
            });
        }
        private void OnEvent(EventStorePersistentSubscription subscription, ResolvedEvent evnt)
        {
            var transportMessage = evnt.ToTransportMessage();

            while (true) //First-level retry loop
            {
                Exception processingError = null;
                try
                {
                    if (tryProcessMessage(transportMessage))
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    processingError = ex;
                }
                finally
                {
                    endProcessMessage(transportMessage, processingError);
                }
            }
        }
        public EventStorePersistentSubscriptionBase ConnectToPersistentSubscription(
            string stream, 
            string groupName, 
            Action<EventStorePersistentSubscriptionBase, ResolvedEvent> eventAppeared, 
            Action<EventStorePersistentSubscriptionBase, SubscriptionDropReason, Exception> subscriptionDropped = null,
            UserCredentials userCredentials = null, 
            int bufferSize = 10,
            bool autoAck = true)
        {
            Ensure.NotNullOrEmpty(groupName, "groupName");
            Ensure.NotNullOrEmpty(stream, "stream");
            Ensure.NotNull(eventAppeared, "eventAppeared");

            var subscription = new EventStorePersistentSubscription(
                groupName, stream, eventAppeared, subscriptionDropped, userCredentials, _settings.Log,
                _settings.VerboseLogging, _settings, _handler, bufferSize, autoAck);

            subscription.Start();

            return subscription;
        }