ChannelTest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.Threading.Tasks;
  18. using Grpc.Core;
  19. using Grpc.Core.Internal;
  20. using Grpc.Core.Utils;
  21. using NUnit.Framework;
  22. namespace Grpc.Core.Tests
  23. {
  24. public class ChannelTest
  25. {
  26. [Test]
  27. public void Constructor_RejectsInvalidParams()
  28. {
  29. Assert.Throws(typeof(ArgumentNullException), () => new Channel(null, ChannelCredentials.Insecure));
  30. }
  31. [Test]
  32. public void Constructor_RejectsDuplicateOptions()
  33. {
  34. var options = new ChannelOption[]
  35. {
  36. new ChannelOption(ChannelOptions.PrimaryUserAgentString, "ABC"),
  37. new ChannelOption(ChannelOptions.PrimaryUserAgentString, "XYZ")
  38. };
  39. Assert.Throws(typeof(ArgumentException), () => new Channel("127.0.0.1", ChannelCredentials.Insecure, options));
  40. }
  41. [Test]
  42. public void State_IdleAfterCreation()
  43. {
  44. var channel = new Channel("localhost", ChannelCredentials.Insecure);
  45. Assert.AreEqual(ChannelState.Idle, channel.State);
  46. channel.ShutdownAsync().Wait();
  47. }
  48. [Test]
  49. public void WaitForStateChangedAsync_InvalidArgument()
  50. {
  51. var channel = new Channel("localhost", ChannelCredentials.Insecure);
  52. Assert.ThrowsAsync(typeof(ArgumentException), async () => await channel.WaitForStateChangedAsync(ChannelState.Shutdown));
  53. channel.ShutdownAsync().Wait();
  54. }
  55. [Test]
  56. public void ResolvedTarget()
  57. {
  58. var channel = new Channel("127.0.0.1", ChannelCredentials.Insecure);
  59. Assert.IsTrue(channel.ResolvedTarget.Contains("127.0.0.1"));
  60. channel.ShutdownAsync().Wait();
  61. }
  62. [Test]
  63. public void Shutdown_AllowedOnlyOnce()
  64. {
  65. var channel = new Channel("localhost", ChannelCredentials.Insecure);
  66. channel.ShutdownAsync().Wait();
  67. Assert.ThrowsAsync(typeof(InvalidOperationException), async () => await channel.ShutdownAsync());
  68. }
  69. [Test]
  70. public async Task ShutdownTokenCancelledAfterShutdown()
  71. {
  72. var channel = new Channel("localhost", ChannelCredentials.Insecure);
  73. Assert.IsFalse(channel.ShutdownToken.IsCancellationRequested);
  74. var shutdownTask = channel.ShutdownAsync();
  75. Assert.IsTrue(channel.ShutdownToken.IsCancellationRequested);
  76. await shutdownTask;
  77. }
  78. [Test]
  79. public async Task StateIsShutdownAfterShutdown()
  80. {
  81. var channel = new Channel("localhost", ChannelCredentials.Insecure);
  82. await channel.ShutdownAsync();
  83. Assert.AreEqual(ChannelState.Shutdown, channel.State);
  84. }
  85. [Test]
  86. public async Task ShutdownFinishesWaitForStateChangedAsync()
  87. {
  88. var channel = new Channel("localhost", ChannelCredentials.Insecure);
  89. var stateChangedTask = channel.WaitForStateChangedAsync(ChannelState.Idle);
  90. var shutdownTask = channel.ShutdownAsync();
  91. await stateChangedTask;
  92. await shutdownTask;
  93. }
  94. [Test]
  95. public async Task OperationsThrowAfterShutdown()
  96. {
  97. var channel = new Channel("localhost", ChannelCredentials.Insecure);
  98. await channel.ShutdownAsync();
  99. Assert.ThrowsAsync(typeof(ObjectDisposedException), async () => await channel.WaitForStateChangedAsync(ChannelState.Idle));
  100. Assert.Throws(typeof(ObjectDisposedException), () => { var x = channel.ResolvedTarget; });
  101. Assert.ThrowsAsync(typeof(TaskCanceledException), async () => await channel.ConnectAsync());
  102. }
  103. [Test]
  104. public async Task ChannelBaseShutdownAsyncInvokesShutdownAsync()
  105. {
  106. var channel = new Channel("localhost", ChannelCredentials.Insecure);
  107. ChannelBase channelBase = channel;
  108. await channelBase.ShutdownAsync();
  109. // check that Channel.ShutdownAsync has run
  110. Assert.AreEqual(ChannelState.Shutdown, channel.State);
  111. }
  112. }
  113. }