SanityTest.cs 4.1 KB

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