TimeoutsTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.Diagnostics;
  18. using System.Linq;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using Grpc.Core;
  22. using Grpc.Core.Internal;
  23. using Grpc.Core.Utils;
  24. using NUnit.Framework;
  25. namespace Grpc.Core.Tests
  26. {
  27. /// <summary>
  28. /// Tests for Deadline support.
  29. /// </summary>
  30. public class TimeoutsTest
  31. {
  32. MockServiceHelper helper;
  33. Server server;
  34. Channel channel;
  35. [SetUp]
  36. public void Init()
  37. {
  38. helper = new MockServiceHelper();
  39. server = helper.GetServer();
  40. server.Start();
  41. channel = helper.GetChannel();
  42. }
  43. [TearDown]
  44. public void Cleanup()
  45. {
  46. channel.ShutdownAsync().Wait();
  47. server.ShutdownAsync().Wait();
  48. }
  49. [Test]
  50. public void InfiniteDeadline()
  51. {
  52. helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
  53. {
  54. Assert.AreEqual(DateTime.MaxValue, context.Deadline);
  55. return Task.FromResult("PASS");
  56. });
  57. // no deadline specified, check server sees infinite deadline
  58. Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"));
  59. // DateTime.MaxValue deadline specified, check server sees infinite deadline
  60. Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(new CallOptions(deadline: DateTime.MaxValue)), "abc"));
  61. }
  62. [Test]
  63. public void DeadlineTransferredToServer()
  64. {
  65. var clientDeadline = DateTime.UtcNow + TimeSpan.FromDays(7);
  66. helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
  67. {
  68. // A fairly relaxed check that the deadline set by client and deadline seen by server
  69. // are in agreement. C core takes care of the work with transferring deadline over the wire,
  70. // so we don't need an exact check here.
  71. Assert.IsTrue(Math.Abs((clientDeadline - context.Deadline).TotalMilliseconds) < 5000);
  72. return Task.FromResult("PASS");
  73. });
  74. Calls.BlockingUnaryCall(helper.CreateUnaryCall(new CallOptions(deadline: clientDeadline)), "abc");
  75. }
  76. [Test]
  77. public void DeadlineInThePast()
  78. {
  79. helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) =>
  80. {
  81. await Task.Delay(60000);
  82. return "FAIL";
  83. });
  84. var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(new CallOptions(deadline: DateTime.MinValue)), "abc"));
  85. // We can't guarantee the status code always DeadlineExceeded. See issue #2685.
  86. Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExceeded, StatusCode.Internal });
  87. }
  88. [Test]
  89. public void DeadlineExceededStatusOnTimeout()
  90. {
  91. helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) =>
  92. {
  93. await Task.Delay(60000);
  94. return "FAIL";
  95. });
  96. var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(new CallOptions(deadline: DateTime.UtcNow.Add(TimeSpan.FromSeconds(5)))), "abc"));
  97. // We can't guarantee the status code always DeadlineExceeded. See issue #2685.
  98. Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExceeded, StatusCode.Internal });
  99. }
  100. [Test]
  101. public async Task ServerReceivesCancellationOnTimeout()
  102. {
  103. var serverReceivedCancellationTcs = new TaskCompletionSource<bool>();
  104. helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) =>
  105. {
  106. // wait until cancellation token is fired.
  107. var tcs = new TaskCompletionSource<object>();
  108. context.CancellationToken.Register(() => { tcs.SetResult(null); });
  109. await tcs.Task;
  110. serverReceivedCancellationTcs.SetResult(true);
  111. return "";
  112. });
  113. var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(new CallOptions(deadline: DateTime.UtcNow.Add(TimeSpan.FromSeconds(5)))), "abc"));
  114. // We can't guarantee the status code always DeadlineExceeded. See issue #2685.
  115. Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExceeded, StatusCode.Internal });
  116. Assert.IsTrue(await serverReceivedCancellationTcs.Task);
  117. }
  118. }
  119. }