Exemplo n.º 1
0
        public SubscriptionFixture(Action <string> writeLine,
                                   long maxPosition = 20,
                                   long handlerExceptionPosition = -2)
        {
            _writeLine  = writeLine;
            _cts        = new CancellationTokenSource();
            MaxPosition = maxPosition;
            AllStreamPosition hasCaughtUpTrigger = AllStreamPosition.None;
            AllStreamPosition notifierTrigger    = AllStreamPosition.None;
            ReadAllPageFunc   readAllPage        = (from, ct) => Task.FromResult(Read(from));
            MessageReceived   handler            = (s, m, ct) =>
            {
                LastProcessed = m.Checkpoint;
                if (m.Checkpoint == handlerExceptionPosition)
                {
                    throw new InvalidOperationException("Custom exception thrown");
                }
                return(Task.CompletedTask);
            };
            Func <Exception, Task> onError     = ex => Async(() => _exceptionSource.SetResult(ex));
            HasCaughtUp            hasCaughtUp = () => Async(() => hasCaughtUpTrigger = hasCaughtUpTrigger.Shift());

            var hasCaughtUpNotifier = new PollingNotifier(_ => Task.FromResult(hasCaughtUpTrigger));
            var notifier            = new PollingNotifier(_ => Task.FromResult(notifierTrigger));

            Subscription = new GenericSubscription(
                readAllPage,
                AllStreamPosition.None,
                notifier.WaitForNotification,
                handler,
                onError,
                hasCaughtUp);

            var cancellationToken = _cts.Token;

            cancellationToken.Register(() =>
            {
                Subscription.Dispose();
                notifier.Dispose();
                hasCaughtUpNotifier.Dispose();
            });

            WaitForCaughtUp = () => hasCaughtUpNotifier.WaitForNotification(cancellationToken);
            AppendEvents    = c =>
            {
                MaxPosition    += c;
                notifierTrigger = notifierTrigger.Shift();
            };
        }
        public GenericSubscription(
            ReadAllPageFunc readAllPage,
            AllStreamPosition fromPosition,
            Func <CancellationToken, Task> waitForEvent,
            MessageReceived onMessage,
            Func <Exception, Task> onSubscriptionError,
            HasCaughtUp hasCaughtUp)
        {
            _readAllPage         = readAllPage;
            FromPosition         = fromPosition;
            _nextPosition        = new AllStreamPosition(fromPosition.ToNullableInt64() + 1 ?? 0);
            _waitForEvent        = waitForEvent;
            _onMessage           = onMessage;
            _hasCaughtUp         = hasCaughtUp ?? (() => Task.CompletedTask);
            _onSubscriptionError = onSubscriptionError ?? (_ => Task.CompletedTask);
            LastPosition         = fromPosition;

            Task.Run(PullAndPush);
        }