Beispiel #1
0
        public void AreSingletonHashcodeInstancesSame()
        {
            var singletonOne = SingletonBase.GetInstance <SingletonBase>();
            var singletonTwo = SingletonBase.GetInstance <SingletonBase>();

            Assert.IsInstanceOfType(singletonOne, typeof(SingletonBase));
            Assert.IsInstanceOfType(singletonTwo, typeof(SingletonBase));
            Assert.AreEqual(singletonOne.GetHashCode(), singletonTwo.GetHashCode());
        }
Beispiel #2
0
        public void AreSingletonInstancesSame()
        {
            var singletonOne = SingletonBase.GetInstance <SingletonBase>();
            var singletonTwo = SingletonBase.GetInstance <SingletonBase>();;

            Assert.IsInstanceOfType(singletonOne, typeof(SingletonBase));
            Assert.IsInstanceOfType(singletonTwo, typeof(SingletonBase));
            Assert.AreSame(singletonOne, singletonTwo);
        }
Beispiel #3
0
        public void AreTwoInheritedInstancesFromSingletonAreSame()
        {
            var singletonOne = SingletonBase.GetInstance <ChildOneSingleton>();
            var singletonTwo = SingletonBase.GetInstance <ChildTwoSingleton>();

            Assert.AreEqual(singletonOne.TestMethod(), singletonTwo.TestMethod());
            Console.WriteLine(singletonOne.TestMethod());
            Assert.IsInstanceOfType(singletonOne, typeof(ChildOneSingleton));
            Assert.IsInstanceOfType(singletonTwo, typeof(ChildOneSingleton));
            Assert.AreSame(singletonOne, singletonTwo);
        }
Beispiel #4
0
        public void AreSingletonInstancesSame_MultipleThread()
        {
            var thread_count = 100;
            var threads      = new List <Thread>();
            var instances    = new List <SingletonBase>();

            for (int i = 0; i < thread_count; i++)
            {
                var thread = new Thread(() =>
                {
                    instances.Add(SingletonBase.GetInstance <SingletonBase>());
                });
                threads.Add(thread);
                thread.Start();
            }

            threads.ForEach(x => x.Join());

            instances.ForEach(x => Assert.AreSame(instances[0], x));
        }