Esempio n. 1
0
        public void ParallelTest_SingletonBuildOnDeclarationAndVolatile()
        {
            int size             = 10000;
            var numberExecutions = 0;

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

            Assert.Equal(size, numberExecutions);
        }
Esempio n. 2
0
        public void SingletonBuildOnDeclarationAndVolatile_ValidateConcept()
        {
            var    a = SingletonBuildOnDeclarationAndVolatile.GetInstance();
            object r = null;

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

            if (r != null)
            {
                Assert.Equal(a, r);
            }
        }
Esempio n. 3
0
        public void CheckIfFromAllUsageIsTheSameObjectReference_SingletonBuildOnDeclarationAndVolatile()
        {
            int size = 300;
            var r    = new List <SingletonBuildOnDeclarationAndVolatile>();

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

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