GeneratedServiceBaseTest.cs 3.3 KB

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