Exemplo n.º 1
0
            protected override async Task Given()
            {
                await StreamsClient.AppendToStreamAsync(Stream, StreamState.NoStream, Events);

                await Client.CreateAsync(Stream, Group,
                                         new PersistentSubscriptionSettings(startFrom : StreamPosition.Start), TestCredentials.Root);

                _subscription = await Client.SubscribeAsync(Stream, Group,
                                                            async (subscription, e, r, ct) => {
                    if (r > 4)
                    {
                        _retryCountSource.TrySetResult(r.Value);
                        await subscription.Ack(e.Event.EventId);
                    }
                    else
                    {
                        await subscription.Nack(PersistentSubscriptionNakEventAction.Retry,
                                                "Not yet tried enough times", e);
                    }
                }, autoAck : false, subscriptionDropped : (subscription, reason, ex) => {
                    if (reason != SubscriptionDroppedReason.Disposed)
                    {
                        _retryCountSource.TrySetException(ex !);
                    }
                }, userCredentials : TestCredentials.TestUser1);
            }
            protected override async Task Given()
            {
                foreach (var e in _events)
                {
                    await StreamsClient.AppendToStreamAsync(Stream, StreamState.Any, new[] { e });
                }

                await Client.CreateAsync(Stream, Group,
                                         new PersistentSubscriptionSettings(startFrom : StreamPosition.Start, resolveLinkTos : true),
                                         TestCredentials.Root);

                _subscription = await Client.SubscribeAsync(Stream, Group,
                                                            (subscription, e, retryCount, ct) => {
                    subscription.Ack(e);

                    if (Interlocked.Increment(ref _eventReceivedCount) == _events.Length)
                    {
                        _eventsReceived.TrySetResult(true);
                    }

                    return(Task.CompletedTask);
                }, (s, r, e) => {
                    if (e != null)
                    {
                        _eventsReceived.TrySetException(e);
                    }
                },
                                                            autoAck : false,
                                                            bufferSize : BufferCount,
                                                            userCredentials : TestCredentials.Root);
            }
        public async Task <PersistentSubscription> SubscribeAsync(string streamName, string groupName,
                                                                  Func <PersistentSubscription, ResolvedEvent, int?, CancellationToken, Task> eventAppeared,
                                                                  Action <PersistentSubscription, SubscriptionDroppedReason, Exception?>?subscriptionDropped = null,
                                                                  UserCredentials?userCredentials     = null, int bufferSize = 10, bool autoAck = true,
                                                                  CancellationToken cancellationToken = default)
        {
            if (streamName == null)
            {
                throw new ArgumentNullException(nameof(streamName));
            }

            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }

            if (eventAppeared == null)
            {
                throw new ArgumentNullException(nameof(eventAppeared));
            }

            if (streamName == string.Empty)
            {
                throw new ArgumentException($"{nameof(streamName)} may not be empty.", nameof(streamName));
            }

            if (groupName == string.Empty)
            {
                throw new ArgumentException($"{nameof(groupName)} may not be empty.", nameof(groupName));
            }

            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize));
            }

            var operationOptions = Settings.OperationOptions.Clone();

            operationOptions.TimeoutAfter = new TimeSpan?();

            var call = new PersistentSubscriptions.PersistentSubscriptions.PersistentSubscriptionsClient(
                await SelectCallInvoker(cancellationToken).ConfigureAwait(false)).Read(EventStoreCallOptions.Create(
                                                                                           Settings, operationOptions, userCredentials, cancellationToken));

            return(await PersistentSubscription.Confirm(call, new ReadReq.Types.Options {
                BufferSize = bufferSize,
                GroupName = groupName,
                StreamIdentifier = streamName,
                UuidOption = new ReadReq.Types.Options.Types.UUIDOption {
                    Structured = new Empty()
                }
            }, autoAck, eventAppeared, subscriptionDropped ?? delegate { }, cancellationToken).ConfigureAwait(false));
        }
 protected override async Task When()
 {
     _subscription = await Client.SubscribeAsync(Stream, Group,
                                                 (subscription, e, r, ct) => {
         _firstEventSource.TrySetResult(e);
         return(Task.CompletedTask);
     }, (subscription, reason, ex) => {
         if (reason != SubscriptionDroppedReason.Disposed)
         {
             _firstEventSource.TrySetException(ex !);
         }
     }, TestCredentials.TestUser1);
 }
        public Task <PersistentSubscription> SubscribeAsync(string streamName, string groupName,
                                                            Func <PersistentSubscription, ResolvedEvent, int?, CancellationToken, Task> eventAppeared,
                                                            Action <PersistentSubscription, SubscriptionDroppedReason, Exception?>?subscriptionDropped = null,
                                                            UserCredentials?userCredentials     = null, int bufferSize = 10, bool autoAck = true,
                                                            CancellationToken cancellationToken = default)
        {
            if (streamName == null)
            {
                throw new ArgumentNullException(nameof(streamName));
            }

            if (groupName == null)
            {
                throw new ArgumentNullException(nameof(groupName));
            }

            if (eventAppeared == null)
            {
                throw new ArgumentNullException(nameof(eventAppeared));
            }

            if (streamName == string.Empty)
            {
                throw new ArgumentException($"{nameof(streamName)} may not be empty.", nameof(streamName));
            }

            if (groupName == string.Empty)
            {
                throw new ArgumentException($"{nameof(groupName)} may not be empty.", nameof(groupName));
            }

            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize));
            }

            var call = _client.Read(RequestMetadata.Create(userCredentials ?? Settings.DefaultCredentials), cancellationToken: cancellationToken);

            return(PersistentSubscription.Confirm(call, new ReadReq.Types.Options {
                BufferSize = bufferSize,
                GroupName = groupName,
                StreamName = streamName,
                UuidOption = new ReadReq.Types.Options.Types.UUIDOption {
                    Structured = new Empty()
                }
            }, autoAck, eventAppeared,
                                                  subscriptionDropped ?? delegate { }, cancellationToken));
        }
            protected override async Task Given()
            {
                await Client.CreateAsync(Stream, Group,
                                         new PersistentSubscriptionSettings(startFrom : new StreamPosition(2)), TestCredentials.Root);

                _subscription = await Client.SubscribeAsync(Stream, Group,
                                                            (subscription, e, r, ct) => {
                    _firstEventSource.TrySetResult(e);
                    return(Task.CompletedTask);
                }, (subscription, reason, ex) => {
                    if (reason != SubscriptionDroppedReason.Disposed)
                    {
                        _firstEventSource.TrySetException(ex !);
                    }
                }, TestCredentials.TestUser1);
            }
            protected override async Task Given()
            {
                await Client.CreateAsync(Stream, Group,
                                         new PersistentSubscriptionSettings(startFrom : StreamPosition.End, resolveLinkTos : true),
                                         TestCredentials.Root);

                _subscription = await Client.SubscribeAsync(Stream, Group,
                                                            async (subscription, e, retryCount, ct) => {
                    await subscription.Ack(e);
                    if (Interlocked.Increment(ref _eventReceivedCount) == _events.Length)
                    {
                        _eventsReceived.TrySetResult(true);
                    }
                }, (s, r, e) => {
                    if (e != null)
                    {
                        _eventsReceived.TrySetException(e);
                    }
                }, autoAck : false,
                                                            bufferSize : BufferCount,
                                                            userCredentials : TestCredentials.Root);
            }