示例#1
0
            public void It_stops_processing()
            {
                var cancellationTokenSource = new CancellationTokenSource();

                Func <int, bool> processItem = i =>
                {
                    if (i > 0)
                    {
                        Thread.Sleep(10);
                    }

                    return(true);
                };

                using (var threadPool = new ConsumerThreadPool <int>(2, processItem, cancellationTokenSource.Token))
                {
                    for (var i = 0; i < 100; i++)
                    {
                        threadPool.Add(i);
                    }

                    Thread.Sleep(20);

                    cancellationTokenSource.Cancel();

                    Assert.That(threadPool.ItemsProcessed < threadPool.TotalItems);
                    Assert.That(threadPool.IsCancelled);
                }
            }
示例#2
0
            public void It_completes_adding()
            {
                var cancellationTokenSource = new CancellationTokenSource();

                Func <int, bool> processItem = i =>
                {
                    if (i > 0)
                    {
                        Thread.Sleep(10);
                    }

                    return(true);
                };

                using (var threadPool = new ConsumerThreadPool <int>(2, processItem, cancellationTokenSource.Token))
                {
                    for (var i = 0; i < 100; i++)
                    {
                        threadPool.Add(i);
                    }

                    Thread.Sleep(20);

                    cancellationTokenSource.Cancel();

                    for (var i = 0; i < 100; i++)
                    {
                        threadPool.Add(i);
                    }

                    Assert.That(threadPool.IsIndeterminate, Is.False);
                    Assert.That(threadPool.TotalItems, Is.EqualTo(100));
                }
            }
示例#3
0
            public void It_throws_exception_if_adding_is_not_completed()
            {
                using (var threadPool = new ConsumerThreadPool <int>(2, item => { Thread.Sleep(1000); return(true); }))
                {
                    for (var i = 0; i < 10; i++)
                    {
                        threadPool.Add(i);
                    }

                    // ReSharper disable once AccessToDisposedClosure
                    Assert.ThrowsAsync <InvalidOperationException>(() => threadPool.WaitForCompletion());
                }
            }
示例#4
0
            public async Task It_reports_successful_when_processItem_returns_true()
            {
                using (var threadPool = new ConsumerThreadPool <int>(2, item => true))
                {
                    for (var i = 0; i < 100; i++)
                    {
                        threadPool.Add(i);
                    }

                    threadPool.CompleteAdding();
                    await threadPool.WaitForCompletion();

                    Assert.That(threadPool.ItemsSuccessful, Is.EqualTo(threadPool.TotalItems));
                }
            }
示例#5
0
            public async Task It_reports_progress()
            {
                using (var threadPool = new ConsumerThreadPool <int>(2, item => false))
                {
                    for (var i = 0; i < 100; i++)
                    {
                        threadPool.Add(i);
                    }

                    threadPool.CompleteAdding();
                    await threadPool.WaitForCompletion();

                    Assert.That(threadPool.Progress, Is.EqualTo(100));
                }
            }
示例#6
0
            public void It_does_not_throw_exception_if_ignoreIsAddingCompleted_is_true()
            {
                var cancellationTokenSource = new CancellationTokenSource();

                using (var threadPool = new ConsumerThreadPool <int>(2, item => true, cancellationTokenSource.Token))
                {
                    for (var i = 0; i < 100; i++)
                    {
                        threadPool.Add(i);
                    }

                    Task.Run(async() => { await Task.Delay(100); cancellationTokenSource.Cancel(); });

                    // ReSharper disable once AccessToDisposedClosure
                    Assert.DoesNotThrowAsync(() => threadPool.WaitForCompletion(true));
                }
            }
示例#7
0
 public void It_does_not_throw_exception()
 {
     Assert.DoesNotThrow(() => { using (var threadPool = new ConsumerThreadPool <int>(2, i => true)) {} });
     Assert.DoesNotThrow(() => { using (var threadPool = new ConsumerThreadPool <int>(2, i => true, CancellationToken.None)) { } });
 }