public void ShouldOnlyAllowOneThreadToResolveEachType()
		{
			TypeToTypeMap sharedMap = new HashtableTypeMap(Hashtable.Synchronized(new Hashtable()));
			
			DynamicMock expectedToBeUsed = new DynamicMock(typeof(ImplementationResolver));
			expectedToBeUsed.ExpectAndReturn("ResolveImplementation", typeof(TestClass), typeof(TestInterface));
			DynamicMock notExpectedToBeUsed = new DynamicMock(typeof(ImplementationResolver));
			notExpectedToBeUsed.ExpectNoCall("ResolveImplementation", typeof(Type));

			StallingImplementationResolver stallingResolver = new StallingImplementationResolver((ImplementationResolver) expectedToBeUsed.MockInstance);
			ImplementationResolver resolvingResolver = new CachingImplementationResolver(
				stallingResolver, sharedMap);
			ImplementationResolver moochingResolver = new CachingImplementationResolver((ImplementationResolver) notExpectedToBeUsed.MockInstance, sharedMap);

			ImplementationResolverRunner resolvingRunner = new ImplementationResolverRunner(resolvingResolver, typeof(TestInterface));
			Thread resolvingThread = new Thread(
				new ThreadStart(resolvingRunner.runResolution));
			ImplementationResolverRunner moochingRunner = new ImplementationResolverRunner(moochingResolver, typeof(TestInterface));
			Thread moochingThread = new Thread(
				new ThreadStart(moochingRunner.runResolution));

			resolvingThread.Start();
			moochingThread.Start();
			Thread.Sleep(500); // allow moochingThread to catch up to resolvingThread
			stallingResolver.Resume();
			
			Assert.IsTrue(resolvingThread.Join(200), "Resolving thread did not complete before timeout.");
			Assert.IsTrue(moochingThread.Join(200), "Mooching thread did not complete before timeout.");
			
			expectedToBeUsed.Verify();
			notExpectedToBeUsed.Verify();

			Assert.AreEqual(typeof(TestClass), resolvingRunner.implementationType);
			Assert.AreEqual(typeof(TestClass), moochingRunner.implementationType);
		}
        public void ShouldOnlyAllowOneThreadToResolveEachType()
        {
            TypeToTypeMap sharedMap = new HashtableTypeMap(Hashtable.Synchronized(new Hashtable()));

            DynamicMock expectedToBeUsed = new DynamicMock(typeof(ImplementationResolver));

            expectedToBeUsed.ExpectAndReturn("ResolveImplementation", typeof(TestClass), typeof(TestInterface));
            DynamicMock notExpectedToBeUsed = new DynamicMock(typeof(ImplementationResolver));

            notExpectedToBeUsed.ExpectNoCall("ResolveImplementation", typeof(Type));

            StallingImplementationResolver stallingResolver  = new StallingImplementationResolver((ImplementationResolver)expectedToBeUsed.MockInstance);
            ImplementationResolver         resolvingResolver = new CachingImplementationResolver(
                stallingResolver, sharedMap);
            ImplementationResolver moochingResolver = new CachingImplementationResolver((ImplementationResolver)notExpectedToBeUsed.MockInstance, sharedMap);

            ImplementationResolverRunner resolvingRunner = new ImplementationResolverRunner(resolvingResolver, typeof(TestInterface));
            Thread resolvingThread = new Thread(
                new ThreadStart(resolvingRunner.runResolution));
            ImplementationResolverRunner moochingRunner = new ImplementationResolverRunner(moochingResolver, typeof(TestInterface));
            Thread moochingThread = new Thread(
                new ThreadStart(moochingRunner.runResolution));

            resolvingThread.Start();
            moochingThread.Start();
            Thread.Sleep(500);             // allow moochingThread to catch up to resolvingThread
            stallingResolver.Resume();

            Assert.IsTrue(resolvingThread.Join(200), "Resolving thread did not complete before timeout.");
            Assert.IsTrue(moochingThread.Join(200), "Mooching thread did not complete before timeout.");

            expectedToBeUsed.Verify();
            notExpectedToBeUsed.Verify();

            Assert.AreEqual(typeof(TestClass), resolvingRunner.implementationType);
            Assert.AreEqual(typeof(TestClass), moochingRunner.implementationType);
        }
Ejemplo n.º 3
0
        public void ShouldOnlyAllowOneThreadToResolveEachType()
        {
            TypeToTypeMap sharedMap = new HashtableTypeMap(Hashtable.Synchronized(new Hashtable()));

            var expectedToBeUsed = new Mock <ImplementationResolver>();

            expectedToBeUsed.Setup(resolver => resolver.ResolveImplementation(typeof(TestInterface))).Returns(typeof(TestClass)).Verifiable();
            var notExpectedToBeUsed = new Mock <ImplementationResolver>();

            StallingImplementationResolver stallingResolver  = new StallingImplementationResolver((ImplementationResolver)expectedToBeUsed.Object);
            ImplementationResolver         resolvingResolver = new CachingImplementationResolver(
                stallingResolver, sharedMap);
            ImplementationResolver moochingResolver = new CachingImplementationResolver((ImplementationResolver)notExpectedToBeUsed.Object, sharedMap);

            ImplementationResolverRunner resolvingRunner = new ImplementationResolverRunner(resolvingResolver, typeof(TestInterface));
            Thread resolvingThread = new Thread(
                new ThreadStart(resolvingRunner.runResolution));
            ImplementationResolverRunner moochingRunner = new ImplementationResolverRunner(moochingResolver, typeof(TestInterface));
            Thread moochingThread = new Thread(
                new ThreadStart(moochingRunner.runResolution));

            resolvingThread.Start();
            moochingThread.Start();
            Thread.Sleep(500);             // allow moochingThread to catch up to resolvingThread
            stallingResolver.Resume();

            Assert.IsTrue(resolvingThread.Join(200), "Resolving thread did not complete before timeout.");
            Assert.IsTrue(moochingThread.Join(200), "Mooching thread did not complete before timeout.");

            expectedToBeUsed.Verify();
            notExpectedToBeUsed.Verify();
            notExpectedToBeUsed.VerifyNoOtherCalls();

            Assert.AreEqual(typeof(TestClass), resolvingRunner.implementationType);
            Assert.AreEqual(typeof(TestClass), moochingRunner.implementationType);
        }