Beispiel #1
0
        public void Read_2ConcurrentEnumerations_BothEnumerationsAreExecutedAtSameTime()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }

            var col = new ConcurrentObservableCollection <int>(array);

            int res1  = 1;
            var task1 = new Task(() => col.ForEach(c =>
            {
                Task.Delay(1).Wait();
                res1 = 1;
            }));
            var task2 = new Task(() => col.ForEach(c =>
            {
                Task.Delay(1).Wait();
                res1++;
            }));

            task1.Start();
            task2.Start();
            Task.WaitAll(task1, task2);

            res1.Should().BeLessThan(99);
        }
        public void Read_2ConcurrentEnumerations_BothEnumerationsAreExecutedAtSameTime()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }
            var col = new ConcurrentObservableCollection <int>(array);

            int res1  = 1;
            var task1 = new Task(() => col.ForEach(c =>
            {
                Thread.Sleep(1);
                res1 = 1;
            }));
            var task2 = new Task(() => col.ForEach(c =>
            {
                Thread.Sleep(1);
                res1++;
            }));

            task1.Start();
            task2.Start();
            Task.WaitAll(new[] { task1, task2 });
            Assert.That(res1, Is.LessThan(99));
#if NET45
            Debug.Print("result: {0}", res1);
#endif
        }
Beispiel #3
0
        public void WriteAndRead_ConcurrentReadAndWrite_SuccessfullyWritesElementsToCollection()
        {
            var array = new int[50];

            for (int i = 0; i < 50; i++)
            {
                array[i] = i;
            }

            var col = new ConcurrentObservableCollection <int>(array);

            var task1 = new Task(() =>
            {
                for (int i = 50; i < 100; i++)
                {
                    col.Add(i);
                }
            });
            int current = 0;
            var task2   = new Task(() => col.ForEach(c => { current = c; }));

            task1.Start();
            task2.Start();
            Task.WaitAll(task1, task2);

            col.Count.Should().Be(100, "the collection should be populated properly");
            current.Should().BeInRange(49, 100, " the enumeration should have run in-sync with the update");
        }
Beispiel #4
0
        public void Read_ExceptionDuringEnumeration_LockReleased()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }

            var col = new ConcurrentObservableCollection <int>(array);

            try
            {
                int x = 0;
                col.ForEach(c =>
                {
                    if (x++ > 50)
                    {
                        throw new Exception();
                    }
                });
            }
            catch (Exception)
            {
                _testOutputHelper.WriteLine("Exception was fired");
            }

            col.Add(3);

            col.Count.Should().Be(101);
        }
        public void WriteAndRead_ConcurrentReadAndWrite_SuccessfullyWritesElementsToCollection()
        {
            var array = new int[50];

            for (int i = 0; i < 50; i++)
            {
                array[i] = i;
            }
            var col = new ConcurrentObservableCollection <int>(array);

            var task1 = new Task(() =>
            {
                for (int i = 50; i < 100; i++)
                {
                    col.Add(i);
                }
            });
            int current = 0;
            var task2   = new Task(() => col.ForEach(c => { current = c; }));

            task1.Start();
            task2.Start();
            Task.WaitAll(new[] { task1, task2 });

            Assert.That(col.Count, Is.EqualTo(100), "collection was not filled");
            Assert.That(current, Is.InRange(49, 100), "enumeration was not running simultaneously with update");
#if NET45
            Debug.Print("Collection size: {0}", col.Count);
            Debug.Print("current: {0}", current);
#endif
        }
        public void Read_ExceptionDuringEnumeration_LockReleased()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }
            var col = new ConcurrentObservableCollection <int>(array);

            try
            {
                int x = 0;
                col.ForEach(c =>
                {
                    if (x++ > 50)
                    {
                        throw new Exception();
                    }
                });
            }
            catch (Exception)
            {
                Console.WriteLine("Exception was fired");
            }

            col.Add(3);

            Assert.That(col.Count, Is.EqualTo(101));
        }
Beispiel #7
0
        public void Write_ComplexUpdateOperationFrom2ThreadsAndEnumerationInTheMiddle_CollectionUpdatedProperly()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }

            var col = new ConcurrentObservableCollection <int>(array);

            var task1 = new Task(() =>
            {
                for (int i = 100; i < 200; i++)
                {
                    _testOutputHelper.WriteLine("Add {0}", i);
                    col.Add(i);
                }
            });
            var task2 = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    _testOutputHelper.WriteLine("Remove {0}", i);
                    col.Remove(i);
                }
            });

            var list  = new List <int>();
            var task3 = new Task(() => col.ForEach(c =>
            {
                _testOutputHelper.WriteLine("Enumerating {0}", c);
                list.Add(c);
            }));

            task1.Start();
            task2.Start();
            task3.Start();

            Task.WaitAll(task1, task2, task3);

            var expected = new int[100];

            for (int i = 100; i < 200; i++)
            {
                expected[i - 100] = i;
            }

            col.Should().BeEquivalentTo(expected, "the collection should be updated properly");
            list.Should().NotBeEmpty("the enumeration should find at least one element");
        }
        public void Write_ComplexUpdateOperationFrom2ThreadsAndEnumerationInTheMiddle_CollectionUpdatedProperly()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }
            var col = new ConcurrentObservableCollection <int>(array);

            var task1 = new Task(() =>
            {
                for (int i = 100; i < 200; i++)
                {
                    Console.WriteLine("Add {0}", i);
                    col.Add(i);
                }
            });
            var task2 = new Task(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine("Remove {0}", i);
                    col.Remove(i);
                }
            });

            var list  = new List <int>();
            var task3 = new Task(() => col.ForEach(c =>
            {
                Console.WriteLine("Enumerating {0}", c);
                list.Add(c);
            }));

            task1.Start();
            task2.Start();
            task3.Start();

            Task.WaitAll(new[] { task1, task2, task3 });

            var expected = new int[100];

            for (int i = 100; i < 200; i++)
            {
                expected[i - 100] = i;
            }
            CollectionAssert.AreEquivalent(expected, col, "collection was not properly updated");
            CollectionAssert.IsNotEmpty(list, "Enumeration didnt find any element");
        }
        public void ReadWrite_EnumeratingThreadTriesToWriteToCollection_NoDeadlock()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }
            var col = new ConcurrentObservableCollection <int>(array);

            var task1 = new Task(() => col.ForEach(col.Add));

            task1.Start();

            Task.WaitAll(new[] { task1 });

            Assert.That(col.Count, Is.EqualTo(200));
        }
Beispiel #10
0
        public void ReadWrite_EnumeratingThreadTriesToWriteToCollection_NoDeadlock()
        {
            var array = new int[100];

            for (int i = 0; i < 100; i++)
            {
                array[i] = i;
            }

            var col = new ConcurrentObservableCollection <int>(array);

            var task1 = new Task(() => col.ForEach(col.Add));

            task1.Start();

            Task.WaitAll(task1);

            col.Count.Should().Be(200);
        }