Example #1
0
        public void TestServerStreamingWithResult()
        {
            GoogleAdsConfig config  = new GoogleAdsConfig();
            LoggingHandler  handler = new LoggingHandler(config);

            handler.WriteLogs = delegate(LogEntry logEntry)
            {
                Assert.AreEqual(config.ServerUrl, logEntry.Host);
                Assert.AreEqual(TEST_METHOD_IN_LOGS, logEntry.Method);
                CompareMetadata(TEST_REQUEST_METADATA, logEntry.RequestHeaders);
                CompareMetadata(TEST_RESPONSE_METADATA, logEntry.ResponseHeaders);
                Assert.AreSame(TEST_REQUEST, logEntry.Request);
                Assert.AreSame(TEST_RESPONSE, logEntry.Response);
                Assert.AreEqual(TEST_CUSTOMER_ID, logEntry.CustomerId);
                Assert.False(logEntry.IsFailure);
            };

            ClientInterceptorContext <HelloRequest, HelloResponse> context =
                GetClientInterceptorContext();
            AsyncServerStreamingCall <HelloResponse> call =
                StreamingContinuationWithResult <HelloResponse>();

            handler.HandleAsyncServerStreamingLogging(TEST_REQUEST,
                                                      TEST_RESPONSE, context, null, call);
        }
        public void TestServerStreamingWithException()
        {
            GoogleAdsConfig config  = new GoogleAdsConfig();
            LoggingHandler  handler = new LoggingHandler(config);

            handler.WriteSummaryLogs = delegate(LogEntry logEntry)
            {
                Assert.AreEqual(config.ServerUrl, logEntry.Host);
                Assert.AreEqual(TEST_METHOD_IN_LOGS, logEntry.Method);
                CompareMetadata(TEST_REQUEST_METADATA, logEntry.RequestHeaders);
                Assert.True(logEntry.IsFailure);
                HelloException helloException = logEntry.Exception as HelloException;
                Assert.NotNull(helloException);

                Assert.AreEqual(TEST_REQUEST_ID, helloException.RequestId);

                Assert.NotNull(helloException.Failure);
                Assert.AreEqual(1, helloException.Failure.Errors.Count);
                Assert.NotNull(helloException.Failure.Errors[0].ErrorCode);
                Assert.NotNull(helloException.Failure.Errors[0].ErrorCode.RequestError);
            };
            handler.WriteDetailedLogs = delegate(LogEntry logEntry)
            {
                Assert.AreEqual(config.ServerUrl, logEntry.Host);
                Assert.AreEqual(TEST_METHOD_IN_LOGS, logEntry.Method);
                CompareMetadata(TEST_REQUEST_METADATA, logEntry.RequestHeaders);
                CompareMetadata(TEST_RESPONSE_METADATA, logEntry.ResponseHeaders);
                Assert.AreSame(TEST_REQUEST, logEntry.Request);

                // Response is null if there's an exception.
                Assert.IsNull(logEntry.Response);

                Assert.AreEqual(TEST_CUSTOMER_ID, logEntry.CustomerId);
                Assert.True(logEntry.IsFailure);
                HelloException helloException = logEntry.Exception as HelloException;
                Assert.NotNull(helloException);

                Assert.AreEqual(TEST_REQUEST_ID, helloException.RequestId);

                Assert.NotNull(helloException.Failure);
                Assert.AreEqual(1, helloException.Failure.Errors.Count);
                Assert.NotNull(helloException.Failure.Errors[0].ErrorCode);
                Assert.NotNull(helloException.Failure.Errors[0].ErrorCode.RequestError);
            };

            ClientInterceptorContext <HelloRequest, HelloResponse> context =
                GetClientInterceptorContext();
            AsyncServerStreamingCall <HelloResponse> call =
                StreamingContinuationWithException <HelloResponse>();

            handler.HandleAsyncServerStreamingLogging(TEST_REQUEST,
                                                      null, context, new AggregateException(TEST_EXCEPTION),
                                                      call);
        }
Example #3
0
        /// <summary>
        /// Intercepts an asynchronous invocation of a streaming remote call.
        /// </summary>
        /// <typeparam name="TRequest"></typeparam>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="request">The request message of the invocation.</param>
        /// <param name="context">The <see cref="T:Grpc.Core.Interceptors.ClientInterceptorContext`2" />
        /// associated with the current invocation.</param>
        /// <param name="continuation">The callback that continues the invocation process.
        /// This can be invoked zero or more times by the interceptor.
        /// The interceptor can invoke the continuation passing the given
        /// request value and context arguments, or substitute them as it sees fit.</param>
        /// <returns>
        /// An instance of <see cref="T:Grpc.Core.AsyncServerStreamingCall`1" />
        /// representing an asynchronous server-streaming invocation.
        /// The interceptor can simply return the return value of the
        /// continuation delegate passed to it intact, or construct its
        /// own substitute as it sees fit.
        /// </returns>
        public override AsyncServerStreamingCall <TResponse> AsyncServerStreamingCall <TRequest, TResponse>(
            TRequest request, ClientInterceptorContext <TRequest, TResponse> context,
            AsyncServerStreamingCallContinuation <TRequest, TResponse> continuation)
        {
            AsyncServerStreamingCall <TResponse> call           = continuation(request, context);
            StreamingRpcInterceptor <TResponse>  responseStream = null;

            responseStream = new StreamingRpcInterceptor <TResponse>(call.ResponseStream,
                                                                     delegate(TResponse response, AggregateException rpcException)
            {
                loggingHandler.HandleAsyncServerStreamingLogging(request, response,
                                                                 context, rpcException, call);
            });

            return(new AsyncServerStreamingCall <TResponse>(
                       responseStream,
                       call.ResponseHeadersAsync,
                       call.GetStatus,
                       call.GetTrailers,
                       call.Dispose
                       ));
        }