Esempio 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);
        }
Esempio 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);
        }
Esempio 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);
        }
        public Task ResetAsync()
        {
            return(DoAndUpdateStateAsync(async() =>
            {
                Unsubscribe();

                await ClearAsync();

                Subscribe(null);

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

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

                state = state.Failed(exception.Value);
            }));
        }
        public Task StopAsync()
        {
            if (state.IsStopped)
            {
                return(TaskHelper.Done);
            }

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

                state = state.Stopped();
            }));
        }
        public Task StartAsync()
        {
            if (!state.IsStopped)
            {
                return(TaskHelper.Done);
            }

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

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

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

                state = state.Failed(exception);
            }));
        }
Esempio 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);
        }
Esempio 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();
        }
Esempio 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);
            }));
        }
Esempio 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);
            }));
        }
Esempio n. 13
0
 private void AssetGrainState(EventConsumerState state)
 {
     grainState.Value.Should().BeEquivalentTo(state);
 }
Esempio 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());
        }
Esempio n. 15
0
        public Task ActivateAsync(string key, IStore <string> store)
        {
            persistence = store.WithSnapshots <EventConsumerState, string>(key, s => state = s);

            return(persistence.ReadAsync());
        }