示例#1
0
        public void OneThreadSimpleMethodTest()
        {
            Func <float> simple = () => 0.1f;
            var          lazy   = LazyFactory <float> .CreateOneThreading(simple);

            Assert.AreEqual(0.1f, lazy.Get());
        }
示例#2
0
        public void RaceConditionsCheck()
        {
            var       count        = 0;
            var       lazy         = LazyFactory.CreateMultiThreadedLazy(() => Interlocked.Increment(ref count));
            const int threadsCount = 100;
            var       threads      = new Thread[threadsCount];

            for (int i = 0; i < threads.Length; ++i)
            {
                threads[i] = new Thread(() => {
                    lazy.Get();
                });
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }
            foreach (var thread in threads)
            {
                thread.Join();
            }

            Assert.AreEqual(1, count);
        }
        public void CheckThatLazyStoresPassedFunction()
        {
            var simpleLazy = LazyFactory.CreateMultithreadLazy(() => { return(42); });
            var lazyResult = simpleLazy.Get();

            Assert.AreEqual(42, lazyResult);
        }       /// <summary>
示例#4
0
        public void GetTest()
        {
            var testString = "test";
            var lazy       = LazyFactory <string> .CreateLazyMultitreaded(() => testString);

            Assert.AreEqual(testString, lazy.Get());
        }
示例#5
0
        public void ValueIsNotCalculatedMoreThanOnceTest()
        {
            int count = 0;
            var lazy  = LazyFactory <int> .CreateLazyMultitreaded(() => ++ count);

            const int amountOfThreads = 10;
            var       threads         = new List <Thread>();

            for (int i = 0; i < amountOfThreads; ++i)
            {
                threads.Add(new Thread(() =>
                {
                    const int amountOfCalls = 10;
                    for (int j = 0; j < amountOfCalls; ++j)
                    {
                        Assert.AreEqual(1, lazy.Get());
                    }
                }));
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }
        }
示例#6
0
        public void GetTest_CanSupplierReturnNullByGet()
        {
            Func <string> supplierNull = () => null;
            var           lazy         = LazyFactory.SingleThreadLazy(supplierNull);

            Assert.IsNull(lazy.Get());
        }
        public void CalculateMultiThreadTest()
        {
            var x    = 4;
            var lazy = LazyFactory.CreateMultiThreadLazy <int>(() => x * x);

            Assert.AreEqual(16, lazy.Get());
        }
示例#8
0
        public void ManyThreadsTest()
        {
            var lazy = LazyFactory <Thread> .CreateConcurrentLazy(() => Thread.CurrentThread);

            const int numberOfThreads = 10;
            var       threads         = new Thread[numberOfThreads];
            var       results         = new Thread[numberOfThreads];

            for (int i = 0; i < numberOfThreads; ++i)
            {
                var localI = i;
                threads[localI] = new Thread(() => results[localI] = lazy.Get());
                threads[localI].Start();
            }

            for (int i = 0; i < numberOfThreads; ++i)
            {
                threads[i].Join();
            }

            for (int i = 0; i < numberOfThreads; ++i)
            {
                Assert.AreSame(results[0], results[i]);
            }
        }
        public void GetReturnSameObject()
        {
            var lazyObject = LazyFactory <object> .CreateMymultithreadedLazy(() => new object());

            var threadMass = new List <Thread>();

            var results = new object[5];

            for (var i = 0; i < 5; ++i)
            {
                var    local     = i;
                Thread devThread = new Thread(() => results[local] = lazyObject.Get());
                threadMass.Add(devThread);
                threadMass[i].Start();
            }

            foreach (var thread in threadMass)
            {
                thread.Join();
            }

            for (var i = 0; i < results.Length - 1; ++i)
            {
                Assert.IsTrue(results[i] == results[i + 1]);
            }
        }
示例#10
0
        public void TestBinpow()
        {
            int Binpow()
            {
                int a = 3;
                int p = 8;

                int res = 1;

                while (p != 0)
                {
                    if (p % 2 == 1)
                    {
                        res *= a;
                    }

                    a *= a;

                    p /= 2;
                }

                return(res);
            }

            SimpleLazy <int> binpowLazy = LazyFactory.CreateSimpleLazy(Binpow);

            Assert.AreEqual(Binpow(), binpowLazy.Get);
        }
示例#11
0
        public void ThreadTest()
        {
            string HelloFunc() => "Hello";

            const int         n         = 10;
            SafeLazy <string> helloLazy = LazyFactory.CreateSafeLazy(HelloFunc);

            string[] resStrings = new string[n];

            Thread[] threads = new Thread[n];

            for (int i = 0; i < n; ++i)
            {
                int k = i;
                threads[i] = new Thread(() => { resStrings[k] = helloLazy.Get; });
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }

            for (int i = 0; i < n; ++i)
            {
                Assert.AreEqual(HelloFunc(), resStrings[i]);
            }
        }
