示例#1
0
        public void OncePerThreadFactoryShouldCreateTheSameInstanceFromWithinTheSameThread()
        {
            IFactory<ISerializable> localFactory = new OncePerThreadFactory<ISerializable>(createInstance);

            ISerializable first = localFactory.CreateInstance(null);
            ISerializable second = localFactory.CreateInstance(null);

            // The two instances should be the same
            // since they were created from the same thread
            Assert.IsNotNull(first);
            Assert.AreSame(first, second);
        }
示例#2
0
        public void OncePerThreadFactoryShouldCreateTheSameInstanceFromWithinTheSameThread()
        {
            IFactory <ISerializable> localFactory = new OncePerThreadFactory <ISerializable>(createInstance);

            ISerializable first  = localFactory.CreateInstance(null);
            ISerializable second = localFactory.CreateInstance(null);

            // The two instances should be the same
            // since they were created from the same thread
            Assert.IsNotNull(first);
            Assert.AreSame(first, second);
        }
示例#3
0
        public void OncePerThreadFactoryShouldCreateUniqueInstancesFromDifferentThreads()
        {
            IFactory <ISerializable> localFactory = new OncePerThreadFactory <ISerializable>(createInstance);
            var resultList = new List <ISerializable>();

            Action <IFactory <ISerializable> > doCreate = factory =>
            {
                ISerializable instance      = factory.CreateInstance(null);
                ISerializable otherInstance = factory.CreateInstance(null);

                // The two instances
                // within the same thread must match
                Assert.AreSame(instance, otherInstance);
                lock (resultList)
                {
                    resultList.Add(instance);
                }
            };


            // Create the instance in another thread
            IAsyncResult  asyncResult   = doCreate.BeginInvoke(localFactory, null, null);
            ISerializable localInstance = localFactory.CreateInstance(null);

            // Wait for the previous thread
            // to finish executing
            doCreate.EndInvoke(asyncResult);

            Assert.IsTrue(resultList.Count > 0);

            // Collect the results from the other thread
            ISerializable instanceFromOtherThread = resultList[0];

            Assert.IsNotNull(localInstance);
            Assert.IsNotNull(instanceFromOtherThread);
            Assert.AreNotSame(localInstance, instanceFromOtherThread);
        }
示例#4
0
        public void OncePerThreadFactoryShouldCreateUniqueInstancesFromDifferentThreads()
        {
            IFactory<ISerializable> localFactory = new OncePerThreadFactory<ISerializable>(createInstance);
            var resultList = new List<ISerializable>();

            Action<IFactory<ISerializable>> doCreate = factory =>
                                                           {
                                                               ISerializable instance = factory.CreateInstance(null);
                                                               ISerializable otherInstance = factory.CreateInstance(null);

                                                               // The two instances
                                                               // within the same thread must match
                                                               Assert.AreSame(instance, otherInstance);
                                                               lock (resultList)
                                                               {
                                                                   resultList.Add(instance);
                                                               }
                                                           };

            // Create the instance in another thread
            IAsyncResult asyncResult = doCreate.BeginInvoke(localFactory, null, null);
            ISerializable localInstance = localFactory.CreateInstance(null);

            // Wait for the previous thread
            // to finish executing
            doCreate.EndInvoke(asyncResult);

            Assert.IsTrue(resultList.Count > 0);

            // Collect the results from the other thread
            ISerializable instanceFromOtherThread = resultList[0];

            Assert.IsNotNull(localInstance);
            Assert.IsNotNull(instanceFromOtherThread);
            Assert.AreNotSame(localInstance, instanceFromOtherThread);
        }