public void ContainerShouldUseFirstRegisteredDependencyOfTypeByDefault_EmailRegisteredFirst() { IKernel kernel = new DefaultKernel(); kernel.AddComponent("email", typeof(IAlarmSender), typeof(EmailSender)); kernel.AddComponent("sms", typeof(IAlarmSender), typeof(SmsSender)); kernel.AddComponent("generator", typeof(AlarmGenerator)); AlarmGenerator gen = (AlarmGenerator)kernel["generator"]; Assert.AreEqual(typeof(EmailSender), gen.Sender.GetType()); }
public void UsingPropertyWithPrivateSetter() { IKernel container = new DefaultKernel(); container.AddComponent("service1", typeof(IService1), typeof(Service1)); container.AddComponent("service2", typeof(IService2), typeof(Service2)); Service2 service2 = (Service2)container.Resolve<IService2>(); Assert.IsNull(service2.S,"Kernel should ignore private setter properties"); }
public void ComponentGraph() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new NamingPartsSubSystem()); kernel.AddComponent("common:key1=true", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("common:secure=true", typeof(ICommon), typeof(CommonImpl2)); GraphNode[] nodes = kernel.GraphNodes; Assert.IsNotNull(nodes); Assert.AreEqual(2, nodes.Length); }
public void ServiceLookup() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new NamingPartsSubSystem()); kernel.AddComponent("common:key1=true", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("common:secure=true", typeof(ICommon), typeof(CommonImpl2)); ICommon common = kernel[typeof(ICommon)] as ICommon; Assert.IsNotNull(common); Assert.AreEqual(typeof(CommonImpl1), common.GetType()); }
public void GetAssignableHandlers() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new NamingPartsSubSystem()); kernel.AddComponent("common:key1=true", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("common:secure=true", typeof(ICommon), typeof(CommonImpl2)); IHandler[] handlers = kernel.GetAssignableHandlers(typeof(ICommon)); Assert.IsNotNull(handlers); Assert.AreEqual(2, handlers.Length); }
public void ComponentGraph() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new NamingPartsSubSystem()); kernel.AddComponent("common:key1=true", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("common:secure=true", typeof(ICommon), typeof(CommonImpl2)); GraphNode[] nodes = kernel.GraphNodes; Assert.IsNotNull(nodes); Assert.AreEqual(2, nodes.Length); }
public void EmptyDelegateReturnsFirstTypeLoaded() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new KeySearchNamingSubSystem()); kernel.AddComponent("1.common", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("2.common", typeof(ICommon), typeof(CommonImpl2)); ICommon common = kernel[typeof(ICommon)] as ICommon; Assert.IsNotNull(common); Assert.AreEqual(typeof(CommonImpl1), common.GetType()); }
public void GetAssignableHandlers() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new NamingPartsSubSystem()); kernel.AddComponent("common:key1=true", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("common:secure=true", typeof(ICommon), typeof(CommonImpl2)); IHandler[] handlers = kernel.GetAssignableHandlers(typeof(ICommon)); Assert.IsNotNull(handlers); Assert.AreEqual(2, handlers.Length); }
public void ServiceLookup() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new NamingPartsSubSystem()); kernel.AddComponent("common:key1=true", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("common:secure=true", typeof(ICommon), typeof(CommonImpl2)); ICommon common = kernel[typeof(ICommon)] as ICommon; Assert.IsNotNull(common); Assert.AreEqual(typeof(CommonImpl1), common.GetType()); }
public void ReturnsFirstMatchingType() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new KeySearchNamingSubSystem(delegate(string key) { return key.StartsWith("1"); })); kernel.AddComponent("1.common", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("11.common", typeof(ICommon), typeof(CommonImpl2)); ICommon common = kernel[typeof(ICommon)] as ICommon; Assert.IsNotNull(common); Assert.AreEqual(typeof(CommonImpl1), common.GetType()); }
public void ReturnsCorrectType() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new KeySearchNamingSubSystem( delegate(string key) { return key.StartsWith("castlestronghold.com"); })); kernel.AddComponent("castleproject.org.common", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("castlestronghold.com.common", typeof(ICommon), typeof(CommonImpl2)); ICommon common = kernel[typeof(ICommon)] as ICommon; Assert.IsNotNull(common); Assert.AreEqual(typeof(CommonImpl2), common.GetType()); }
public void TransientMultipleConstructorNonValueTypeTest() { DefaultKernel container = new DefaultKernel(); container.AddComponent("FooBar", typeof(FooBarNonValue)); Tester1 bla1 = new Tester1("FOOBAR"); Tester2 bla2 = new Tester2(666); Hashtable arguments1 = new Hashtable(); arguments1.Add("test1", bla1); Hashtable arguments2 = new Hashtable(); arguments2.Add("test2", bla2); object a = container.Resolve(typeof(FooBarNonValue), arguments1); object b = container.Resolve(typeof(FooBarNonValue), arguments2); Assert.AreNotSame(a, b, "A should not be B"); // multi resolve test a = container.Resolve(typeof(FooBarNonValue), arguments1); b = container.Resolve(typeof(FooBarNonValue), arguments2); Assert.AreNotSame(a, b, "A should not be B"); }
public void MaxSize() { IKernel kernel = new DefaultKernel(); kernel.AddComponent("a", typeof(PoolableComponent1)); ArrayList instances = new ArrayList(); instances.Add(kernel["a"] as PoolableComponent1); instances.Add(kernel["a"] as PoolableComponent1); instances.Add(kernel["a"] as PoolableComponent1); instances.Add(kernel["a"] as PoolableComponent1); instances.Add(kernel["a"] as PoolableComponent1); PoolableComponent1 other1 = kernel["a"] as PoolableComponent1; Assert.IsNotNull(other1); Assert.IsTrue(!instances.Contains(other1)); foreach (object inst in instances) { kernel.ReleaseComponent(inst); } kernel.ReleaseComponent(other1); PoolableComponent1 other2 = kernel["a"] as PoolableComponent1; Assert.IsNotNull(other2); Assert.IsTrue(other1 != other2); Assert.IsTrue(instances.Contains(other2)); kernel.ReleaseComponent(other2); }
public void SimpleUsage() { IKernel kernel = new DefaultKernel(); kernel.AddComponent("a", typeof(PoolableComponent1)); PoolableComponent1 inst1 = kernel["a"] as PoolableComponent1; PoolableComponent1 inst2 = kernel["a"] as PoolableComponent1; Assert.IsNotNull(inst1); Assert.IsNotNull(inst2); kernel.ReleaseComponent(inst2); kernel.ReleaseComponent(inst1); PoolableComponent1 other1 = kernel["a"] as PoolableComponent1; PoolableComponent1 other2 = kernel["a"] as PoolableComponent1; Assert.IsNotNull(other1); Assert.IsNotNull(other2); Assert.AreSame(inst1, other1); Assert.AreSame(inst2, other2); kernel.ReleaseComponent(inst2); kernel.ReleaseComponent(inst1); }
public void SimpleUsage() { IKernel kernel = new DefaultKernel(); kernel.AddComponent("a", typeof(PoolableComponent1)); PoolableComponent1 inst1 = kernel["a"] as PoolableComponent1; PoolableComponent1 inst2 = kernel["a"] as PoolableComponent1; Assert.IsNotNull(inst1); Assert.IsNotNull(inst2); kernel.ReleaseComponent(inst2); kernel.ReleaseComponent(inst1); PoolableComponent1 other1 = kernel["a"] as PoolableComponent1; PoolableComponent1 other2 = kernel["a"] as PoolableComponent1; Assert.IsNotNull(other1); Assert.IsNotNull(other2); Assert.AreSame(inst1, other1); Assert.AreSame(inst2, other2); kernel.ReleaseComponent(inst2); kernel.ReleaseComponent(inst1); }
public void MaxSize() { IKernel kernel = new DefaultKernel(); kernel.AddComponent("a", typeof(PoolableComponent1)); ArrayList instances = new ArrayList(); instances.Add(kernel["a"] as PoolableComponent1); instances.Add(kernel["a"] as PoolableComponent1); instances.Add(kernel["a"] as PoolableComponent1); instances.Add(kernel["a"] as PoolableComponent1); instances.Add(kernel["a"] as PoolableComponent1); PoolableComponent1 other1 = kernel["a"] as PoolableComponent1; Assert.IsNotNull(other1); Assert.IsTrue(!instances.Contains(other1)); foreach(object inst in instances) { kernel.ReleaseComponent(inst); } kernel.ReleaseComponent(other1); PoolableComponent1 other2 = kernel["a"] as PoolableComponent1; Assert.IsNotNull(other2); Assert.IsTrue(other1 != other2); Assert.IsTrue(instances.Contains(other2)); kernel.ReleaseComponent(other2); }
public void Should_not_fail_when_constructor_parameter_and_public_property_with_private_setter_have_same_name() { IKernel kernel = new DefaultKernel(); kernel.AddComponent<Service2>(); kernel.AddComponent<Service1>(); try { Service2 svc = kernel.Resolve<Service2>(); Assert.IsNotNull(svc); } catch (NullReferenceException) { Assert.Fail("Should not have thrown a NullReferenceException"); } }
public void AddingComponentToRootKernelWhenChildKernelHasStartableFacility() { IKernel kernel = new DefaultKernel(); IKernel childKernel = new DefaultKernel(); kernel.AddChildKernel(childKernel); childKernel.AddFacility("StartableFacility", new StartableFacility()); kernel.AddComponent("string", typeof(String)); // exception here }
public void Public_property_with_Protected_setter_causes_Object_Reference_exception() { IKernel kernel = new DefaultKernel(); kernel.AddComponent<Presenter>(); kernel.AddComponent<View>(); try { Presenter p = (Presenter)kernel.Resolve(typeof(Presenter)); Assert.IsNotNull(p); } catch (NullReferenceException) { Assert.Fail("Should not have thrown a NullReferenceException"); } }
public void ParentKernelFindsAndCreateChildComponent() { IKernel subkernel = new DefaultKernel(); subkernel.AddComponent("templateengine", typeof(DefaultTemplateEngine)); kernel.AddChildKernel(subkernel); Assert.IsFalse(kernel.HasComponent(typeof(DefaultTemplateEngine))); object engine = kernel[typeof(DefaultTemplateEngine)]; }
public void ChildDependenciesIsSatisfiedEvenWhenComponentTakesLongToBeAddedToParentContainer() { DefaultKernel container = new DefaultKernel(); DefaultKernel childContainer = new DefaultKernel(); container.AddChildKernel(childContainer); childContainer.AddComponent("component", typeof(Component)); container.AddComponent("service1", typeof(IService), typeof(Service)); Component comp = (Component) childContainer[typeof(Component)]; }
private static IKernel InitializeContainer() { IKernel container = new DefaultKernel(); container.AddComponentInstance <AccountRepository>( typeof(IAccountRepository), new AccountRepository( Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "DinDin"))); container.AddComponent <AccountServices>(typeof(IAccountServices), LifestyleType.Singleton, true); return(container); }
public void LoadWindsorAssembly() { IKernel kernel = new DefaultKernel(); kernel.AddComponent("A", typeof(AssemblyResolverComponent)); AssemblyResolverComponent comp = (AssemblyResolverComponent)kernel["A"]; comp.Start(); Type windsor = Type.GetType( "Castle.Windsor.WindsorContainer, Castle.Windsor", false, false); Assert.IsNotNull(windsor); }
public void TransientMultiConstructorTest() { DefaultKernel container = new DefaultKernel(); container.AddComponent("FooBar", typeof(FooBar)); Hashtable arguments1 = new Hashtable(); arguments1.Add("integer", 1); Hashtable arguments2 = new Hashtable(); arguments2.Add("datetime", DateTime.Now.AddDays(1)); object a = container.Resolve(typeof(FooBar), arguments1); object b = container.Resolve(typeof(FooBar), arguments2); Assert.AreNotSame(a, b, "A should not be B"); }
public void KernelSerialization() { IKernel kernel = new DefaultKernel(); kernel.AddComponent("key", typeof(CustomerImpl)); Assert.IsTrue(kernel.HasComponent("key")); MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, kernel); stream.Position = 0; IKernel desKernel = (IKernel) formatter.Deserialize(stream); Assert.IsTrue(desKernel.HasComponent("key")); }
public void ChildDependenciesSatisfiedAmongContainers() { IKernel subkernel = new DefaultKernel(); kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine)); kernel.AddComponent("mailsender", typeof(DefaultMailSenderService)); kernel.AddChildKernel(subkernel); subkernel.AddComponent("spamservice", typeof(DefaultSpamService)); DefaultSpamService spamservice = (DefaultSpamService) subkernel["spamservice"]; Assert.IsNotNull(spamservice); Assert.IsNotNull(spamservice.MailSender); Assert.IsNotNull(spamservice.TemplateEngine); }
public void DependenciesSatisfiedAmongContainers() { IKernel subkernel = new DefaultKernel(); kernel.AddComponent("mailsender", typeof(DefaultMailSenderService)); kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine)); kernel.AddChildKernel(subkernel); subkernel.AddComponent("spamservice", typeof(DefaultSpamService)); DefaultSpamService spamservice = (DefaultSpamService)subkernel["spamservice"]; Assert.IsNotNull(spamservice); Assert.IsNotNull(spamservice.MailSender); Assert.IsNotNull(spamservice.TemplateEngine); }
public void TestInterfaceBasedStartable() { IKernel kernel = new DefaultKernel(); kernel.AddFacility("startable", new StartableFacility()); kernel.AddComponent("a", typeof(StartableComponent)); StartableComponent component = kernel["a"] as StartableComponent; Assert.IsNotNull(component); Assert.IsTrue(component.Started); Assert.IsFalse(component.Stopped); kernel.ReleaseComponent(component); Assert.IsTrue(component.Stopped); }
public void WillAskResolverWhenTryingToResolveDependencyAfterAnotherHandlerWasRegistered() { FooBarResolver resolver = new FooBarResolver(); IKernel kernel = new DefaultKernel(); kernel.Resolver.AddSubResolver(resolver); kernel.AddComponent("foo", typeof(Foo)); IHandler handler = kernel.GetHandler("foo"); Assert.AreEqual(HandlerState.WaitingDependency, handler.CurrentState); resolver.Result = 15; kernel.RaiseHandlerRegistered(null);//should force reevaluation of state Assert.AreEqual(HandlerState.Valid, handler.CurrentState); }
public void SameLevelDependenciesSatisfied() { IKernel child = new DefaultKernel(); kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine)); kernel.AddComponent("spamservice", typeof(DefaultSpamService)); kernel.AddChildKernel(child); child.AddComponent("mailsender", typeof(DefaultMailSenderService)); DefaultSpamService spamservice = (DefaultSpamService) child["spamservice"]; Assert.IsNotNull(spamservice); Assert.IsNotNull(spamservice.MailSender); Assert.IsNotNull(spamservice.TemplateEngine); }
public void KernelSerialization() { IKernel kernel = new DefaultKernel(); kernel.AddComponent("key", typeof(CustomerImpl)); Assert.IsTrue(kernel.HasComponent("key")); MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, kernel); stream.Position = 0; IKernel desKernel = (IKernel)formatter.Deserialize(stream); Assert.IsTrue(desKernel.HasComponent("key")); }
public void When_attemting_to_resolve_component_with_nonpublic_ctor_should_throw_meaningfull_exception() { IKernel kernel = new DefaultKernel(); kernel.AddComponent<WithNonPublicCtor>(); bool exceptionThrown = false; try { WithNonPublicCtor svc = kernel.Resolve<WithNonPublicCtor>(); Assert.IsNotNull(svc); } catch (ComponentActivatorException exception) { ComponentActivatorException inner = exception.InnerException as ComponentActivatorException; Assert.IsNotNull(inner); StringAssert.Contains("public", inner.Message, "Exception should say that constructor has to be public."); exceptionThrown = true; } Assert.IsTrue(exceptionThrown, "A {0} should have been thrown.", typeof(ComponentActivatorException).Name); }
public void TestComponentWithNoInterface() { IKernel kernel = new DefaultKernel(); MutableConfiguration compNode = new MutableConfiguration("component"); compNode.Attributes["id"] = "b"; compNode.Attributes["startable"] = "true"; compNode.Attributes["startMethod"] = "Start"; compNode.Attributes["stopMethod"] = "Stop"; kernel.ConfigurationStore.AddComponentConfiguration("b", compNode); kernel.AddFacility("startable", new StartableFacility()); kernel.AddComponent("b", typeof(NoInterfaceStartableComponent)); NoInterfaceStartableComponent component = kernel["b"] as NoInterfaceStartableComponent; Assert.IsNotNull(component); Assert.IsTrue(component.Started); Assert.IsFalse(component.Stopped); kernel.ReleaseComponent(component); Assert.IsTrue(component.Stopped); }
public void MultiThreaded_RemoveResolve_Throws_When_LargeRatio_Of_ComponentsToService() { int threadCount = 1000; ArrayList list = ArrayList.Synchronized(new ArrayList()); Random rand = new Random(); ManualResetEvent waitEvent = new ManualResetEvent(false); IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new KeySearchNamingSubSystem(delegate { return false; })); kernel.AddComponent("common", typeof(ICommon), typeof(CommonImpl1)); WaitCallback resolveThread = delegate { waitEvent.WaitOne(); while(threadCount > 0 && list.Count == 0) { try { ICommon common = kernel[typeof(ICommon)] as ICommon; } catch(Exception e) { list.Add(e); } } }; ThreadPool.QueueUserWorkItem(resolveThread); WaitCallback removeThread = delegate { waitEvent.WaitOne(); kernel.RemoveComponent(threadCount + ".common"); Interlocked.Decrement(ref threadCount); }; for(int i = 0; i < threadCount; i++) { kernel.AddComponent(i + ".common", typeof(ICommon), typeof(CommonImpl1)); ThreadPool.QueueUserWorkItem(removeThread); } waitEvent.Set(); while(threadCount > 0 && list.Count == 0) { Thread.Sleep(15); } if (list.Count > 0) { throw (Exception) list[0]; } }
public void UseChildComponentsForParentDependenciesWhenRequestedFromChild() { IKernel subkernel = new DefaultKernel(); kernel.AddComponent("spamservice", typeof(DefaultSpamService), LifestyleType.Transient); kernel.AddComponent("mailsender", typeof(DefaultMailSenderService)); kernel.AddComponent("templateengine", typeof(DefaultTemplateEngine)); kernel.AddChildKernel(subkernel); subkernel.AddComponent("templateengine", typeof(DefaultTemplateEngine)); DefaultTemplateEngine templateengine = (DefaultTemplateEngine) kernel["templateengine"]; DefaultTemplateEngine sub_templateengine = (DefaultTemplateEngine) subkernel["templateengine"]; DefaultSpamService spamservice = (DefaultSpamService) subkernel["spamservice"]; Assert.AreNotEqual(spamservice.TemplateEngine, templateengine); Assert.AreEqual(spamservice.TemplateEngine, sub_templateengine); spamservice = (DefaultSpamService) kernel["spamservice"]; Assert.AreNotEqual(spamservice.TemplateEngine, sub_templateengine); Assert.AreEqual(spamservice.TemplateEngine, templateengine); kernel.RemoveComponent("templateengine"); spamservice = (DefaultSpamService) kernel["spamservice"]; Assert.IsNull(spamservice.TemplateEngine); }
public void AddingDependencyToServiceWithCustomDependency() { DefaultKernel k = new DefaultKernel(); k.AddComponent("NeedClassWithCustomerDependency",typeof(NeedClassWithCustomerDependency)); k.AddComponent("HasCustomDependency", typeof(HasCustomDependency)); Assert.AreEqual(HandlerState.WaitingDependency, k.GetHandler("HasCustomDependency").CurrentState); Hashtable hash = new Hashtable(); hash["name"] = new CompA(); k.RegisterCustomDependencies("HasCustomDependency", hash); Assert.AreEqual(HandlerState.Valid, k.GetHandler("HasCustomDependency").CurrentState); Assert.IsNotNull(k.Resolve(typeof(NeedClassWithCustomerDependency))); }
public void ParentKernelFindsAndCreateChildComponent() { IKernel subkernel = new DefaultKernel(); subkernel.AddComponent("templateengine", typeof(DefaultTemplateEngine)); kernel.AddChildKernel(subkernel); Assert.IsFalse(kernel.HasComponent(typeof(DefaultTemplateEngine))); object engine = kernel[typeof(DefaultTemplateEngine)]; }
public void Singleton_WithNonSingletonDependencies_DoesNotReResolveDependencies() { kernel.AddComponent("spamservice", typeof(DefaultSpamService)); kernel.AddComponent("mailsender", typeof(DefaultMailSenderService)); IKernel subkernel1 = new DefaultKernel(); subkernel1.AddComponent("templateengine", typeof(DefaultTemplateEngine)); kernel.AddChildKernel(subkernel1); IKernel subkernel2 = new DefaultKernel(); subkernel2.AddComponent("templateengine", typeof(DefaultTemplateEngine), LifestyleType.Transient); kernel.AddChildKernel(subkernel2); DefaultTemplateEngine templateengine1 = (DefaultTemplateEngine) subkernel1["templateengine"]; DefaultSpamService spamservice1 = (DefaultSpamService) subkernel1["spamservice"]; Assert.IsNotNull(spamservice1); Assert.AreEqual(spamservice1.TemplateEngine.Key, templateengine1.Key); DefaultTemplateEngine templateengine2 = (DefaultTemplateEngine) subkernel2["templateengine"]; DefaultSpamService spamservice2 = (DefaultSpamService) subkernel2["spamservice"]; Assert.IsNotNull(spamservice2); Assert.AreEqual(spamservice1, spamservice2); Assert.AreEqual(spamservice1.TemplateEngine.Key, templateengine1.Key); Assert.AreNotEqual(spamservice2.TemplateEngine.Key, templateengine2.Key); }
public void ComponentUnregistersProperly() { IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new KeySearchNamingSubSystem(delegate(string key) { return key.StartsWith("2"); })); kernel.AddComponent("1.common", typeof(ICommon), typeof(CommonImpl1)); kernel.AddComponent("2.common", typeof(ICommon), typeof(CommonImpl2)); ICommon common = kernel[typeof(ICommon)] as ICommon; Assert.IsNotNull(common); Assert.AreEqual(typeof(CommonImpl2), common.GetType()); kernel.RemoveComponent("2.common"); common = kernel[typeof(ICommon)] as ICommon; Assert.IsNotNull(common); Assert.AreEqual(typeof(CommonImpl1), common.GetType()); kernel.RemoveComponent("1.common"); Assert.AreEqual(0, kernel.GetHandlers(typeof(ICommon)).Length); }
public void MultiThreadedAddResolve([Values(100)] int threadCount) { object locker = new object(); IList<string> list = new List<string>(); ManualResetEvent waitEvent = new ManualResetEvent(false); IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new KeySearchNamingSubSystem()); kernel.AddComponent("common", typeof(ICommon), typeof(CommonImpl1)); WaitCallback resolveThread = delegate { waitEvent.WaitOne(); while(threadCount > 0 && list.Count == 0) { try { ICommon common = kernel[typeof(ICommon)] as ICommon; } catch(Exception e) { lock (locker) { list.Add(e.ToString()); } } } }; ThreadPool.QueueUserWorkItem(resolveThread); WaitCallback addThread = delegate { waitEvent.WaitOne(); kernel.AddComponent(Guid.NewGuid() + ".common", typeof(ICommon), typeof(CommonImpl1)); Interlocked.Decrement(ref threadCount); }; for(int i = 0; i < threadCount; i++) { ThreadPool.QueueUserWorkItem(addThread); } waitEvent.Set(); while(threadCount > 0 && list.Count == 0) { Thread.Sleep(15); } if (list.Count > 0) { Assert.Fail(list[0].ToString()); } }
public void MultiThreadedAddResolve() { int threadCount = 100; ArrayList list = ArrayList.Synchronized(new ArrayList()); Random rand = new Random(); ManualResetEvent waitEvent = new ManualResetEvent(false); IKernel kernel = new DefaultKernel(); kernel.AddSubSystem(SubSystemConstants.NamingKey, new KeySearchNamingSubSystem()); kernel.AddComponent("common", typeof(ICommon), typeof(CommonImpl1)); WaitCallback resolveThread = delegate { waitEvent.WaitOne(); while(threadCount > 0 && list.Count == 0) { try { ICommon common = kernel[typeof(ICommon)] as ICommon; } catch(Exception e) { list.Add(e.ToString()); } } }; ThreadPool.QueueUserWorkItem(resolveThread); WaitCallback addThread = delegate { waitEvent.WaitOne(); kernel.AddComponent(rand.Next() + ".common", typeof(ICommon), typeof(CommonImpl1)); Interlocked.Decrement(ref threadCount); }; for(int i = 0; i < threadCount; i++) { ThreadPool.QueueUserWorkItem(addThread); } waitEvent.Set(); while(threadCount > 0 && list.Count == 0) { Thread.Sleep(15); } if (list.Count > 0) { Assert.Fail(list[0].ToString()); } }