Exemplo n.º 1
0
        public EventConsumerGrainTests()
        {
            state.Position = initialPosition;

            consumerName = eventConsumer.GetType().Name;

            A.CallTo(() => store.WithSnapshots(A <Type> .Ignored, consumerName, A <Func <EventConsumerState, Task> > .Ignored))
            .Invokes(new Action <Type, string, Func <EventConsumerState, Task> >((t, key, a) => apply = a))
            .Returns(persistence);

            A.CallTo(() => eventStore.CreateSubscription(A <IEventSubscriber> .Ignored, A <string> .Ignored, A <string> .Ignored))
            .Returns(eventSubscription);

            A.CallTo(() => eventConsumer.Name)
            .Returns(consumerName);

            A.CallTo(() => persistence.ReadAsync(EtagVersion.Any))
            .Invokes(new Action <long>(s => apply(state)));

            A.CallTo(() => persistence.WriteSnapshotAsync(A <EventConsumerState> .Ignored))
            .Invokes(new Action <EventConsumerState>(s => state = s));

            A.CallTo(() => formatter.Parse(eventData, true)).Returns(envelope);

            sut = new MyEventConsumerGrain(
                x => eventConsumer,
                store,
                eventStore,
                formatter,
                log);
        }
Exemplo n.º 2
0
        private async Task DoAndUpdateStateAsync(Func <Task> action, [CallerMemberName] string caller = null)
        {
            try
            {
                await action();
            }
            catch (Exception ex)
            {
                try
                {
                    Unsubscribe();
                }
                catch (Exception unsubscribeException)
                {
                    ex = new AggregateException(ex, unsubscribeException);
                }

                log.LogFatal(ex, w => w
                             .WriteProperty("action", caller)
                             .WriteProperty("status", "Failed")
                             .WriteProperty("eventConsumer", eventConsumer.Name));

                state = state.Failed(ex);
            }

            await persistence.WriteSnapshotAsync(state);
        }
Exemplo n.º 3
0
        private void AssertGrainState(bool isStopped = false, string?position = null, string?error = null, int count = 0)
        {
            var expected = new EventConsumerState {
                IsStopped = isStopped, Position = position, Error = error, Count = count
            };

            grainState.Value.Should().BeEquivalentTo(expected);
        }
Exemplo n.º 4
0
        public Task ResetAsync()
        {
            return(DoAndUpdateStateAsync(async() =>
            {
                Unsubscribe();

                await ClearAsync();

                Subscribe(null);

                state = state.Reset();
            }));
        }
Exemplo n.º 5
0
        public Task OnErrorAsync(Immutable <IEventSubscription> subscription, Immutable <Exception> exception)
        {
            if (subscription.Value != currentSubscription)
            {
                return(TaskHelper.Done);
            }

            return(DoAndUpdateStateAsync(() =>
            {
                Unsubscribe();

                state = state.Failed(exception.Value);
            }));
        }
Exemplo n.º 6
0
        public Task StopAsync()
        {
            if (state.IsStopped)
            {
                return(TaskHelper.Done);
            }

            return(DoAndUpdateStateAsync(() =>
            {
                Unsubscribe();

                state = state.Stopped();
            }));
        }
Exemplo n.º 7
0
        public Task StartAsync()
        {
            if (!state.IsStopped)
            {
                return(TaskHelper.Done);
            }

            return(DoAndUpdateStateAsync(() =>
            {
                Subscribe(state.Position);

                state = state.Started();
            }));
        }
Exemplo n.º 8
0
        private Task HandleErrorAsync(IEventSubscription subscription, Exception exception)
        {
            if (subscription != currentSubscription)
            {
                return(TaskHelper.Done);
            }

            return(DoAndUpdateStateAsync(() =>
            {
                Unsubscribe();

                state = state.Failed(exception);
            }));
        }
Exemplo n.º 9
0
        public void Should_serialize_and_deserialize()
        {
            var state = new EventConsumerState
            {
                Count     = 1,
                IsStopped = true,
                Error     = "Error",
                Position  = "Position"
            };

            var serialized = state.SerializeAndDeserialize();

            serialized.Should().BeEquivalentTo(state);
        }
Exemplo n.º 10
0
        public async Task Should_not_subscribe_to_event_store_when_stopped_in_db()
        {
            state = state.Stopped();

            await sut.OnActivateAsync(consumerName);

            await sut.ActivateAsync();

            state.Should().BeEquivalentTo(new EventConsumerState {
                IsStopped = true, Position = initialPosition, Error = null
            });

            A.CallTo(() => eventStore.CreateSubscription(A <IEventSubscriber> .Ignored, A <string> .Ignored, A <string> .Ignored))
            .MustNotHaveHappened();
        }
Exemplo n.º 11
0
        public Task OnEventAsync(Immutable <IEventSubscription> subscription, Immutable <StoredEvent> storedEvent)
        {
            if (subscription.Value != currentSubscription)
            {
                return(TaskHelper.Done);
            }

            return(DoAndUpdateStateAsync(async() =>
            {
                var @event = ParseKnownEvent(storedEvent.Value);

                if (@event != null)
                {
                    await DispatchConsumerAsync(@event);
                }

                state = state.Handled(storedEvent.Value.EventPosition);
            }));
        }
Exemplo n.º 12
0
        private Task HandleEventAsync(IEventSubscription subscription, StoredEvent storedEvent)
        {
            if (subscription != currentSubscription)
            {
                return(TaskHelper.Done);
            }

            return(DoAndUpdateStateAsync(async() =>
            {
                var @event = ParseKnownEvent(storedEvent);

                if (@event != null)
                {
                    await DispatchConsumerAsync(@event);
                }

                state = state.Handled(storedEvent.EventPosition);
            }));
        }
Exemplo n.º 13
0
 private void AssetGrainState(EventConsumerState state)
 {
     grainState.Value.Should().BeEquivalentTo(state);
 }
Exemplo n.º 14
0
        public override Task OnActivateAsync(string key)
        {
            scheduler = TaskScheduler.Current;

            eventConsumer = eventConsumerFactory(key);

            persistence = store.WithSnapshots <EventConsumerState, string>(GetType(), eventConsumer.Name, s => state = s);

            return(persistence.ReadAsync());
        }
Exemplo n.º 15
0
        public Task ActivateAsync(string key, IStore <string> store)
        {
            persistence = store.WithSnapshots <EventConsumerState, string>(key, s => state = s);

            return(persistence.ReadAsync());
        }