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