none
Unit tests and Parallel.Foreach - does not seem totally parallel RRS feed

  • Question

  • Hi all,

    I'm trying to run a unit test where I want to launch the same method call in parallel in order to perform a few tests.

    Inside the following code, method 'DoStuff' represents just a sample with a Thread.Sleep of 3 seconds which represents the duration of my real method.

    I'm using a Parallel.Foreach in order to launch as many calls as I wish in order to launch them theorically at the same time (in my real case there is no more than 8 calls).

    [TestClass]
        public class UnitTest1
        {
            [TestMethod]
            public void TestMethod1()
            {
                List<int> numbers = new List<int>();
    
                for (int i = 0; i < 10; i++)
                {
                    numbers.Add(i);
                }
    
                Parallel.ForEach(numbers, 
                                 n =>
                {
                    DoStuff(n);
                });
            }
    
            private void DoStuff(int i)
            {
                Console.WriteLine("{0} - Start {1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff"), i.ToString());
    
                System.Threading.Thread.Sleep(3000);
    
                Console.WriteLine("{0} - End {1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff"), i.ToString());
            }
        }

    When the test is run, the output is the following:

    2017-03-24 09:50:44.952 - Start 2
    2017-03-24 09:50:44.951 - Start 1
    2017-03-24 09:50:44.951 - Start 0
    2017-03-24 09:50:44.952 - Start 4
    2017-03-24 09:50:44.952 - Start 3
    2017-03-24 09:50:45.865 - Start 5
    2017-03-24 09:50:46.864 - Start 6
    2017-03-24 09:50:47.366 - Start 7
    2017-03-24 09:50:47.954 - End 0
    2017-03-24 09:50:47.954 - End 3
    2017-03-24 09:50:47.954 - End 4
    2017-03-24 09:50:47.954 - Start 8
    2017-03-24 09:50:47.954 - End 2
    2017-03-24 09:50:47.954 - Start 9
    2017-03-24 09:50:47.954 - End 1
    2017-03-24 09:50:48.865 - End 5
    2017-03-24 09:50:49.865 - End 6
    2017-03-24 09:50:50.367 - End 7
    2017-03-24 09:50:50.954 - End 8
    2017-03-24 09:50:50.956 - End 9

    Shouldn't all 'DoStuff' calls be started before any of them begin?

    It seems to me that at some point the execution is no longer parallel, but I don't know why. I've tried to apply parallel options to set max degree of parallelism to 10 but the output is the same. My question is, is this a normal behavior, or should I be using another technique instead of Parallel.Foreach?

    Friday, March 24, 2017 9:59 PM

Answers

  • Hi Pakojones,

    After testing your code in my end, I found out, if wait for the thread long enough, e.g I gave it 10s,

    System.Threading.Thread.Sleep(10000);

    Then I could get the output like below:

    so I think the it's related to the thread time in your end.

    Please feel free to let me know if I misunderstood.

    Best regards,

    Fletch


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Tuesday, March 28, 2017 7:46 AM
    • Proposed as answer by Fletch Zhou Saturday, April 1, 2017 4:19 AM
    • Marked as answer by Pakojones Monday, April 3, 2017 4:43 PM
    Tuesday, March 28, 2017 7:46 AM

All replies

  • Sorry to insist, but anybody can help? I'm not sure I can do anything else for my test scenario but if there is something I can do it is worth. 
    Saturday, March 25, 2017 9:20 PM
  • Hi Pakojones,

    Thank you for posting here.

    According to your question is more related to Unit Testing, I will move it to Visual Studio Unit Testing forum for suitable support.

    The Visual C# discuss and ask the C# Programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 27, 2017 2:23 AM
  • Hi Pakojones,

    After testing your code in my end, I found out, if wait for the thread long enough, e.g I gave it 10s,

    System.Threading.Thread.Sleep(10000);

    Then I could get the output like below:

    so I think the it's related to the thread time in your end.

    Please feel free to let me know if I misunderstood.

    Best regards,

    Fletch


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Fletch Zhou Tuesday, March 28, 2017 7:46 AM
    • Proposed as answer by Fletch Zhou Saturday, April 1, 2017 4:19 AM
    • Marked as answer by Pakojones Monday, April 3, 2017 4:43 PM
    Tuesday, March 28, 2017 7:46 AM