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()); }
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); }
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); }
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); }
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(); })); } }
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); }
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); }
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); }); }
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); }