1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/*
*
* Copyright 2018 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "test/cpp/end2end/interceptors_util.h"
#include <util/string/cast.h>
namespace grpc {
namespace testing {
std::atomic<int> DummyInterceptor::num_times_run_;
std::atomic<int> DummyInterceptor::num_times_run_reverse_;
std::atomic<int> DummyInterceptor::num_times_cancel_;
void MakeCall(const std::shared_ptr<Channel>& channel) {
auto stub = grpc::testing::EchoTestService::NewStub(channel);
ClientContext ctx;
EchoRequest req;
req.mutable_param()->set_echo_metadata(true);
ctx.AddMetadata("testkey", "testvalue");
req.set_message("Hello");
EchoResponse resp;
Status s = stub->Echo(&ctx, req, &resp);
EXPECT_EQ(s.ok(), true);
EXPECT_EQ(resp.message(), "Hello");
}
void MakeClientStreamingCall(const std::shared_ptr<Channel>& channel) {
auto stub = grpc::testing::EchoTestService::NewStub(channel);
ClientContext ctx;
EchoRequest req;
req.mutable_param()->set_echo_metadata(true);
ctx.AddMetadata("testkey", "testvalue");
req.set_message("Hello");
EchoResponse resp;
string expected_resp = "";
auto writer = stub->RequestStream(&ctx, &resp);
for (int i = 0; i < kNumStreamingMessages; i++) {
writer->Write(req);
expected_resp += "Hello";
}
writer->WritesDone();
Status s = writer->Finish();
EXPECT_EQ(s.ok(), true);
EXPECT_EQ(resp.message(), expected_resp);
}
void MakeServerStreamingCall(const std::shared_ptr<Channel>& channel) {
auto stub = grpc::testing::EchoTestService::NewStub(channel);
ClientContext ctx;
EchoRequest req;
req.mutable_param()->set_echo_metadata(true);
ctx.AddMetadata("testkey", "testvalue");
req.set_message("Hello");
EchoResponse resp;
auto reader = stub->ResponseStream(&ctx, req);
int count = 0;
while (reader->Read(&resp)) {
EXPECT_EQ(resp.message(), "Hello");
count++;
}
ASSERT_EQ(count, kNumStreamingMessages);
Status s = reader->Finish();
EXPECT_EQ(s.ok(), true);
}
void MakeBidiStreamingCall(const std::shared_ptr<Channel>& channel) {
auto stub = grpc::testing::EchoTestService::NewStub(channel);
ClientContext ctx;
EchoRequest req;
EchoResponse resp;
ctx.AddMetadata("testkey", "testvalue");
req.mutable_param()->set_echo_metadata(true);
auto stream = stub->BidiStream(&ctx);
for (auto i = 0; i < kNumStreamingMessages; i++) {
req.set_message(TString("Hello") + ::ToString(i));
stream->Write(req);
stream->Read(&resp);
EXPECT_EQ(req.message(), resp.message());
}
ASSERT_TRUE(stream->WritesDone());
Status s = stream->Finish();
EXPECT_EQ(s.ok(), true);
}
void MakeAsyncCQCall(const std::shared_ptr<Channel>& channel) {
auto stub = grpc::testing::EchoTestService::NewStub(channel);
CompletionQueue cq;
EchoRequest send_request;
EchoResponse recv_response;
Status recv_status;
ClientContext cli_ctx;
send_request.set_message("Hello");
cli_ctx.AddMetadata("testkey", "testvalue");
std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
stub->AsyncEcho(&cli_ctx, send_request, &cq));
response_reader->Finish(&recv_response, &recv_status, tag(1));
Verifier().Expect(1, true).Verify(&cq);
EXPECT_EQ(send_request.message(), recv_response.message());
EXPECT_TRUE(recv_status.ok());
}
void MakeAsyncCQClientStreamingCall(
const std::shared_ptr<Channel>& /*channel*/) {
// TODO(yashykt) : Fill this out
}
void MakeAsyncCQServerStreamingCall(const std::shared_ptr<Channel>& channel) {
auto stub = grpc::testing::EchoTestService::NewStub(channel);
CompletionQueue cq;
EchoRequest send_request;
EchoResponse recv_response;
Status recv_status;
ClientContext cli_ctx;
cli_ctx.AddMetadata("testkey", "testvalue");
send_request.set_message("Hello");
std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream(
stub->AsyncResponseStream(&cli_ctx, send_request, &cq, tag(1)));
Verifier().Expect(1, true).Verify(&cq);
// Read the expected number of messages
for (int i = 0; i < kNumStreamingMessages; i++) {
cli_stream->Read(&recv_response, tag(2));
Verifier().Expect(2, true).Verify(&cq);
ASSERT_EQ(recv_response.message(), send_request.message());
}
// The next read should fail
cli_stream->Read(&recv_response, tag(3));
Verifier().Expect(3, false).Verify(&cq);
// Get the status
cli_stream->Finish(&recv_status, tag(4));
Verifier().Expect(4, true).Verify(&cq);
EXPECT_TRUE(recv_status.ok());
}
void MakeAsyncCQBidiStreamingCall(const std::shared_ptr<Channel>& /*channel*/) {
// TODO(yashykt) : Fill this out
}
void MakeCallbackCall(const std::shared_ptr<Channel>& channel) {
auto stub = grpc::testing::EchoTestService::NewStub(channel);
ClientContext ctx;
EchoRequest req;
std::mutex mu;
std::condition_variable cv;
bool done = false;
req.mutable_param()->set_echo_metadata(true);
ctx.AddMetadata("testkey", "testvalue");
req.set_message("Hello");
EchoResponse resp;
stub->experimental_async()->Echo(&ctx, &req, &resp,
[&resp, &mu, &done, &cv](Status s) {
EXPECT_EQ(s.ok(), true);
EXPECT_EQ(resp.message(), "Hello");
std::lock_guard<std::mutex> l(mu);
done = true;
cv.notify_one();
});
std::unique_lock<std::mutex> l(mu);
while (!done) {
cv.wait(l);
}
}
bool CheckMetadata(const std::multimap<grpc::string_ref, grpc::string_ref>& map,
const string& key, const string& value) {
for (const auto& pair : map) {
if (pair.first.starts_with(key) && pair.second.starts_with(value)) {
return true;
}
}
return false;
}
bool CheckMetadata(const std::multimap<TString, TString>& map,
const string& key, const string& value) {
for (const auto& pair : map) {
if (pair.first == key.c_str() && pair.second == value.c_str()) {
return true;
}
}
return false;
}
std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
CreateDummyClientInterceptors() {
std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
creators;
// Add 20 dummy interceptors before hijacking interceptor
creators.reserve(20);
for (auto i = 0; i < 20; i++) {
creators.push_back(std::unique_ptr<DummyInterceptorFactory>(
new DummyInterceptorFactory()));
}
return creators;
}
} // namespace testing
} // namespace grpc
|