diff options
author | Mehrdad Afshari <mmx@google.com> | 2018-02-21 18:08:57 -0800 |
---|---|---|
committer | Mehrdad Afshari <mmx@google.com> | 2018-02-21 18:30:19 -0800 |
commit | 23b9e0de2e6f9e6389ad56f7aa22b94c57b9188b (patch) | |
tree | 04beb4e274004328f98a81818ec816bb463acbd3 /src/csharp | |
parent | c1c29e30466c851eb3073d292ea605a3325719e5 (diff) |
Add test for interceptor registration code paths
Diffstat (limited to 'src/csharp')
-rw-r--r-- | src/csharp/Grpc.Core.Tests/Interceptors/ClientInterceptorTest.cs | 40 | ||||
-rw-r--r-- | src/csharp/Grpc.Core.Tests/Interceptors/ServerInterceptorTest.cs | 30 |
2 files changed, 56 insertions, 14 deletions
diff --git a/src/csharp/Grpc.Core.Tests/Interceptors/ClientInterceptorTest.cs b/src/csharp/Grpc.Core.Tests/Interceptors/ClientInterceptorTest.cs index 0ec2d848f0..d7c01d08ac 100644 --- a/src/csharp/Grpc.Core.Tests/Interceptors/ClientInterceptorTest.cs +++ b/src/csharp/Grpc.Core.Tests/Interceptors/ClientInterceptorTest.cs @@ -58,6 +58,22 @@ namespace Grpc.Core.Interceptors.Tests Assert.AreEqual("PASS", callInvoker.BlockingUnaryCall(new Method<string, string>(MethodType.Unary, MockServiceHelper.ServiceName, "Unary", Marshallers.StringMarshaller, Marshallers.StringMarshaller), Host, new CallOptions(), "")); } + private class CallbackInterceptor : GenericInterceptor + { + readonly Action callback; + + public CallbackInterceptor(Action callback) + { + this.callback = callback; + } + + protected override ClientCallHooks<TRequest, TResponse> InterceptCall<TRequest, TResponse>(ClientInterceptorContext<TRequest, TResponse> context, bool clientStreaming, bool serverStreaming, TRequest request) + { + callback(); + return null; + } + } + [Test] public void CheckInterceptorOrderInClientInterceptors() { @@ -69,11 +85,13 @@ namespace Grpc.Core.Interceptors.Tests var server = helper.GetServer(); server.Start(); var stringBuilder = new StringBuilder(); - var callInvoker = helper.GetChannel().Intercept(metadata => - { + var callInvoker = helper.GetChannel().Intercept(metadata => { stringBuilder.Append("interceptor1"); return metadata; - }).Intercept(metadata => + }).Intercept(new CallbackInterceptor(() => stringBuilder.Append("array1")), + new CallbackInterceptor(() => stringBuilder.Append("array2")), + new CallbackInterceptor(() => stringBuilder.Append("array3"))) + .Intercept(metadata => { stringBuilder.Append("interceptor2"); return metadata; @@ -83,7 +101,21 @@ namespace Grpc.Core.Interceptors.Tests return metadata; }); Assert.AreEqual("PASS", callInvoker.BlockingUnaryCall(new Method<string, string>(MethodType.Unary, MockServiceHelper.ServiceName, "Unary", Marshallers.StringMarshaller, Marshallers.StringMarshaller), Host, new CallOptions(), "")); - Assert.AreEqual("interceptor3interceptor2interceptor1", stringBuilder.ToString()); + Assert.AreEqual("interceptor3interceptor2array1array2array3interceptor1", stringBuilder.ToString()); + } + + [Test] + public void CheckNullInterceptorRegistrationFails() + { + var helper = new MockServiceHelper(Host); + helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) => + { + return Task.FromResult("PASS"); + }); + Assert.Throws<ArgumentNullException>(() => helper.GetChannel().Intercept(default(Interceptor))); + Assert.Throws<ArgumentNullException>(() => helper.GetChannel().Intercept(new[]{default(Interceptor)})); + Assert.Throws<ArgumentNullException>(() => helper.GetChannel().Intercept(new[]{new CallbackInterceptor(()=>{}), null})); + Assert.Throws<ArgumentNullException>(() => helper.GetChannel().Intercept(default(Interceptor[]))); } private class CountingInterceptor : GenericInterceptor diff --git a/src/csharp/Grpc.Core.Tests/Interceptors/ServerInterceptorTest.cs b/src/csharp/Grpc.Core.Tests/Interceptors/ServerInterceptorTest.cs index 57dd68b1eb..c0957a2b42 100644 --- a/src/csharp/Grpc.Core.Tests/Interceptors/ServerInterceptorTest.cs +++ b/src/csharp/Grpc.Core.Tests/Interceptors/ServerInterceptorTest.cs @@ -50,13 +50,7 @@ namespace Grpc.Core.Interceptors.Tests return Task.FromResult<ServerCallHooks<TRequest, TResponse>>(null); } - public Metadata.Entry Header - { - get - { - return header; - } - } + public Metadata.Entry Header => header; } [Test] @@ -81,7 +75,6 @@ namespace Grpc.Core.Interceptors.Tests { readonly Action action; - public ArbitraryActionInterceptor(Action action) { this.action = action; @@ -105,13 +98,30 @@ namespace Grpc.Core.Interceptors.Tests var stringBuilder = new StringBuilder(); helper.ServiceDefinition = helper.ServiceDefinition .Intercept(new ArbitraryActionInterceptor(() => stringBuilder.Append("A"))) - .Intercept(new ArbitraryActionInterceptor(() => stringBuilder.Append("B"))) + .Intercept(new ArbitraryActionInterceptor(() => stringBuilder.Append("B1")), + new ArbitraryActionInterceptor(() => stringBuilder.Append("B2")), + new ArbitraryActionInterceptor(() => stringBuilder.Append("B3"))) .Intercept(new ArbitraryActionInterceptor(() => stringBuilder.Append("C"))); var server = helper.GetServer(); server.Start(); var channel = helper.GetChannel(); Assert.AreEqual("PASS", Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "")); - Assert.AreEqual("CBA", stringBuilder.ToString()); + Assert.AreEqual("CB1B2B3A", stringBuilder.ToString()); + } + + [Test] + public void CheckNullInterceptorRegistrationFails() + { + var helper = new MockServiceHelper(Host); + helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) => + { + return Task.FromResult("PASS"); + }); + var sd = helper.ServiceDefinition; + Assert.Throws<ArgumentNullException>(() => sd.Intercept(default(Interceptor))); + Assert.Throws<ArgumentNullException>(() => sd.Intercept(new[]{default(Interceptor)})); + Assert.Throws<ArgumentNullException>(() => sd.Intercept(new[]{new ArbitraryActionInterceptor(()=>{}), null})); + Assert.Throws<ArgumentNullException>(() => sd.Intercept(default(Interceptor[]))); } } } |