1 // 2 // 3 // Copyright 2017 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 // 18 19 #ifndef GRPCPP_TEST_MOCK_STREAM_H 20 #define GRPCPP_TEST_MOCK_STREAM_H 21 22 #include <stdint.h> 23 24 #include <gmock/gmock.h> 25 26 #include <grpcpp/impl/call.h> 27 #include <grpcpp/support/async_stream.h> 28 #include <grpcpp/support/async_unary_call.h> 29 #include <grpcpp/support/sync_stream.h> 30 31 namespace grpc { 32 namespace testing { 33 34 template <class R> 35 class MockClientReader : public grpc::ClientReaderInterface<R> { 36 public: 37 MockClientReader() = default; 38 39 /// ClientStreamingInterface 40 MOCK_METHOD0_T(Finish, Status()); 41 42 /// ReaderInterface 43 MOCK_METHOD1_T(NextMessageSize, bool(uint32_t*)); 44 MOCK_METHOD1_T(Read, bool(R*)); 45 46 /// ClientReaderInterface 47 MOCK_METHOD0_T(WaitForInitialMetadata, void()); 48 }; 49 50 template <class W> 51 class MockClientWriter : public grpc::ClientWriterInterface<W> { 52 public: 53 MockClientWriter() = default; 54 55 /// ClientStreamingInterface 56 MOCK_METHOD0_T(Finish, Status()); 57 58 /// WriterInterface 59 MOCK_METHOD2_T(Write, bool(const W&, const WriteOptions)); 60 61 /// ClientWriterInterface 62 MOCK_METHOD0_T(WritesDone, bool()); 63 }; 64 65 template <class W, class R> 66 class MockClientReaderWriter : public grpc::ClientReaderWriterInterface<W, R> { 67 public: 68 MockClientReaderWriter() = default; 69 70 /// ClientStreamingInterface 71 MOCK_METHOD0_T(Finish, Status()); 72 73 /// ReaderInterface 74 MOCK_METHOD1_T(NextMessageSize, bool(uint32_t*)); 75 MOCK_METHOD1_T(Read, bool(R*)); 76 77 /// WriterInterface 78 MOCK_METHOD2_T(Write, bool(const W&, const WriteOptions)); 79 80 /// ClientReaderWriterInterface 81 MOCK_METHOD0_T(WaitForInitialMetadata, void()); 82 MOCK_METHOD0_T(WritesDone, bool()); 83 }; 84 85 /// TODO: We do not support mocking an async RPC for now. 86 87 template <class R> 88 class MockClientAsyncResponseReader 89 : public grpc::ClientAsyncResponseReaderInterface<R> { 90 public: 91 MockClientAsyncResponseReader() = default; 92 93 /// ClientAsyncResponseReaderInterface 94 MOCK_METHOD0_T(StartCall, void()); 95 MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); 96 MOCK_METHOD3_T(Finish, void(R*, Status*, void*)); 97 }; 98 99 template <class R> 100 class MockClientAsyncReader : public ClientAsyncReaderInterface<R> { 101 public: 102 MockClientAsyncReader() = default; 103 104 /// ClientAsyncStreamingInterface 105 MOCK_METHOD1_T(StartCall, void(void*)); 106 MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); 107 MOCK_METHOD2_T(Finish, void(Status*, void*)); 108 109 /// AsyncReaderInterface 110 MOCK_METHOD2_T(Read, void(R*, void*)); 111 }; 112 113 template <class W> 114 class MockClientAsyncWriter : public grpc::ClientAsyncWriterInterface<W> { 115 public: 116 MockClientAsyncWriter() = default; 117 118 /// ClientAsyncStreamingInterface 119 MOCK_METHOD1_T(StartCall, void(void*)); 120 MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); 121 MOCK_METHOD2_T(Finish, void(Status*, void*)); 122 123 /// AsyncWriterInterface 124 MOCK_METHOD2_T(Write, void(const W&, void*)); 125 MOCK_METHOD3_T(Write, void(const W&, grpc::WriteOptions, void*)); 126 127 /// ClientAsyncWriterInterface 128 MOCK_METHOD1_T(WritesDone, void(void*)); 129 }; 130 131 template <class W, class R> 132 class MockClientAsyncReaderWriter 133 : public ClientAsyncReaderWriterInterface<W, R> { 134 public: 135 MockClientAsyncReaderWriter() = default; 136 137 /// ClientAsyncStreamingInterface 138 MOCK_METHOD1_T(StartCall, void(void*)); 139 MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); 140 MOCK_METHOD2_T(Finish, void(Status*, void*)); 141 142 /// AsyncWriterInterface 143 MOCK_METHOD2_T(Write, void(const W&, void*)); 144 MOCK_METHOD3_T(Write, void(const W&, grpc::WriteOptions, void*)); 145 146 /// AsyncReaderInterface 147 MOCK_METHOD2_T(Read, void(R*, void*)); 148 149 /// ClientAsyncReaderWriterInterface 150 MOCK_METHOD1_T(WritesDone, void(void*)); 151 }; 152 153 template <class R> 154 class MockServerReader : public grpc::ServerReaderInterface<R> { 155 public: 156 MockServerReader() = default; 157 158 /// ServerStreamingInterface 159 MOCK_METHOD0_T(SendInitialMetadata, void()); 160 161 /// ReaderInterface 162 MOCK_METHOD1_T(NextMessageSize, bool(uint32_t*)); 163 MOCK_METHOD1_T(Read, bool(R*)); 164 }; 165 166 template <class W> 167 class MockServerWriter : public grpc::ServerWriterInterface<W> { 168 public: 169 MockServerWriter() = default; 170 171 /// ServerStreamingInterface 172 MOCK_METHOD0_T(SendInitialMetadata, void()); 173 174 /// WriterInterface 175 MOCK_METHOD2_T(Write, bool(const W&, const WriteOptions)); 176 }; 177 178 template <class W, class R> 179 class MockServerReaderWriter : public grpc::ServerReaderWriterInterface<W, R> { 180 public: 181 MockServerReaderWriter() = default; 182 183 /// ServerStreamingInterface 184 MOCK_METHOD0_T(SendInitialMetadata, void()); 185 186 /// ReaderInterface 187 MOCK_METHOD1_T(NextMessageSize, bool(uint32_t*)); 188 MOCK_METHOD1_T(Read, bool(R*)); 189 190 /// WriterInterface 191 MOCK_METHOD2_T(Write, bool(const W&, const WriteOptions)); 192 }; 193 194 } // namespace testing 195 } // namespace grpc 196 197 #endif // GRPCPP_TEST_MOCK_STREAM_H 198