|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using Grpc.Core; |
| 16 | +using Grpc.Core.Interceptors; |
| 17 | +using System; |
| 18 | +using System.Threading; |
| 19 | +using System.Threading.Tasks; |
| 20 | +using Xunit; |
| 21 | + |
| 22 | +namespace Google.Cloud.Spanner.V1.Tests; |
| 23 | + |
| 24 | +public class SpannerRequestIdCallInterceptorTest |
| 25 | +{ |
| 26 | + private static readonly Method<string, string> s_unaryMethod = new Method<string, string>(MethodType.Unary, "service", "method", Marshallers.StringMarshaller, Marshallers.StringMarshaller); |
| 27 | + private static readonly Method<string, string> s_clientStreamingMethod = new Method<string, string>(MethodType.ClientStreaming, "service", "method", Marshallers.StringMarshaller, Marshallers.StringMarshaller); |
| 28 | + private static readonly Method<string, string> s_serverStreamingMethod = new Method<string, string>(MethodType.ServerStreaming, "service", "method", Marshallers.StringMarshaller, Marshallers.StringMarshaller); |
| 29 | + private static readonly Method<string, string> s_duplexStreamingMethod = new Method<string, string>(MethodType.DuplexStreaming, "service", "method", Marshallers.StringMarshaller, Marshallers.StringMarshaller); |
| 30 | + |
| 31 | + public static TheoryData<Func<CallInvoker, CallOptions, Task>> CallInvokerActions { get; } = new TheoryData<Func<CallInvoker, CallOptions, Task>> |
| 32 | + { |
| 33 | + (invoker, options) => Task.Run(() => invoker.BlockingUnaryCall(s_unaryMethod, null, options, "")), |
| 34 | + (invoker, options) => invoker.AsyncUnaryCall(s_unaryMethod, null, options, "").ResponseAsync, |
| 35 | + (invoker, options) => invoker.AsyncClientStreamingCall(s_clientStreamingMethod, null, options).ResponseAsync, |
| 36 | + (invoker, options) => invoker.AsyncServerStreamingCall(s_serverStreamingMethod, null, options, "").ResponseStream.MoveNext(), |
| 37 | + (invoker, options) => invoker.AsyncDuplexStreamingCall(s_duplexStreamingMethod, null, options).ResponseStream.MoveNext() |
| 38 | + }; |
| 39 | + |
| 40 | + [Theory] |
| 41 | + [MemberData(nameof(CallInvokerActions))] |
| 42 | + public async Task RpcCall_ExceptionContainsRequestId(Func<CallInvoker, CallOptions, Task> action) |
| 43 | + { |
| 44 | + var invoker = new ThrowingCallInvoker(); |
| 45 | + var interceptedInvoker = invoker.Intercept(new SpannerRequestIdCallInterceptor()); |
| 46 | + var headers = new Metadata { { SpannerRequestId.HeaderKey, "test-request-id" } }; |
| 47 | + var options = new CallOptions(headers); |
| 48 | + |
| 49 | + var exception = await Assert.ThrowsAsync<RpcException>(() => action(interceptedInvoker, options)); |
| 50 | + |
| 51 | + Assert.True(exception.Data.Contains(SpannerRequestId.HeaderKey)); |
| 52 | + Assert.Equal("test-request-id", exception.Data[SpannerRequestId.HeaderKey]); |
| 53 | + } |
| 54 | + |
| 55 | + private class ThrowingCallInvoker : CallInvoker |
| 56 | + { |
| 57 | + public override TResponse BlockingUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request) => |
| 58 | + throw new RpcException(new Status(StatusCode.Internal, "Test exception")); |
| 59 | + |
| 60 | + public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request) => |
| 61 | + new AsyncUnaryCall<TResponse>( |
| 62 | + Task.FromException<TResponse>(new RpcException(new Status(StatusCode.Internal, "Test exception"))), |
| 63 | + Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); |
| 64 | + |
| 65 | + public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options) => |
| 66 | + new AsyncClientStreamingCall<TRequest, TResponse>( |
| 67 | + new MockClientStreamWriter<TRequest>(), |
| 68 | + Task.FromException<TResponse>(new RpcException(new Status(StatusCode.Internal, "Test exception"))), |
| 69 | + Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); |
| 70 | + |
| 71 | + public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options, TRequest request) => |
| 72 | + new AsyncServerStreamingCall<TResponse>( |
| 73 | + new ThrowingStreamReader<TResponse>(), |
| 74 | + Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); |
| 75 | + |
| 76 | + public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string host, CallOptions options) => |
| 77 | + new AsyncDuplexStreamingCall<TRequest, TResponse>( |
| 78 | + new MockClientStreamWriter<TRequest>(), |
| 79 | + new ThrowingStreamReader<TResponse>(), |
| 80 | + Task.FromResult(new Metadata()), () => Status.DefaultSuccess, () => new Metadata(), () => { }); |
| 81 | + } |
| 82 | + |
| 83 | + private class MockClientStreamWriter<T> : IClientStreamWriter<T> |
| 84 | + { |
| 85 | + public WriteOptions WriteOptions { get; set; } |
| 86 | + public Task CompleteAsync() => Task.CompletedTask; |
| 87 | + public Task WriteAsync(T message) => Task.CompletedTask; |
| 88 | + } |
| 89 | + |
| 90 | + private class ThrowingStreamReader<T> : IAsyncStreamReader<T> |
| 91 | + { |
| 92 | + public T Current => default; |
| 93 | + |
| 94 | + public Task<bool> MoveNext(CancellationToken cancellationToken) => |
| 95 | + Task.FromException<bool>(new RpcException(new Status(StatusCode.Internal, "Test exception"))); |
| 96 | + } |
| 97 | +} |
0 commit comments