1 #region Copyright notice and license 2 3 // Copyright 2015-2016 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using System.Collections.Generic; 21 using System.IO; 22 using System.Linq; 23 using System.Threading; 24 using System.Threading.Tasks; 25 using Grpc.Core; 26 using Grpc.Core.Utils; 27 using Grpc.Testing; 28 using NUnit.Framework; 29 30 namespace Grpc.IntegrationTesting 31 { 32 public class GeneratedServiceBaseTest 33 { 34 const string Host = "localhost"; 35 Server server; 36 Channel channel; 37 TestService.TestServiceClient client; 38 39 [SetUp] Init()40 public void Init() 41 { 42 // Disable SO_REUSEPORT to prevent https://github.com/grpc/grpc/issues/10755 43 server = new Server(new[] { new ChannelOption(ChannelOptions.SoReuseport, 0) }) 44 { 45 Services = { TestService.BindService(new UnimplementedTestServiceImpl()) }, 46 Ports = { { Host, ServerPort.PickUnused, SslServerCredentials.Insecure } } 47 }; 48 server.Start(); 49 channel = new Channel(Host, server.Ports.Single().BoundPort, ChannelCredentials.Insecure); 50 client = new TestService.TestServiceClient(channel); 51 } 52 53 [TearDown] Cleanup()54 public void Cleanup() 55 { 56 channel.ShutdownAsync().Wait(); 57 server.ShutdownAsync().Wait(); 58 } 59 60 [Test] UnimplementedByDefault_Unary()61 public void UnimplementedByDefault_Unary() 62 { 63 var ex = Assert.Throws<RpcException>(() => client.UnaryCall(new SimpleRequest { })); 64 Assert.AreEqual(StatusCode.Unimplemented, ex.Status.StatusCode); 65 } 66 67 [Test] UnimplementedByDefault_ClientStreaming()68 public void UnimplementedByDefault_ClientStreaming() 69 { 70 var call = client.StreamingInputCall(); 71 72 var ex = Assert.ThrowsAsync<RpcException>(async () => await call); 73 Assert.AreEqual(StatusCode.Unimplemented, ex.Status.StatusCode); 74 } 75 76 [Test] UnimplementedByDefault_ServerStreamingCall()77 public void UnimplementedByDefault_ServerStreamingCall() 78 { 79 var call = client.StreamingOutputCall(new StreamingOutputCallRequest()); 80 81 var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.MoveNext()); 82 Assert.AreEqual(StatusCode.Unimplemented, ex.Status.StatusCode); 83 } 84 85 [Test] UnimplementedByDefault_DuplexStreamingCall()86 public void UnimplementedByDefault_DuplexStreamingCall() 87 { 88 var call = client.FullDuplexCall(); 89 90 var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.MoveNext()); 91 Assert.AreEqual(StatusCode.Unimplemented, ex.Status.StatusCode); 92 } 93 94 /// <summary> 95 /// Implementation of TestService that doesn't override any methods. 96 /// </summary> 97 private class UnimplementedTestServiceImpl : TestService.TestServiceBase 98 { 99 } 100 } 101 } 102