[jit] Cleanup the signature of the mono_arch_get_static_rgctx_trampoline () function...
[mono.git] / mono / mini / TestDriver.cs
index c779920808f702cc341dc7231d0c80b6385a8b9f..639c52539709b1d228f866f5d7b5caae0a24cc8b 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) {
@@ -12,10 +13,22 @@ public class CategoryAttribute : Attribute
                get; set;
        }
 }
+public class TestDriverReporter
+{
+       public int FailedTests { get; private set; }
+       public int SkippedTests { get; private set; }
+       public int ExecutedTests { get; private set; }
+
+       public void ReportResults (int executed, int skipped, int failed) {
+               ExecutedTests = executed;
+               SkippedTests = skipped;
+               FailedTests = failed;
+       }
+};
 
 public class TestDriver {
 
-       static public int RunTests (Type type, string[] args) {
+       static public int RunTests(Type type, string[] args, TestDriverReporter reporter) {
                int failed = 0, ran = 0;
                int result, expected;
                int i, j, iterations;
@@ -31,6 +44,7 @@ public class TestDriver {
 
                var exclude = new Dictionary<string, string> ();
                List<string> run_only = new List<string> ();
+               List<string> exclude_test = new List<string> ();
                if (args != null && args.Length > 0) {
                        for (j = 0; j < args.Length;) {
                                if (args [j] == "--time") {
@@ -50,6 +64,9 @@ public class TestDriver {
                                } else if (args [j] == "--exclude") {
                                        exclude [args [j + 1]] = args [j + 1];
                                        j += 2;
+                               } else if (args [j] == "--exclude-test") {
+                                       exclude_test.Add (args [j + 1]);
+                                       j += 2;
                                } else if (args [j] == "--run-only") {
                                        run_only.Add (args [j + 1]);
                                        j += 2;
@@ -77,9 +94,15 @@ public class TestDriver {
                                        if (!found)
                                                continue;
                                }
-                               if (exclude.Count > 0) {
+                               if (exclude.Count > 0 || exclude_test.Count > 0) {
                                        var attrs = methods [i].GetCustomAttributes (typeof (CategoryAttribute), false);
                                        bool skip = false;
+                                       for (j = 0; j < exclude_test.Count; j++) {
+                                               if (name.EndsWith (exclude_test [j])) {
+                                                       skip = true;
+                                                       break;
+                                               }
+                                       }
                                        foreach (CategoryAttribute attr in attrs) {
                                                if (exclude.ContainsKey (attr.Category))
                                                        skip = true;
@@ -124,11 +147,82 @@ public class TestDriver {
                        }
                }
 
+               if (reporter != null) {
+                       reporter.ReportResults (ran, nskipped, failed);
+               }
+
                //Console.WriteLine ("Regression tests: {0} ran, {1} failed in [{2}]{3}", ran, failed, type.Assembly.GetName().Name, type);
                return failed;
        }
+
+       static public int RunTests (Type type, string[] args) {
+               return RunTests (type, args, null);
+       }
+
        static public int RunTests (Type type) {
-               return RunTests (type, null);
+               return RunTests (type, null, null);
        }
 }
 
+/// 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;
+               }
+       }
+
+}