Ejemplo n.º 1
0
            protected override void ApplyState(ReadModelState snapshot)
            {
                if (snapshot?.State == null)
                {
                    throw new ArgumentNullException(nameof(snapshot), $"Null State provided to {nameof(TestSnapShotReadModel)}");
                }
                if (!(snapshot.State is MyState))
                {
                    throw new ArgumentException($"Unknown state object: Expected {nameof(MyState)}, got {snapshot.State.GetType().Name}");
                }
                var state = (MyState)snapshot.State;

                Count = state.Count;
                Sum   = state.Sum;
            }
Ejemplo n.º 2
0
        public void can_apply_snapshot_to_read_model()
        {
            var snapshot = new ReadModelState(
                nameof(TestSnapShotReadModel),
                new List <Tuple <string, long> > {
                new Tuple <string, long>(_stream, 9)
            },
                new TestSnapShotReadModel.MyState {
                Count = 10, Sum = 20
            });

            var rm = new TestSnapShotReadModel(_aggId, GetListener, snapshot);

            AssertEx.IsOrBecomesTrue(() => rm.Count == 10);
            AssertEx.IsOrBecomesTrue(() => rm.Sum == 20);
            AppendEvents(1, _conn, _stream, 5);
            AssertEx.IsOrBecomesTrue(() => rm.Count == 11, 1000);
            AssertEx.IsOrBecomesTrue(() => rm.Sum == 25);
        }
Ejemplo n.º 3
0
        public void can_restore_state_without_checkpoints()
        {
            var snapshot = new ReadModelState(
                nameof(TestSnapShotReadModel),
                null, //no streams provided
                new TestSnapShotReadModel.MyState {
                Count = 10, Sum = 20
            });

            var rm = new TestSnapShotReadModel(_aggId, GetListener, snapshot);

            AssertEx.IsOrBecomesTrue(() => rm.Count == 10);
            AssertEx.IsOrBecomesTrue(() => rm.Sum == 20);

            //n.b. will not start listening because no streams are provided
            //confirm not listening
            AppendEvents(1, _conn, _stream, 5);
            AssertEx.IsOrBecomesTrue(() => rm.Count == 10, 1000);
            AssertEx.IsOrBecomesTrue(() => rm.Sum == 20);

            //can manually start
            rm.Start <SnapReadModelTestAggregate>(_aggId, 9, true);
            AssertEx.IsOrBecomesTrue(() => rm.Count == 11, 1000);
            AssertEx.IsOrBecomesTrue(() => rm.Sum == 25);
            AppendEvents(1, _conn, _stream, 5);
            AssertEx.IsOrBecomesTrue(() => rm.Count == 12, 1000);
            AssertEx.IsOrBecomesTrue(() => rm.Sum == 30);

            //can still get complete snapshot
            var snap2 = rm.GetState();
            //works as expected
            var rm2 = new TestSnapShotReadModel(_aggId, GetListener, snap2);

            AssertEx.IsOrBecomesTrue(() => rm2.Count == 12, 1000);
            AssertEx.IsOrBecomesTrue(() => rm2.Sum == 30);
            AppendEvents(1, _conn, _stream, 5);
            AssertEx.IsOrBecomesTrue(() => rm2.Count == 13, 1000);
            AssertEx.IsOrBecomesTrue(() => rm2.Sum == 35);
        }
Ejemplo n.º 4
0
            public TestSnapShotReadModel(
                Guid aggId,
                Func <IListener> getListener,
                ReadModelState snapshot,
                bool forceDoubleRestoreError = false) :
                base(nameof(TestSnapShotReadModel), getListener)
            {
                // ReSharper disable once RedundantTypeArgumentsOfMethod
                EventStream.Subscribe <SnapReadModelTestEvent>(this);

                if (snapshot is null)
                {
                    Start <SnapReadModelTestAggregate>(aggId, null, true);
                }
                else
                {
                    Restore(snapshot);
                }
                if (forceDoubleRestoreError)
                {
                    Restore(snapshot);
                }
            }