Beispiel #1
0
        public void InitialisedSingleton_WithDuplicateKeys_InitialisedOnce()
        {
            DateTime key = DateTime.Now;

            Assert.AreEqual(
                0,
                TestInitialisedSingletonWithKeyedConstructor <DateTime> .TimesInitialised,
                "This test relies on there being no previously created singletons of the type used here.");
            TestInitialisedSingletonWithKeyedConstructor <DateTime>[] testSingletons =
                Enumerable.Range(1, 10).AsParallel().Select(
                    x => TestInitialisedSingletonWithKeyedConstructor <DateTime> .Get(key)).ToArray();
            Assert.AreEqual(
                1,
                TestInitialisedSingletonWithKeyedConstructor <DateTime> .TimesInitialised,
                "When duplicate Initialised Singletons are created with the same key, the initialise method should only be called once.");
        }
Beispiel #2
0
        public void InitialisedSingleton_WithDuplicateKeys_AreIdentical()
        {
            int key = Random.Next();
            TestInitialisedSingletonWithKeyedConstructor <int> testInitialisedSingleton1 =
                TestInitialisedSingletonWithKeyedConstructor <int> .Get(key);

            TestInitialisedSingletonWithKeyedConstructor <int> testInitialisedSingleton2 =
                TestInitialisedSingletonWithKeyedConstructor <int> .Get(key);

            TestInitialisedSingletonWithKeyedConstructor <int> testInitialisedSingleton3 =
                TestInitialisedSingletonWithKeyedConstructor <int> .Get(key);

            Assert.AreSame(
                testInitialisedSingleton1,
                testInitialisedSingleton2,
                "Creating multiple Initialised Singletons with the same key should result in the same object each time.");
            Assert.AreSame(
                testInitialisedSingleton2,
                testInitialisedSingleton3,
                "Creating multiple Initialised Singletons with the same key should result in the same object each time.");
        }