Esempio n. 1
0
        public void TestMultithreadRemoveAndContains()
        {
            var list   = new ConcurrentSortedList <int>();
            var idList = Enumerable.Range(0, 10000).ToList();

            Parallel.ForEach(idList, x => list.Add(x));
            Parallel.ForEach(idList, x =>
            {
                if (x < 5000)
                {
                    Assert.True(list.Remove(x));
                }
                else
                {
                    Assert.True(list.Contains(x));
                }
            });
            Assert.Equal(5000, list.Count);

            for (var i = 0; i < 5000; i++)
            {
                Assert.False(list.Contains(idList[i]));
            }
            AssertIntList(list, 5000);
        }
Esempio n. 2
0
        public void TestParallelMixedAddAndRemove()
        {
            var list    = new ConcurrentSortedList <int>();
            var prepare = Enumerable.Range(0, 10000).Select(x => x * 2).ToList();

            Parallel.ForEach(prepare, x => list.Add(x));
            var tasks = new Task[4000];

            for (var i = 0; i < 2000; i++)
            {
                var id = i;
                tasks[i] = new Task(() => Assert.True(list.Remove(id * 2)));
            }
            for (var i = 0; i < 2000; i++)
            {
                var id = i;
                tasks[i + 2000] = new Task(() => list.Add(id * 2 + 1));
            }
            Parallel.ForEach(tasks, x => x.Start());
            Task.WaitAll(tasks);
            Assert.Equal(10000, list.Count);
            foreach (var i in list)
            {
                if (i < 4000)
                {
                    Assert.Equal(1, i % 2);
                }
                else
                {
                    Assert.Equal(0, i % 2);
                }
            }
            AssertIntList(list, 10000);
        }
Esempio n. 3
0
        public void TestSingleThreadRemove()
        {
            var list   = new ConcurrentSortedList <Order>(new OrderComparer());
            var idList = ConstructIdList(10000);

            Parallel.ForEach(idList, x => list.Add(new Order {
                Id = x, Name = x.ToString()
            }));
            for (var i = 0; i < 1000; i++)
            {
                Assert.True(list.Remove(new Order {
                    Id = idList[i], Name = idList[i].ToString()
                }));
            }
            Assert.Equal(9000, list.Count);
            for (var i = 0; i < 1000; i++)
            {
                Assert.False(list.Contains(new Order {
                    Id = idList[i], Name = idList[i].ToString()
                }));
            }
            for (var i = 0; i < 9000; i++)
            {
                Assert.True(list.Contains(new Order {
                    Id = idList[i + 1000], Name = idList[i + 1000].ToString()
                }));
            }
            AssertOrderList(list, 9000);
        }
Esempio n. 4
0
        public void TestMultithreadRemoveAndThenEnumerate()
        {
            var list       = new ConcurrentSortedList <int>();
            var idList     = Enumerable.Range(0, 10000).ToList();
            var removeList = Enumerable.Range(0, 5000).Select(x => x * 2).ToList();

            Parallel.ForEach(idList, x => list.Add(x));
            Parallel.ForEach(removeList, x => list.Remove(x));
            Assert.Equal(5000, list.Count);
            foreach (var i in list)
            {
                Assert.Equal(1, i % 2);
            }
            AssertIntList(list, 5000);
        }
Esempio n. 5
0
        public void TestSingleThreadRemoveNotExist()
        {
            var list   = new ConcurrentSortedList <Order>(new OrderComparer());
            var idList = ConstructIdList(10000);

            Parallel.ForEach(idList, x => list.Add(new Order {
                Id = x, Name = x.ToString()
            }));
            var random = new Random((int)DateTime.Now.Ticks & 0x0000ffff);

            for (var i = 0; i < 1000; i++)
            {
                var rand = random.Next();
                if (!idList.Contains(rand))
                {
                    Assert.False(list.Remove(new Order {
                        Id = rand, Name = rand.ToString()
                    }));
                }
            }
        }
