title: How to test race condition with async unit tests using NUnit?
body: <p>This is the code I'm trying to test for a racing condition,
  I have a legacy code which I can change to a certain extent.
  Inside my main function, I have a function which returns a Task that we await and another function for UI that must not be blocked because it runs on a different thread.</p>
  
  <p>In the original code we do not care about the result we are just initializing some classes, and I test that the ViewModel is not null.</p>
  
  <p>In the example code I gave here, I changed the code to exemplify the problem of the race condition. 
  Currently, my test will fail. <strong>How do I properly test my racing condition</strong>?</p>
  
  <pre><code> [TestClass]
      public class UnitTest1
      {
          [TestMethod]
          public void TestMethod1()
          {
              Myclass tempClass = new Myclass();
              tempClass.CreateImage();
              Assert.AreEqual(3, tempClass.Sum);
          }
      }
  
      public class Myclass
      {
          public int Sum;
  
          public Myclass()
          {
              Sum = 0;
          }
          public async Task CreateImage()
          {
              await InternalFunction();
              await Task.Run(() =&gt; Physics());
          }
  
          public async Task InternalFunction()
          {
              await Task.Run(() =&gt;
              {
                  Math();
              });
          }
  
          public void Math()
          {
              Sum += 1;
          }
  
          public void Physics()
          {
              Sum += 2;
          }
      }
  </code></pre>
  
  <p>If I run the test, the sum property will be 1, and not 3 as expected. How do I change the code to be able to perform all of the flow of <code>CreateImage()</code> function and only once all of the threads are done, I want to assert the result/content of TempClass.</p>