public void UpdateCacheOnSuccessfulSave() { var aggregate = new FakeAggregate(); var decoratedAggregateStore = new Mock <IStoreAggregates>(); var memoryCache = new MemoryCache(Guid.NewGuid().ToString()); var cachedAggregateStore = new CachedAggregateStore(decoratedAggregateStore.Object, TimeSpan.FromMinutes(1), memoryCache); memoryCache.Add(aggregate.CacheKey, aggregate, new CacheItemPolicy()); using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty)) cachedAggregateStore.Save(aggregate, context); Assert.NotSame(aggregate, memoryCache.Get(aggregate.CacheKey)); }
public void CopyAggregateBeforeSaving() { var aggregate = new FakeAggregate(); var decoratedAggregateStore = new Mock<IStoreAggregates>(); var memoryCache = new MemoryCache(Guid.NewGuid().ToString()); var cachedAggregateStore = new CachedAggregateStore(decoratedAggregateStore.Object, TimeSpan.FromMinutes(1), memoryCache); // ReSharper disable AccessToDisposedClosure using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty)) { cachedAggregateStore.Save(aggregate, context); decoratedAggregateStore.Verify(mock => mock.Save(It.Is<Aggregate>(copy => !ReferenceEquals(aggregate, copy)), context), Times.Once()); } // ReSharper restore AccessToDisposedClosure }
public void CopyAggregateBeforeSaving() { var aggregate = new FakeAggregate(); var decoratedAggregateStore = new Mock <IStoreAggregates>(); var memoryCache = new MemoryCache(Guid.NewGuid().ToString()); var cachedAggregateStore = new CachedAggregateStore(decoratedAggregateStore.Object, TimeSpan.FromMinutes(1), memoryCache); // ReSharper disable AccessToDisposedClosure using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty)) { cachedAggregateStore.Save(aggregate, context); decoratedAggregateStore.Verify(mock => mock.Save(It.Is <Aggregate>(copy => !ReferenceEquals(aggregate, copy)), context), Times.Once()); } // ReSharper restore AccessToDisposedClosure }
public void RemoveAggregateFromCacheOnConcurrencyException() { var aggregate = new FakeAggregate(); var decoratedAggregateStore = new Mock <IStoreAggregates>(); var memoryCache = new MemoryCache(Guid.NewGuid().ToString()); var cachedAggregateStore = new CachedAggregateStore(decoratedAggregateStore.Object, TimeSpan.FromMinutes(1), memoryCache); // ReSharper disable AccessToDisposedClosure using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty)) { memoryCache.Add(aggregate.CacheKey, aggregate, new CacheItemPolicy()); decoratedAggregateStore.Setup(mock => mock.Save(It.Is <Aggregate>(copy => !ReferenceEquals(aggregate, copy)), context)).Throws <ConcurrencyException>(); Assert.Throws <ConcurrencyException>(() => cachedAggregateStore.Save(aggregate, context)); Assert.False(memoryCache.Contains(aggregate.CacheKey)); } // ReSharper restore AccessToDisposedClosure }
public void RemoveAggregateFromCacheOnConcurrencyException() { var aggregate = new FakeAggregate(); var decoratedAggregateStore = new Mock<IStoreAggregates>(); var memoryCache = new MemoryCache(Guid.NewGuid().ToString()); var cachedAggregateStore = new CachedAggregateStore(decoratedAggregateStore.Object, TimeSpan.FromMinutes(1), memoryCache); // ReSharper disable AccessToDisposedClosure using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty)) { memoryCache.Add(aggregate.CacheKey, aggregate, new CacheItemPolicy()); decoratedAggregateStore.Setup(mock => mock.Save(It.Is<Aggregate>(copy => !ReferenceEquals(aggregate, copy)), context)).Throws<ConcurrencyException>(); Assert.Throws<ConcurrencyException>(() => cachedAggregateStore.Save(aggregate, context)); Assert.False(memoryCache.Contains(aggregate.CacheKey)); } // ReSharper restore AccessToDisposedClosure }
public void UpdateCacheOnSuccessfulSave() { var aggregate = new FakeAggregate(); var decoratedAggregateStore = new Mock<IStoreAggregates>(); var memoryCache = new MemoryCache(Guid.NewGuid().ToString()); var cachedAggregateStore = new CachedAggregateStore(decoratedAggregateStore.Object, TimeSpan.FromMinutes(1), memoryCache); memoryCache.Add(aggregate.CacheKey, aggregate, new CacheItemPolicy()); using (var context = new CommandContext(GuidStrategy.NewGuid(), HeaderCollection.Empty, CommandEnvelope.Empty)) cachedAggregateStore.Save(aggregate, context); Assert.NotSame(aggregate, memoryCache.Get(aggregate.CacheKey)); }