Removed unnecessary string compares and Sleeps.
authorvsadov <vsadov@microsoft.com>
Wed, 20 Mar 2019 23:32:10 +0000 (16:32 -0700)
committervsadov <vsadov@microsoft.com>
Thu, 21 Mar 2019 23:59:03 +0000 (16:59 -0700)
Made sure tests actually test something

tests/src/baseservices/threading/interlocked/compareexchange/CompareExchangeTClass.csproj
tests/src/baseservices/threading/interlocked/compareexchange/CompareExchangeTClass_1.csproj
tests/src/baseservices/threading/interlocked/compareexchange/CompareExchangeTString.csproj
tests/src/baseservices/threading/interlocked/compareexchange/compareexchangetclass.cs
tests/src/baseservices/threading/interlocked/compareexchange/compareexchangetstring.cs
tests/src/baseservices/threading/interlocked/exchange/ExchangeTClass.csproj
tests/src/baseservices/threading/interlocked/exchange/ExchangeTString.csproj
tests/src/baseservices/threading/interlocked/exchange/ExchangeTString_1.csproj
tests/src/baseservices/threading/interlocked/exchange/ExchangeTString_2.csproj
tests/src/baseservices/threading/interlocked/exchange/exchangetstring.cs

index a3db344..ac5a60f 100644 (file)
@@ -10,8 +10,8 @@
     <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-    <CLRTestExecutionArguments>null</CLRTestExecutionArguments>
-    <CLRTestPriority>1</CLRTestPriority>
+    <CLRTestExecutionArguments>""</CLRTestExecutionArguments>
+    <CLRTestPriority>0</CLRTestPriority>
     <GCStressIncompatible>true</GCStressIncompatible>
   </PropertyGroup>
   <!-- Default configurations to help VS understand the configurations -->
index 50cdc0b..2024d11 100644 (file)
@@ -10,7 +10,7 @@
     <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <CLRTestKind>RunOnly</CLRTestKind>
-    <CLRTestPriority>1</CLRTestPriority>
+    <CLRTestPriority>0</CLRTestPriority>
     <CLRTestProjectToRun>CompareExchangeTClass.csproj</CLRTestProjectToRun>
     <CLRTestExecutionArguments>"hello world"</CLRTestExecutionArguments>
     <GCStressIncompatible>true</GCStressIncompatible>
index 2e34b48..6700721 100644 (file)
@@ -11,8 +11,8 @@
     <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <CLRTestKind>BuildAndRun</CLRTestKind>
-    <CLRTestPriority>1</CLRTestPriority>
-    <CLRTestExecutionArguments>empty</CLRTestExecutionArguments>
+    <CLRTestPriority>0</CLRTestPriority>
+    <CLRTestExecutionArguments>"hello"</CLRTestExecutionArguments>
   </PropertyGroup>
   <!-- Default configurations to help VS understand the configurations -->
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
index a119d6b..956efba 100644 (file)
@@ -6,7 +6,7 @@ using System.Threading;
 
 class Class1
 {
-    
+
     static int Main(string[] args)
     {
         int rValue = 0;
@@ -21,21 +21,21 @@ class Class1
             threads[i] = new Thread(new ParameterizedThreadStart(tsi.ThreadWorker));
             threads[i].Start(kcIn);
         }
-                       
+
         tsi.Signal();
 
         Console.WriteLine("Joining threads");
-        for(int i=0;i<threads.Length;i++)
+        for (int i = 0; i < threads.Length; i++)
             threads[i].Join();
-        
+
         // Build the expected string
-        KrisClass kcExpected = new KrisClass("hello world! ");      
-        for(int i=0;i<threads.Length * 100;i++)
+        KrisClass kcExpected = new KrisClass("hello world! ");
+        for (int i = 0; i < threads.Length * 100; i++)
             kcExpected = kcExpected + kcIn;
 
-        if(kcExpected == tsi.GetValue)
+        if (kcExpected == tsi.GetValue)
             rValue = 100;
-       Console.WriteLine("Test Expected {0}, but found {1}", kcExpected, tsi.GetValue);
+        Console.WriteLine("Test Expected {0}, but found {1}", kcExpected, tsi.GetValue);
         Console.WriteLine("Test {0}", rValue == 100 ? "Passed" : "Failed");
         return rValue;
     }
