/// <summary> /// Invokes a simple remote call in a blocking fashion. /// </summary> public override TResponse BlockingUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { var trace = this.ResolveTraceSpan().Child(); trace.Record(Annotations.ClientSend()); trace.Record(Annotations.ServiceName(_serviceName)); trace.Record(Annotations.Rpc("grpc")); var channel = new Channel(_target, ChannelCredentials.Insecure); var call = CreateCall(channel, method, host, options, trace); try { trace.Record(Annotations.Tag("grpc.host", _target)); trace.Record(Annotations.Tag("grpc.request", JsonConvert.SerializeObject(request))); var response = Calls.BlockingUnaryCall(call, request); trace.Record(Annotations.Tag("grpc.response", JsonConvert.SerializeObject(response))); return(response as TResponse); } finally { trace.Record(Annotations.ClientRecv()); channel.ShutdownAsync(); } }
/// <summary> /// Invokes a simple remote call in a blocking fashion. /// </summary> public override TResponse BlockingUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { int retryCount = 0; while (true) { try { var call = CreateCall(method, host, options); return(Calls.BlockingUnaryCall(call, request)); } catch (RpcException ex) { retryCount++; if (channel.State != ChannelState.TransientFailure || retryCount > maxRetryCount) { throw; } } catch (Exception ex) { throw; } } }
public override TResponse BlockingUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method , string host , CallOptions options , TRequest request) { return(Calls.BlockingUnaryCall(CreateCall(method, host, options), request)); }
/// <summary> /// Invokes a simple remote call in a blocking fashion. /// </summary> public override TResponse BlockingUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { var sd = ContextManager.GlobalTraceId; var result = Calls.BlockingUnaryCall(CreateCall(method, host, options), request); return(result); }
public void UnaryCallPerformance() { var call = new Call <string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); BenchmarkUtil.RunBenchmark(100, 100, () => { Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); }); }
public void UnknownMethodHandler() { Server server = new Server(); server.AddServiceDefinition( ServerServiceDefinition.CreateBuilder(serviceName).Build()); int port = server.AddListeningPort(host + ":0"); server.Start(); using (Channel channel = new Channel(host + ":" + port)) { var call = new Call <string, string>(serviceName, unaryEchoStringMethod, channel, Metadata.Empty); try { Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); Assert.Fail(); } catch (RpcException e) { Assert.AreEqual(StatusCode.Unimplemented, e.Status.StatusCode); } } server.ShutdownAsync().Wait(); }
public async Task PropagateDeadline() { var deadline = DateTime.UtcNow.AddDays(7); helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => { Assert.IsTrue(context.Deadline < deadline.AddMinutes(1)); Assert.IsTrue(context.Deadline > deadline.AddMinutes(-1)); return(Task.FromResult("PASS")); }); helper.ClientStreamingHandler = new ClientStreamingServerMethod <string, string>(async(requestStream, context) => { Assert.Throws(typeof(ArgumentException), () => { // Trying to override deadline while propagating deadline from parent call will throw. Calls.BlockingUnaryCall(helper.CreateUnaryCall( new CallOptions(deadline: DateTime.UtcNow.AddDays(8), propagationToken: context.CreatePropagationToken())), ""); }); 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); }
public void UnaryCallPerformance() { var profiler = new BasicProfiler(); Profilers.SetForCurrentThread(profiler); helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { return(request); }); var callDetails = helper.CreateUnaryCall(); for (int i = 0; i < 3000; i++) { Calls.BlockingUnaryCall(callDetails, "ABC"); } profiler.Reset(); for (int i = 0; i < 3000; i++) { Calls.BlockingUnaryCall(callDetails, "ABC"); } profiler.Dump("latency_trace_csharp.txt"); }
public void UnaryCall() { helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => { return(Task.FromResult(request)); }); Assert.AreEqual("ABC", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "ABC")); }
public void UnaryCall_DisposedChannel() { channel.Dispose(); var call = new Call <string, string>(ServiceName, EchoMethod, channel, Metadata.Empty); Assert.Throws(typeof(ObjectDisposedException), () => Calls.BlockingUnaryCall(call, "ABC", CancellationToken.None)); }
/* * Missing in gRPC library, but very useful (not used in this example, though) */ public virtual TResponse BlockingUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) where TRequest : class where TResponse : class { var call = new CallInvocationDetails <TRequest, TResponse>(this.ch, method, host, options); return(Calls.BlockingUnaryCall(call, request)); }
/// <summary> /// Invokes a simple remote call in a blocking fashion. /// </summary> public override TResponse BlockingUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { return(_resourceManager.ExecuteAction((grpcResource, retryCount) => { var overriddenOptions = OverrideCallOptions(options); var call = new CallInvocationDetails <TRequest, TResponse>(grpcResource.Channel, method, host, overriddenOptions); return Calls.BlockingUnaryCall(call, request); }, _shouldRetry, _onError)); }
public void StartBlockingUnaryCallAfterChannelShutdown() { // create a channel and immediately shut it down. var channel = new Channel("127.0.0.1", 1000, ChannelCredentials.Insecure); channel.ShutdownAsync().Wait(); // also shuts down GrpcEnvironment Assert.Throws(typeof(ObjectDisposedException), () => Calls.BlockingUnaryCall(new CallInvocationDetails <string, string>(channel, dummyUnaryMethod, new CallOptions()), "THE REQUEST")); }
public void ServerCallContext_HostAndMethodPresent() { helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => { Assert.IsTrue(context.Host.Contains(Host)); Assert.AreEqual("/tests.Test/Unary", context.Method); return(Task.FromResult("PASS")); }); Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc")); }
public void ServerCallContext_AuthContextNotPopulated() { helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => { Assert.IsFalse(context.AuthContext.IsPeerAuthenticated); Assert.AreEqual(0, context.AuthContext.Properties.Count()); return(Task.FromResult("PASS")); }); Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc")); }
public void WriteResponseHeaders_NullNotAllowed() { helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { Assert.Throws(typeof(ArgumentNullException), async() => await context.WriteResponseHeadersAsync(null)); return("PASS"); }); Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "")); }
public async Task UnaryCall() { helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { return(request); }); Assert.AreEqual("ABC", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "ABC")); Assert.AreEqual("ABC", await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "ABC")); }
public void PeerInfoPresent() { helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { return(context.Peer); }); string peer = Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"); Assert.IsTrue(peer.Contains(Host)); }
/// <summary> /// Invokes a simple remote call in a blocking fashion. /// </summary> public override TResponse BlockingUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { var call = CreateCall(method, host, options); TResponse response = null; _UnaryCallPolicy.Execute(() => { response = Calls.BlockingUnaryCall(call, request); }); return(response); }
public void UserAgentStringPresent() { helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { return(context.RequestHeaders.Where(entry => entry.Key == "user-agent").Single().Value); }); string userAgent = Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc"); Assert.IsTrue(userAgent.StartsWith("grpc-csharp/")); }
/// <summary> /// Invokes a simple remote call in a blocking fashion. /// </summary> public override TResponse BlockingUnaryCall <TRequest, TResponse>( Method <TRequest, TResponse> method, string host, CallOptions options, TRequest request) { CallOptions updateOptions = ProcessOptions(options); var call = CreateCall(method, host, updateOptions); return(Calls.BlockingUnaryCall(call, request)); }
public void ResponseParsingError_UnaryResponse() { helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => { return(Task.FromResult("UNPARSEABLE_VALUE")); }); var ex = Assert.Throws <RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "REQUEST")); Assert.AreEqual(StatusCode.Internal, ex.Status.StatusCode); }
public void ServerCallContext_AuthContextNotPopulated() { helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => { Assert.IsFalse(context.AuthContext.IsPeerAuthenticated); // 1) security_level: TSI_SECURITY_NONE // 2) transport_security_type: 'insecure' Assert.AreEqual(2, context.AuthContext.Properties.Count()); return(Task.FromResult("PASS")); }); Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "abc")); }
public void UnaryCallPerformance() { helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { return(request); }); var callDetails = helper.CreateUnaryCall(); BenchmarkUtil.RunBenchmark(100, 100, () => { Calls.BlockingUnaryCall(callDetails, "ABC"); }); }
public void DeadlineInThePast() { helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { await Task.Delay(60000); return("FAIL"); }); var ex = Assert.Throws <RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(new CallOptions(deadline: DateTime.MinValue)), "abc")); Assert.AreEqual(StatusCode.DeadlineExceeded, ex.Status.StatusCode); }
public void WriteOptions_Unary() { helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => { context.WriteOptions = new WriteOptions(WriteFlags.NoCompress); return(Task.FromResult(request)); }); var callOptions = new CallOptions(writeOptions: new WriteOptions(WriteFlags.NoCompress)); Calls.BlockingUnaryCall(helper.CreateUnaryCall(callOptions), "abc"); }
public void RequestParsingError_UnaryRequest() { helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => { return(Task.FromResult("RESPONSE")); }); var ex = Assert.Throws <RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "UNPARSEABLE_VALUE")); // Spec doesn't define the behavior. With the current implementation server handler throws exception which results in StatusCode.Unknown. Assert.AreEqual(StatusCode.Unknown, ex.Status.StatusCode); }
public async Task UnaryCall() { helper.UnaryHandler = new UnaryServerMethod <string, string>((request, context) => { return(Task.FromResult(request)); }); Assert.AreEqual("ABC", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "ABC")); Assert.AreEqual("ABC", await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "ABC")); Assert.AreEqual("ABC", await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "ABC").ConfigureAwait(false)); }
public void HandlerDoesNotRunOnGrpcThread() { helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { if (IsRunningOnGrpcThreadPool()) { return("Server handler should not run on gRPC threadpool thread."); } return(request); }); Assert.AreEqual("ABC", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "ABC")); }
public void DeadlineExceededStatusOnTimeout() { helper.UnaryHandler = new UnaryServerMethod <string, string>(async(request, context) => { await Task.Delay(60000); return("FAIL"); }); var ex = Assert.Throws <RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(new CallOptions(deadline: DateTime.UtcNow.Add(TimeSpan.FromSeconds(5)))), "abc")); // We can't guarantee the status code always DeadlineExceeded. See issue #2685. Assert.Contains(ex.Status.StatusCode, new[] { StatusCode.DeadlineExceeded, StatusCode.Internal }); }