1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System;
6 using System.Collections.Generic;
7 using System.Reflection;
8 using System.Reflection.Emit;
9 using System.Diagnostics;
10 using System.Runtime.InteropServices;
11 using System.Threading;
12 
13 namespace DPStressHarness
14 {
15     public abstract class TestBase
16     {
17         private TestAttributeBase _attr;
18         private string _variationSuffix = "";
19 
20         [System.CLSCompliantAttribute(false)]
21         protected MethodInfo _testMethod;
22         [System.CLSCompliantAttribute(false)]
23         protected Type _type;
24 
25         [System.CLSCompliantAttribute(false)]
26         protected List<MethodInfo> _setupMethods;
27 
28         [System.CLSCompliantAttribute(false)]
29         protected List<MethodInfo> _cleanupMethods;
30 
TestMethodDelegate(object t)31         protected delegate void TestMethodDelegate(object t);
32 
TestBase(TestAttributeBase attr, MethodInfo testMethodInfo, Type type, List<MethodInfo> setupMethods, List<MethodInfo> cleanupMethods)33         public TestBase(TestAttributeBase attr,
34                         MethodInfo testMethodInfo,
35                         Type type,
36                         List<MethodInfo> setupMethods,
37                         List<MethodInfo> cleanupMethods)
38         {
39             _attr = attr;
40             _testMethod = testMethodInfo;
41             _type = type;
42             _setupMethods = setupMethods;
43             _cleanupMethods = cleanupMethods;
44         }
45 
46         public string Title
47         {
48             get { return _attr.Title + _variationSuffix; }
49         }
50 
51         public string Description
52         {
53             get { return _attr.Description; }
54         }
55 
56         public string Category
57         {
58             get { return _attr.Category; }
59         }
60 
61         public TestPriority Priority
62         {
63             get { return _attr.Priority; }
64         }
65 
GetVariations()66         public List<string> GetVariations()
67         {
68             FieldInfo[] fields = _type.GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
69 
70             List<string> variations = new List<string>(10);
71             foreach (FieldInfo fi in fields)
72             {
73                 TestVariationAttribute[] attrs = (TestVariationAttribute[])fi.GetCustomAttributes(typeof(TestVariationAttribute), false);
74 
75                 foreach (TestVariationAttribute testVarAttr in attrs)
76                 {
77                     if (!variations.Contains(testVarAttr.VariationName))
78                     {
79                         variations.Add(testVarAttr.VariationName);
80                     }
81                 }
82             }
83 
84             return variations;
85         }
86 
Run()87         public abstract void Run();
88 
89 
ExecuteSetupPhase(Object targetInstance)90         protected void ExecuteSetupPhase(Object targetInstance)
91         {
92             if (_setupMethods != null)
93             {
94                 foreach (MethodInfo setupMthd in _setupMethods)
95                 {
96                     setupMthd.Invoke(targetInstance, null);
97                 }
98             }
99         }
100 
ExecuteCleanupPhase(Object targetInstance)101         protected void ExecuteCleanupPhase(Object targetInstance)
102         {
103             if (_cleanupMethods != null)
104             {
105                 foreach (MethodInfo cleanupMethod in _cleanupMethods)
106                 {
107                     cleanupMethod.Invoke(targetInstance, null);
108                 }
109             }
110         }
111 
SetVariations(Object targetInstance)112         protected void SetVariations(Object targetInstance)
113         {
114             FieldInfo[] fields = targetInstance.GetType().GetFields(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
115 
116             foreach (FieldInfo fi in fields)
117             {
118                 TestVariationAttribute[] attrs = (TestVariationAttribute[])fi.GetCustomAttributes(typeof(TestVariationAttribute), false);
119 
120                 foreach (TestVariationAttribute testVarAttr in attrs)
121                 {
122                     foreach (string specifiedVariation in TestMetrics.Variations)
123                     {
124                         if (specifiedVariation.Equals(testVarAttr.VariationName))
125                         {
126                             fi.SetValue(targetInstance, testVarAttr.VariationValue);
127                             _variationSuffix += "_" + testVarAttr.VariationName;
128                             break;
129                         }
130                     }
131                 }
132             }
133         }
134 
CreateTestMethodDelegate()135         protected TestMethodDelegate CreateTestMethodDelegate()
136         {
137             Type[] args = { typeof(object) };
138             return new TestMethodDelegate((instance) => _testMethod.Invoke(instance, null));
139         }
140 
141 
LogTestFailure(string exceptionData)142         protected void LogTestFailure(string exceptionData)
143         {
144             Console.WriteLine("{0}: Failed", this.Title);
145             Console.WriteLine(exceptionData);
146 
147             Logger logger = new Logger(TestMetrics.RunLabel, false, TestMetrics.Milestone, TestMetrics.Branch);
148             logger.AddTest(this.Title);
149             logger.AddTestMetric("Test Assembly", _testMethod.Module.FullyQualifiedName, null);
150             logger.AddTestException(exceptionData);
151             logger.Save();
152         }
153 
LogStandardMetrics(Logger logger)154         protected void LogStandardMetrics(Logger logger)
155         {
156             logger.AddTestMetric(Constants.TEST_METRIC_TEST_ASSEMBLY, _testMethod.Module.FullyQualifiedName, null);
157             logger.AddTestMetric(Constants.TEST_METRIC_TEST_IMPROVEMENT, _attr.Improvement, null);
158             logger.AddTestMetric(Constants.TEST_METRIC_TEST_OWNER, _attr.Owner, null);
159             logger.AddTestMetric(Constants.TEST_METRIC_TEST_CATEGORY, _attr.Category, null);
160             logger.AddTestMetric(Constants.TEST_METRIC_TEST_PRIORITY, _attr.Priority.ToString(), null);
161             logger.AddTestMetric(Constants.TEST_METRIC_APPLICATION_NAME, _attr.Improvement, null);
162 
163             if (TestMetrics.TargetAssembly != null)
164             {
165                 logger.AddTestMetric(Constants.TEST_METRIC_TARGET_ASSEMBLY_NAME, (new AssemblyName(TestMetrics.TargetAssembly.FullName)).Name, null);
166             }
167 
168             logger.AddTestMetric(Constants.TEST_METRIC_PEAK_WORKING_SET, String.Format("{0}", TestMetrics.PeakWorkingSet), "bytes");
169             logger.AddTestMetric(Constants.TEST_METRIC_WORKING_SET, String.Format("{0}", TestMetrics.WorkingSet), "bytes");
170             logger.AddTestMetric(Constants.TEST_METRIC_PRIVATE_BYTES, String.Format("{0}", TestMetrics.PrivateBytes), "bytes");
171         }
172     }
173 }
174