System.Collections.Tests.SortedList_SyncRootTests.GetSyncRootBasic C# (CSharp) Метод

GetSyncRootBasic() приватный Метод

private GetSyncRootBasic ( ) : void
Результат void
        public void GetSyncRootBasic()
        {
            // Testing SyncRoot is not as simple as its implementation looks like. This is the working
            // scenario we have in mind.
            // 1) Create your Down to earth mother SortedList
            // 2) Get a synchronized wrapper from it
            // 3) Get a Synchronized wrapper from 2)
            // 4) Get a synchronized wrapper of the mother from 1)
            // 5) all of these should SyncRoot to the mother earth

            var sortListMother = new SortedList();
            for (int i = 0; i < NumberOfElements; i++)
            {
                sortListMother.Add("Key_" + i, "Value_" + i);
            }

            Assert.Equal(sortListMother.SyncRoot.GetType(), typeof(object));

            SortedList sortListSon = SortedList.Synchronized(sortListMother);
            _sortListGrandDaughter = SortedList.Synchronized(sortListSon);
            _sortListDaughter = SortedList.Synchronized(sortListMother);

            Assert.Equal(sortListSon.SyncRoot, sortListMother.SyncRoot);
            Assert.Equal(sortListMother.SyncRoot, sortListSon.SyncRoot);

            Assert.Equal(_sortListGrandDaughter.SyncRoot, sortListMother.SyncRoot);
            Assert.Equal(_sortListDaughter.SyncRoot, sortListMother.SyncRoot);
            Assert.Equal(sortListSon.SyncRoot, sortListMother.SyncRoot);

            //we are going to rumble with the SortedLists with some threads
            
            var workers = new Task[4];
            for (int i = 0; i < workers.Length; i += 2)
            {
                var name = "Thread_worker_" + i;
                var action1 = new Action(() => AddMoreElements(name));
                var action2 = new Action(RemoveElements);

                workers[i] = Task.Run(action1);
                workers[i + 1] = Task.Run(action2);
            }

            Task.WaitAll(workers);

            // Checking time
            // Now lets see how this is done.
            // Either there are some elements or none
            var sortListPossible = new SortedList();
            for (int i = 0; i < NumberOfElements; i++)
            {
                sortListPossible.Add("Key_" + i, "Value_" + i);
            }
            for (int i = 0; i < workers.Length; i++)
            {
                sortListPossible.Add("Key_Thread_worker_" + i, "Thread_worker_" + i);
            }

            //lets check the values if
            IDictionaryEnumerator enumerator = sortListMother.GetEnumerator();
            while (enumerator.MoveNext())
            {
                Assert.True(sortListPossible.ContainsKey(enumerator.Key));
                Assert.True(sortListPossible.ContainsValue(enumerator.Value));
            }
        }