示例#12
0
        public void OneThreadExceptionTest()
        {
            Func <string> exception = () => throw new ArgumentException("this method throws an exception");
            var           lazy      = LazyFactory <string> .CreateOneThreading(exception);

            Assert.ThrowsException <ArgumentException>(lazy.Get);
        }
示例#13
0
        public void OneThreadNullTest()
        {
            Func <string> empty = () => null;
            var           lazy  = LazyFactory <string> .CreateOneThreading(empty);

            Assert.IsNull(lazy.Get());
        }
示例#14
0
        public void MultiThreadArrayTest()
        {
            Func <int[]> makeArray = () =>
            {
                var array = new int[3];
                array[0] = 10;
                return(array);
            };
            var lazyMultiThread = LazyFactory <int[]> .CreateMultiThreading(makeArray);

            var threads = new Thread[3];
            var results = new int[3][];

            for (var i = 0; i < 3; i++)
            {
                threads[i] = new Thread(() =>
                {
                    results[i] = lazyMultiThread.Get();
                });
            }

            StartThreads(threads);

            Assert.AreEqual(results[0], results[1]);
            Assert.AreEqual(results[1], results[2]);
        }
示例#15
0
        public void MultithreadedTest()
        {
            var        value = 0;
            Func <int> func  = () => -- value;
            var        lazy  = LazyFactory.CreateThreadSafeLazy(func);

            var threads = new Thread[Environment.ProcessorCount];
            var results = new int[threads.Length * 2];

            for (var i = 0; i < threads.Length; ++i)
            {
                var localI = i;
                threads[i] = new Thread(() =>
                {
                    results[localI * 2]     = lazy.Get();
                    results[localI * 2 + 1] = lazy.Get();
                });
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }
            foreach (var thread in threads)
            {
                thread.Join();
            }

            foreach (var result in results)
            {
                Assert.AreEqual(-1, result);
            }
        }
示例#16
0
        public void SingleThreadedLazyShouldReturnNull()
        {
            var func = new Func <object>(() => { return(null); });
            var lazy = LazyFactory.CreateSingleThreadedLazy(func);

            Assert.IsNull(lazy.Get());
        }
        public void SeveralThreadsResultIsCalculatedOnlyOnceTest()
        {
            int count = 0;

            lazy = LazyFactory <int> .CreateMultithreadedLazy(() => ++ count);

            threads = new Thread[4];
            for (var i = 0; i < threads.Length; ++i)
            {
                threads[i] = new Thread(() =>
                {
                    Assert.AreEqual(1, lazy.Get());
                    Assert.AreEqual(1, lazy.Get());
                    Assert.AreEqual(1, lazy.Get());
                });
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }
        }
        public void Setup()
        {
            lazyGiantMatrix = LazyFactory <GiantMatrix>
                              .CreateConcurrentLazy(() => new GiantMatrix(1000, 1000));

            resetEvent = new ManualResetEvent(false);
        }
        public ConstructorMap(ConstructorInfo ctor, IEnumerable <ConstructorParameterMap> ctorParams)
        {
            Ctor       = ctor;
            CtorParams = ctorParams;

            _runtimeCtor = LazyFactory.Create(() => DelegateFactory.CreateCtor(ctor, CtorParams));
        }
        public void MultiThreadedLazyShouldReturnCorrectValue()
        {
            var       func       = new Func <int>(() => { return(1); });
            var       lazy       = LazyFactory.CreateMultiThreadedLazy(func);
            var       threads    = new Thread[100];
            const int trueResult = 1;

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(() =>
                {
                    Assert.AreEqual(trueResult, lazy.Get());
                    Assert.AreEqual(trueResult, lazy.Get());
                });
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }
            foreach (var thread in threads)
            {
                thread.Join();
            }
        }
        public void CorrectValueTest()
        {
            var lazy = LazyFactory <int> .CreateSimpleLazy(() => 345);

            Assert.AreEqual(345, lazy.Get());
            Assert.AreEqual(345, lazy.Get());
        }
示例#22
0
        public LeafBase(IServiceContainer ctx, SpanStream stream)
        {
            this.ctx    = ctx;
            this.stream = stream;

            reader = LazyFactory.CreateLazy(() => new TypeDataReader(ctx, stream));
        }
