Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / mini / TestDriver.cs
index 639c52539709b1d228f866f5d7b5caae0a24cc8b..b17cb654fa2903fb1327eb7dd2ff4a299eecd179 100644 (file)
@@ -165,64 +165,29 @@ public class TestDriver {
 }
 
 /// Provide tests with the ability to find out how much time they have to run before being timed out.
-public class TestTimeout {
-       const string ENV_TIMEOUT = "TEST_DRIVER_TIMEOUT_SEC";
-       private readonly TimeSpan availableTime;
-       private TimeSpan slack;
-       private DateTime startTime;
-
-       /// <summary>
-       ///   How much time the test runner provided for us or TimeSpan.Zero if there is no bound.
-       /// </summary>
-       public TimeSpan AvailableTime { get { return availableTime; } }
-
-       public DateTime StartTime { get { return startTime; } }
-
-       /// <summary> Extra time to add when deciding if there
-       ///   is still time to run.  Bigger slack means less
-       ///   time left.
-       /// </summary>
-       public TimeSpan Slack {
-               get { return slack; }
-               set { slack = value; }
-       }
+public class TestTimeout
+{
+       private TimeSpan Timeout { get; }
 
-       public TestTimeout () {
-               availableTime = initializeAvailableTime ();
-               slack = defaultSlack ();
-       }
+       private DateTime StartTime { get; }
 
-       /// <summary>
-       ///    Consider the test started.
-       /// </summary>
-       public void Start ()
-       {
-               startTime = DateTime.UtcNow;
-       }
+       public bool HaveTimeLeft { get { return DateTime.UtcNow - StartTime < Timeout; } }
 
-       public bool HaveTimeLeft ()
-       {
-               if (availableTime == TimeSpan.Zero)
-                       return true;
-               var t = DateTime.UtcNow;
-               var finishTime = startTime + availableTime - slack;
-               return (t < finishTime);
-       }
+       public static bool IsStressTest { get { return Environment.GetEnvironmentVariable("MONO_TESTS_STRESS") == "1"; } }
 
-       private TimeSpan defaultSlack ()
+       private TestTimeout (TimeSpan timeout)
        {
-               return TimeSpan.FromSeconds (5);
+               Timeout = timeout;
+               StartTime = DateTime.UtcNow;
        }
 
-       private TimeSpan initializeAvailableTime ()
+       public static TestTimeout Start(TimeSpan timeout)
        {
-               var e = System.Environment.GetEnvironmentVariable(ENV_TIMEOUT);
-               double d;
-               if (Double.TryParse(e, out d)) {
-                       return TimeSpan.FromSeconds(d);
-               } else {
-                       return TimeSpan.Zero;
+               if (timeout.Ticks < 0)
+               {
+                       throw new ArgumentException("timeout");
                }
-       }
 
+               return new TestTimeout(timeout);
+       }
 }