예제 #1
0
        public async void DisposingCollectionShouldPreventMoreItemsAdded()
        {
            var collection = new NagleBlockingCollection <int>(100);

            using (collection)
            {
                await collection.AddAsync(1, CancellationToken.None);
            }

            Assert.That(collection.Count, Is.EqualTo(1));
            await collection.AddAsync(1, CancellationToken.None);
        }
예제 #2
0
        public void CollectionAddAsyncShouldBeAbleToCancel()
        {
            var collection = new NagleBlockingCollection <int>(1);

            using (var cancellationToken = new CancellationTokenSource())
            {
                collection.AddAsync(1, CancellationToken.None);
                var cancelledTask = collection.AddAsync(1, cancellationToken.Token);

                cancellationToken.Cancel();
                Assert.That(cancelledTask.IsCanceled, Is.True);
            }
        }
        public void CollectionAddAsyncShouldBeAbleToCancel()
        {
            var collection = new NagleBlockingCollection<int>(1);
            using (var cancellationToken = new CancellationTokenSource())
            {
                collection.AddAsync(1, CancellationToken.None);
                var cancelledTask = collection.AddAsync(1, cancellationToken.Token);

                cancellationToken.Cancel();
                Assert.That(cancelledTask.IsCanceled, Is.True);
            }

        }
예제 #4
0
        public void CollectionShouldDisposeEvenWithQueueAddAsync()
        {
            var addTasks = new List <Task>();

            using (var collection = new NagleBlockingCollection <int>(1))
            {
                addTasks.Add(collection.AddAsync(1, CancellationToken.None));
                addTasks.Add(collection.AddAsync(1, CancellationToken.None));
                addTasks.Add(collection.AddAsync(1, CancellationToken.None));
            }

            Assert.That(addTasks[0].IsCompleted);
            Assert.False(addTasks[1].IsCompleted);
            Assert.False(addTasks[2].IsCompleted);
        }
예제 #5
0
        public void TakeAsyncShouldBeThreadSafe()
        {
            const int expected   = 10;
            const int max        = 100;
            var       exit       = false;
            var       collection = new NagleBlockingCollection <int>(10000000);

            var take1 = collection.TakeBatch(expected, TimeSpan.FromSeconds(100), CancellationToken.None);
            var take2 = collection.TakeBatch(expected, TimeSpan.FromSeconds(100), CancellationToken.None);
            var take3 = collection.TakeBatch(expected, TimeSpan.FromSeconds(100), CancellationToken.None);

            take1.ContinueWith(t => Console.WriteLine("Take1 done..."));
            take2.ContinueWith(t => Console.WriteLine("Take2 done..."));
            take3.ContinueWith(t => Console.WriteLine("Take3 done..."));
            Task.WhenAll(take1, take2, take3).ContinueWith(x => exit = true);

            Parallel.ForEach(Enumerable.Range(0, max).ToList(),
                             new ParallelOptions {
                MaxDegreeOfParallelism = 20
            },
                             async x =>
            {
                while (exit == false)
                {
                    await collection.AddAsync(x, CancellationToken.None);
                    Thread.Sleep(100);
                }
            });

            Console.WriteLine("Left in collection: {0}", collection.Count);
            Assert.That(take1.Result.Count, Is.EqualTo(expected));
            Assert.That(take2.Result.Count, Is.EqualTo(expected));
            Assert.That(take3.Result.Count, Is.EqualTo(expected));
            Assert.That(collection.Count, Is.LessThan(max - (expected * 3)));
        }
예제 #6
0
        public void TakeAsyncShouldPlayNiceWithTPL()
        {
            const int expected   = 200;
            const int max        = 400;
            var       exit       = false;
            var       collection = new NagleBlockingCollection <int>(10000000);

            var dataTask = collection.TakeBatch(expected, TimeSpan.FromSeconds(100), CancellationToken.None);

            dataTask.ContinueWith(x => exit = true);

            Parallel.ForEach(Enumerable.Range(0, max).ToList(),
                             new ParallelOptions {
                MaxDegreeOfParallelism = 20
            },
                             async x =>
            {
                while (exit == false)
                {
                    await collection.AddAsync(x, CancellationToken.None);
                    Thread.Sleep(100);
                }
            });

            Console.WriteLine("Left in collection: {0}", collection.Count);
            Assert.That(dataTask.Result.Count, Is.EqualTo(expected));
            Assert.That(collection.Count, Is.LessThan(max - expected));
        }
예제 #7
0
        public async void TakeAsyncShouldReturnEvenWhileMoreDataArrives()
        {
            var exit       = false;
            var collection = new NagleBlockingCollection <int>(10000000);

            var sw       = Stopwatch.StartNew();
            var dataTask = collection.TakeBatch(10, TimeSpan.FromMilliseconds(5000), CancellationToken.None);


            var highVolumeAdding = Task.Factory.StartNew(async() =>
            {
                //high volume of data adds
                while (exit == false)
                {
                    await collection.AddAsync(1, CancellationToken.None);
                    Thread.Sleep(5);
                }
            });

            Console.WriteLine("Awaiting data...");
            await dataTask;

            Assert.That(dataTask.Result.Count, Is.EqualTo(10));
            Assert.That(sw.ElapsedMilliseconds, Is.LessThan(5000));
            exit = true;

            Console.WriteLine("Waiting to unwind test...");
            await highVolumeAdding;
        }
