Esempio n. 1
0
        public async Task Pushing_and_complete_with_no_drain_without_start_empties_slots()
        {
            var pushedItems = new ConcurrentQueue <List <int> >[4]
            {
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
            };

            // choose insanely high batchSize to force push interval picking up all the content
            var completion = new MultiProducerConcurrentCompletion <int>(batchSize: 100, pushInterval: TimeSpan.FromMilliseconds(1), maxConcurrency: 4, numberOfSlots: 4);

            await PushConcurrentlyTwoThousandItemsInPackagesOfFiveHundredIntoFourSlots(completion);

            await completion.Complete(drain : false);

            var countDownEvent = new CountdownEvent(1);

            completion.Start((items, slot, state, token) =>
            {
                pushedItems[slot].Enqueue(new List <int>(items)); // take a copy
                countDownEvent.Signal();
                return(Task.FromResult(0));
            });

            completion.Push(1, slotNumber: 1);

            await Task.Run(() => countDownEvent.Wait(TimeSpan.FromSeconds(5)));

            await completion.Complete();

            Assert.AreEqual(1, Flatten(pushedItems).Sum(i => i));
        }
Esempio n. 2
0
        public async Task Pushing_for_slots_after_start_works_when_batch_size_is_reached()
        {
            var receivedItems = new ConcurrentQueue <List <int> >[4]
            {
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
            };

            var countDownEvent = new CountdownEvent(16);

            // choose insanely high push interval
            var completion = new MultiProducerConcurrentCompletion <int>(batchSize: 100, pushInterval: TimeSpan.FromDays(1), maxConcurrency: 4, numberOfSlots: 4);

            completion.Start((items, slot, state, token) =>
            {
                receivedItems[slot].Enqueue(new List <int>(items)); // take a copy
                if (!countDownEvent.IsSet)
                {
                    countDownEvent.Signal();
                }
                return(Task.FromResult(0));
            });

            var numberOfItems = await PushConcurrentlyTwoThousandItemsInPackagesOfFiveHundredIntoFourSlots(completion);

            // we wait for 16 counts and then complete midway
            await Task.Run(() => countDownEvent.Wait(TimeSpan.FromSeconds(5)));

            await completion.Complete();

            Assert.AreEqual(TriangularNumber(numberOfItems), Flatten(receivedItems).Sum(i => i));
        }
Esempio n. 3
0
        public async Task Complete_works_even_when_push_interval_and_batch_size_not_reached()
        {
            var receivedItems = new ConcurrentQueue <List <int> >[4]
            {
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
            };

            // choose insanely high batchSize and pushInterval to force the loop hanging
            var completion = new MultiProducerConcurrentCompletion <int>(batchSize: 10000, pushInterval: TimeSpan.FromDays(1), maxConcurrency: 4, numberOfSlots: 4);

            completion.Start((items, slot, state, token) =>
            {
                receivedItems[slot].Enqueue(new List <int>(items)); // take a copy
                return(Task.FromResult(0));
            });

            var numberOfItems = await PushConcurrentlyTwoThousandItemsInPackagesOfFiveHundredIntoFourSlots(completion);

            await completion.Complete();

            var sumOfAllElementsSeenSoFar = Flatten(receivedItems).Sum(i => i);

            Assert.AreEqual(TriangularNumber(numberOfItems), sumOfAllElementsSeenSoFar);
            Assert.AreEqual(TriangularNumber(numberOfItems), Flatten(receivedItems).Sum(i => i), "Dispatcher complete brought more items than expected");
        }
Esempio n. 4
0
        public async Task Pushing_for_slots_after_start_works_when_pushInterval_reached()
        {
            var receivedItems = new ConcurrentQueue <List <int> >[4]
            {
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
                new ConcurrentQueue <List <int> >(),
            };

            var countDownEvent = new CountdownEvent(4);

            // choose insanely high batchSize to force push interval picking up all the content
            var completion = new MultiProducerConcurrentCompletion <int>(batchSize: 10000, pushInterval: TimeSpan.FromMilliseconds(1), maxConcurrency: 4, numberOfSlots: 4);

            completion.Start((items, slot, state, token) =>
            {
                receivedItems[slot].Enqueue(new List <int>(items)); // take a copy
                if (!countDownEvent.IsSet)
                {
                    countDownEvent.Signal();
                }
                return(Task.FromResult(0));
            });

            var numberOfItems = await PushConcurrentlyTwoThousandItemsInPackagesOfFiveHundredIntoFourSlots(completion);

            await Task.Run(() => countDownEvent.Wait(TimeSpan.FromSeconds(5)));

            var sumOfAllElementsSeenSoFar = Flatten(receivedItems).Sum(i => i);

            await completion.Complete();

            Assert.AreEqual(TriangularNumber(numberOfItems), sumOfAllElementsSeenSoFar);
            Assert.AreEqual(TriangularNumber(numberOfItems), Flatten(receivedItems).Sum(i => i), "Dispatcher complete brought more items than expected");
        }