示例#23
0
        public void RepeatedGetCallTest()
        {
            var lazy = LazyFactory <int[]> .CreateLazyMultitreaded(() => (new [] { 1, 2, 3, 4, 5, 6 }));

            const int amountOfThreads = 10;
            var       threads         = new List <Thread>();

            for (int i = 0; i < amountOfThreads; ++i)
            {
                threads.Add(new Thread(() =>
                {
                    var firstResult = lazy.Get();
                    Assert.AreEqual(firstResult, lazy.Get());
                }));
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }
        }
        public void ParallelCalculationTest()
        {
            var result      = 0;
            var rightResult = 10000;
            var threads     = new Thread[Environment.ProcessorCount];

            var lazy = LazyFactory <int> .CreateAlternativeLazy(() =>
            {
                for (int i = 0; i < rightResult; i++)
                {
                    Interlocked.Increment(ref result);
                }

                return(result);
            });

            for (int iter = 0; iter < threads.Length; iter++)
            {
                var localI = iter;
                threads[localI] = new Thread(() => lazy.Get());
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }

            Assert.AreEqual(rightResult, result);
        }
 /// <summary>
 /// Clear out all existing configuration
 /// </summary>
 public static void Reset()
 {
     MapperRegistry.Reset();
     Extensions.ClearExpressionCache();
     _configuration = LazyFactory.Create(_configurationInit);
     _mappingEngine = LazyFactory.Create(_mappingEngineInit);
 }
示例#26
0
        public void MultiThreadNormalIntTest()
        {
            int        value = 1;
            Func <int> supp  = () =>
            {
                value *= 10;
                return(value);
            };

            var intLazy = LazyFactory <int> .CreateMultiThread(supp);

            for (int i = 0; i < magicNumber; ++i)
            {
                var index = i;
                threads[i] = new Thread(() =>
                {
                    intResults[index] = intLazy.Get();
                });
                threads[i].Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }

            for (int i = 0; i < magicNumber; ++i)
            {
                Assert.AreEqual(10, intResults[i]);
            }
        }
示例#27
0
        /// <summary>
        ///     Instantiates the given factory using the <paramref name="getStronglyTypedFactory">factory functor.</paramref>
        /// </summary>
        /// <param name="currentServiceType">The service type that will be created by the factory.</param>
        /// <param name="getStronglyTypedFactory">The functor that will be responsible for creating the factory itself.</param>
        /// <param name="implementedInterfaces">
        ///     The list of <see cref="IFactory{T}" /> interfaces that are implemented by the
        ///     source type.
        /// </param>
        /// <returns>A valid factory instance.</returns>
        private static IFactory GetFactory(Type currentServiceType,
                                           Func <IFactoryRequest, IFactory> getStronglyTypedFactory,
                                           ICollection <Type> implementedInterfaces)
        {
            var genericType = typeof(IFactory <>).MakeGenericType(currentServiceType);

            // Lazy-instantiate the factories so that they can be injected by the container
            IFactory lazyFactory = new LazyFactory(getStronglyTypedFactory);

            IFactory result;

            if (implementedInterfaces.Contains(genericType))
            {
                // Convert the IFactory<T> instance down to an IFactory
                // instance so that it can be used by the target container
                var lazyFactoryType = typeof(LazyFactory <>).MakeGenericType(currentServiceType);
                var adapterType     = typeof(FactoryAdapter <>).MakeGenericType(currentServiceType);

                lazyFactory = (IFactory)Activator.CreateInstance(lazyFactoryType, getStronglyTypedFactory);
                result      = (IFactory)Activator.CreateInstance(adapterType, lazyFactory);

                return(result);
            }

            // Otherwise, use the untyped IFactory instance instead
            result = lazyFactory;
            return(result);
        }
示例#28
0
        public void MultiThreadNormalStringTest()
        {
            var           value = "Nate ";
            Func <string> supp  = () => value + "Higgers";

            var strLazy = LazyFactory <string> .CreateMultiThread(supp);

            for (int i = 0; i < magicNumber; ++i)
            {
                var index = i;
                threads[i] = new Thread(() =>
                {
                    strResults[index] = strLazy.Get();
                });
                threads[i].Start();
            }

            foreach (var thread in threads)
            {
                thread.Join();
            }

            for (int i = 0; i < magicNumber; ++i)
            {
                Assert.AreEqual("Nate Higgers", strResults[i]);
            }
        }
示例#29
0
        public void MultiThreadListTest()
        {
            Func <List <int> > makeList = () =>
            {
                var list = new List <int>();
                list.Add(5);
                return(list);
            };
            var lazyMultiThread = LazyFactory <List <int> > .CreateMultiThreading(makeList);

            var threads = new Thread[3];
            var results = new List <int> [3];

            for (var i = 0; i < 3; i++)
            {
                threads[i] = new Thread(() =>
                {
                    results[i] = lazyMultiThread.Get();
                });
            }

            StartThreads(threads);

            Assert.AreEqual(results[0], results[1]);
            Assert.AreEqual(results[1], results[2]);
        }
        public void CheckThatLazyWorksWithNull()
        {
            var simpleLazy = LazyFactory.CreateMultithreadLazy <object>(() => { return(null); });

            var lazyResult = simpleLazy.Get();

            Assert.AreEqual(null, lazyResult);
        }