예제 #8
0
        public async void CollectionShouldReportCorrectBufferCount()
        {
            var collection = new NagleBlockingCollection <int>(100);

            var dataTask = collection.TakeBatch(10, TimeSpan.FromSeconds(5), CancellationToken.None);

            await collection.AddRangeAsync(Enumerable.Range(0, 9), CancellationToken.None);

            Assert.That(collection.Count, Is.EqualTo(9));

            collection.AddAsync(1, CancellationToken.None);
            var data = await dataTask;

            Assert.That(data.Count, Is.EqualTo(10));
            Assert.That(collection.Count, Is.EqualTo(0));
        }
        public void CollectionShouldDisposeEvenWithQueueAddAsync()
        {
            var addTasks = new List<Task>();
            using (var collection = new NagleBlockingCollection<int>(1))
            {
                addTasks.Add(collection.AddAsync(1, CancellationToken.None));
                addTasks.Add(collection.AddAsync(1, CancellationToken.None));
                addTasks.Add(collection.AddAsync(1, CancellationToken.None));
            }

            Assert.That(addTasks[0].IsCompleted);
            Assert.False(addTasks[1].IsCompleted);
            Assert.False(addTasks[2].IsCompleted);
        }
        public async void DisposingCollectionShouldPreventMoreItemsAdded()
        {
            var collection = new NagleBlockingCollection<int>(100);
            using (collection)
            {
                await collection.AddAsync(1, CancellationToken.None);
            }

            Assert.That(collection.Count, Is.EqualTo(1));
            await collection.AddAsync(1, CancellationToken.None);
        }
        public void TakeAsyncShouldBeThreadSafe()
        {
            const int expected = 10;
            const int max = 100;
            var exit = false;
            var collection = new NagleBlockingCollection<int>(10000000);

            var take1 = collection.TakeBatch(expected, TimeSpan.FromSeconds(100), CancellationToken.None);
            var take2 = collection.TakeBatch(expected, TimeSpan.FromSeconds(100), CancellationToken.None);
            var take3 = collection.TakeBatch(expected, TimeSpan.FromSeconds(100), CancellationToken.None);

            take1.ContinueWith(t => Console.WriteLine("Take1 done..."));
            take2.ContinueWith(t => Console.WriteLine("Take2 done..."));
            take3.ContinueWith(t => Console.WriteLine("Take3 done..."));
            Task.WhenAll(take1, take2, take3).ContinueWith(x => exit = true);

            Parallel.ForEach(Enumerable.Range(0, max).ToList(),
                   new ParallelOptions { MaxDegreeOfParallelism = 20 },
                   async x =>
                   {
                       while (exit == false)
                       {
                           await collection.AddAsync(x, CancellationToken.None);
                           Thread.Sleep(100);
                       }
                   });

            Console.WriteLine("Left in collection: {0}", collection.Count);
            Assert.That(take1.Result.Count, Is.EqualTo(expected));
            Assert.That(take2.Result.Count, Is.EqualTo(expected));
            Assert.That(take3.Result.Count, Is.EqualTo(expected));
            Assert.That(collection.Count, Is.LessThan(max - (expected*3)));
        }
        public void TakeAsyncShouldPlayNiceWithTPL()
        {
            const int expected = 200;
            const int max = 400;
            var exit = false;
            var collection = new NagleBlockingCollection<int>(10000000);

            var dataTask = collection.TakeBatch(expected, TimeSpan.FromSeconds(100), CancellationToken.None);

            dataTask.ContinueWith(x => exit = true);

            Parallel.ForEach(Enumerable.Range(0, max).ToList(),
                   new ParallelOptions { MaxDegreeOfParallelism = 20 },
                   async x =>
                   {
                       while (exit == false)
                       {
                           await collection.AddAsync(x, CancellationToken.None);
                           Thread.Sleep(100);
                       }
                   });

            Console.WriteLine("Left in collection: {0}", collection.Count);
            Assert.That(dataTask.Result.Count, Is.EqualTo(expected));
            Assert.That(collection.Count, Is.LessThan(max - expected));
        }
        public async void TakeAsyncShouldReturnEvenWhileMoreDataArrives()
        {
            var exit = false;
            var collection = new NagleBlockingCollection<int>(10000000);

            var sw = Stopwatch.StartNew();
            var dataTask = collection.TakeBatch(10, TimeSpan.FromMilliseconds(5000), CancellationToken.None);


            var highVolumeAdding = Task.Factory.StartNew(async () =>
            {
                //high volume of data adds
                while (exit == false)
                {
                    await collection.AddAsync(1, CancellationToken.None);
                    Thread.Sleep(5);
                }
            });

            Console.WriteLine("Awaiting data...");
            await dataTask;

            Assert.That(dataTask.Result.Count, Is.EqualTo(10));
            Assert.That(sw.ElapsedMilliseconds, Is.LessThan(5000));
            exit = true;

            Console.WriteLine("Waiting to unwind test...");
            await highVolumeAdding;
        }
        public async void CollectionShouldReportCorrectBufferCount()
        {
            var collection = new NagleBlockingCollection<int>(100);

            var dataTask = collection.TakeBatch(10, TimeSpan.FromSeconds(5), CancellationToken.None);

            await collection.AddRangeAsync(Enumerable.Range(0, 9), CancellationToken.None);
            Assert.That(collection.Count, Is.EqualTo(9));

            collection.AddAsync(1, CancellationToken.None);
            var data = await dataTask;
            Assert.That(data.Count, Is.EqualTo(10));
            Assert.That(collection.Count, Is.EqualTo(0));
        }