MarshallingErrorsTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #region Copyright notice and license
  2. // Copyright 2015 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.Diagnostics;
  19. using System.IO;
  20. using System.Linq;
  21. using System.Threading;
  22. using System.Threading.Tasks;
  23. using Grpc.Core;
  24. using Grpc.Core.Internal;
  25. using Grpc.Core.Utils;
  26. using NUnit.Framework;
  27. namespace Grpc.Core.Tests
  28. {
  29. public class MarshallingErrorsTest
  30. {
  31. const string Host = "127.0.0.1";
  32. MockServiceHelper helper;
  33. Server server;
  34. Channel channel;
  35. [SetUp]
  36. public void Init()
  37. {
  38. var marshaller = new Marshaller<string>(
  39. (str) =>
  40. {
  41. if (str == "UNSERIALIZABLE_VALUE")
  42. {
  43. // Google.Protobuf throws exception inherited from IOException
  44. throw new IOException("Error serializing the message.");
  45. }
  46. return System.Text.Encoding.UTF8.GetBytes(str);
  47. },
  48. (payload) =>
  49. {
  50. var s = System.Text.Encoding.UTF8.GetString(payload);
  51. if (s == "UNPARSEABLE_VALUE")
  52. {
  53. // Google.Protobuf throws exception inherited from IOException
  54. throw new IOException("Error parsing the message.");
  55. }
  56. return s;
  57. });
  58. helper = new MockServiceHelper(Host, marshaller);
  59. server = helper.GetServer();
  60. server.Start();
  61. channel = helper.GetChannel();
  62. }
  63. [TearDown]
  64. public void Cleanup()
  65. {
  66. channel.ShutdownAsync().Wait();
  67. server.ShutdownAsync().Wait();
  68. }
  69. [Test]
  70. public void ResponseParsingError_UnaryResponse()
  71. {
  72. helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
  73. {
  74. return Task.FromResult("UNPARSEABLE_VALUE");
  75. });
  76. var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "REQUEST"));
  77. Assert.AreEqual(StatusCode.Internal, ex.Status.StatusCode);
  78. }
  79. [Test]
  80. public void ResponseParsingError_StreamingResponse()
  81. {
  82. helper.ServerStreamingHandler = new ServerStreamingServerMethod<string, string>(async (request, responseStream, context) =>
  83. {
  84. await responseStream.WriteAsync("UNPARSEABLE_VALUE");
  85. await Task.Delay(10000);
  86. });
  87. var call = Calls.AsyncServerStreamingCall(helper.CreateServerStreamingCall(), "REQUEST");
  88. var ex = Assert.ThrowsAsync<RpcException>(async () => await call.ResponseStream.MoveNext());
  89. Assert.AreEqual(StatusCode.Internal, ex.Status.StatusCode);
  90. }
  91. [Test]
  92. public void RequestParsingError_UnaryRequest()
  93. {
  94. helper.UnaryHandler = new UnaryServerMethod<string, string>((request, context) =>
  95. {
  96. return Task.FromResult("RESPONSE");
  97. });
  98. var ex = Assert.Throws<RpcException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "UNPARSEABLE_VALUE"));
  99. // Spec doesn't define the behavior. With the current implementation server handler throws exception which results in StatusCode.Unknown.
  100. Assert.AreEqual(StatusCode.Unknown, ex.Status.StatusCode);
  101. }
  102. [Test]
  103. public async Task RequestParsingError_StreamingRequest()
  104. {
  105. helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) =>
  106. {
  107. try
  108. {
  109. // cannot use Assert.ThrowsAsync because it uses Task.Wait and would deadlock.
  110. await requestStream.MoveNext();
  111. Assert.Fail();
  112. }
  113. catch (IOException)
  114. {
  115. }
  116. return "RESPONSE";
  117. });
  118. var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
  119. await call.RequestStream.WriteAsync("UNPARSEABLE_VALUE");
  120. Assert.AreEqual("RESPONSE", await call);
  121. }
  122. [Test]
  123. public void RequestSerializationError_BlockingUnary()
  124. {
  125. Assert.Throws<IOException>(() => Calls.BlockingUnaryCall(helper.CreateUnaryCall(), "UNSERIALIZABLE_VALUE"));
  126. }
  127. [Test]
  128. public void RequestSerializationError_AsyncUnary()
  129. {
  130. Assert.ThrowsAsync<IOException>(async () => await Calls.AsyncUnaryCall(helper.CreateUnaryCall(), "UNSERIALIZABLE_VALUE"));
  131. }
  132. [Test]
  133. public async Task RequestSerializationError_ClientStreaming()
  134. {
  135. helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) =>
  136. {
  137. CollectionAssert.AreEqual(new[] { "A", "B" }, await requestStream.ToListAsync());
  138. return "RESPONSE";
  139. });
  140. var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall());
  141. await call.RequestStream.WriteAsync("A");
  142. Assert.ThrowsAsync<IOException>(async () => await call.RequestStream.WriteAsync("UNSERIALIZABLE_VALUE"));
  143. await call.RequestStream.WriteAsync("B");
  144. await call.RequestStream.CompleteAsync();
  145. Assert.AreEqual("RESPONSE", await call);
  146. }
  147. }
  148. }