mirror of https://github.com/grpc/grpc.git
Conflicts: src/csharp/ext/grpc_csharp_ext.cpull/2612/head
commit
b457cd831a
91 changed files with 2045 additions and 639 deletions
@ -0,0 +1,70 @@ |
||||
GRPC Health Checking Protocol |
||||
================================ |
||||
|
||||
Health checks are used to probe whether the server is able to handle rpcs. The |
||||
client-to-server health checking can happen from point to point or via some |
||||
control system. A server may choose to reply “unhealthy” because it |
||||
is not ready to take requests, it is shutting down or some other reason. |
||||
The client can act accordingly if the response is not received within some time |
||||
window or the response says unhealthy in it. |
||||
|
||||
|
||||
A GRPC service is used as the health checking mechanism for both simple |
||||
client-to-server scenario and other control systems such as load-balancing. |
||||
Being a high |
||||
level service provides some benefits. Firstly, since it is a GRPC service |
||||
itself, doing a health check is in the same format as a normal rpc. Secondly, |
||||
it has rich semantics such as per-service health status. Thirdly, as a GRPC |
||||
service, it is able reuse all the existing billing, quota infrastructure, etc, |
||||
and thus the server has full control over the access of the health checking |
||||
service. |
||||
|
||||
## Service Definition |
||||
|
||||
The server should export a service defined in the following proto: |
||||
|
||||
``` |
||||
syntax = "proto3"; |
||||
|
||||
package grpc.health.v1alpha; |
||||
|
||||
message HealthCheckRequest { |
||||
string service = 1; |
||||
} |
||||
|
||||
message HealthCheckResponse { |
||||
enum ServingStatus { |
||||
UNKNOWN = 0; |
||||
SERVING = 1; |
||||
NOT_SERVING = 2; |
||||
} |
||||
ServingStatus status = 1; |
||||
} |
||||
|
||||
service Health { |
||||
rpc Check(HealthCheckRequest) returns (HealthCheckResponse); |
||||
} |
||||
``` |
||||
|
||||
A client can query the server’s health status by calling the `Check` method, and |
||||
a deadline should be set on the rpc. The client can optionally set the service |
||||
name it wants to query for health status. The suggested format of service name |
||||
is `package_names.ServiceName`, such as `grpc.health.v1alpha.Health`. |
||||
|
||||
The server should register all the services manually and set |
||||
the individual status, including an empty service name and its status. For each |
||||
request received, if the service name can be found in the registry, |
||||
a response must be sent back with an `OK` status and the status field should be |
||||
set to `SERVING` or `NOT_SERVING` accordingly. If the service name is not |
||||
registered, the server returns a `NOT_FOUND` GRPC status. |
||||
|
||||
The server should use an empty string as the key for server’s |
||||
overall health status, so that a client not interested in a specific service can |
||||
query the server's status with an empty request. The server can just do exact |
||||
matching of the service name without support of any kind of wildcard matching. |
||||
However, the service owner has the freedom to implement more complicated |
||||
matching semantics that both the client and server agree upon. |
||||
|
||||
A client can declare the server as unhealthy if the rpc is not finished after |
||||
some amount of time. The client should be able to handle the case where server |
||||
does not have the Health service. |
@ -0,0 +1,128 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
public class CompressionTest |
||||
{ |
||||
MockServiceHelper helper; |
||||
Server server; |
||||
Channel channel; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
helper = new MockServiceHelper(); |
||||
|
||||
server = helper.GetServer(); |
||||
server.Start(); |
||||
channel = helper.GetChannel(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
channel.Dispose(); |
||||
server.ShutdownAsync().Wait(); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void CleanupClass() |
||||
{ |
||||
GrpcEnvironment.Shutdown(); |
||||
} |
||||
|
||||
[Test] |
||||
public void WriteOptions_Unary() |
||||
{ |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
context.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
||||
return request; |
||||
}); |
||||
|
||||
var callOptions = new CallOptions(writeOptions: new WriteOptions(WriteFlags.NoCompress)); |
||||
Calls.BlockingUnaryCall(helper.CreateUnaryCall(callOptions), "abc"); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task WriteOptions_DuplexStreaming() |
||||
{ |
||||
helper.DuplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) => |
||||
{ |
||||
await requestStream.ToList(); |
||||
|
||||
context.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
||||
|
||||
await context.WriteResponseHeadersAsync(new Metadata { { "ascii-header", "abcdefg" } }); |
||||
|
||||
await responseStream.WriteAsync("X"); |
||||
|
||||
responseStream.WriteOptions = null; |
||||
await responseStream.WriteAsync("Y"); |
||||
|
||||
responseStream.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
||||
await responseStream.WriteAsync("Z"); |
||||
}); |
||||
|
||||
var callOptions = new CallOptions(writeOptions: new WriteOptions(WriteFlags.NoCompress)); |
||||
var call = Calls.AsyncDuplexStreamingCall(helper.CreateDuplexStreamingCall(callOptions)); |
||||
|
||||
// check that write options from call options are propagated to request stream. |
||||
Assert.IsTrue((call.RequestStream.WriteOptions.Flags & WriteFlags.NoCompress) != 0); |
||||
|
||||
call.RequestStream.WriteOptions = new WriteOptions(); |
||||
await call.RequestStream.WriteAsync("A"); |
||||
|
||||
call.RequestStream.WriteOptions = null; |
||||
await call.RequestStream.WriteAsync("B"); |
||||
|
||||
call.RequestStream.WriteOptions = new WriteOptions(WriteFlags.NoCompress); |
||||
await call.RequestStream.WriteAsync("C"); |
||||
|
||||
await call.RequestStream.CompleteAsync(); |
||||
|
||||
await call.ResponseStream.ToList(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,122 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
public class ContextPropagationTest |
||||
{ |
||||
MockServiceHelper helper; |
||||
Server server; |
||||
Channel channel; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
helper = new MockServiceHelper(); |
||||
|
||||
server = helper.GetServer(); |
||||
server.Start(); |
||||
channel = helper.GetChannel(); |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
channel.Dispose(); |
||||
server.ShutdownAsync().Wait(); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void CleanupClass() |
||||
{ |
||||
GrpcEnvironment.Shutdown(); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task PropagateCancellation() |
||||
{ |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
// check that we didn't obtain the default cancellation token. |
||||
Assert.IsTrue(context.CancellationToken.CanBeCanceled); |
||||
return "PASS"; |
||||
}); |
||||
|
||||
helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) => |
||||
{ |
||||
var propagationToken = context.CreatePropagationToken(); |
||||
Assert.IsNotNull(propagationToken.ParentCall); |
||||
|
||||
var callOptions = new CallOptions(propagationToken: propagationToken); |
||||
return await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOptions), "xyz"); |
||||
}); |
||||
|
||||
var cts = new CancellationTokenSource(); |
||||
var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall(new CallOptions(cancellationToken: cts.Token))); |
||||
await call.RequestStream.CompleteAsync(); |
||||
Assert.AreEqual("PASS", await call); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task PropagateDeadline() |
||||
{ |
||||
var deadline = DateTime.UtcNow.AddDays(7); |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
Assert.IsTrue(context.Deadline < deadline.AddMinutes(1)); |
||||
Assert.IsTrue(context.Deadline > deadline.AddMinutes(-1)); |
||||
return "PASS"; |
||||
}); |
||||
|
||||
helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) => |
||||
{ |
||||
var callOptions = new CallOptions(propagationToken: context.CreatePropagationToken()); |
||||
return await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOptions), "xyz"); |
||||
}); |
||||
|
||||
var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall(new CallOptions(deadline: deadline))); |
||||
await call.RequestStream.CompleteAsync(); |
||||
Assert.AreEqual("PASS", await call); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,248 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
/// <summary> |
||||
/// Allows setting up a mock service in the client-server tests easily. |
||||
/// </summary> |
||||
public class MockServiceHelper |
||||
{ |
||||
public const string ServiceName = "tests.Test"; |
||||
|
||||
public static readonly Method<string, string> UnaryMethod = new Method<string, string>( |
||||
MethodType.Unary, |
||||
ServiceName, |
||||
"Unary", |
||||
Marshallers.StringMarshaller, |
||||
Marshallers.StringMarshaller); |
||||
|
||||
public static readonly Method<string, string> ClientStreamingMethod = new Method<string, string>( |
||||
MethodType.ClientStreaming, |
||||
ServiceName, |
||||
"ClientStreaming", |
||||
Marshallers.StringMarshaller, |
||||
Marshallers.StringMarshaller); |
||||
|
||||
public static readonly Method<string, string> ServerStreamingMethod = new Method<string, string>( |
||||
MethodType.ServerStreaming, |
||||
ServiceName, |
||||
"ServerStreaming", |
||||
Marshallers.StringMarshaller, |
||||
Marshallers.StringMarshaller); |
||||
|
||||
public static readonly Method<string, string> DuplexStreamingMethod = new Method<string, string>( |
||||
MethodType.DuplexStreaming, |
||||
ServiceName, |
||||
"DuplexStreaming", |
||||
Marshallers.StringMarshaller, |
||||
Marshallers.StringMarshaller); |
||||
|
||||
readonly string host; |
||||
readonly ServerServiceDefinition serviceDefinition; |
||||
|
||||
UnaryServerMethod<string, string> unaryHandler; |
||||
ClientStreamingServerMethod<string, string> clientStreamingHandler; |
||||
ServerStreamingServerMethod<string, string> serverStreamingHandler; |
||||
DuplexStreamingServerMethod<string, string> duplexStreamingHandler; |
||||
|
||||
Server server; |
||||
Channel channel; |
||||
|
||||
public MockServiceHelper(string host = null) |
||||
{ |
||||
this.host = host ?? "localhost"; |
||||
|
||||
serviceDefinition = ServerServiceDefinition.CreateBuilder(ServiceName) |
||||
.AddMethod(UnaryMethod, (request, context) => unaryHandler(request, context)) |
||||
.AddMethod(ClientStreamingMethod, (requestStream, context) => clientStreamingHandler(requestStream, context)) |
||||
.AddMethod(ServerStreamingMethod, (request, responseStream, context) => serverStreamingHandler(request, responseStream, context)) |
||||
.AddMethod(DuplexStreamingMethod, (requestStream, responseStream, context) => duplexStreamingHandler(requestStream, responseStream, context)) |
||||
.Build(); |
||||
|
||||
var defaultStatus = new Status(StatusCode.Unknown, "Default mock implementation. Please provide your own."); |
||||
|
||||
unaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
context.Status = defaultStatus; |
||||
return ""; |
||||
}); |
||||
|
||||
clientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) => |
||||
{ |
||||
context.Status = defaultStatus; |
||||
return ""; |
||||
}); |
||||
|
||||
serverStreamingHandler = new ServerStreamingServerMethod<string, string>(async (request, responseStream, context) => |
||||
{ |
||||
context.Status = defaultStatus; |
||||
}); |
||||
|
||||
duplexStreamingHandler = new DuplexStreamingServerMethod<string, string>(async (requestStream, responseStream, context) => |
||||
{ |
||||
context.Status = defaultStatus; |
||||
}); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Returns the default server for this service and creates one if not yet created. |
||||
/// </summary> |
||||
public Server GetServer() |
||||
{ |
||||
if (server == null) |
||||
{ |
||||
server = new Server |
||||
{ |
||||
Services = { serviceDefinition }, |
||||
Ports = { { Host, ServerPort.PickUnused, ServerCredentials.Insecure } } |
||||
}; |
||||
} |
||||
return server; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Returns the default channel for this service and creates one if not yet created. |
||||
/// </summary> |
||||
public Channel GetChannel() |
||||
{ |
||||
if (channel == null) |
||||
{ |
||||
channel = new Channel(Host, GetServer().Ports.Single().BoundPort, Credentials.Insecure); |
||||
} |
||||
return channel; |
||||
} |
||||
|
||||
public CallInvocationDetails<string, string> CreateUnaryCall(CallOptions options = null) |
||||
{ |
||||
options = options ?? new CallOptions(); |
||||
return new CallInvocationDetails<string, string>(channel, UnaryMethod, options); |
||||
} |
||||
|
||||
public CallInvocationDetails<string, string> CreateClientStreamingCall(CallOptions options = null) |
||||
{ |
||||
options = options ?? new CallOptions(); |
||||
return new CallInvocationDetails<string, string>(channel, ClientStreamingMethod, options); |
||||
} |
||||
|
||||
public CallInvocationDetails<string, string> CreateServerStreamingCall(CallOptions options = null) |
||||
{ |
||||
options = options ?? new CallOptions(); |
||||
return new CallInvocationDetails<string, string>(channel, ServerStreamingMethod, options); |
||||
} |
||||
|
||||
public CallInvocationDetails<string, string> CreateDuplexStreamingCall(CallOptions options = null) |
||||
{ |
||||
options = options ?? new CallOptions(); |
||||
return new CallInvocationDetails<string, string>(channel, DuplexStreamingMethod, options); |
||||
} |
||||
|
||||
public string Host |
||||
{ |
||||
get |
||||
{ |
||||
return this.host; |
||||
} |
||||
} |
||||
|
||||
public ServerServiceDefinition ServiceDefinition |
||||
{ |
||||
get |
||||
{ |
||||
return this.serviceDefinition; |
||||
} |
||||
} |
||||
|
||||
public UnaryServerMethod<string, string> UnaryHandler |
||||
{ |
||||
get |
||||
{ |
||||
return this.unaryHandler; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
unaryHandler = value; |
||||
} |
||||
} |
||||
|
||||
public ClientStreamingServerMethod<string, string> ClientStreamingHandler |
||||
{ |
||||
get |
||||
{ |
||||
return this.clientStreamingHandler; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
clientStreamingHandler = value; |
||||
} |
||||
} |
||||
|
||||
public ServerStreamingServerMethod<string, string> ServerStreamingHandler |
||||
{ |
||||
get |
||||
{ |
||||
return this.serverStreamingHandler; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
serverStreamingHandler = value; |
||||
} |
||||
} |
||||
|
||||
public DuplexStreamingServerMethod<string, string> DuplexStreamingHandler |
||||
{ |
||||
get |
||||
{ |
||||
return this.duplexStreamingHandler; |
||||
} |
||||
|
||||
set |
||||
{ |
||||
duplexStreamingHandler = value; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,136 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Diagnostics; |
||||
using System.Linq; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
using NUnit.Framework; |
||||
|
||||
namespace Grpc.Core.Tests |
||||
{ |
||||
/// <summary> |
||||
/// Tests for response headers support. |
||||
/// </summary> |
||||
public class ResponseHeadersTest |
||||
{ |
||||
MockServiceHelper helper; |
||||
Server server; |
||||
Channel channel; |
||||
|
||||
Metadata headers; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
helper = new MockServiceHelper(); |
||||
|
||||
server = helper.GetServer(); |
||||
server.Start(); |
||||
channel = helper.GetChannel(); |
||||
|
||||
headers = new Metadata { { "ascii-header", "abcdefg" } }; |
||||
} |
||||
|
||||
[TearDown] |
||||
public void Cleanup() |
||||
{ |
||||
channel.Dispose(); |
||||
server.ShutdownAsync().Wait(); |
||||
} |
||||
|
||||
[TestFixtureTearDown] |
||||
public void CleanupClass() |
||||
{ |
||||
GrpcEnvironment.Shutdown(); |
||||
} |
||||
|
||||
[Test] |
||||
public void WriteResponseHeaders_NullNotAllowed() |
||||
{ |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
Assert.Throws(typeof(NullReferenceException), async () => await context.WriteResponseHeadersAsync(null)); |
||||
return "PASS"; |
||||
}); |
||||
|
||||
Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "")); |
||||
} |
||||
|
||||
[Test] |
||||
public void WriteResponseHeaders_AllowedOnlyOnce() |
||||
{ |
||||
helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) => |
||||
{ |
||||
await context.WriteResponseHeadersAsync(headers); |
||||
try |
||||
{ |
||||
await context.WriteResponseHeadersAsync(headers); |
||||
Assert.Fail(); |
||||
} |
||||
catch (InvalidOperationException expected) |
||||
{ |
||||
} |
||||
return "PASS"; |
||||
}); |
||||
|
||||
Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "")); |
||||
} |
||||
|
||||
[Test] |
||||
public async Task WriteResponseHeaders_NotAllowedAfterWrite() |
||||
{ |
||||
helper.ServerStreamingHandler = new ServerStreamingServerMethod<string, string>(async (request, responseStream, context) => |
||||
{ |
||||
await responseStream.WriteAsync("A"); |
||||
try |
||||
{ |
||||
await context.WriteResponseHeadersAsync(headers); |
||||
Assert.Fail(); |
||||
} |
||||
catch (InvalidOperationException expected) |
||||
{ |
||||
} |
||||
await responseStream.WriteAsync("B"); |
||||
}); |
||||
|
||||
var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), ""); |
||||
var responses = await call.ResponseStream.ToList(); |
||||
CollectionAssert.AreEqual(new[] { "A", "B" }, responses); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,63 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Compression level based on grpc_compression_level from grpc/compression.h |
||||
/// </summary> |
||||
public enum CompressionLevel |
||||
{ |
||||
/// <summary> |
||||
/// No compression. |
||||
/// </summary> |
||||
None = 0, |
||||
|
||||
/// <summary> |
||||
/// Low compression. |
||||
/// </summary> |
||||
Low, |
||||
|
||||
/// <summary> |
||||
/// Medium compression. |
||||
/// </summary> |
||||
Medium, |
||||
|
||||
/// <summary> |
||||
/// High compression. |
||||
/// </summary> |
||||
High, |
||||
} |
||||
} |
@ -0,0 +1,139 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
using System.Threading; |
||||
|
||||
using Grpc.Core.Internal; |
||||
using Grpc.Core.Utils; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Token for propagating context of server side handlers to child calls. |
||||
/// In situations when a backend is making calls to another backend, |
||||
/// it makes sense to propagate properties like deadline and cancellation |
||||
/// token of the server call to the child call. |
||||
/// C core provides some other contexts (like tracing context) that |
||||
/// are not accessible to C# layer, but this token still allows propagating them. |
||||
/// </summary> |
||||
public class ContextPropagationToken |
||||
{ |
||||
/// <summary> |
||||
/// Default propagation mask used by C core. |
||||
/// </summary> |
||||
const ContextPropagationFlags DefaultCoreMask = (ContextPropagationFlags)0xffff; |
||||
|
||||
/// <summary> |
||||
/// Default propagation mask used by C# - we want to propagate deadline |
||||
/// and cancellation token by our own means. |
||||
/// </summary> |
||||
internal const ContextPropagationFlags DefaultMask = DefaultCoreMask |
||||
& ~ContextPropagationFlags.Deadline & ~ContextPropagationFlags.Cancellation; |
||||
|
||||
readonly CallSafeHandle parentCall; |
||||
readonly DateTime deadline; |
||||
readonly CancellationToken cancellationToken; |
||||
readonly ContextPropagationOptions options; |
||||
|
||||
internal ContextPropagationToken(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options) |
||||
{ |
||||
this.parentCall = Preconditions.CheckNotNull(parentCall); |
||||
this.deadline = deadline; |
||||
this.cancellationToken = cancellationToken; |
||||
this.options = options ?? ContextPropagationOptions.Default; |
||||
} |
||||
|
||||
internal CallSafeHandle ParentCall |
||||
{ |
||||
get |
||||
{ |
||||
return this.parentCall; |
||||
} |
||||
} |
||||
|
||||
internal DateTime Deadline |
||||
{ |
||||
get |
||||
{ |
||||
return this.deadline; |
||||
} |
||||
} |
||||
|
||||
internal CancellationToken CancellationToken |
||||
{ |
||||
get |
||||
{ |
||||
return this.cancellationToken; |
||||
} |
||||
} |
||||
|
||||
internal ContextPropagationOptions Options |
||||
{ |
||||
get |
||||
{ |
||||
return this.options; |
||||
} |
||||
} |
||||
|
||||
internal bool IsPropagateDeadline |
||||
{ |
||||
get { return false; } |
||||
} |
||||
|
||||
internal bool IsPropagateCancellation |
||||
{ |
||||
get { return false; } |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Options for <see cref="ContextPropagationToken"/>. |
||||
/// </summary> |
||||
public class ContextPropagationOptions |
||||
{ |
||||
public static readonly ContextPropagationOptions Default = new ContextPropagationOptions(); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Context propagation flags from grpc/grpc.h. |
||||
/// </summary> |
||||
[Flags] |
||||
internal enum ContextPropagationFlags |
||||
{ |
||||
Deadline = 1, |
||||
CensusStatsContext = 2, |
||||
CensusTracingContext = 4, |
||||
Cancellation = 8 |
||||
} |
||||
} |
@ -0,0 +1,82 @@ |
||||
#region Copyright notice and license |
||||
|
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
#endregion |
||||
|
||||
using System; |
||||
|
||||
namespace Grpc.Core |
||||
{ |
||||
/// <summary> |
||||
/// Flags for write operations. |
||||
/// </summary> |
||||
[Flags] |
||||
public enum WriteFlags |
||||
{ |
||||
/// <summary> |
||||
/// Hint that the write may be buffered and need not go out on the wire immediately. |
||||
/// gRPC is free to buffer the message until the next non-buffered |
||||
/// write, or until write stream completion, but it need not buffer completely or at all. |
||||
/// </summary> |
||||
BufferHint = 0x1, |
||||
|
||||
/// <summary> |
||||
/// Force compression to be disabled for a particular write. |
||||
/// </summary> |
||||
NoCompress = 0x2 |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Options for write operations. |
||||
/// </summary> |
||||
public class WriteOptions |
||||
{ |
||||
/// <summary> |
||||
/// Default write options. |
||||
/// </summary> |
||||
public static readonly WriteOptions Default = new WriteOptions(); |
||||
|
||||
private WriteFlags flags; |
||||
|
||||
public WriteOptions(WriteFlags flags = default(WriteFlags)) |
||||
{ |
||||
this.flags = flags; |
||||
} |
||||
|
||||
public WriteFlags Flags |
||||
{ |
||||
get |
||||
{ |
||||
return this.flags; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,2 @@ |
||||
|
||||
SandCastle project files to generate HTML reference documentation. |
@ -0,0 +1,30 @@ |
||||
# Copyright 2015, Google Inc. |
||||
# All rights reserved. |
||||
# |
||||
# Redistribution and use in source and binary forms, with or without |
||||
# modification, are permitted provided that the following conditions are |
||||
# met: |
||||
# |
||||
# * Redistributions of source code must retain the above copyright |
||||
# notice, this list of conditions and the following disclaimer. |
||||
# * Redistributions in binary form must reproduce the above |
||||
# copyright notice, this list of conditions and the following disclaimer |
||||
# in the documentation and/or other materials provided with the |
||||
# distribution. |
||||
# * Neither the name of Google Inc. nor the names of its |
||||
# contributors may be used to endorse or promote products derived from |
||||
# this software without specific prior written permission. |
||||
# |
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
|
Loading…
Reference in new issue