• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 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.Runtime.InteropServices;
22 using System.Threading.Tasks;
23 
24 using Grpc.Core.Internal;
25 using NUnit.Framework;
26 
27 namespace Grpc.Core.Internal.Tests
28 {
29     /// <summary>
30     /// For testing purposes.
31     /// </summary>
32     internal class FakeNativeCall : INativeCall
33     {
34         private bool shouldStartCallFail;
35         public IUnaryResponseClientCallback UnaryResponseClientCallback
36         {
37             get;
38             set;
39         }
40 
41         public IReceivedStatusOnClientCallback ReceivedStatusOnClientCallback
42         {
43             get;
44             set;
45         }
46 
47         public IReceivedMessageCallback ReceivedMessageCallback
48         {
49             get;
50             set;
51         }
52 
53         public IReceivedResponseHeadersCallback ReceivedResponseHeadersCallback
54         {
55             get;
56             set;
57         }
58 
59         public ISendCompletionCallback SendCompletionCallback
60         {
61             get;
62             set;
63         }
64 
65         public ISendStatusFromServerCompletionCallback SendStatusFromServerCallback
66         {
67             get;
68             set;
69         }
70 
71         public IReceivedCloseOnServerCallback ReceivedCloseOnServerCallback
72         {
73             get;
74             set;
75         }
76 
77         public bool IsCancelled
78         {
79             get;
80             set;
81         }
82 
83         public bool IsDisposed
84         {
85             get;
86             set;
87         }
88 
Cancel()89         public void Cancel()
90         {
91             IsCancelled = true;
92         }
93 
CancelWithStatus(Status status)94         public void CancelWithStatus(Status status)
95         {
96             IsCancelled = true;
97         }
98 
GetPeer()99         public string GetPeer()
100         {
101             return "PEER";
102         }
103 
StartUnary(IUnaryResponseClientCallback callback, byte[] payload, WriteFlags writeFlags, MetadataArraySafeHandle metadataArray, CallFlags callFlags)104         public void StartUnary(IUnaryResponseClientCallback callback, byte[] payload, WriteFlags writeFlags, MetadataArraySafeHandle metadataArray, CallFlags callFlags)
105         {
106             StartCallMaybeFail();
107             UnaryResponseClientCallback = callback;
108         }
109 
StartUnary(BatchContextSafeHandle ctx, byte[] payload, WriteFlags writeFlags, MetadataArraySafeHandle metadataArray, CallFlags callFlags)110         public void StartUnary(BatchContextSafeHandle ctx, byte[] payload, WriteFlags writeFlags, MetadataArraySafeHandle metadataArray, CallFlags callFlags)
111         {
112             StartCallMaybeFail();
113             throw new NotImplementedException();
114         }
115 
StartClientStreaming(IUnaryResponseClientCallback callback, MetadataArraySafeHandle metadataArray, CallFlags callFlags)116         public void StartClientStreaming(IUnaryResponseClientCallback callback, MetadataArraySafeHandle metadataArray, CallFlags callFlags)
117         {
118             StartCallMaybeFail();
119             UnaryResponseClientCallback = callback;
120         }
121 
StartServerStreaming(IReceivedStatusOnClientCallback callback, byte[] payload, WriteFlags writeFlags, MetadataArraySafeHandle metadataArray, CallFlags callFlags)122         public void StartServerStreaming(IReceivedStatusOnClientCallback callback, byte[] payload, WriteFlags writeFlags, MetadataArraySafeHandle metadataArray, CallFlags callFlags)
123         {
124             StartCallMaybeFail();
125             ReceivedStatusOnClientCallback = callback;
126         }
127 
StartDuplexStreaming(IReceivedStatusOnClientCallback callback, MetadataArraySafeHandle metadataArray, CallFlags callFlags)128         public void StartDuplexStreaming(IReceivedStatusOnClientCallback callback, MetadataArraySafeHandle metadataArray, CallFlags callFlags)
129         {
130             StartCallMaybeFail();
131             ReceivedStatusOnClientCallback = callback;
132         }
133 
StartReceiveMessage(IReceivedMessageCallback callback)134         public void StartReceiveMessage(IReceivedMessageCallback callback)
135         {
136             ReceivedMessageCallback = callback;
137         }
138 
StartReceiveInitialMetadata(IReceivedResponseHeadersCallback callback)139         public void StartReceiveInitialMetadata(IReceivedResponseHeadersCallback callback)
140         {
141             ReceivedResponseHeadersCallback = callback;
142         }
143 
StartSendInitialMetadata(ISendCompletionCallback callback, MetadataArraySafeHandle metadataArray)144         public void StartSendInitialMetadata(ISendCompletionCallback callback, MetadataArraySafeHandle metadataArray)
145         {
146             SendCompletionCallback = callback;
147         }
148 
StartSendMessage(ISendCompletionCallback callback, byte[] payload, WriteFlags writeFlags, bool sendEmptyInitialMetadata)149         public void StartSendMessage(ISendCompletionCallback callback, byte[] payload, WriteFlags writeFlags, bool sendEmptyInitialMetadata)
150         {
151             SendCompletionCallback = callback;
152         }
153 
StartSendCloseFromClient(ISendCompletionCallback callback)154         public void StartSendCloseFromClient(ISendCompletionCallback callback)
155         {
156             SendCompletionCallback = callback;
157         }
158 
StartSendStatusFromServer(ISendStatusFromServerCompletionCallback callback, Status status, MetadataArraySafeHandle metadataArray, bool sendEmptyInitialMetadata, byte[] optionalPayload, WriteFlags writeFlags)159         public void StartSendStatusFromServer(ISendStatusFromServerCompletionCallback callback, Status status, MetadataArraySafeHandle metadataArray, bool sendEmptyInitialMetadata,
160             byte[] optionalPayload, WriteFlags writeFlags)
161         {
162             SendStatusFromServerCallback = callback;
163         }
164 
StartServerSide(IReceivedCloseOnServerCallback callback)165         public void StartServerSide(IReceivedCloseOnServerCallback callback)
166         {
167             ReceivedCloseOnServerCallback = callback;
168         }
169 
Dispose()170         public void Dispose()
171         {
172             IsDisposed = true;
173         }
174 
175         /// <summary>
176         /// Emulate CallSafeHandle.CheckOk() failure for all future attempts
177         /// to start a call.
178         /// </summary>
MakeStartCallFail()179         public void MakeStartCallFail()
180         {
181             shouldStartCallFail = true;
182         }
183 
StartCallMaybeFail()184         private void StartCallMaybeFail()
185         {
186             if (shouldStartCallFail)
187             {
188                 throw new InvalidOperationException("Start call has failed.");
189             }
190         }
191     }
192 }
193