コード例 #1
0
 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();
 }
コード例 #2
0
        protected override void TestSetup()
        {
            CacheBuilder
            .For <ObjectReturningNewGuids>()
            .CacheMethod(c => c.CachedMethod())
            .PerInstance()
            .As <IObjectReturningNewGuids>();

            factory = CacheBuilder.BuildFactory();
        }
コード例 #3
0
        protected override void TestSetup()
        {
            CacheBuilder.For <ObjectTakes100MsToFill>()
            .CacheMethod(c => c.Execute(0))
            .AsImplemented();

            var factory = CacheBuilder.BuildFactory();

            instance = factory.Create <ObjectTakes100MsToFill>();
        }
コード例 #4
0
        protected override void TestSetup()
        {
            CacheBuilder
            .For <ObjectWithMutableList>()
            .CacheMethod(c => c.GetListContents())
            .AsImplemented();

            factory  = CacheBuilder.BuildFactory();
            instance = factory.Create <ObjectWithMutableList>();
        }
コード例 #5
0
        protected override void TestSetup()
        {
            CacheBuilder.For <ReturningRandomNumbers>()
            .CacheMethod(c => c.CachedNumber())
            .As <IReturningRandomNumbers>();

            var factory = CacheBuilder.BuildFactory();

            instance = factory.Create <IReturningRandomNumbers>();
        }
コード例 #6
0
        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());
        }
コード例 #7
0
ファイル: CacheBuilderTest.cs プロジェクト: KBoudich/mbcache
        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());
        }
コード例 #8
0
        protected override void TestSetup()
        {
            CacheBuilder.For <ReturningRandomNumbers>()
            .CacheMethod(c => c.CachedNumber())
            .CacheMethod(c => c.CachedNumber2())
            .As <IReturningRandomNumbers>();

            var factoryTemp = CacheBuilder.BuildFactory();

            factory = serializeAndDeserialize(factoryTemp);
        }
コード例 #9
0
        protected override void TestSetup()
        {
            eventListener = new StatisticsEventListener();
            CacheBuilder
            .For <ObjectReturningNewGuids>()
            .CacheMethod(c => c.CachedMethod())
            .As <IObjectReturningNewGuids>()
            .AddEventListener(eventListener);

            factory = CacheBuilder.BuildFactory();
        }
コード例 #10
0
ファイル: SearchService.cs プロジェクト: MMonrad/Demo2
        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);
        }
コード例 #11
0
        public void ShouldReturnCorrectTypeForClass()
        {
            CacheBuilder
            .For <ObjectWithNonInterfaceMethod>()
            .CacheMethod(c => c.ReturnsFour())
            .AsImplemented();
            var factory = CacheBuilder.BuildFactory();

            factory.ImplementationTypeFor(typeof(ObjectWithNonInterfaceMethod))
            .Should().Be.EqualTo(typeof(ObjectWithNonInterfaceMethod));
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        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));
        }
コード例 #14
0
ファイル: NonVirtualTest.cs プロジェクト: jesuissur/mbcache
        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());
        }
コード例 #15
0
        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);
        }
コード例 #16
0
ファイル: CacheKeyTest.cs プロジェクト: jesuissur/mbcache
        protected override void TestSetup()
        {
            eventListener = new EventListenerForTest();
            CacheBuilder.AddEventListener(eventListener);

            CacheBuilder
            .For <ObjectWithParametersOnCachedMethod>()
            .CacheMethod(c => c.CachedMethod(null))
            .As <IObjectWithParametersOnCachedMethod>();

            factory = CacheBuilder.BuildFactory();
        }
コード例 #17
0
        public void ShouldThrowIfUnknownType()
        {
            CacheBuilder
            .For <ObjectWithNonInterfaceMethod>()
            .CacheMethod(c => c.ReturnsFour())
            .AsImplemented();
            var factory = CacheBuilder.BuildFactory();

            Assert.Throws <ArgumentException>(() =>
                                              factory.ImplementationTypeFor(typeof(IObjectReturningNewGuids))
                                              );
        }
コード例 #18
0
ファイル: CacheBuilderTest.cs プロジェクト: KBoudich/mbcache
        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);
        }
コード例 #19
0
        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);
        }
コード例 #20
0
        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());
        }
コード例 #21
0
ファイル: CacheBuilderTest.cs プロジェクト: KBoudich/mbcache
 public void OnlyDeclareTypeOnce()
 {
     CacheBuilder
     .For <ObjectReturningNewGuids>()
     .CacheMethod(c => c.CachedMethod())
     .As <IObjectReturningNewGuids>();
     Assert.Throws <ArgumentException>(()
                                       =>
                                       CacheBuilder
                                       .For <ObjectReturningNewGuids>()
                                       .CacheMethod(c => c.CachedMethod2())
                                       .As <IObjectReturningNewGuids>());
 }
コード例 #22
0
        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);
        }
コード例 #23
0
        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);
            }
        }
コード例 #25
0
        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());
        }
コード例 #26
0
        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();
        }
コード例 #27
0
        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());
            }
        }
コード例 #28
0
        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));
        }
コード例 #29
0
        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;
        }
コード例 #30
0
        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);
        }