GrpcEnvironmentTest.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.Linq;
  18. using System.Threading;
  19. using Grpc.Core;
  20. using NUnit.Framework;
  21. namespace Grpc.Core.Tests
  22. {
  23. public class GrpcEnvironmentTest
  24. {
  25. [Test]
  26. public void InitializeAndShutdownGrpcEnvironment()
  27. {
  28. var env = GrpcEnvironment.AddRef();
  29. Assert.IsTrue(env.CompletionQueues.Count > 0);
  30. for (int i = 0; i < env.CompletionQueues.Count; i++)
  31. {
  32. Assert.IsNotNull(env.CompletionQueues.ElementAt(i));
  33. }
  34. GrpcEnvironment.ReleaseAsync().Wait();
  35. }
  36. [Test]
  37. public void SubsequentInvocations()
  38. {
  39. var env1 = GrpcEnvironment.AddRef();
  40. var env2 = GrpcEnvironment.AddRef();
  41. Assert.AreSame(env1, env2);
  42. GrpcEnvironment.ReleaseAsync().Wait();
  43. GrpcEnvironment.ReleaseAsync().Wait();
  44. }
  45. [Test]
  46. public void InitializeAfterShutdown()
  47. {
  48. Assert.AreEqual(0, GrpcEnvironment.GetRefCount());
  49. var env1 = GrpcEnvironment.AddRef();
  50. GrpcEnvironment.ReleaseAsync().Wait();
  51. var env2 = GrpcEnvironment.AddRef();
  52. GrpcEnvironment.ReleaseAsync().Wait();
  53. Assert.AreNotSame(env1, env2);
  54. }
  55. [Test]
  56. public void ReleaseWithoutAddRef()
  57. {
  58. Assert.AreEqual(0, GrpcEnvironment.GetRefCount());
  59. Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await GrpcEnvironment.ReleaseAsync());
  60. }
  61. [Test]
  62. public void GetCoreVersionString()
  63. {
  64. var coreVersion = GrpcEnvironment.GetCoreVersionString();
  65. var parts = coreVersion.Split('.');
  66. Assert.AreEqual(3, parts.Length);
  67. }
  68. [Test]
  69. public void ShuttingDownEventIsFired()
  70. {
  71. var cts = new CancellationTokenSource();
  72. var handler = new EventHandler((sender, args) => { cts.Cancel(); });
  73. GrpcEnvironment.ShuttingDown += handler;
  74. var env = GrpcEnvironment.AddRef();
  75. GrpcEnvironment.ReleaseAsync().Wait();
  76. GrpcEnvironment.ShuttingDown -= handler;
  77. Assert.IsTrue(cts.Token.IsCancellationRequested);
  78. }
  79. }
  80. }