Ejemplo n.º 1
0
        public void Remove_Should_WorkWhenIteratingDirectlyOnTheCollection()
        {
            // Arrange
            var concurrentCollection = new ConcurrentCollection <int>();

            for (int i = 0; i < 500; i++)
            {
                concurrentCollection.Add(i);
            }

            // Act
            var collectionConsumeTasks = new List <Task>();

            foreach (var item in concurrentCollection)
            {
                collectionConsumeTasks.Add(Task.Run(() =>
                {
                    concurrentCollection.Remove(item);
                }));
            }
            Task.WaitAll(collectionConsumeTasks.ToArray());

            // Assert
            Assert.Empty(concurrentCollection);
        }
Ejemplo n.º 2
0
        public void Remove_Should_WorkWhenLockIsDoneBetweenIteratorAndModifier()
        {
            // Arrange
            var concurrentCollection = new ConcurrentCollection <int>();

            for (int i = 0; i < 500; i++)
            {
                concurrentCollection.Add(i);
            }

            // Act
            var collectionConsumeTasks = new List <Task>();
            int collectionTotalSize    = concurrentCollection.Count;

            for (int i = 0; i < collectionTotalSize; i++)
            {
                collectionConsumeTasks.Add(Task.Run(() =>
                {
                    lock (_lock)
                    {
                        var item = concurrentCollection.FirstOrDefault();
                        concurrentCollection.Remove(item);
                    }
                }));
            }
            Task.WaitAll(collectionConsumeTasks.ToArray());

            // Assert
            Assert.Empty(concurrentCollection);
        }
Ejemplo n.º 3
0
        public void Remove_Should_ReturnFalseForSomeItemsWhenNoExternalLockIsUsed()
        {
            // Arrange
            var concurrentCollection = new ConcurrentCollection <int>();

            for (int i = 0; i < 500; i++)
            {
                concurrentCollection.Add(i);
            }

            // Act
            var collectionConsumeTasks = new List <Task>();
            int collectionTotalSize    = concurrentCollection.Count;
            int removedItems           = 0;

            for (int i = 0; i < collectionTotalSize; i++)
            {
                collectionConsumeTasks.Add(Task.Run(() =>
                {
                    var item    = concurrentCollection.FirstOrDefault();
                    var removed = concurrentCollection.Remove(item);
                    if (removed)
                    {
                        removedItems++;
                    }
                }));
            }
            Task.WaitAll(collectionConsumeTasks.ToArray());

            // Assert
            Assert.Equal(collectionTotalSize - removedItems, concurrentCollection.Count);
        }
Ejemplo n.º 4
0
        private void ConcurrentCollectionTest()
        {
            // Add to ConcurrentCollection concurrently
            var concurrentCollection = new ConcurrentCollection <int>();
            var collectionAddTasks   = new List <Task>();

            for (int i = 0; i < 500; i++)
            {
                var numberToAdd = i;
                collectionAddTasks.Add(Task.Run(() => concurrentCollection.Add(numberToAdd)));
            }

            // Wait for all tasks to complete
            Task.WaitAll(collectionAddTasks.ToArray());

            // Consume the items in the collection
            var collectionConsumeTasks = new List <Task>();
            int itemsInCollection      = 0;
            int notRemoved             = 0;

            foreach (var item in concurrentCollection)
            {
                collectionConsumeTasks.Add(Task.Run(() =>
                {
                    if (concurrentCollection.Remove(item))
                    {
                        itemsInCollection++;
                    }
                    else
                    {
                        notRemoved++;
                    }
                }));
            }
            Task.WaitAll(collectionConsumeTasks.ToArray());

            _testOutputHelper.WriteLine($"There were {itemsInCollection} items in the collection");
            _testOutputHelper.WriteLine($"Not removed {notRemoved}");

            // Checks the bag for an item
            // The bag should be empty and this should not print anything
            if (concurrentCollection.Count > 0)
            {
                _testOutputHelper.WriteLine("Found an item in the collection when it should be empty");
            }
        }