SanityTest.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #region Copyright notice and license
  2. // Copyright 2015, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Collections.Generic;
  33. using System.IO;
  34. using System.Reflection;
  35. using Grpc.Core;
  36. using Grpc.Core.Internal;
  37. using Grpc.Core.Utils;
  38. using Newtonsoft.Json;
  39. using NUnit.Framework;
  40. namespace Grpc.Core.Tests
  41. {
  42. public class SanityTest
  43. {
  44. // TODO: make sanity test work for CoreCLR as well
  45. #if !NETCOREAPP1_0
  46. /// <summary>
  47. /// Because we depend on a native library, sometimes when things go wrong, the
  48. /// entire NUnit test process crashes. To be able to track down problems better,
  49. /// the NUnit tests are run by run_tests.py script in a separate process per test class.
  50. /// The list of tests to run is stored in src/csharp/tests.json.
  51. /// This test checks that the tests.json file is up to date by discovering all the
  52. /// existing NUnit tests in all test assemblies and comparing to contents of tests.json.
  53. /// </summary>
  54. [Test]
  55. public void TestsJsonUpToDate()
  56. {
  57. var discoveredTests = DiscoverAllTestClasses();
  58. string discoveredTestsJson = JsonConvert.SerializeObject(discoveredTests, Formatting.Indented);
  59. Assert.AreEqual(discoveredTestsJson, ReadTestsJson());
  60. }
  61. /// <summary>
  62. /// Gets list of all test classes obtained by inspecting all the test assemblies.
  63. /// </summary>
  64. private Dictionary<string, List<string>> DiscoverAllTestClasses()
  65. {
  66. var assemblies = GetTestAssemblies();
  67. var testsByAssembly = new Dictionary<string, List<string>>();
  68. foreach (var assembly in assemblies)
  69. {
  70. var testClasses = new List<string>();
  71. foreach (var t in assembly.GetTypes())
  72. {
  73. foreach (var m in t.GetMethods())
  74. {
  75. var attributes = m.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), true);
  76. if (attributes.Length > 0)
  77. {
  78. testClasses.Add(t.FullName);
  79. break;
  80. }
  81. }
  82. }
  83. testClasses.Sort();
  84. testsByAssembly.Add(assembly.GetName().Name, testClasses);
  85. }
  86. return testsByAssembly;
  87. }
  88. /// <summary>
  89. /// Reads contents of tests.json file.
  90. /// </summary>
  91. private string ReadTestsJson()
  92. {
  93. var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  94. var testsJsonFile = Path.Combine(assemblyDir, "..", "..", "..", "tests.json");
  95. return File.ReadAllText(testsJsonFile);
  96. }
  97. private List<Assembly> GetTestAssemblies()
  98. {
  99. var result = new List<Assembly>();
  100. var executingAssembly = Assembly.GetExecutingAssembly();
  101. result.Add(executingAssembly);
  102. var otherAssemblies = new[] {
  103. "Grpc.Examples.Tests",
  104. "Grpc.HealthCheck.Tests",
  105. "Grpc.IntegrationTesting"
  106. };
  107. foreach (var assemblyName in otherAssemblies)
  108. {
  109. var location = executingAssembly.Location.Replace("Grpc.Core.Tests", assemblyName);
  110. result.Add(Assembly.LoadFrom(location));
  111. }
  112. return result;
  113. }
  114. #endif
  115. }
  116. }