@@ -44,9 +44,9 @@ class Class1
 public class ThreadSafe
 {
     ManualResetEvent signal;
-    public KrisClass Val = new KrisClass("hello world! ");             
+    public KrisClass Val = new KrisClass("hello world! ");
     private int numberOfIterations;
-    public ThreadSafe(): this(100) { }
+    public ThreadSafe() : this(100) { }
     public ThreadSafe(int loops)
     {
         signal = new ManualResetEvent(false);
@@ -62,21 +62,23 @@ public class ThreadSafe
     {
         KrisClass kcIn = (KrisClass)objIn;
         signal.WaitOne();
-        for(int i=0;i<numberOfIterations;i++)
+        for (int i = 0; i < numberOfIterations; i++)
             AddToTotal(kcIn);
     }
 
     private KrisClass AddToTotal(KrisClass addend)
     {
-        KrisClass initialValue = new KrisClass(string.Empty);
-        KrisClass newValue = new KrisClass(string.Empty);
+        KrisClass initialValue;
+        KrisClass newValue;
+
         do
         {
             initialValue = Val;
             newValue = initialValue + addend;
-        } 
-        while (initialValue != Interlocked.CompareExchange<KrisClass>(
+        }
+        while ((object)initialValue != Interlocked.CompareExchange<KrisClass>(
             ref Val, newValue, initialValue));
+
         return newValue;
     }
 
@@ -104,7 +106,7 @@ public class KrisClass
             return retVal;
         }
     }
-  
+
     public static KrisClass operator +(KrisClass kc1, KrisClass kc2)
     {
         return new KrisClass(kc1.ClassVal + kc2.ClassVal);
@@ -112,7 +114,7 @@ public class KrisClass
 
     public static bool operator ==(KrisClass kc1, KrisClass kc2)
     {
-        if(kc1.ClassVal == kc2.ClassVal)
+        if (kc1.ClassVal == kc2.ClassVal)
             return true;
         else
             return false;
@@ -120,26 +122,26 @@ public class KrisClass
 
     public static bool operator !=(KrisClass kc1, KrisClass kc2)
     {
-        if(kc1.ClassVal != kc2.ClassVal)
+        if (kc1.ClassVal != kc2.ClassVal)
             return true;
         else
             return false;
     }
 
-    public override bool Equals(object o) 
-    { 
-        try 
-        { 
-            return (bool) (this == (KrisClass) o); 
-        } 
-        catch 
-        { 
-            return false; 
-        } 
-    } 
-
-    public override int GetHashCode() 
-    { 
-        return 0; 
-    } 
+    public override bool Equals(object o)
+    {
+        try
+        {
+            return (bool)(this == (KrisClass)o);
+        }
+        catch
+        {
+            return false;
+        }
+    }
+
+    public override int GetHashCode()
+    {
+        return 0;
+    }
 }
\ No newline at end of file
index 82f1520..386b081 100644 (file)
@@ -95,8 +95,9 @@ public class ThreadSafe
             initialValue = Val;
             newValue = initialValue + addend;
         } 
-        while (initialValue != Interlocked.CompareExchange<string>(
+        while ((object)initialValue != Interlocked.CompareExchange<string>(
             ref Val, newValue, initialValue));
+
         return newValue;
     }  
 }
index 32ac313..c0b021d 100644 (file)
@@ -11,7 +11,7 @@
     <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <CLRTestKind>BuildAndRun</CLRTestKind>
-    <CLRTestPriority>1</CLRTestPriority>
+    <CLRTestPriority>0</CLRTestPriority>
   </PropertyGroup>
   <!-- Default configurations to help VS understand the configurations -->
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
index f40d779..6140001 100644 (file)
@@ -10,7 +10,7 @@
     <ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
     <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-    <CLRTestPriority>1</CLRTestPriority>
+    <CLRTestPriority>0</CLRTestPriority>
     <GCStressIncompatible>true</GCStressIncompatible>
   </PropertyGroup>
   <!-- Default configurations to help VS understand the configurations -->
index 5909d1c..81d5c1f 100644 (file)
@@ -10,7 +10,7 @@
     <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <CLRTestKind>RunOnly</CLRTestKind>
-    <CLRTestPriority>1</CLRTestPriority>
+    <CLRTestPriority>0</CLRTestPriority>
     <CLRTestProjectToRun>ExchangeTString.csproj</CLRTestProjectToRun>
     <CLRTestExecutionArguments>empty "This is a long string that I am trying to test to be sure that the Exchange can handle this long of a string.  If it can not then that is bad and we will have to fix it."</CLRTestExecutionArguments>
     <GCStressIncompatible>true</GCStressIncompatible>
index ede760b..ac01648 100644 (file)
@@ -10,7 +10,7 @@
     <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
     <CLRTestKind>RunOnly</CLRTestKind>
-    <CLRTestPriority>1</CLRTestPriority>
+    <CLRTestPriority>0</CLRTestPriority>
     <CLRTestProjectToRun>ExchangeTString.csproj</CLRTestProjectToRun>
     <CLRTestExecutionArguments>null "This is a string"</CLRTestExecutionArguments>
     <GCStressIncompatible>true</GCStressIncompatible>
index 6c6f486..26cfe38 100644 (file)
@@ -6,7 +6,7 @@ using System.Threading;
 using System.Collections;
 
 class Class1
-{    
+{
     static int Main(string[] args)
     {
         int rValue = 0;
@@ -17,10 +17,10 @@ class Class1
         for (int i = 0; i < threads.Length - 1; i++)
         {
             if (i % 2 == 0)
-                threads[i] = new Thread(new 
+                threads[i] = new Thread(new
                     ParameterizedThreadStart(tsi.ThreadWorkerA));
             else
-                threads[i] = new Thread(new 
+                threads[i] = new Thread(new
                     ParameterizedThreadStart(tsi.ThreadWorkerB));
 
             threads[i].Start(args);
@@ -30,12 +30,6 @@ class Class1
         threads[threads.Length - 1] = new Thread(new ThreadStart(tsi.ThreadChecker));
         threads[threads.Length - 1].Start();
 
-        //Added to prevent races where Checker does not get to Wait before ManualEvent is Set
-        while(tsi.ThreadCount < 100)
-        {
-               Thread.Sleep(100);
-        }
-        Thread.Sleep(100);
         tsi.Signal();
 
         Console.WriteLine("Joining threads");
@@ -57,11 +51,8 @@ public class ThreadSafe
     private string newValueA = "hello";
     private string newValueB = "world";
     private bool success;
-    public volatile int ThreadCount = 0;
 
-    private volatile static object syncroot = new object();
-       
-    public ThreadSafe(): this(10000) { }
+    public ThreadSafe() : this(10000) { }
 
     public ThreadSafe(int loops)
     {
@@ -81,28 +72,23 @@ public class ThreadSafe
         string ret = null;
 
         // get the value
-        if(0 < str.Length)
+        if (0 < str.Length)
         {
-            if("null" == str[0])
+            if ("null" == str[0])
                 newValueA = null;
-            else if("empty" == str[0])
+            else if ("empty" == str[0])
                 newValueA = string.Empty;
             else
                 newValueA = str[0];
         }
-               
-        lock(syncroot)
-        {
-               ThreadCount++;
-        }
-        
+
         signal.WaitOne();
         for (int i = 0; i < numberOfIterations; i++)
         {
             ret = Interlocked.Exchange<string>(ref curVal, newValueA);
-            
+
             // Check return value
-            if(ret != newValueB && ret != newValueA && ret != "start string")
+            if (ret != newValueB && ret != newValueA && ret != "start string")
             {
                 Console.WriteLine(ret + "," + newValueB + "," + newValueA);
                 success = false;
@@ -117,28 +103,24 @@ public class ThreadSafe
         string ret = null;
 
         // get the value
-        if(2 == str.Length)
+        if (2 == str.Length)
         {
-            if("null" == str[1])
+            if ("null" == str[1])
                 newValueB = null;
-            else if("empty" == str[1])
+            else if ("empty" == str[1])
                 newValueB = string.Empty;
             else
                 newValueB = str[1];
         }
 
-        lock(syncroot)
-        {
-               ThreadCount++;
-        }
+        signal.WaitOne();
 
-        signal.WaitOne();
         for (int i = 0; i < numberOfIterations; i++)
         {
             ret = Interlocked.Exchange<string>(ref curVal, newValueB);
 
             // Check return value
-            if(ret != newValueB && ret != newValueA && ret != "start string")
+            if (ret != newValueB && ret != newValueA && ret != "start string")
             {
                 Console.WriteLine(ret + "," + newValueB + "," + newValueA);
                 success = false;
@@ -148,17 +130,18 @@ public class ThreadSafe
 
     public void ThreadChecker()
     {
-        lock(syncroot)
-        {
-               ThreadCount++;
-        }
-
         signal.WaitOne();
+
+        while(curVal == "start string")
+        {
+            Thread.Sleep(0);
+        }
+
         string tmpVal;
         for (int i = 0; i < numberOfIterations; i++)
         {
             tmpVal = curVal;
-            if (tmpVal != newValueB && tmpVal != newValueA && tmpVal != "start string")
+            if (tmpVal != newValueB && tmpVal != newValueA)
             {
                 Console.WriteLine(tmpVal + "," + newValueB + "," + newValueA);
                 success = false;