예제 #1
0
        public async Task RemoveEntity()
        {
            // arrange
            var cts         = new CancellationTokenSource(2000);
            var entityStore = new EntityStore();
            var entityId    = new EntityId(nameof(MockEntity), 1);

            entityStore.Update(session =>
            {
                session.SetEntity(entityId, new MockEntity("abc", 1));
            });

            // act
            entityStore.Update(session =>
            {
                session.RemoveEntity(entityId);
            });

            while (entityStore.CurrentSnapshot.GetEntityIds().Count > 0 &&
                   !cts.IsCancellationRequested)
            {
                await Task.Delay(50, cts.Token);
            }

            // assert
            Assert.Empty(entityStore.CurrentSnapshot.GetEntityIds());
        }
예제 #2
0
        public void UpdateEntity()
        {
            // arrange
            var entityStore = new EntityStore();
            var entityId    = new EntityId(nameof(MockEntity), 1);

            IReadOnlyList <MockEntity> updated = Array.Empty <MockEntity>();
            ulong version = 0;

            entityStore.Watch().Subscribe(update =>
            {
                updated = entityStore.GetEntities <MockEntity>(update.UpdatedEntityIds);
                version = update.Version;
            });

            // act
            using (entityStore.BeginUpdate())
            {
                MockEntity entity = entityStore.GetOrCreate <MockEntity>(entityId);
                entity.Foo = "abc";
                entity.Bar = 1;
            }

            // assert
            Assert.Collection(
                updated,
                item =>
            {
                Assert.Equal("abc", item.Foo);
                Assert.Equal(1, item.Bar);
            });
            Assert.Equal(1ul, version);
        }
예제 #3
0
        public async Task UpdateEntity()
        {
            // arrange
            var entityStore = new EntityStore();
            var entityId    = new EntityId(nameof(MockEntity), 1);

            IReadOnlyList <MockEntity> updated = Array.Empty <MockEntity>();
            ulong version = 0;

            using IDisposable subscription = entityStore.Watch().Subscribe(update =>
            {
                updated = update.Snapshot.GetEntities <MockEntity>(update.UpdatedEntityIds);
                version = update.Version;
            });

            // act
            entityStore.Update(session =>
            {
                session.SetEntity(entityId, new MockEntity("abc", 1));
            });

            await Task.Delay(250);

            // assert
            Assert.Collection(
                updated,
                item =>
            {
                Assert.Equal("abc", item.Foo);
                Assert.Equal(1, item.Bar);
            });
            Assert.Equal(1ul, version);
        }
예제 #4
0
        public void TryGet_Not_Found()
        {
            // arrange
            var entityStore = new EntityStore();
            var document    = new Mock <IDocument>();
            var store       = new OperationStore(entityStore);
            var request     = new OperationRequest("abc", document.Object);

            // act
            var success = store.TryGet(request, out IOperationResult <string>?retrieved);

            // assert
            Assert.False(success);
            Assert.Null(retrieved);
        }
예제 #5
0
        public async Task EnsureUpdatesAreExecutedOneAfterTheOther()
        {
            // arrange
            var entityStore = new EntityStore();
            var entityId    = new EntityId(nameof(MockEntity), 1);

            List <string> updated = new();
            ulong         version = 0;

            entityStore.Watch().Subscribe(update =>
            {
                updated.Add(
                    entityStore.GetEntities <MockEntity>(update.UpdatedEntityIds).Single().Foo !);
                version = update.Version;
            });

            // act
            Task task1 = BeginUpdate(entityStore, "abc");
            Task task2 = BeginUpdate(entityStore, "def");
            await Task.WhenAll(task1, task2);

            // assert
            Assert.Collection(
                updated,
                item =>
            {
                Assert.Equal("abc", item);
            },
                item =>
            {
                Assert.Equal("def", item);
            });
            Assert.Equal(2ul, version);

            Task BeginUpdate(IEntityStore entityStore, string foo)
            {
                IEntityUpdateSession session = entityStore.BeginUpdate();

                return(Task.Run(async() =>
                {
                    await Task.Delay(50);
                    MockEntity entity = entityStore.GetOrCreate <MockEntity>(entityId);
                    entity.Foo = foo;
                    session.Dispose();
                }));
            }
        }
예제 #6
0
        public void Store_And_Retrieve_Result()
        {
            // arrange
            var entityStore = new EntityStore();
            var document    = new Mock <IDocument>();
            var result      = new Mock <IOperationResult <string> >();
            var store       = new OperationStore(entityStore);
            var request     = new OperationRequest("abc", document.Object);

            // act
            store.Set(request, result.Object);
            var success = store.TryGet(request, out IOperationResult <string>?retrieved);

            // assert
            Assert.True(success);
            Assert.Same(result.Object, retrieved);
        }
예제 #7
0
        public void Watch_For_Updates()
        {
            // arrange
            var entityStore = new EntityStore();
            var document    = new Mock <IDocument>();
            var result      = new Mock <IOperationResult <string> >();
            var store       = new OperationStore(entityStore);
            var request     = new OperationRequest("abc", document.Object);
            var observer    = new ResultObserver();

            // act
            using IDisposable session = store
                                        .Watch <string>(request)
                                        .Subscribe(observer);

            // assert
            store.Set(request, result.Object);
            Assert.Same(result.Object, observer.LastResult);
        }
예제 #8
0
        public void GetAllEntities()
        {
            // arrange
            var entityStore = new EntityStore();
            var entityId1   = new EntityId(nameof(MockEntity), 1);
            var entityId2   = new EntityId(nameof(MockEntity) + "a", 2);

            entityStore.Update(session =>
            {
                session.SetEntity(entityId1, new MockEntity("abc", 1));
                session.SetEntity(entityId2, new MockEntity("abc", 2));
            });

            // act
            var allEntities  = entityStore.CurrentSnapshot.GetEntities().ToList();
            var mockEntities = entityStore.CurrentSnapshot.GetEntities(nameof(MockEntity));

            // assert
            Assert.Collection(
                allEntities.OrderBy(t => t.Id.Value),
                item =>
            {
                var entity = Assert.IsType <MockEntity>(item.Entity);
                Assert.Equal("abc", entity.Foo);
                Assert.Equal(1, entity.Bar);
            },
                item =>
            {
                var entity = Assert.IsType <MockEntity>(item.Entity);
                Assert.Equal("abc", entity.Foo);
                Assert.Equal(2, entity.Bar);
            });

            Assert.Collection(
                mockEntities.OrderBy(t => t.Id.Value),
                item =>
            {
                var entity = Assert.IsType <MockEntity>(item.Entity);
                Assert.Equal("abc", entity.Foo);
                Assert.Equal(1, entity.Bar);
            });
        }
예제 #9
0
        public void Watch_For_Updates_With_SystemReactive()
        {
            // arrange
            var entityStore = new EntityStore();
            var document    = new Mock <IDocument>();
            var result      = new Mock <IOperationResult <string> >();
            var store       = new OperationStore(entityStore);
            var request     = new OperationRequest("abc", document.Object);
            IOperationResult <string>?lastResult = null;


            // act
            using IDisposable session =
                      System.ObservableExtensions.Subscribe(
                          store.Watch <string>(request),
                          r =>
            {
                lastResult = r;
            });

            // assert
            store.Set(request, result.Object);
            Assert.Same(result.Object, lastResult);
        }