コード例 #1
0
        public void GivenBufferListWhenDisposeTwiceShouldNotThrow()
        {
            var list = new BufferList <int>(1000, TimeSpan.FromSeconds(10));

            list.Dispose();
            list.Invoking(x => x.Dispose()).Should().NotThrow();
        }
コード例 #2
0
        public void GivenBufferShouldTryToCleanListUntilBagIsEmpty()
        {
            var read           = 0;
            var maxSize        = 0;
            var count          = 0;
            var list           = new BufferList <int>(10, Timeout.InfiniteTimeSpan);
            var autoResetEvent = new AutoResetEvent(false);

            list.Cleared += removed =>
            {
                maxSize = Math.Max(maxSize, removed.Count);
                count  += removed.Count;
                ++read;
                if (read >= 10)
                {
                    autoResetEvent.Set();
                }
                ;
            };

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

            autoResetEvent.WaitOne();
            maxSize.Should().Be(10);
            count.Should().BeCloseTo(100, 10);
            list.Dispose();
        }
コード例 #3
0
        public void GivenBufferListWhenAddAndDisposedShouldThrow()
        {
            var list = new BufferList <int>(1, Timeout.InfiniteTimeSpan);

            list.Dispose();

            Action action = () => list.Add(1);

            action.Should().Throw <InvalidOperationException>()
            .WithMessage("The buffer has been disposed.");
        }
コード例 #4
0
        public void GivenBufferListWhenDisposeShouldClear()
        {
            var removedCount = 0;

            var list = new BufferList <int>(1000, TimeSpan.FromSeconds(10));

            list.Cleared += removed => { removedCount = removed.Count(); };
            for (var i = 0; i < 1000; i++)
            {
                list.Add(i);
            }

            list.Dispose();
            removedCount.Should().Be(1000);
        }
コード例 #5
0
        public void GivenBufferWhenThrowOnClearingShouldRequeue()
        {
            const int BATCHING_SIZE = 100;
            var       list          = new BufferList <int>(BATCHING_SIZE, TimeSpan.FromSeconds(1));
            var       faultCount    = 0;

            list.Cleared  += removed => throw new Exception();
            list.Disposed += failed => Interlocked.Add(ref faultCount, failed.Count);
            list.Dropped  += dropped => Interlocked.Add(ref faultCount, dropped.Count);

            for (var i = 0; i < 1000; i++)
            {
                list.Add(i);
            }

            list.Dispose();
            faultCount.Should().Be(1000);
        }
コード例 #6
0
        public async Task GivenBufferListWhenDisposeShouldFullClearList()
        {
            var count  = 0;
            var source = new CancellationTokenSource(TimeSpan.FromSeconds(5));

            var list = new BufferList <int>(10, Timeout.InfiniteTimeSpan);

            list.Cleared += removed => Interlocked.Add(ref count, removed.Count);
            var tasks    = new Task[10];
            var expected = 0;

            for (var i = 0; i < 10; i++)
            {
                tasks[i] = Task.Factory.StartNew(async() =>
                {
                    while (!source.Token.IsCancellationRequested)
                    {
                        list.Add(i);
                        Interlocked.Increment(ref expected);
                        await Task.Delay(10);
                    }
                });
            }

            var autoResetEvent = new AutoResetEvent(false);

            source.Token.Register(() =>
            {
                list.Dispose();
                autoResetEvent.Set();
            });

            autoResetEvent.WaitOne();
            await Task.WhenAll(tasks);

            count.Should().Be(expected);
        }