Beispiel #1
0
        public void CachedLifetimeManagerReturnsDifferentObjectIfAbsoluteTimeExpired()
        {
            var cachedltm = new CachedLifetime()
                .ExpiresOn(DateTime.UtcNow.AddSeconds(2));

            var ireg = iocContainer.Register<IFoo>(c => new Foo1())
                .WithLifetimeManager(cachedltm);

            var result1 = iocContainer.Resolve<IFoo>();
            var result2 = iocContainer.Resolve<IFoo>();

            // simulate expiry
            Thread.Sleep(3000);

            var result3 = iocContainer.Resolve<IFoo>();

            Verify.That(result1).IsNotNull()
                        .IsTheSameObjectAs(result2)
                        .IsNotTheSameObjectAs(result3);
            Verify.That(result3).IsNotNull();
        }
Beispiel #2
0
        public void IsDependentOnTest()
        {
            using (var iocContainer = new IocContainer())
            {
                var executionDirectory = Environment.CurrentDirectory;
                // create a file for the cached item to be dependent on
                var filePath = executionDirectory + "\\DependencyFile.txt";

                if (File.Exists(filePath))
                    File.Delete(filePath);

                var dependencyFile = File.CreateText(filePath);

                dependencyFile.WriteLine("This is a file that the cache item is dependent on.");
                dependencyFile.Close();

                var cachedltm = new CachedLifetime()
                                    .IsDependentOn(new CacheDependency(filePath));

                iocContainer.Register<IFoo>(c => new Foo1())
                    .WithLifetimeManager(cachedltm);

                var result1 = iocContainer.Resolve<IFoo>();
                var result2 = iocContainer.Resolve<IFoo>();

                // change the dependency file
                dependencyFile = File.AppendText(filePath);
                dependencyFile.WriteLine("Modified dependecy file.");
                dependencyFile.Close();

                // need to give the system time to detect the change.
                Thread.Sleep(500);

                var result3 = iocContainer.Resolve<IFoo>();

                // cleanup
                if (File.Exists(filePath))
                    File.Delete(filePath);

                Verify.That(result3).IsNotNull();
                Verify.That(result1).IsNotNull()
                            .IsTheSameObjectAs(result2)
                            .IsNotTheSameObjectAs(result3);
            }
        }
Beispiel #3
0
        public void CanSetDefaultLifetimeManagerToCachedLifetime()
        {
            using (var iocContainer = new IocContainer())
            {
                var lifetime = new CachedLifetime();
                iocContainer.UsesDefaultLifetimeManagerOf(lifetime);

                Verify.That(iocContainer.DefaultLifetimeManager).IsTheSameObjectAs(lifetime);
            }
        }
Beispiel #4
0
        public void CallbackIsCalledWhenItemRemovedFromCache()
        {
            using (var iocContainer = new IocContainer())
            {
                var cachedltm = new CachedLifetime()
                    .ExpiresAfterNotAccessedFor(new TimeSpan(0, 0, 1))
                    .CallbackOnRemoval(RemovedCallback);

                iocContainer.Register<IFoo>(c => new Foo1())
                    .WithLifetimeManager(cachedltm);

                itemRemoved = false;
                var result1 = iocContainer.Resolve<IFoo>();

                // simulate expiry
                Thread.Sleep(2000);
                var result2 = iocContainer.Resolve<IFoo>();

                Verify.That(result1).IsNotNull();
                Verify.That(result2).IsNotNull()
                         .IsNotTheSameObjectAs(result1);

                Verify.That(itemRemoved).IsTrue();
            }
        }
Beispiel #5
0
        public void CachedLifetimeManagerReturnsSameObjectIfCacheNotExpired()
        {
            using (var iocContainer = new IocContainer())
            {
                var cachedltm = new CachedLifetime();

                iocContainer.Register<IFoo>(c => new Foo1())
                    .WithLifetimeManager(cachedltm);

                var result1 = iocContainer.Resolve<IFoo>();
                var result2 = iocContainer.Resolve<IFoo>();

                var result3 = iocContainer.Resolve<IFoo>();

                Verify.That(result1).IsNotNull()
                            .IsTheSameObjectAs(result2)
                            .IsTheSameObjectAs(result3);
            }
        }
Beispiel #6
0
        public void CachedLifetimeManagerReturnsDifferentObjectIfSlidingTimeExpired()
        {
            using (var iocContainer = new IocContainer())
            {
                var cachedltm = new CachedLifetime()
                    .ExpiresAfterNotAccessedFor(new TimeSpan(0, 0, 1));

                iocContainer.Register<IFoo>(c => new Foo1())
                    .WithLifetimeManager(cachedltm);

                var result1 = iocContainer.Resolve<IFoo>();
                var result2 = iocContainer.Resolve<IFoo>();

                // simulate expiry
                Thread.Sleep(2000);

                var result3 = iocContainer.Resolve<IFoo>();

                Verify.That(result1).IsNotNull()
                            .IsTheSameObjectAs(result2)
                            .IsNotTheSameObjectAs(result3);
                Verify.That(result3).IsNotNull();
            }
        }
Beispiel #7
0
        public void CachedLifetimeManagerReturnsDifferentObjectIfCacheExpired()
        {
            using (var iocContainer = new IocContainer())
            {
                var cachedltm = new CachedLifetime();

                var ireg = iocContainer.Register<IFoo>(c => new Foo1())
                    .WithLifetimeManager(cachedltm);

                var result1 = iocContainer.Resolve<IFoo>();
                var result2 = iocContainer.Resolve<IFoo>();

                // simulate expiry
                ireg.InvalidateInstanceCache();

                var result3 = iocContainer.Resolve<IFoo>();

                Verify.That(result1).IsNotNull()
                            .IsTheSameObjectAs(result2)
                            .IsNotTheSameObjectAs(result3);
                Verify.That(result3).IsNotNull();
            }
        }