OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Diagnostics; |
| 36 using System.Linq; |
| 37 using System.Threading; |
| 38 using System.Threading.Tasks; |
| 39 using Grpc.Core; |
| 40 using Grpc.Core.Internal; |
| 41 using Grpc.Core.Utils; |
| 42 using NUnit.Framework; |
| 43 |
| 44 namespace Grpc.Core.Tests |
| 45 { |
| 46 /// <summary> |
| 47 /// Tests for Deadline support. |
| 48 /// </summary> |
| 49 public class TimeoutsTest |
| 50 { |
| 51 MockServiceHelper helper; |
| 52 Server server; |
| 53 Channel channel; |
| 54 |
| 55 [SetUp] |
| 56 public void Init() |
| 57 { |
| 58 helper = new MockServiceHelper(); |
| 59 |
| 60 server = helper.GetServer(); |
| 61 server.Start(); |
| 62 channel = helper.GetChannel(); |
| 63 } |
| 64 |
| 65 [TearDown] |
| 66 public void Cleanup() |
| 67 { |
| 68 channel.ShutdownAsync().Wait(); |
| 69 server.ShutdownAsync().Wait(); |
| 70 } |
| 71 |
| 72 [Test] |
| 73 public void InfiniteDeadline() |
| 74 { |
| 75 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r
equest, context) => |
| 76 { |
| 77 Assert.AreEqual(DateTime.MaxValue, context.Deadline); |
| 78 return "PASS"; |
| 79 }); |
| 80 |
| 81 // no deadline specified, check server sees infinite deadline |
| 82 Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCa
ll(), "abc")); |
| 83 |
| 84 // DateTime.MaxValue deadline specified, check server sees infinite
deadline |
| 85 Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCa
ll(new CallOptions(deadline: DateTime.MaxValue)), "abc")); |
| 86 } |
| 87 |
| 88 [Test] |
| 89 public void DeadlineTransferredToServer() |
| 90 { |
| 91 var clientDeadline = DateTime.UtcNow + TimeSpan.FromDays(7); |
| 92 |
| 93 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r
equest, context) => |
| 94 { |
| 95 // A fairly relaxed check that the deadline set by client and de
adline seen by server |
| 96 // are in agreement. C core takes care of the work with transfer
ring deadline over the wire, |
| 97 // so we don't need an exact check here. |
| 98 Assert.IsTrue(Math.Abs((clientDeadline - context.Deadline).Total
Milliseconds) < 5000); |
| 99 return "PASS"; |
| 100 }); |
| 101 Calls.BlockingUnaryCall(helper.CreateUnaryCall(new CallOptions(deadl
ine: clientDeadline)), "abc"); |
| 102 } |
| 103 |
| 104 [Test] |
| 105 public void DeadlineInThePast() |
| 106 { |
| 107 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r
equest, context) => |
| 108 { |
| 109 await Task.Delay(60000); |
| 110 return "FAIL"; |
| 111 }); |
| 112 |
| 113 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(h
elper.CreateUnaryCall(new CallOptions(deadline: DateTime.MinValue)), "abc")); |
| 114 // We can't guarantee the status code always DeadlineExceeded. See i
ssue #2685. |
| 115 Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExc
eeded, StatusCode.Internal }); |
| 116 } |
| 117 |
| 118 [Test] |
| 119 public void DeadlineExceededStatusOnTimeout() |
| 120 { |
| 121 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r
equest, context) => |
| 122 { |
| 123 await Task.Delay(60000); |
| 124 return "FAIL"; |
| 125 }); |
| 126 |
| 127 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(h
elper.CreateUnaryCall(new CallOptions(deadline: DateTime.UtcNow.Add(TimeSpan.Fro
mSeconds(5)))), "abc")); |
| 128 // We can't guarantee the status code always DeadlineExceeded. See i
ssue #2685. |
| 129 Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExc
eeded, StatusCode.Internal }); |
| 130 } |
| 131 |
| 132 [Test] |
| 133 public async Task ServerReceivesCancellationOnTimeout() |
| 134 { |
| 135 var serverReceivedCancellationTcs = new TaskCompletionSource<bool>()
; |
| 136 |
| 137 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r
equest, context) => |
| 138 { |
| 139 // wait until cancellation token is fired. |
| 140 var tcs = new TaskCompletionSource<object>(); |
| 141 context.CancellationToken.Register(() => { tcs.SetResult(null);
}); |
| 142 await tcs.Task; |
| 143 serverReceivedCancellationTcs.SetResult(true); |
| 144 return ""; |
| 145 }); |
| 146 |
| 147 var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(h
elper.CreateUnaryCall(new CallOptions(deadline: DateTime.UtcNow.Add(TimeSpan.Fro
mSeconds(5)))), "abc")); |
| 148 // We can't guarantee the status code always DeadlineExceeded. See i
ssue #2685. |
| 149 Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExc
eeded, StatusCode.Internal }); |
| 150 |
| 151 Assert.IsTrue(await serverReceivedCancellationTcs.Task); |
| 152 } |
| 153 } |
| 154 } |
OLD | NEW |