Пример #1
0
        public async Task save_reset_inconsistent()
        {
            var cache = new Testing.InMemoryCache();

            var projection = new Mock <IProjection <int, string> >();

            projection.Setup(p => p.Initial).Returns("0");
            projection.Setup(p => p.FullName).Returns("test");
            projection.Setup(p => p.State).Returns(typeof(string));
            projection.Setup(p => p.TrySaveAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Returns <Stream, string, CancellationToken>((s, state, c) =>
            {
                var bytes = Encoding.UTF8.GetBytes(state);
                s.Write(bytes, 0, bytes.Length);
                return(Task.FromResult(true));
            });

            var reified = Make(projection.Object, cache);

            reified.SetPossiblyInconsistent();
            reified.Reset();

            await reified.TrySaveAsync(CancellationToken.None);

            Assert.Equal(new byte[]
            {
                0x00, 0x00, 0x00, 0x00, // Sequence number (first)
                0x30,                   // Contents "0"
                0x00, 0x00, 0x00, 0x00  // Sequence number (last)
            }, cache.Streams["test"].ToArray());
        }
Пример #2
0
        public async Task with_restart()
        {
            var memory = new MemoryStorageDriver();
            var cache  = new Testing.InMemoryCache();
            var ew     = new EventStreamWrapper <TstEvent, CheckSequence>(
                memory,
                new [] { new CheckSequence.Projection() },
                cache
                );
            await ew.AppendEventsAsync(new[] { new TstEvent(1) });

            await ew.AppendEventsAsync(new[] { new TstEvent(2) });

            await ew.AppendEventsAsync(new[] { new TstEvent(3) });

            await ew.TrySaveAsync();

            await ew.AppendEventsAsync(new[] { new TstEvent(4) });

            await ew.AppendEventsAsync(new[] { new TstEvent(5) });

            Assert.Equal(5u, ew.Current.LastEvt);

            // try to read:
            var ew2 = new EventStreamWrapper <TstEvent, CheckSequence>(
                memory, new[] { new CheckSequence.Projection() },
                cache);

            await ew2.InitializeAsync();

            Assert.Equal(5u, ew2.Current.LastEvt);
        }
Пример #3
0
        public async Task save_throws()
        {
            var cache = new Testing.InMemoryCache();

            var projection = new Mock <IProjection <int, string> >();

            projection.Setup(p => p.Initial).Returns("0");
            projection.Setup(p => p.FullName).Returns("test");
            projection.Setup(p => p.State).Returns(typeof(string));
            projection.Setup(p => p.TrySaveAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Throws(new Exception("Projection.TrySaveAsync"));

            var reified = Make(projection.Object, cache);

            try
            {
                await reified.TrySaveAsync(CancellationToken.None);

                Assert.True(false);
            }
            catch (Exception e)
            {
                Assert.Equal("Projection.TrySaveAsync", e.Message);
            }

            Assert.Empty(cache.Streams);
        }
Пример #4
0
        public async Task load_state_bad_name()
        {
            var cache = new Testing.InMemoryCache
            {
                { "test", new byte[]
                  {
                      0x02, 0x00, 0x00, 0x00, // Current position (beginning)
                      0x30, 0x30, 0x30, 0x30, // Event data "0000"
                      0x02, 0x00, 0x00, 0x00  // Current position (end)
                  } },
                { "other", new byte[0] }
            };

            var projection = new Mock <IProjection <int, string> >();

            projection.Setup(p => p.Initial).Returns("I");
            projection.Setup(p => p.FullName).Returns("other");
            projection.Setup(p => p.State).Returns(typeof(string));
            ReturnsExtensions.ReturnsAsync(projection.Setup(p => p.TryLoadAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>())), "bad");

            var reified = Make(projection.Object, cache);

            await reified.TryLoadAsync(CancellationToken.None);

            Assert.Equal(0U, reified.Sequence);
            Assert.Equal("I", reified.Current);
        }
Пример #5
0
        public async Task load_state()
        {
            var cache = new Testing.InMemoryCache {
                { "test", new byte[]
                  {
                      0x02, 0x00, 0x00, 0x00, // Current position (beginning)
                      0x30, 0x30, 0x30, 0x30, // Event data "0000"
                      0x02, 0x00, 0x00, 0x00  // Current position (end)
                  } }
            };

            var projection = new Mock <IProjection <int, string> >();

            projection.Setup(p => p.Initial).Returns("I");
            projection.Setup(p => p.FullName).Returns("test");
            projection.Setup(p => p.State).Returns(typeof(string));
            projection.Setup(p => p.TryLoadAsync(It.IsAny <Stream>(), It.IsAny <CancellationToken>()))
            .Returns <Stream, CancellationToken>((s, c) =>
            {
                var bytes = new byte[4];
                Assert.Equal(0, s.Position);
                s.Read(bytes, 0, 4);
                return(Task.FromResult(Encoding.UTF8.GetString(bytes)));
            });

            var reified = Make(projection.Object, cache);

            await reified.TryLoadAsync(CancellationToken.None);

            Assert.Equal(2U, reified.Sequence);
            Assert.Equal("0000", reified.Current);
        }
Пример #6
0
        public async Task skip_for_projection()
        {
            var stream = GetStream();
            var imm    = new Testing.InMemoryCache();
            var ess1   = EventStreamService <Ev, Sta> .StartNew(
                stream,
                new[] { new Project() },
                imm,
                null,
                CancellationToken.None);

            await ess1.Ready;

            await ess1.TrySaveAsync();

            await ess1.AppendEventsAsync(new[] { new Ev {
                                                     Value = 41
                                                 } });

            var evs  = new List <int>();
            var seqs = new List <uint>();
            var ess  = EventStreamService <Ev, Sta> .StartNew(
                stream,
                new[] { new Project() },
                null,
                new[]
            {
                Tuple.Create <EventStream <Ev> .Listener, uint>(
                    (e, s) =>
                {
                    evs.Add(e.Value);
                    seqs.Add(s);
                },
                    2u)
            },
                null,
                CancellationToken.None);

            await ess.Ready;

            CollectionAssert.AreEqual(new[] { 25, 30, 41 }, evs);
            CollectionAssert.AreEqual(new[] { 2u, 3u, 4u }, seqs);
            Assert.AreEqual(11 + 25 + 30 + 41, ess.LocalState.Sum);
        }
Пример #7
0
        public async Task save_inconsistent()
        {
            var ms = new MemoryStream();

            var cache = new Testing.InMemoryCache();

            var projection = new Mock <IProjection <int, string> >();

            projection.Setup(p => p.Initial).Returns("0");
            projection.Setup(p => p.FullName).Returns("test");
            projection.Setup(p => p.State).Returns(typeof(string));
            projection.Setup(p => p.TrySaveAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Throws(new Exception("Projection.TrySaveAsync"));

            var reified = Make(projection.Object, cache);

            reified.SetPossiblyInconsistent();

            await reified.TrySaveAsync(CancellationToken.None);

            Assert.False(cache.Streams.ContainsKey("test"));
        }
Пример #8
0
        public virtual async Task save_auto_inconsistent()
        {
            var cache = new Testing.InMemoryCache();

            var projection = new Mock <IProjection <int, string> >();

            projection.Setup(p => p.Initial).Returns("0");
            projection.Setup(p => p.FullName).Returns("test");
            projection.Setup(p => p.State).Returns(typeof(string));
            projection.Setup(p => p.TrySaveAsync(It.IsAny <Stream>(), It.IsAny <string>(), It.IsAny <CancellationToken>()))
            .Throws(new Exception("Projection.TrySaveAsync"));

            projection.Setup(p => p.Apply(It.IsAny <uint>(), It.IsAny <int>(), It.IsAny <string>()))
            .Throws(new Exception("Boo."));

            var reified = Make(projection.Object, cache);

            try { reified.Apply(1U, 13); /* Sets 'inconsistent' */ } catch {}

            await reified.TrySaveAsync(CancellationToken.None);

            Assert.Empty(cache.Streams);
        }
Пример #9
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);
        }