OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015-2016, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Runtime.InteropServices; |
| 36 using System.Threading.Tasks; |
| 37 |
| 38 using Grpc.Core.Internal; |
| 39 using NUnit.Framework; |
| 40 |
| 41 namespace Grpc.Core.Internal.Tests |
| 42 { |
| 43 public class AsyncCallTest |
| 44 { |
| 45 Channel channel; |
| 46 FakeNativeCall fakeCall; |
| 47 AsyncCall<string, string> asyncCall; |
| 48 |
| 49 [SetUp] |
| 50 public void Init() |
| 51 { |
| 52 channel = new Channel("localhost", ChannelCredentials.Insecure); |
| 53 |
| 54 fakeCall = new FakeNativeCall(); |
| 55 |
| 56 var callDetails = new CallInvocationDetails<string, string>(channel,
"someMethod", null, Marshallers.StringMarshaller, Marshallers.StringMarshaller,
new CallOptions()); |
| 57 asyncCall = new AsyncCall<string, string>(callDetails, fakeCall); |
| 58 } |
| 59 |
| 60 [TearDown] |
| 61 public void Cleanup() |
| 62 { |
| 63 channel.ShutdownAsync().Wait(); |
| 64 } |
| 65 |
| 66 [Test] |
| 67 public void AsyncUnary_CompletionSuccess() |
| 68 { |
| 69 var resultTask = asyncCall.UnaryCallAsync("abc"); |
| 70 fakeCall.UnaryResponseClientHandler(true, new ClientSideStatus(Statu
s.DefaultSuccess, new Metadata()), new byte[] { 1, 2, 3 }, new Metadata()); |
| 71 Assert.IsTrue(resultTask.IsCompleted); |
| 72 Assert.IsTrue(fakeCall.IsDisposed); |
| 73 Assert.AreEqual(Status.DefaultSuccess, asyncCall.GetStatus()); |
| 74 } |
| 75 |
| 76 [Test] |
| 77 public void AsyncUnary_CompletionFailure() |
| 78 { |
| 79 var resultTask = asyncCall.UnaryCallAsync("abc"); |
| 80 fakeCall.UnaryResponseClientHandler(false, new ClientSideStatus(new
Status(StatusCode.Internal, ""), null), new byte[] { 1, 2, 3 }, new Metadata()); |
| 81 |
| 82 Assert.IsTrue(resultTask.IsCompleted); |
| 83 Assert.IsTrue(fakeCall.IsDisposed); |
| 84 |
| 85 Assert.AreEqual(StatusCode.Internal, asyncCall.GetStatus().StatusCod
e); |
| 86 Assert.IsNull(asyncCall.GetTrailers()); |
| 87 var ex = Assert.Throws<RpcException>(() => resultTask.GetAwaiter().G
etResult()); |
| 88 Assert.AreEqual(StatusCode.Internal, ex.Status.StatusCode); |
| 89 } |
| 90 |
| 91 internal class FakeNativeCall : INativeCall |
| 92 { |
| 93 public UnaryResponseClientHandler UnaryResponseClientHandler |
| 94 { |
| 95 get; |
| 96 set; |
| 97 } |
| 98 |
| 99 public ReceivedStatusOnClientHandler ReceivedStatusOnClientHandler |
| 100 { |
| 101 get; |
| 102 set; |
| 103 } |
| 104 |
| 105 public ReceivedMessageHandler ReceivedMessageHandler |
| 106 { |
| 107 get; |
| 108 set; |
| 109 } |
| 110 |
| 111 public ReceivedResponseHeadersHandler ReceivedResponseHeadersHandler |
| 112 { |
| 113 get; |
| 114 set; |
| 115 } |
| 116 |
| 117 public SendCompletionHandler SendCompletionHandler |
| 118 { |
| 119 get; |
| 120 set; |
| 121 } |
| 122 |
| 123 public ReceivedCloseOnServerHandler ReceivedCloseOnServerHandler |
| 124 { |
| 125 get; |
| 126 set; |
| 127 } |
| 128 |
| 129 public bool IsCancelled |
| 130 { |
| 131 get; |
| 132 set; |
| 133 } |
| 134 |
| 135 public bool IsDisposed |
| 136 { |
| 137 get; |
| 138 set; |
| 139 } |
| 140 |
| 141 public void Cancel() |
| 142 { |
| 143 IsCancelled = true; |
| 144 } |
| 145 |
| 146 public void CancelWithStatus(Status status) |
| 147 { |
| 148 IsCancelled = true; |
| 149 } |
| 150 |
| 151 public string GetPeer() |
| 152 { |
| 153 return "PEER"; |
| 154 } |
| 155 |
| 156 public void StartUnary(UnaryResponseClientHandler callback, byte[] p
ayload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlags) |
| 157 { |
| 158 UnaryResponseClientHandler = callback; |
| 159 } |
| 160 |
| 161 public void StartUnary(BatchContextSafeHandle ctx, byte[] payload, M
etadataArraySafeHandle metadataArray, WriteFlags writeFlags) |
| 162 { |
| 163 throw new NotImplementedException(); |
| 164 } |
| 165 |
| 166 public void StartClientStreaming(UnaryResponseClientHandler callback
, MetadataArraySafeHandle metadataArray) |
| 167 { |
| 168 UnaryResponseClientHandler = callback; |
| 169 } |
| 170 |
| 171 public void StartServerStreaming(ReceivedStatusOnClientHandler callb
ack, byte[] payload, MetadataArraySafeHandle metadataArray, WriteFlags writeFlag
s) |
| 172 { |
| 173 ReceivedStatusOnClientHandler = callback; |
| 174 } |
| 175 |
| 176 public void StartDuplexStreaming(ReceivedStatusOnClientHandler callb
ack, MetadataArraySafeHandle metadataArray) |
| 177 { |
| 178 ReceivedStatusOnClientHandler = callback; |
| 179 } |
| 180 |
| 181 public void StartReceiveMessage(ReceivedMessageHandler callback) |
| 182 { |
| 183 ReceivedMessageHandler = callback; |
| 184 } |
| 185 |
| 186 public void StartReceiveInitialMetadata(ReceivedResponseHeadersHandl
er callback) |
| 187 { |
| 188 ReceivedResponseHeadersHandler = callback; |
| 189 } |
| 190 |
| 191 public void StartSendInitialMetadata(SendCompletionHandler callback,
MetadataArraySafeHandle metadataArray) |
| 192 { |
| 193 SendCompletionHandler = callback; |
| 194 } |
| 195 |
| 196 public void StartSendMessage(SendCompletionHandler callback, byte[]
payload, WriteFlags writeFlags, bool sendEmptyInitialMetadata) |
| 197 { |
| 198 SendCompletionHandler = callback; |
| 199 } |
| 200 |
| 201 public void StartSendCloseFromClient(SendCompletionHandler callback) |
| 202 { |
| 203 SendCompletionHandler = callback; |
| 204 } |
| 205 |
| 206 public void StartSendStatusFromServer(SendCompletionHandler callback
, Status status, MetadataArraySafeHandle metadataArray, bool sendEmptyInitialMet
adata) |
| 207 { |
| 208 SendCompletionHandler = callback; |
| 209 } |
| 210 |
| 211 public void StartServerSide(ReceivedCloseOnServerHandler callback) |
| 212 { |
| 213 ReceivedCloseOnServerHandler = callback; |
| 214 } |
| 215 |
| 216 public void Dispose() |
| 217 { |
| 218 IsDisposed = true; |
| 219 } |
| 220 } |
| 221 } |
| 222 } |
OLD | NEW |