Esempio n. 6
0
        public void TestMultithreadAddAndReomove()
        {
            var list      = new ConcurrentSortedList <int>();
            var idList    = Enumerable.Range(0, 10000).ToList();
            var newIdList = Enumerable.Range(10000, 2000).ToList();

            Parallel.ForEach(idList, x => list.Add(x));
            var tasks = new Task[3000];

            for (var i = 0; i < 2000; i++)
            {
                var id = newIdList[i];
                tasks[i] = new Task(() => list.Add(id));
            }
            for (var i = 0; i < 1000; i++)
            {
                var id = idList[i];
                tasks[i + 2000] = new Task(() => list.Remove(id));
            }
            Parallel.ForEach(tasks, x => x.Start());
            Task.WaitAll(tasks);
            Assert.Equal(11000, list.Count);
            for (var i = 0; i < 2000; i++)
            {
                Assert.True(list.Contains(newIdList[i]));
            }

            for (var i = 0; i < 1000; i++)
            {
                Assert.False(list.Contains(idList[i]));
            }

            for (var i = 0; i < 9000; i++)
            {
                Assert.True(list.Contains(idList[i + 1000]));
            }
            AssertIntList(list, 11000);
        }
Esempio n. 7
0
        /// <summary>
        /// Run each of the Service/Type in the Collection
        /// </summary>
        public void Run()
        {
            while (m_Running)
            {
                ToDoList.AddRange(DoneList);
                DoneList.Clear();

                foreach (var item in new ConcurrentSortedList <TimeSpan, TypeParam>(ToDoList))
                {
                    if (string.IsNullOrWhiteSpace(item.Value.Type))
                    {
                        continue;
                    }

                    var toRun = DateTime.Now.Date.Add(item.Key);
                    if (toRun < DateTime.Now)
                    {
                        continue;
                    }
                    else
                    {
                        Thread.Sleep(toRun.Subtract(DateTime.Now));
                    }

                    Task.Factory.StartNew((o) =>
                    {
                        #region SUB THREAD FUNCTION

                        KeyValuePair <TimeSpan, TypeParam> job = (KeyValuePair <TimeSpan, TypeParam>)o;
                        try
                        {
                            string[] typeName        = job.Value.Type.Split(',');
                            string assemblyName      = typeName[0];
                            string typeToInstantiate = typeName[1];

                            if (!m_assemblies.ContainsKey(assemblyName))
                            {
                                FireInfo(string.Concat("Loading assembly ", assemblyName));

                                var asm = Assembly.LoadFile(assemblyName);
                                m_assemblies[assemblyName] = asm;
                            }

                            FireInfo(string.Format("Calling {0}.{1} ", typeToInstantiate, job.Value.Method));

                            Type class1 = m_assemblies[assemblyName].GetType(typeToInstantiate);

                            Object obj = Activator.CreateInstance(class1);

                            MethodInfo mi = class1.GetMethod(job.Value.Method);
                            // Invoke method ('null' for no parameters).

                            //Get the parameters
                            string[] p          = job.Value.Parameters.Split(',');
                            object[] paramArray = new object[p.Length];

                            if (!string.IsNullOrWhiteSpace(job.Value.Parameters))
                            {
                                FireInfo(string.Concat("Parameters ", job.Value.Parameters));

                                for (int cnt = 0; cnt < paramArray.Length; cnt++)
                                {
                                    string[] p2 = p[cnt].Split('-');
                                    switch (p2[1])
                                    {
                                    case "string":
                                        paramArray[cnt] = p2[0];
                                        break;

                                    case "int":
                                        paramArray[cnt] = Convert.ToInt32(p2[0]);
                                        break;

                                    case "bool":
                                        paramArray[cnt] = Convert.ToBoolean(p2[0]);
                                        break;

                                    case "double":
                                        paramArray[cnt] = Convert.ToDouble(p2[0]);
                                        break;

                                    case "char":
                                        paramArray[cnt] = Convert.ToChar(p2[0]);
                                        break;

                                    case "decimal":
                                        paramArray[cnt] = Convert.ToDecimal(p2[0]);
                                        break;
                                    }
                                }

                                mi.Invoke(obj, paramArray);
                            }
                            else
                            {
                                mi.Invoke(obj, null);
                            }
                        }
                        catch (Exception ex)
                        {
                            FireError(ex);
                        }
                        finally
                        {
                            ToDoList.Remove(job.Key);
                            DoneList.Add(job.Key, job.Value);
                        }

                        #endregion
                    }, item);

                    Thread.Yield();
                }

                var tillTomorrow = DateTime.Now.Date.AddDays(1) - DateTime.Now;
                Thread.Sleep(tillTomorrow);
            }
        }