Exemplo n.º 1
0
        public void StringSplitterTest()
        {
            StringSplitter stringSplitter = new StringSplitter("\"");

            stringSplitter.Delimiter = ",";

            ConcurrentQueue <string>   input       = new ConcurrentQueue <string>();
            ConcurrentQueue <object[]> output      = new ConcurrentQueue <object[]>();
            ManualResetEvent           pauseButton = new ManualResetEvent(true);
            Progress <int>             progress    = new Progress <int>();
            int numinput = 2000;

            for (int i = 0; i < numinput; i++)
            {
                input.Enqueue("\"Input, number\"," + i);
            }
            totalrecords              = 0;
            progress.ProgressChanged += progresshandler;
            var         action   = stringSplitter.GetReportingPausableWorkItem();
            List <Task> WorkList = new List <Task>();

            for (int i = 0; i < 2; i++)
            {
                WorkList.Add(Task.Factory.StartNew(() => action(input, output, pauseButton, progress)));
            }
            while (!input.IsEmpty)
            {
                Task.Delay(200).Wait();
            }
            stringSplitter.SignalCompletion();
            Task.WaitAll(WorkList.ToArray());

            Assert.IsTrue(input.IsEmpty);

            Assert.IsTrue(output.Count == numinput);

            Assert.IsTrue(output.All(x => x.ElementAt(0).Equals("Input, number")));
            var linqtime = output.AsEnumerable();
            IEnumerable <int> numbers = linqtime.Select(x => Int32.Parse((string)x.ElementAt(1))); //forgive the weird casting it used to be a string collection
            int Sum = numbers.Sum();
            IEnumerable <int> checknumbers = Enumerable.Range(0, numinput);                        //range is non inclusive
            int CheckSum = checknumbers.Sum();

            Assert.IsTrue(Sum == CheckSum);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Continues monitoring the list of tasks for failures. While no failures occur, it checks the given buffers for any remaining items. When empty it signals the
        /// m_stringsplitter object and then it monitors the given number of tasks in the list starting at given index.
        /// </summary>
        /// <param name="tasks"></param>
        /// <param name="indexOfTask"></param>
        /// <param name="numTasks"></param>
        /// <param name="buffers"></param>
        /// <returns></returns>
        protected async Task UnwindStringSplitter(List <Task> tasks, int indexOfTask, int numTasks, List <BoundedConcurrentQueu <string> > buffers)
        {
            while (!
                   (tasks.Any(
                        task => task.IsFaulted)))
            {
                //if any buffer in the list has anything in it
                if (buffers.Any(
                        buffer => buffer.Any()))
                {
                    await Task.Delay(DefaultMonitoringDelayInMilliSeconds);
                }
                else
                {
                    m_StringSplitter.SignalCompletion();
                    await Task.WhenAll(tasks.GetRange(indexOfTask, numTasks));

                    return;
                }
            }
            GatherExceptionsAndThrow(tasks);
        }