/// <summary>
        /// Constructs state for EventStoreCatchUpSubscription.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="log">The <see cref="ILogger"/> to use.</param>
        /// <param name="streamId">The stream name.</param>
        /// <param name="userCredentials">User credentials for the operations.</param>
        /// <param name="eventAppeared">Action invoked when events are received.</param>
        /// <param name="liveProcessingStarted">Action invoked when the read phase finishes.</param>
        /// <param name="subscriptionDropped">Action invoked if the subscription drops.</param>
        /// <param name="settings">Settings for this subscription.</param>
        protected EventStoreCatchUpSubscription(IEventStoreConnection connection,
                                                ILogger log,
                                                string streamId,
                                                UserCredentials userCredentials,
                                                Action <EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
                                                Action <EventStoreCatchUpSubscription> liveProcessingStarted,
                                                Action <EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped,
                                                CatchUpSubscriptionSettings settings)
        {
            Ensure.NotNull(connection, "connection");
            Ensure.NotNull(log, "log");
            Ensure.NotNull(eventAppeared, "eventAppeared");
            _connection      = connection;
            Log              = log;
            _streamId        = string.IsNullOrEmpty(streamId) ? string.Empty : streamId;
            _resolveLinkTos  = settings.ResolveLinkTos;
            _userCredentials = userCredentials;
            ReadBatchSize    = settings.ReadBatchSize;
            MaxPushQueueSize = settings.MaxLiveQueueSize;

            EventAppeared          = eventAppeared;
            _liveProcessingStarted = liveProcessingStarted;
            _subscriptionDropped   = subscriptionDropped;
            Verbose = settings.VerboseLogging;
        }
        public void SetUp()
        {
            _connection = new FakeEventStoreConnection();
            _raisedEvents = new List<ResolvedEvent>();
            _dropEvent = new ManualResetEventSlim();
            _raisedEventEvent = new ManualResetEventSlim();
            _liveProcessingStarted = false;
            _isDropped = false;
            _dropReason = SubscriptionDropReason.Unknown;
            _dropException = null;

            var settings = new CatchUpSubscriptionSettings(1, 1, false, false);
            _subscription = new EventStoreStreamCatchUpSubscription(_connection, new NoopLogger(), StreamId, null, null,
                (subscription, ev) =>
                {
                    _raisedEvents.Add(ev);
                    _raisedEventEvent.Set();
                },
                subscription =>
                {
                    _liveProcessingStarted = true;
                },
                (subscription, reason, ex) =>
                {
                    _isDropped = true;
                    _dropReason = reason;
                    _dropException = ex;
                    _dropEvent.Set();
                },
                settings);
        }
 internal EventStoreAllCatchUpSubscription(IEventStoreConnection connection,
                                           ILogger log,
                                           Position?fromPositionExclusive,  /* if null -- from the very beginning */
                                           UserCredentials userCredentials,
                                           Action <EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
                                           Action <EventStoreCatchUpSubscription> liveProcessingStarted,
                                           Action <EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped,
                                           CatchUpSubscriptionSettings settings)
     : base(connection, log, string.Empty, userCredentials,
            eventAppeared, liveProcessingStarted, subscriptionDropped, settings)
 {
     _lastProcessedPosition = fromPositionExclusive ?? new Position(-1, -1);
     _nextReadPosition      = fromPositionExclusive ?? Position.Start;
 }
        public override void TestFixtureSetUp()
        {
            base.TestFixtureSetUp();
            _node = new MiniNode(PathName, inMemDb: true);
            _node.Start();

            _conn = BuildConnection(_node);
            _conn.ConnectAsync().Wait();
            //Create 80000 events
            for(var i = 0; i < 80; i++)
            {
                _conn.AppendToStreamAsync(_streamName, ExpectedVersion.Any, CreateThousandEvents()).Wait();
            }

            _settings = new CatchUpSubscriptionSettings(100, 1, false, true);
        }
 /// <summary>
 /// Subscribes to a all events. Existing events from lastCheckpoint
 /// onwards are read from the Event Store and presented to the user of
 /// <see cref="EventStoreCatchUpSubscription"/> as if they had been pushed.
 ///
 /// Once the end of the stream is read the subscription is
 /// transparently (to the user) switched to push new events as
 /// they are written.
 ///
 /// The action liveProcessingStarted is called when the
 /// <see cref="EventStoreCatchUpSubscription"/> switches from the reading
 /// phase to the live subscription phase.
 /// </summary>
 /// <param name="lastCheckpoint">The position from which to start.
 ///
 /// To receive all events in the database, use <see cref="AllCheckpoint.AllStart" />.
 /// If events have already been received and resubscription from the same point
 /// is desired, use the position representing the last event processed which
 /// appeared on the subscription.
 ///
 /// NOTE: Using <see cref="Position.Start" /> here will result in missing
 /// the first event in the stream.</param>
 /// <param name="target">The connection to subscribe to</param>
 /// <param name="eventAppeared">An action invoked when a new event is received over the subscription</param>
 /// <param name="liveProcessingStarted">An action invoked when the subscription switches to newly-pushed events</param>
 /// <param name="subscriptionDropped">An action invoked if the subscription is dropped</param>
 /// <param name="userCredentials">User credentials to use for the operation</param>
 /// <param name="settings">The <see cref="CatchUpSubscriptionSettings"/> for the subscription</param>
 /// <returns>An <see cref="EventStoreSubscription"/> representing the subscription</returns>
 public static EventStoreAllCatchUpSubscription SubscribeToAllFrom(
     this IEventStoreConnection target,
     Position?lastCheckpoint,
     CatchUpSubscriptionSettings settings,
     Action <EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
     Action <EventStoreCatchUpSubscription> liveProcessingStarted = null,
     Action <EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped = null,
     UserCredentials userCredentials = null) =>
 target.SubscribeToAllFrom(
     lastCheckpoint,
     settings,
     ToTask(eventAppeared),
     liveProcessingStarted,
     subscriptionDropped,
     userCredentials
     );
        internal EventStoreStreamCatchUpSubscription(IEventStoreConnection connection,
                                                     ILogger log,
                                                     string streamId,
                                                     int?fromEventNumberExclusive,  /* if null -- from the very beginning */
                                                     UserCredentials userCredentials,
                                                     Action <EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
                                                     Action <EventStoreCatchUpSubscription> liveProcessingStarted,
                                                     Action <EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped,
                                                     CatchUpSubscriptionSettings settings)
            : base(connection, log, streamId, userCredentials,
                   eventAppeared, liveProcessingStarted, subscriptionDropped, settings)
        {
            Ensure.NotNullOrEmpty(streamId, "streamId");

            _lastProcessedEventNumber = fromEventNumberExclusive ?? -1;
            _nextReadEventNumber      = fromEventNumberExclusive ?? 0;
        }
        internal EventStoreStreamCatchUpSubscription(IEventStoreConnection connection,
                                                     ILogger log,
                                                     string streamId,
                                                     int? fromEventNumberExclusive, /* if null -- from the very beginning */
                                                     UserCredentials userCredentials,
                                                     Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
                                                     Action<EventStoreCatchUpSubscription> liveProcessingStarted,
                                                     Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped,
                                                     CatchUpSubscriptionSettings settings)
            : base(connection, log, streamId, userCredentials,
                   eventAppeared, liveProcessingStarted, subscriptionDropped, settings)
        {
            Ensure.NotNullOrEmpty(streamId, "streamId");

            _lastProcessedEventNumber = fromEventNumberExclusive ?? -1;
            _nextReadEventNumber = fromEventNumberExclusive ?? 0;
        }
 internal EventStoreAllCatchUpSubscription(IEventStoreConnection connection,
                                           ILogger log,
                                           Position? fromPositionExclusive, /* if null -- from the very beginning */
                                           UserCredentials userCredentials,
                                           Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
                                           Action<EventStoreCatchUpSubscription> liveProcessingStarted,
                                           Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped,
                                          CatchUpSubscriptionSettings settings)
         : base(connection, log, string.Empty, userCredentials,
                eventAppeared, liveProcessingStarted, subscriptionDropped, settings)
 {
     _lastProcessedPosition = fromPositionExclusive ?? new Position(-1, -1);
     _nextReadPosition = fromPositionExclusive ?? Position.Start;
 }
        /// <summary>
        /// Constructs state for EventStoreCatchUpSubscription.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="log">The <see cref="ILogger"/> to use.</param>
        /// <param name="streamId">The stream name.</param>
        /// <param name="userCredentials">User credentials for the operations.</param>
        /// <param name="eventAppeared">Action invoked when events are received.</param>
        /// <param name="liveProcessingStarted">Action invoked when the read phase finishes.</param>
        /// <param name="subscriptionDropped">Action invoked if the subscription drops.</param>
        /// <param name="settings">Settings for this subscription.</param>
        protected EventStoreCatchUpSubscription(IEventStoreConnection connection,
                                                ILogger log,
                                                string streamId,
                                                UserCredentials userCredentials,
                                                Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
                                                Action<EventStoreCatchUpSubscription> liveProcessingStarted,
                                                Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped,
                                                CatchUpSubscriptionSettings settings)
        {
            Ensure.NotNull(connection, "connection");
            Ensure.NotNull(log, "log");
            Ensure.NotNull(eventAppeared, "eventAppeared");
            _connection = connection;
            Log = log;
            _streamId = string.IsNullOrEmpty(streamId) ? string.Empty : streamId;
            _resolveLinkTos = settings.ResolveLinkTos;
            _userCredentials = userCredentials;
            ReadBatchSize = settings.ReadBatchSize;
            MaxPushQueueSize = settings.MaxLiveQueueSize;

            EventAppeared = eventAppeared;
            _liveProcessingStarted = liveProcessingStarted;
            _subscriptionDropped = subscriptionDropped;
            Verbose = settings.VerboseLogging;
        }
 public EventStoreAllCatchUpSubscription SubscribeToAllFrom(
     Position? lastCheckpoint,
     CatchUpSubscriptionSettings settings,
     Action<EventStoreCatchUpSubscription, ResolvedEvent> eventAppeared,
     Action<EventStoreCatchUpSubscription> liveProcessingStarted = null,
     Action<EventStoreCatchUpSubscription, SubscriptionDropReason, Exception> subscriptionDropped = null,
     UserCredentials userCredentials = null)
 {
     throw new NotImplementedException();
 }