Merge pull request #2274 from esdrubal/udpclientreceive
[mono.git] / mono / mini / TestDriver.cs
index 97f5520d3f709c70e91c5823c18432435d3f4ac4..c3bf999d2729ae30c014fee5a8e32a4f4449ce2f 100644 (file)
@@ -2,6 +2,7 @@ using System;
 using System.Reflection;
 using System.Collections.Generic;
 
+[AttributeUsageAttribute(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
 public class CategoryAttribute : Attribute
 {
        public CategoryAttribute (string category) {
@@ -23,6 +24,7 @@ public class TestDriver {
                MethodInfo[] methods;
                bool do_timings = false;
                bool verbose = false;
+               bool quiet = false;
                int tms = 0;
                DateTime start, end = DateTime.Now;
 
@@ -33,13 +35,18 @@ public class TestDriver {
                if (args != null && args.Length > 0) {
                        for (j = 0; j < args.Length;) {
                                if (args [j] == "--time") {
-                                       do_timings = true;
+                                       do_timings = !quiet;
                                        j ++;
                                } else if (args [j] == "--iter") {
                                        iterations = Int32.Parse (args [j + 1]);
                                        j += 2;
                                } else if ((args [j] == "-v") || (args [j] == "--verbose")) {
-                                       verbose = true;
+                                       verbose = !quiet;
+                                       j += 1;
+                               } else if ((args [j] == "-q") || (args [j] == "--quiet")) {
+                                       quiet = true;
+                                       verbose = false;
+                                       do_timings = false;
                                        j += 1;
                                } else if (args [j] == "--exclude") {
                                        exclude [args [j + 1]] = args [j + 1];
@@ -107,13 +114,15 @@ public class TestDriver {
                                }
                        }
                
-                       if (do_timings) {
-                               Console.WriteLine ("Total ms: {0}", tms);
+                       if (!quiet) {
+                               if (do_timings) {
+                                       Console.WriteLine ("Total ms: {0}", tms);
+                               }
+                               if (nskipped > 0)
+                                       Console.WriteLine ("Regression tests: {0} ran, {1} skipped, {2} failed in {3}", ran, nskipped, failed, type);
+                               else
+                                       Console.WriteLine ("Regression tests: {0} ran, {1} failed in {2}", ran, failed, type);
                        }
-                       if (nskipped > 0)
-                               Console.WriteLine ("Regression tests: {0} ran, {1} skipped, {2} failed in {3}", ran, nskipped, failed, type);
-                       else
-                               Console.WriteLine ("Regression tests: {0} ran, {1} failed in {2}", ran, failed, type);
                }
 
                //Console.WriteLine ("Regression tests: {0} ran, {1} failed in [{2}]{3}", ran, failed, type.Assembly.GetName().Name, type);
@@ -124,3 +133,65 @@ 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 TestTimeout () {
+               availableTime = initializeAvailableTime ();
+               slack = defaultSlack ();
+       }
+
+       /// <summary>
+       ///    Consider the test started.
+       /// </summary>
+       public void Start ()
+       {
+               startTime = DateTime.UtcNow;
+       }
+
+       public bool HaveTimeLeft ()
+       {
+               if (availableTime == TimeSpan.Zero)
+                       return true;
+               var t = DateTime.UtcNow;
+               var finishTime = startTime + availableTime - slack;
+               return (t < finishTime);
+       }
+
+       private TimeSpan defaultSlack ()
+       {
+               return TimeSpan.FromSeconds (5);
+       }
+
+       private TimeSpan initializeAvailableTime ()
+       {
+               var e = System.Environment.GetEnvironmentVariable(ENV_TIMEOUT);
+               double d;
+               if (Double.TryParse(e, out d)) {
+                       return TimeSpan.FromSeconds(d);
+               } else {
+                       return TimeSpan.Zero;
+               }
+       }
+
+}