/// <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();
            }
        }
Beispiel #2
0
        /// <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;
                }
            }
        }
Beispiel #3
0
 public override TResponse BlockingUnaryCall <TRequest, TResponse>(Method <TRequest, TResponse> method
                                                                   , string host
                                                                   , CallOptions options
                                                                   , TRequest request)
 {
     return(Calls.BlockingUnaryCall(CreateCall(method, host, options), request));
 }
Beispiel #4
0
        /// <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);
        }
Beispiel #5
0
        public void UnaryCallPerformance()
        {
            var call = new Call <string, string>(ServiceName, EchoMethod, channel, Metadata.Empty);

            BenchmarkUtil.RunBenchmark(100, 100,
                                       () => { Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); });
        }
Beispiel #6
0
        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();
        }
Beispiel #7
0
        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);
        }
Beispiel #8
0
        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"));
 }
Beispiel #10
0
        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));
        }
Beispiel #11
0
        /*
         * 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));
 }
Beispiel #13
0
        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"));
        }
Beispiel #14
0
 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"));
 }
Beispiel #15
0
 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"));
 }
Beispiel #16
0
        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(), ""));
        }
Beispiel #17
0
        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"));
        }
Beispiel #18
0
        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);
        }
Beispiel #20
0
        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/"));
        }
Beispiel #21
0
        /// <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);
        }
Beispiel #23
0
 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"));
 }
Beispiel #24
0
        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"); });
        }
Beispiel #25
0
        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);
        }
Beispiel #26
0
        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);
        }
Beispiel #28
0
        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));
        }
Beispiel #29
0
        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"));
        }
Beispiel #30
0
        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 });
        }