Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: third_party/grpc/src/csharp/Grpc.Core.Tests/ContextPropagationTest.cs

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 public class ContextPropagationTest
47 {
48 MockServiceHelper helper;
49 Server server;
50 Channel channel;
51
52 [SetUp]
53 public void Init()
54 {
55 helper = new MockServiceHelper();
56
57 server = helper.GetServer();
58 server.Start();
59 channel = helper.GetChannel();
60 }
61
62 [TearDown]
63 public void Cleanup()
64 {
65 channel.ShutdownAsync().Wait();
66 server.ShutdownAsync().Wait();
67 }
68
69 [Test]
70 public async Task PropagateCancellation()
71 {
72 var readyToCancelTcs = new TaskCompletionSource<object>();
73 var successTcs = new TaskCompletionSource<string>();
74
75 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r equest, context) =>
76 {
77 readyToCancelTcs.SetResult(null); // child call running, ready to parent call
78
79 while (!context.CancellationToken.IsCancellationRequested)
80 {
81 await Task.Delay(10);
82 }
83 successTcs.SetResult("CHILD_CALL_CANCELLED");
84 return "";
85 });
86
87 helper.ClientStreamingHandler = new ClientStreamingServerMethod<stri ng, string>(async (requestStream, context) =>
88 {
89 var propagationToken = context.CreatePropagationToken();
90 Assert.IsNotNull(propagationToken.ParentCall);
91
92 var callOptions = new CallOptions(propagationToken: propagationT oken);
93 try
94 {
95 await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOption s), "xyz");
96 }
97 catch(RpcException)
98 {
99 // Child call will get cancelled, eat the exception.
100 }
101 return "";
102 });
103
104 var cts = new CancellationTokenSource();
105 var parentCall = Calls.AsyncClientStreamingCall(helper.CreateClientS treamingCall(new CallOptions(cancellationToken: cts.Token)));
106 await readyToCancelTcs.Task;
107 cts.Cancel();
108 Assert.Throws(typeof(RpcException), async () => await parentCall);
109 Assert.AreEqual("CHILD_CALL_CANCELLED", await successTcs.Task);
110 }
111
112 [Test]
113 public async Task PropagateDeadline()
114 {
115 var deadline = DateTime.UtcNow.AddDays(7);
116 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r equest, context) =>
117 {
118 Assert.IsTrue(context.Deadline < deadline.AddMinutes(1));
119 Assert.IsTrue(context.Deadline > deadline.AddMinutes(-1));
120 return "PASS";
121 });
122
123 helper.ClientStreamingHandler = new ClientStreamingServerMethod<stri ng, string>(async (requestStream, context) =>
124 {
125 Assert.Throws(typeof(ArgumentException), () =>
126 {
127 // Trying to override deadline while propagating deadline fr om parent call will throw.
128 Calls.BlockingUnaryCall(helper.CreateUnaryCall(
129 new CallOptions(deadline: DateTime.UtcNow.AddDays(8),
130 propagationToken: context.CreatePropagat ionToken())), "");
131 });
132
133 var callOptions = new CallOptions(propagationToken: context.Crea tePropagationToken());
134 return await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOpt ions), "xyz");
135 });
136
137 var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreami ngCall(new CallOptions(deadline: deadline)));
138 await call.RequestStream.CompleteAsync();
139 Assert.AreEqual("PASS", await call);
140 }
141
142 [Test]
143 public async Task SuppressDeadlinePropagation()
144 {
145 helper.UnaryHandler = new UnaryServerMethod<string, string>(async (r equest, context) =>
146 {
147 Assert.AreEqual(DateTime.MaxValue, context.Deadline);
148 return "PASS";
149 });
150
151 helper.ClientStreamingHandler = new ClientStreamingServerMethod<stri ng, string>(async (requestStream, context) =>
152 {
153 Assert.IsTrue(context.CancellationToken.CanBeCanceled);
154
155 var callOptions = new CallOptions(propagationToken: context.Crea tePropagationToken(new ContextPropagationOptions(propagateDeadline: false)));
156 return await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOpt ions), "xyz");
157 });
158
159 var cts = new CancellationTokenSource();
160 var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreami ngCall(new CallOptions(deadline: DateTime.UtcNow.AddDays(7))));
161 await call.RequestStream.CompleteAsync();
162 Assert.AreEqual("PASS", await call);
163 }
164 }
165 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698