示例#1
0
        public void ParallelTest_SingletonBuildOnDeclaration()
        {
            int size             = 10000;
            var numberExecutions = 0;

            Parallel.ForEach(Enumerable.Range(1, size), (i) =>
            {
                SingletonBuildOnDeclaration.GetInstance().RandomName = "Random number: " + i;
                var acessing = SingletonBuildOnDeclaration.GetInstance().RandomName;
                Interlocked.Increment(ref numberExecutions);
            });

            Assert.Equal(size, numberExecutions);
        }
示例#2
0
        public void SingletonBuildOnDeclaration_ValidateConcept()
        {
            var    a = SingletonBuildOnDeclaration.GetInstance();
            object r = null;

            Assert.Throws <NullReferenceException>(() => {
                r = a.GetType().GetConstructor(Type.EmptyTypes).Invoke(new object[] {});
            });

            if (r != null)
            {
                Assert.Equal(a, r);
            }
        }
示例#3
0
        public void CheckIfFromAllUsageIsTheSameObjectReference_SingletonBuildOnDeclaration()
        {
            int size = 300;
            var r    = new List <SingletonBuildOnDeclaration>();

            Parallel.ForEach(Enumerable.Range(1, size), (i) =>
            {
                SingletonBuildOnDeclaration.GetInstance().RandomName = "Random number: " + i;
                var acessing = SingletonBuildOnDeclaration.GetInstance().RandomName;
                r.Add(SingletonBuildOnDeclaration.GetInstance());
            });
            var firstItem = r.FirstOrDefault();

            Assert.All(r, (item) => item.Equals(firstItem));
        }