3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "test/cpp/end2end/interceptors_util.h"
21 #include "absl/memory/memory.h"
26 std::atomic<int> DummyInterceptor::num_times_run_;
27 std::atomic<int> DummyInterceptor::num_times_run_reverse_;
28 std::atomic<int> DummyInterceptor::num_times_cancel_;
30 void MakeCall(const std::shared_ptr<Channel>& channel) {
31 auto stub = grpc::testing::EchoTestService::NewStub(channel);
34 req.mutable_param()->set_echo_metadata(true);
35 ctx.AddMetadata("testkey", "testvalue");
36 req.set_message("Hello");
38 Status s = stub->Echo(&ctx, req, &resp);
39 EXPECT_EQ(s.ok(), true);
40 EXPECT_EQ(resp.message(), "Hello");
43 void MakeClientStreamingCall(const std::shared_ptr<Channel>& channel) {
44 auto stub = grpc::testing::EchoTestService::NewStub(channel);
47 req.mutable_param()->set_echo_metadata(true);
48 ctx.AddMetadata("testkey", "testvalue");
49 req.set_message("Hello");
51 string expected_resp = "";
52 auto writer = stub->RequestStream(&ctx, &resp);
53 for (int i = 0; i < kNumStreamingMessages; i++) {
55 expected_resp += "Hello";
58 Status s = writer->Finish();
59 EXPECT_EQ(s.ok(), true);
60 EXPECT_EQ(resp.message(), expected_resp);
63 void MakeServerStreamingCall(const std::shared_ptr<Channel>& channel) {
64 auto stub = grpc::testing::EchoTestService::NewStub(channel);
67 req.mutable_param()->set_echo_metadata(true);
68 ctx.AddMetadata("testkey", "testvalue");
69 req.set_message("Hello");
71 auto reader = stub->ResponseStream(&ctx, req);
73 while (reader->Read(&resp)) {
74 EXPECT_EQ(resp.message(), "Hello");
77 ASSERT_EQ(count, kNumStreamingMessages);
78 Status s = reader->Finish();
79 EXPECT_EQ(s.ok(), true);
82 void MakeBidiStreamingCall(const std::shared_ptr<Channel>& channel) {
83 auto stub = grpc::testing::EchoTestService::NewStub(channel);
87 ctx.AddMetadata("testkey", "testvalue");
88 req.mutable_param()->set_echo_metadata(true);
89 auto stream = stub->BidiStream(&ctx);
90 for (auto i = 0; i < kNumStreamingMessages; i++) {
91 req.set_message("Hello" + std::to_string(i));
94 EXPECT_EQ(req.message(), resp.message());
96 ASSERT_TRUE(stream->WritesDone());
97 Status s = stream->Finish();
98 EXPECT_EQ(s.ok(), true);
101 void MakeAsyncCQCall(const std::shared_ptr<Channel>& channel) {
102 auto stub = grpc::testing::EchoTestService::NewStub(channel);
104 EchoRequest send_request;
105 EchoResponse recv_response;
107 ClientContext cli_ctx;
109 send_request.set_message("Hello");
110 cli_ctx.AddMetadata("testkey", "testvalue");
111 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
112 stub->AsyncEcho(&cli_ctx, send_request, &cq));
113 response_reader->Finish(&recv_response, &recv_status, tag(1));
114 Verifier().Expect(1, true).Verify(&cq);
115 EXPECT_EQ(send_request.message(), recv_response.message());
116 EXPECT_TRUE(recv_status.ok());
119 void MakeAsyncCQClientStreamingCall(
120 const std::shared_ptr<Channel>& /*channel*/) {
121 // TODO(yashykt) : Fill this out
124 void MakeAsyncCQServerStreamingCall(const std::shared_ptr<Channel>& channel) {
125 auto stub = grpc::testing::EchoTestService::NewStub(channel);
127 EchoRequest send_request;
128 EchoResponse recv_response;
130 ClientContext cli_ctx;
132 cli_ctx.AddMetadata("testkey", "testvalue");
133 send_request.set_message("Hello");
134 std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream(
135 stub->AsyncResponseStream(&cli_ctx, send_request, &cq, tag(1)));
136 Verifier().Expect(1, true).Verify(&cq);
137 // Read the expected number of messages
138 for (int i = 0; i < kNumStreamingMessages; i++) {
139 cli_stream->Read(&recv_response, tag(2));
140 Verifier().Expect(2, true).Verify(&cq);
141 ASSERT_EQ(recv_response.message(), send_request.message());
143 // The next read should fail
144 cli_stream->Read(&recv_response, tag(3));
145 Verifier().Expect(3, false).Verify(&cq);
147 cli_stream->Finish(&recv_status, tag(4));
148 Verifier().Expect(4, true).Verify(&cq);
149 EXPECT_TRUE(recv_status.ok());
152 void MakeAsyncCQBidiStreamingCall(const std::shared_ptr<Channel>& /*channel*/) {
153 // TODO(yashykt) : Fill this out
156 void MakeCallbackCall(const std::shared_ptr<Channel>& channel) {
157 auto stub = grpc::testing::EchoTestService::NewStub(channel);
161 std::condition_variable cv;
163 req.mutable_param()->set_echo_metadata(true);
164 ctx.AddMetadata("testkey", "testvalue");
165 req.set_message("Hello");
167 stub->experimental_async()->Echo(&ctx, &req, &resp,
168 [&resp, &mu, &done, &cv](Status s) {
169 EXPECT_EQ(s.ok(), true);
170 EXPECT_EQ(resp.message(), "Hello");
171 std::lock_guard<std::mutex> l(mu);
175 std::unique_lock<std::mutex> l(mu);
181 bool CheckMetadata(const std::multimap<grpc::string_ref, grpc::string_ref>& map,
182 const string& key, const string& value) {
183 for (const auto& pair : map) {
184 if (pair.first.starts_with(key) && pair.second.starts_with(value)) {
191 bool CheckMetadata(const std::multimap<std::string, std::string>& map,
192 const string& key, const string& value) {
193 for (const auto& pair : map) {
194 if (pair.first == key && pair.second == value) {
201 std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
202 CreateDummyClientInterceptors() {
203 std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
205 // Add 20 dummy interceptors before hijacking interceptor
206 creators.reserve(20);
207 for (auto i = 0; i < 20; i++) {
208 creators.push_back(absl::make_unique<DummyInterceptorFactory>());
213 } // namespace testing