protected override void TestSetup() { CacheBuilder.For <ObjectWithCtorParameters>() .CacheMethod(m => m.CachedMethod()) .As <IObjectWithCtorParameters>(); CacheBuilder.For <ObjectReturningNewGuids>() .CacheMethod(m => m.CachedMethod()) .As <IObjectReturningNewGuids>(); factory = CacheBuilder.BuildFactory(); }
protected override void TestSetup() { CacheBuilder .For <ObjectReturningNewGuids>() .CacheMethod(c => c.CachedMethod()) .PerInstance() .As <IObjectReturningNewGuids>(); factory = CacheBuilder.BuildFactory(); }
protected override void TestSetup() { CacheBuilder.For <ObjectTakes100MsToFill>() .CacheMethod(c => c.Execute(0)) .AsImplemented(); var factory = CacheBuilder.BuildFactory(); instance = factory.Create <ObjectTakes100MsToFill>(); }
protected override void TestSetup() { CacheBuilder .For <ObjectWithMutableList>() .CacheMethod(c => c.GetListContents()) .AsImplemented(); factory = CacheBuilder.BuildFactory(); instance = factory.Create <ObjectWithMutableList>(); }
protected override void TestSetup() { CacheBuilder.For <ReturningRandomNumbers>() .CacheMethod(c => c.CachedNumber()) .As <IReturningRandomNumbers>(); var factory = CacheBuilder.BuildFactory(); instance = factory.Create <IReturningRandomNumbers>(); }
public async Task TestArrayCacheReturnsCachedItemsOnSubsequentCallsAndOnlyOneCallToRetrieverPerRefresh() { var throwException = false; var arrayCache = CacheBuilder.For <string>() .WithIdExtractor(v => v) .WithElementRetriever(v => v.InTask()) .GetArrayCache <string>(v => { // ReSharper disable once AccessToModifiedClosure Assert.False(throwException, "Retriever should not have been called!"); throwException = true; return(new[] { $"{v}1", $"{v}2", $"{v}3" }.ToObservable()); }); var id = Guid.NewGuid() .ToString(); Assert.Equal(new[] { $"{id}1", $"{id}2", $"{id}3" }, await arrayCache.Get(id) .Select(v => v.ToArray()) .FirstAsync()); Assert.Equal(new[] { $"{id}1", $"{id}2", $"{id}3" }, await arrayCache.Get(id) .Select(v => v.ToArray()) .FirstAsync()); throwException = false; arrayCache.Refresh(id); Assert.Equal(new[] { $"{id}1", $"{id}2", $"{id}3" }, await arrayCache.Get(id) .Select(v => v.ToArray()) .FirstAsync()); Assert.Equal(new[] { $"{id}1", $"{id}2", $"{id}3" }, await arrayCache.Get(id) .Select(v => v.ToArray()) .FirstAsync()); throwException = false; arrayCache.Refresh(id); Assert.Equal(new[] { $"{id}1", $"{id}2", $"{id}3" }, await arrayCache.Get(id) .Select(v => v.ToArray()) .FirstAsync()); Assert.Equal(new[] { $"{id}1", $"{id}2", $"{id}3" }, await arrayCache.Get(id) .Select(v => v.ToArray()) .FirstAsync()); }
public void CreatingProxiesOfSameDeclaredTypeShouldReturnIdenticalTypes() { CacheBuilder .For <ObjectReturningNewGuids>() .CacheMethod(c => c.CachedMethod()) .As <IObjectReturningNewGuids>(); var factory = CacheBuilder.BuildFactory(); Assert.AreEqual(factory.Create <IObjectReturningNewGuids>().GetType(), factory.Create <IObjectReturningNewGuids>().GetType()); }
protected override void TestSetup() { CacheBuilder.For <ReturningRandomNumbers>() .CacheMethod(c => c.CachedNumber()) .CacheMethod(c => c.CachedNumber2()) .As <IReturningRandomNumbers>(); var factoryTemp = CacheBuilder.BuildFactory(); factory = serializeAndDeserialize(factoryTemp); }
protected override void TestSetup() { eventListener = new StatisticsEventListener(); CacheBuilder .For <ObjectReturningNewGuids>() .CacheMethod(c => c.CachedMethod()) .As <IObjectReturningNewGuids>() .AddEventListener(eventListener); factory = CacheBuilder.BuildFactory(); }
public SearchService() { var searchBuilder = CacheBuilder.For <SearchDto>() .WithIdExtractor(i => i?.NumberPlate ?? "") .WithElementRetriever(i => FetchSerach(i)) .WithElementMaxAge(TimeSpan.FromMinutes(1)) .WithArrayMaxAge(TimeSpan.FromMinutes(1)); _serachCache = searchBuilder.ElementCache; _serachsCache = searchBuilder.GetArrayCache <string>(FetchSerachs); }
public void ShouldReturnCorrectTypeForClass() { CacheBuilder .For <ObjectWithNonInterfaceMethod>() .CacheMethod(c => c.ReturnsFour()) .AsImplemented(); var factory = CacheBuilder.BuildFactory(); factory.ImplementationTypeFor(typeof(ObjectWithNonInterfaceMethod)) .Should().Be.EqualTo(typeof(ObjectWithNonInterfaceMethod)); }
public async Task CacheAsync_GenericImplementation_UsesGenericCache() { var cache = CacheBuilder .For <User>(u => u.UseAsKey(KEY).Complete()) .Build(); await cache.CacheAsync(User.Test); CacheImplementationMock .Verify(i => i.CacheAsync(KEY, User.Test, It.IsAny <CacheOptions>()), Times.Once); }
public void ShouldReturnCorrectTypeForInterface() { CacheBuilder .For <ObjectReturningNewGuids>() .CacheMethod(c => c.CachedMethod()) .As <IObjectReturningNewGuids>(); var factory = CacheBuilder.BuildFactory(); factory.ImplementationTypeFor(typeof(IObjectReturningNewGuids)) .Should().Be.EqualTo(typeof(ObjectReturningNewGuids)); }
public void TypeWithNonVirtualMethodShouldWork() { var fluentBuilder = CacheBuilder .For <HasNonVirtualMethod>() .CacheMethod(m => m.Virtual()) .AsImplemented(); var instance = fluentBuilder.BuildFactory().Create <HasNonVirtualMethod>(); instance.Virtual().Should().Be.EqualTo(instance.Virtual()); instance.NonVirtual().Should().Not.Be.EqualTo(instance.NonVirtual()); }
public async Task TestArrayCacheReturnsCachedItemsOnSubsequentCallsAndHandlesRefresh() { var position = 0; var arrayCache = CacheBuilder.For <string>() .WithIdExtractor(v => v) .WithElementRetriever(v => v.InTask()) .GetArrayCache <string>(v => { // ReSharper disable AccessToModifiedClosure // ReSharper disable once ConvertToLambdaExpression return(new[] { $"{v}1:{position}", $"{v}2:{position}", $"{v}3:{position}" }.ToObservable()); // ReSharper restore AccessToModifiedClosure }); var id = Guid.NewGuid() .ToString(); var manualWaitHandle = new ManualResetEvent(false); var actualItemsTcs = new TaskCompletionSource <string[]>(); arrayCache.Get(id) .Do(v => manualWaitHandle.Set()) .Take(3) .ToArray() .Subscribe(v => { actualItemsTcs.SetResult(v.SelectMany(i => i) .ToArray()); }); manualWaitHandle.WaitOne(); manualWaitHandle.Reset(); position++; arrayCache.Refresh(id); manualWaitHandle.WaitOne(); manualWaitHandle.Reset(); position++; arrayCache.Refresh(id); manualWaitHandle.WaitOne(); manualWaitHandle.Reset(); var expectedItems = new[] { $"{id}1:0", $"{id}2:0", $"{id}3:0", $"{id}1:1", $"{id}2:1", $"{id}3:1", $"{id}1:2", $"{id}2:2", $"{id}3:2" }; var actualItems = await actualItemsTcs.Task.ConfigureAwait(false); Assert.Equal(expectedItems, actualItems); }
protected override void TestSetup() { eventListener = new EventListenerForTest(); CacheBuilder.AddEventListener(eventListener); CacheBuilder .For <ObjectWithParametersOnCachedMethod>() .CacheMethod(c => c.CachedMethod(null)) .As <IObjectWithParametersOnCachedMethod>(); factory = CacheBuilder.BuildFactory(); }
public void ShouldThrowIfUnknownType() { CacheBuilder .For <ObjectWithNonInterfaceMethod>() .CacheMethod(c => c.ReturnsFour()) .AsImplemented(); var factory = CacheBuilder.BuildFactory(); Assert.Throws <ArgumentException>(() => factory.ImplementationTypeFor(typeof(IObjectReturningNewGuids)) ); }
public void FactoryReturnsNewInterfaceInstances() { CacheBuilder .For <ObjectWithIdentifier>() .As <IObjectWithIdentifier>(); var factory = CacheBuilder.BuildFactory(); var obj1 = factory.Create <IObjectWithIdentifier>(); var obj2 = factory.Create <IObjectWithIdentifier>(); Assert.AreNotSame(obj1, obj2); Assert.AreNotEqual(obj1.Id, obj2.Id); }
protected override void TestSetup() { CacheBuilder.For <ReturningRandomNumbers>() .CacheMethod(c => c.CachedNumber()) .CacheMethod(c => c.CachedNumber2()) .As <IReturningRandomNumbers>() .SetLockObjectGenerator(new FixedNumberOfLockObjects(10)); var factoryTemp = CacheBuilder.BuildFactory(); factory = serializeAndDeserialize(factoryTemp); }
public void ShouldThrowIfMultiple() { const string key = "aklsdjf"; CacheBuilder.For <ObjectReturningNewGuids>(key) .CacheMethod(x => x.CachedMethod()) .As <IObjectReturningNewGuids>(); CacheBuilder.For <ObjectReturningNewGuidsNoInterface>(key) .CacheMethod(x => x.CachedMethod()) .AsImplemented(); Assert.Throws <InvalidOperationException>(() => CacheBuilder.BuildFactory()); }
public void OnlyDeclareTypeOnce() { CacheBuilder .For <ObjectReturningNewGuids>() .CacheMethod(c => c.CachedMethod()) .As <IObjectReturningNewGuids>(); Assert.Throws <ArgumentException>(() => CacheBuilder .For <ObjectReturningNewGuids>() .CacheMethod(c => c.CachedMethod2()) .As <IObjectReturningNewGuids>()); }
public async Task CacheAsync_SpecificImplementation_UsesSpecificCache() { var cache = CacheBuilder .For <User>(u => u.UseAsKey(KEY).Complete(_userSpecificImplementationMock.Object)) .Build(); await cache.CacheAsync(User.Test); _userSpecificImplementationMock .Verify(i => i.CacheAsync(KEY, User.Test, It.IsAny <CacheOptions>()), Times.Once); CacheImplementationMock .Verify(i => i.CacheAsync(It.IsAny <string>(), It.IsAny <User>(), It.IsAny <CacheOptions>()), Times.Never); }
public void VerifyCacheWhenLockObjectGeneratorIsUsed() { CacheBuilder .For <ObjectReturningNewGuids>() .CacheMethod(c => c.CachedMethod()) .As <IObjectReturningNewGuids>(); var factory = CacheBuilder.BuildFactory(); var value = factory.Create <IObjectReturningNewGuids>().CachedMethod(); Assert.AreEqual(value, factory.Create <IObjectReturningNewGuids>().CachedMethod()); }
public async Task TestRefreshCallsServerWhenSubscribed_SingleValueInArrayCache() { int callsToServer = 0; string valueFromServer = Guid.NewGuid() .ToString(); var arrayCache = CacheBuilder.For <string>() .WithIdExtractor(v => v) .WithElementRetriever(v => { throw new NotSupportedException("Element retriever not supported."); #pragma warning disable 162 return(Task.FromResult(v)); #pragma warning restore 162 }) .GetArrayCache <string>(v => { Interlocked.Increment(ref callsToServer); // ReSharper disable once AccessToModifiedClosure return(GenerateValues(3, valueFromServer + "|" + v) .ToObservable()); }); var id = Guid.NewGuid() .ToString(); string value = null; using (arrayCache.Get(id) .Subscribe(v => value = v.Join("#"), e => { }, () => { })) { await AssertEx.EqualAsync(GenerateValue(3, valueFromServer + "|" + id), () => value, TimeSpan.FromSeconds(5)) .ConfigureAwait(false); Assert.Equal(1, callsToServer); // Update value being returned by server and refresh valueFromServer = Guid.NewGuid() .ToString(); arrayCache.Refresh(id); // Server should be called and return new value and thus have been called twice await AssertEx.EqualAsync(GenerateValue(3, valueFromServer + "|" + id), () => value, TimeSpan.FromSeconds(5)) .ConfigureAwait(false); Assert.Equal(2, callsToServer); } }
public void ShouldNotAddToCache() { CacheBuilder .For <ObjectReturningNewGuids>() .CacheMethod(c => c.CachedMethod()) .As <IObjectReturningNewGuids>(); var factory = CacheBuilder.BuildFactory(); var svc = factory.Create <IObjectReturningNewGuids>(); svc.CachedMethod() .Should().Not.Be.EqualTo(svc.CachedMethod()); }
protected override void TestSetup() { CacheBuilder .For <ObjectReturningNewGuids>() .CacheMethod(c => c.CachedMethod()) .CacheMethod(c => c.CachedMethod2()) .As <IObjectReturningNewGuids>(); CacheBuilder .For <ObjectWithParametersOnCachedMethod>() .CacheMethod(c => c.CachedMethod(null)) .As <IObjectWithParametersOnCachedMethod>(); factory = CacheBuilder.BuildFactory(); }
public void NonVirtualNonCachedMethod() { var fluentBuilder = CacheBuilder .For <HasNonVirtualMethod>() .CacheMethod(m => m.Virtual()); if (ProxyFactory.AllowNonVirtualMember) { Assert.DoesNotThrow(() => fluentBuilder.AsImplemented()); } else { Assert.Throws <InvalidOperationException>(() => fluentBuilder.AsImplemented()); } }
public void ShouldNotAddToCacheUsingParameters() { CacheBuilder .For <ObjectWithParametersOnCachedMethod>() .CacheMethod(c => c.CachedMethod(null)) .As <IObjectWithParametersOnCachedMethod>(); var factory = CacheBuilder.BuildFactory(); var svc = factory.Create <IObjectWithParametersOnCachedMethod>(); var obj = new object(); svc.CachedMethod(obj) .Should().Not.Be.EqualTo(svc.CachedMethod(obj)); }
public CarService() { var carBuilder = CacheBuilder.For <CarDto>() .WithIdExtractor(i => i.NumberPlate) .WithElementRetriever(i => FetchCarFromServer(i)) .WithElementMaxAge(TimeSpan.FromMinutes(12)); var imageBuilder = CacheBuilder.For <ImageDto>() .WithIdExtractor(i => i.NumberPlate) .WithElementRetriever(i => FetchImageFromServer(i)) .WithElementMaxAge(TimeSpan.FromMinutes(12)); _imageCache = imageBuilder.ElementCache; _carCache = carBuilder.ElementCache; }
protected override void TestSetup() { eventListener = new EventListenerForTest(); CacheBuilder .For <ObjectReturningNull>() .CacheMethod(c => c.ReturnNullIfZero(0)) .As <IObjectReturningNull>() .AddEventListener(eventListener); var factory = CacheBuilder.BuildFactory(); var comp = factory.Create <IObjectReturningNull>(); comp.ReturnNullIfZero(0); comp.ReturnNullIfZero(1); }