Example #1
0
        private static void VerifyServiceIsCached <T>(
            T service,
            CachingDependencyResolver cachingResolver,
            Mock <IDbDependencyResolver> mockResolver,
            string name)
        {
            mockResolver.Setup(m => m.GetService(typeof(T), name)).Returns(service);

            Assert.Same(service, cachingResolver.GetService <T>(name));
            mockResolver.Verify(m => m.GetService(typeof(T), name), Times.Once());
            Assert.Same(service, cachingResolver.GetService <T>(name));
            mockResolver.Verify(m => m.GetService(typeof(T), name), Times.Once()); // Underlying resolver not called again
        }
Example #2
0
        public void GetService_can_be_accessed_from_multiple_threads_concurrently()
        {
            for (var i = 0; i < 30; i++)
            {
                var bag      = new ConcurrentBag <IPilkington>();
                var karl     = new Mock <IPilkington>().Object;
                var resolver = new CachingDependencyResolver(new SingletonDependencyResolver <IPilkington>(karl));

                ExecuteInParallel(() => bag.Add(resolver.GetService <IPilkington>()));

                Assert.Equal(20, bag.Count);
                Assert.True(bag.All(c => karl == c));
            }
        }