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 #include <grpcpp/impl/codegen/call.h> 26 #include <grpcpp/support/async_stream.h> 27 #include <grpcpp/support/async_unary_call.h> 28 #include <grpcpp/support/sync_stream.h> 29 30 namespace grpc { 31 namespace testing { 32 33 template <class R> 34 class MockClientReader : public ::grpc::ClientReaderInterface<R> { 35 public: 36 MockClientReader() = default; 37 38 /// ClientStreamingInterface 39 MOCK_METHOD0_T(Finish, Status()); 40 41 /// ReaderInterface 42 MOCK_METHOD1_T(NextMessageSize, bool(uint32_t*)); 43 MOCK_METHOD1_T(Read, bool(R*)); 44 45 /// ClientReaderInterface 46 MOCK_METHOD0_T(WaitForInitialMetadata, void()); 47 }; 48 49 template <class W> 50 class MockClientWriter : public ::grpc::ClientWriterInterface<W> { 51 public: 52 MockClientWriter() = default; 53 54 /// ClientStreamingInterface 55 MOCK_METHOD0_T(Finish, Status()); 56 57 /// WriterInterface 58 MOCK_METHOD2_T(Write, bool(const W&, const WriteOptions)); 59 60 /// ClientWriterInterface 61 MOCK_METHOD0_T(WritesDone, bool()); 62 }; 63 64 template <class W, class R> 65 class MockClientReaderWriter 66 : 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 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(ReadInitialMetadata, void(void*)); 104 MOCK_METHOD2_T(Finish, void(Status*, void*)); 105 106 /// AsyncReaderInterface 107 MOCK_METHOD2_T(Read, void(R*, void*)); 108 }; 109 110 template <class W> 111 class MockClientAsyncWriter : public ::grpc::ClientAsyncWriterInterface<W> { 112 public: 113 MockClientAsyncWriter() = default; 114 115 /// ClientAsyncStreamingInterface 116 MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); 117 MOCK_METHOD2_T(Finish, void(Status*, void*)); 118 119 /// AsyncWriterInterface 120 MOCK_METHOD2_T(Write, void(const W&, void*)); 121 122 /// ClientAsyncWriterInterface 123 MOCK_METHOD1_T(WritesDone, void(void*)); 124 }; 125 126 template <class W, class R> 127 class MockClientAsyncReaderWriter 128 : public ClientAsyncReaderWriterInterface<W, R> { 129 public: 130 MockClientAsyncReaderWriter() = default; 131 132 /// ClientAsyncStreamingInterface 133 MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); 134 MOCK_METHOD2_T(Finish, void(Status*, void*)); 135 136 /// AsyncWriterInterface 137 MOCK_METHOD2_T(Write, void(const W&, void*)); 138 139 /// AsyncReaderInterface 140 MOCK_METHOD2_T(Read, void(R*, void*)); 141 142 /// ClientAsyncReaderWriterInterface 143 MOCK_METHOD1_T(WritesDone, void(void*)); 144 }; 145 146 } // namespace testing 147 } // namespace grpc 148 149 #endif // GRPCPP_TEST_MOCK_STREAM_H 150