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); }
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); } }
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)); }