示例#1
0
        private static async Task SubscribeToFiltered(EventStoreClient client)
        {
            #region stream-prefix-filtered-subscription
            var prefixStreamFilter = new SubscriptionFilterOptions(StreamFilter.Prefix("test-", "other-"));
            await client.SubscribeToAllAsync(
                EventAppeared,
                filterOptions : prefixStreamFilter);

            #endregion stream-prefix-filtered-subscription

            #region stream-regex-filtered-subscription
            var regexStreamFilter = StreamFilter.RegularExpression(@"/invoice-\d\d\d/g");
            #endregion stream-regex-filtered-subscription
        }
 public async Task Subscribe(CancellationToken stoppingToken)
 {
     await _eventStoreClient.SubscribeToAllAsync(
         await _streamManager.GetPosition(),
         async (subscription, @event, cancellationToken) =>
     {
         _logger.LogInformation("EventAppeared at position: {arg2}", @event.OriginalPosition?.CommitPosition);
         await _streamHandler.Handle(@event.Event);
         await _streamManager.SetPosition(@event.Event.Position);
     },
         filterOptions : new SubscriptionFilterOptions(
             StreamFilter.Prefix("Identity")
             ),
         cancellationToken : stoppingToken
         );
 }
        static async Task CreatePersistentSubscriptionToAll(EventStorePersistentSubscriptionsClient client)
        {
            #region create-persistent-subscription-to-all

            var userCredentials = new UserCredentials("admin", "changeit");
            var filter          = StreamFilter.Prefix("test");

            var settings = new PersistentSubscriptionSettings();
            await client.CreateToAllAsync(
                "subscription-group",
                filter,
                settings,
                userCredentials);

            #endregion create-persistent-subscription-to-all
        }
        private static async Task StreamPrefix(EventStoreClient client)
        {
            #region stream-prefix
            var filter = new SubscriptionFilterOptions(
                StreamFilter.Prefix("user-"));
            #endregion stream-prefix

            await client.SubscribeToAllAsync(Position.Start,
                                             (s, e, c) => {
                Console.WriteLine(
                    $"{e.Event.EventType} @ {e.Event.Position.PreparePosition}");
                return(Task.CompletedTask);
            },
                                             filterOptions : filter
                                             );
        }
            protected override async Task Given()
            {
                foreach (var e in _events)
                {
                    await StreamsClient.AppendToStreamAsync("test-" + Guid.NewGuid(), StreamState.Any, new[] { e });
                }

                await Client.CreateToAllAsync(Group,
                                              StreamFilter.Prefix("test"),
                                              new PersistentSubscriptionSettings(
                                                  minCheckPointCount : 5,
                                                  checkPointAfter : TimeSpan.FromSeconds(1),
                                                  startFrom : Position.Start),
                                              TestCredentials.Root);

                _checkPointSubscription = await StreamsClient.SubscribeToStreamAsync(_checkPointStream,
                                                                                     (s, e, ct) => {
                    if (_checkPoints.Count == 0)
                    {
                        _firstCheckPointSource.TrySetResult(e);
                    }
                    else
                    {
                        _secondCheckPointSource.TrySetResult(e);
                    }
                    _checkPoints.Add(e);
                    return(Task.CompletedTask);
                },
                                                                                     userCredentials : TestCredentials.Root);

                _subscription = await Client.SubscribeToAllAsync(Group,
                                                                 eventAppeared : (s, e, r, ct) => {
                    _appearedEvents.Add(e);

                    if (_appearedEvents.Count == _events.Length)
                    {
                        _appeared.TrySetResult(true);
                    }
                    return(Task.CompletedTask);
                },
                                                                 userCredentials : TestCredentials.Root);

                await Task.WhenAll(_appeared.Task, _firstCheckPointSource.Task).WithTimeout();

                FirstCheckPoint = _firstCheckPointSource.Task.Result.Event.Data.ParsePosition();
            }
        public async Task prefix_stream_name()
        {
            var streamPrefix = _fixture.GetStreamName();
            var events       = _fixture.CreateTestEvents(10).ToArray();

            foreach (var e in events)
            {
                await _fixture.Client.AppendToStreamAsync($"{streamPrefix}_{Guid.NewGuid():n}",
                                                          AnyStreamRevision.NoStream, new[] { e });
            }

            var result = await _fixture.Client.ReadAllAsync(Direction.Backwards, Position.End, 20,
                                                            filterOptions : new FilterOptions(StreamFilter.Prefix(streamPrefix)))
                         .ToArrayAsync();

            Assert.Equal(events.Select(x => x.EventId), result.Reverse().Select(x => x.OriginalEvent.EventId));
        }
示例#7
0
 public async Task SubscribeAll_StreamFilterPrefix()
 {
     var prefixStreamFilter = new SubscriptionFilterOptions(StreamFilter.Prefix("test-", "other-"));
     await _client.SubscribeToAllAsync(SubscribeReturn,
                                       filterOptions : prefixStreamFilter);
 }
        public async Task subscription_does_not_send_checkpoint_reached_after_disposal()
        {
            var streamName                   = _fixture.GetStreamName();
            var ignoredStreamName            = $"ignore_{streamName}";
            var subscriptionDisposed         = new TaskCompletionSource <bool>();
            var eventAppeared                = new TaskCompletionSource <bool>();
            var checkpointReachAfterDisposed = new TaskCompletionSource <bool>();

            await _fixture.Client.AppendToStreamAsync(streamName, StreamRevision.None, _fixture.CreateTestEvents());

            var subscription = await _fixture.Client.SubscribeToAllAsync((_, __, ___) => {
                eventAppeared.TrySetResult(true);
                return(Task.CompletedTask);
            }, false,
                                                                         (_, __, ____) => subscriptionDisposed.TrySetResult(true),
                                                                         new SubscriptionFilterOptions(StreamFilter.Prefix(streamName), 1, (_, __, ____) => {
                if (!subscriptionDisposed.Task.IsCompleted)
                {
                    return(Task.CompletedTask);
                }

                checkpointReachAfterDisposed.TrySetResult(true);
                return(Task.CompletedTask);
            }), userCredentials : new UserCredentials("admin", "changeit"));

            await eventAppeared.Task;

            subscription.Dispose();
            await subscriptionDisposed.Task;

            await _fixture.Client.AppendToStreamAsync(ignoredStreamName, StreamRevision.None,
                                                      _fixture.CreateTestEvents(50));

            var delay  = Task.Delay(300);
            var result = await Task.WhenAny(delay, checkpointReachAfterDisposed.Task);

            Assert.Equal(delay, result);             // iow 300ms have passed without seeing checkpointReachAfterDisposed
        }