• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   /// 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 }  // namespace testing
154 }  // namespace grpc
155 
156 #endif  // GRPCPP_TEST_MOCK_STREAM_H
157