Beispiel #1
0
        public void multi_apply_double()
        {
            var reified = new ReifiedProjectionGroup <int, State>(new IProjection <int>[]
            {
                MockInteger().Object,
                MockString().Object
            });

            reified.Apply(1U, 10);
            reified.Apply(1U, 10);
        }
        /// <summary> Catch up with locally stored data, without remote fetches. </summary>
        /// <returns>
        ///     The number of events that have been passed to the projection.
        /// </returns>
        private uint CatchUpLocal()
        {
            var caughtUpWithProjection   = false;
            var eventsPassedToProjection = 0u;

            while (true)
            {
                TEvent nextEvent;

                try
                {
                    // This might throw due to serialization error
                    //  (but not for other reasons)
                    nextEvent = Stream.TryGetNext();
                }
                catch (Exception ex)
                {
                    _log?.Warning($"[ES read] unreadable event at seq {Stream.Sequence}.", ex);
                    _projection.SetPossiblyInconsistent();
                    Quarantine.Add(Stream.Sequence, ex);
                    continue;
                }

                // No more local events left
                if (nextEvent == null)
                {
                    break;
                }

                var seq = Stream.Sequence;

                if (_log != null && seq % 1000 == 0)
                {
                    _log.Info($"[ES read] processing event at seq {seq}.");
                }

                if (caughtUpWithProjection || seq > _projection.Sequence)
                {
                    caughtUpWithProjection = true;
                    try
                    {
                        ++eventsPassedToProjection;

                        // This might throw due to event processing issues
                        //  by one or more projection components
                        _projection.Apply(seq, nextEvent);
                    }
                    catch (Exception ex)
                    {
                        _log?.Warning($"[ES read] processing error on event at seq {seq}.", ex);
                        _projection.SetPossiblyInconsistent();
                        Quarantine.Add(seq, nextEvent, ex);
                    }
                }
            }

            return(eventsPassedToProjection);
        }
Beispiel #3
0
        public void multi_apply_double()
        {
            var reified = new ReifiedProjectionGroup <int, State>(new IProjection <int>[]
            {
                MockInteger().Object,
                MockString().Object
            });

            try
            {
                reified.Apply(1U, 10);
                reified.Apply(1U, 10);
                Assert.True(false);
            }
            catch (ArgumentException)
            {
                ;
            }
        }
Beispiel #4
0
        public void multi_apply_twice()
        {
            var reified = new ReifiedProjectionGroup <int, State>(new IProjection <int>[]
            {
                MockInteger().Object,
                MockString().Object
            });

            var oldcount = State.Creations;

            reified.Apply(1U, 10);
            reified.Apply(4U, 14);

            Assert.AreEqual((uint)4U, (uint)reified.Sequence);
            Assert.AreEqual(oldcount, State.Creations);
            Assert.AreEqual((int)24, (int)reified.Current.I.Value);
            Assert.AreEqual(oldcount + 1, State.Creations);
            Assert.AreEqual("I(10:1)(14:4)", reified.Current.S);
            Assert.AreEqual(oldcount + 1, State.Creations);
        }
Beispiel #5
0
        public void multi_apply()
        {
            var reified = new ReifiedProjectionGroup <int, State>(new IProjection <int>[]
            {
                MockInteger().Object,
                MockString().Object
            });

            reified.Apply(1U, 10);

            Assert.AreEqual((uint)1U, (uint)reified.Sequence);
            Assert.AreEqual((int)10, (int)reified.Current.I.Value);
            Assert.AreEqual("I(10:1)", reified.Current.S);
        }
Beispiel #6
0
        public async Task multi_apply_separate()
        {
            var cache = new Mock <IProjectionCacheProvider>();

            ReturnsExtensions.ReturnsAsync(cache.Setup(c => c.OpenReadAsync("string")), new MemoryStream(new byte[]
            {
                0x02, 0x00, 0x00, 0x00,     // Current position (beginning)
                0x30, 0x30, 0x30, 0x30,     // Event data "0000"
                0x02, 0x00, 0x00, 0x00      // Current position (end)
            }));

            var str = MockString();

            str.Setup(p => p.TryLoadAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>()))
            .Returns <Stream, CancellationToken>((s, c) =>
            {
                var bytes = new byte[4];
                s.Read(bytes, 0, 4);
                return(Task.FromResult(Encoding.UTF8.GetString(bytes)));
            });

            var reified = new ReifiedProjectionGroup <int, State>(new IProjection <int>[]
            {
                MockInteger().Object,
                str.Object
            }, cache.Object);

            await reified.TryLoadAsync(CancellationToken.None);

            reified.Apply(1U, 10);
            reified.Apply(4U, 14);

            Assert.AreEqual((uint)4U, (uint)reified.Sequence);
            Assert.AreEqual((int)24, (int)reified.Current.I.Value);
            Assert.AreEqual("0000(14:4)", reified.Current.S);
        }
Beispiel #7
0
        public async Task multi_apply_separate()
        {
            var cache = new Testing.InMemoryCache {
                { "string", new byte[]
                  {
                      0x02, 0x00, 0x00, 0x00, // Current position (beginning)
                      0x30, 0x30, 0x30, 0x30, // Event data "0000"
                      0x02, 0x00, 0x00, 0x00  // Current position (end)
                  } }
            };

            var str = MockString();

            str.Setup(p => p.TryLoadAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>()))
            .Returns <Stream, CancellationToken>((s, c) =>
            {
                var bytes = new byte[4];
                s.Read(bytes, 0, 4);
                return(Task.FromResult(Encoding.UTF8.GetString(bytes)));
            });

            var reified = new ReifiedProjectionGroup <int, State>(new IProjection <int>[]
            {
                MockInteger().Object,
                str.Object
            }, cache);

            await reified.TryLoadAsync(CancellationToken.None);

            reified.Apply(1U, 10);
            reified.Apply(4U, 14);

            Assert.Equal(4U, reified.Sequence);
            Assert.Equal(24, reified.Current.I.Value);
            Assert.Equal("0000(14:4)", reified.Current.S);
        }
Beispiel #8
0
        public void multi_apply_reset()
        {
            var reified = new ReifiedProjectionGroup <int, State>(new IProjection <int>[]
            {
                MockInteger().Object,
                MockString().Object
            });

            reified.Apply(1U, 10);

            Assert.NotNull(reified.Current);

            reified.Reset();

            Assert.Equal(0U, reified.Sequence);
            Assert.Equal(0, reified.Current.I.Value);
            Assert.Equal("I", reified.Current.S);
        }