1 #region Copyright notice and license 2 3 // Copyright 2015 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using System.Collections.Generic; 21 using System.IO; 22 using System.Reflection; 23 using Grpc.Core; 24 using Grpc.Core.Internal; 25 using Grpc.Core.Utils; 26 using Newtonsoft.Json; 27 using NUnit.Framework; 28 29 namespace Grpc.Core.Tests 30 { 31 public class SanityTest 32 { 33 // TODO: make sanity test work for CoreCLR as well 34 #if !NETCOREAPP1_0 35 /// <summary> 36 /// Because we depend on a native library, sometimes when things go wrong, the 37 /// entire NUnit test process crashes. To be able to track down problems better, 38 /// the NUnit tests are run by run_tests.py script in a separate process per test class. 39 /// The list of tests to run is stored in src/csharp/tests.json. 40 /// This test checks that the tests.json file is up to date by discovering all the 41 /// existing NUnit tests in all test assemblies and comparing to contents of tests.json. 42 /// </summary> 43 [Test] TestsJsonUpToDate()44 public void TestsJsonUpToDate() 45 { 46 var discoveredTests = DiscoverAllTestClasses(); 47 var testsFromFile 48 = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(ReadTestsJson()); 49 50 Assert.AreEqual(discoveredTests, testsFromFile); 51 } 52 53 /// <summary> 54 /// Gets list of all test classes obtained by inspecting all the test assemblies. 55 /// </summary> DiscoverAllTestClasses()56 private Dictionary<string, List<string>> DiscoverAllTestClasses() 57 { 58 var assemblies = GetTestAssemblies(); 59 60 var testsByAssembly = new Dictionary<string, List<string>>(); 61 foreach (var assembly in assemblies) 62 { 63 var testClasses = new List<string>(); 64 foreach (var t in assembly.GetTypes()) 65 { 66 foreach (var m in t.GetMethods()) 67 { 68 var testAttributes = m.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), true); 69 var testCaseAttributes = m.GetCustomAttributes(typeof(NUnit.Framework.TestCaseAttribute), true); 70 if (testAttributes.Length > 0 || testCaseAttributes.Length > 0) 71 { 72 testClasses.Add(t.FullName); 73 break; 74 } 75 } 76 } 77 testClasses.Sort(); 78 testsByAssembly.Add(assembly.GetName().Name, testClasses); 79 } 80 return testsByAssembly; 81 } 82 83 /// <summary> 84 /// Reads contents of tests.json file. 85 /// </summary> ReadTestsJson()86 private string ReadTestsJson() 87 { 88 var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 89 var testsJsonFile = Path.Combine(assemblyDir, "..", "..", "..", "..", "tests.json"); 90 return File.ReadAllText(testsJsonFile); 91 } 92 GetTestAssemblies()93 private List<Assembly> GetTestAssemblies() 94 { 95 var result = new List<Assembly>(); 96 var executingAssembly = Assembly.GetExecutingAssembly(); 97 98 result.Add(executingAssembly); 99 100 var otherAssemblies = new[] { 101 "Grpc.Examples.Tests", 102 "Grpc.HealthCheck.Tests", 103 "Grpc.IntegrationTesting", 104 "Grpc.Reflection.Tests", 105 }; 106 foreach (var assemblyName in otherAssemblies) 107 { 108 var location = executingAssembly.Location.Replace("Grpc.Core.Tests", assemblyName); 109 result.Add(Assembly.LoadFrom(location)); 110 } 111 return result; 112 } 113 #endif 114 } 115 } 116