SanityTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 NUnit.Framework;
  39. namespace Grpc.Core.Tests
  40. {
  41. public class SanityTest
  42. {
  43. /// <summary>
  44. /// Because we depend on a native library, sometimes when things go wrong, the
  45. /// entire NUnit test process crashes. To be able to track down problems better,
  46. /// the NUnit tests are run by run_tests.py script in a separate process per test class.
  47. /// The list of tests to run is stored in src/csharp/tests.json.
  48. /// This test checks that the tests.json file is up to date by discovering all the
  49. /// existing NUnit tests in all test assemblies and comparing to contents of tests.json.
  50. /// </summary>
  51. [Test]
  52. public void TestsJsonUpToDate()
  53. {
  54. var testClasses = DiscoverAllTestClasses();
  55. string testsJson = GetTestsJson();
  56. // we don't have a JSON parser at hand, but check that the test class
  57. // name is contained in the file instead.
  58. foreach (var className in testClasses) {
  59. Assert.IsTrue(testsJson.Contains(className),
  60. string.Format("Test class \"{0}\" is missing in C# tests.json file", className));
  61. }
  62. }
  63. /// <summary>
  64. /// Gets list of all test classes obtained by inspecting all the test assemblies.
  65. /// </summary>
  66. private List<string> DiscoverAllTestClasses()
  67. {
  68. var assemblies = GetTestAssemblies();
  69. var testClasses = new List<string>();
  70. foreach (var assembly in assemblies)
  71. {
  72. foreach (var t in assembly.GetTypes())
  73. {
  74. foreach (var m in t.GetMethods())
  75. {
  76. var attributes = m.GetCustomAttributes(typeof(NUnit.Framework.TestAttribute), true);
  77. if (attributes.Length > 0)
  78. {
  79. testClasses.Add(t.FullName);
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. testClasses.Sort();
  86. return testClasses;
  87. }
  88. private string GetTestsJson()
  89. {
  90. var assemblyDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  91. var testsJsonFile = Path.Combine(assemblyDir, "..", "..", "..", "tests.json");
  92. return File.ReadAllText(testsJsonFile);
  93. }
  94. private List<Assembly> GetTestAssemblies()
  95. {
  96. var result = new List<Assembly>();
  97. var executingAssembly = Assembly.GetExecutingAssembly();
  98. result.Add(executingAssembly);
  99. var otherAssemblies = new[] {
  100. "Grpc.Examples.Tests",
  101. "Grpc.HealthCheck.Tests",
  102. "Grpc.IntegrationTesting"
  103. };
  104. foreach (var assemblyName in otherAssemblies)
  105. {
  106. var location = executingAssembly.Location.Replace("Grpc.Core.Tests", assemblyName);
  107. result.Add(Assembly.LoadFrom(location));
  108. }
  109. return result;
  110. }
  111. }
  112. }