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"
24 std::atomic<int> DummyInterceptor::num_times_run_;
25 std::atomic<int> DummyInterceptor::num_times_run_reverse_;
26 std::atomic<int> DummyInterceptor::num_times_cancel_;
28 void MakeCall(const std::shared_ptr<Channel>& channel) {
29 auto stub = grpc::testing::EchoTestService::NewStub(channel);
32 req.mutable_param()->set_echo_metadata(true);
33 ctx.AddMetadata("testkey", "testvalue");
34 req.set_message("Hello");
36 Status s = stub->Echo(&ctx, req, &resp);
37 EXPECT_EQ(s.ok(), true);
38 EXPECT_EQ(resp.message(), "Hello");
41 void MakeClientStreamingCall(const std::shared_ptr<Channel>& channel) {
42 auto stub = grpc::testing::EchoTestService::NewStub(channel);
45 req.mutable_param()->set_echo_metadata(true);
46 ctx.AddMetadata("testkey", "testvalue");
47 req.set_message("Hello");
49 string expected_resp = "";
50 auto writer = stub->RequestStream(&ctx, &resp);
51 for (int i = 0; i < kNumStreamingMessages; i++) {
53 expected_resp += "Hello";
56 Status s = writer->Finish();
57 EXPECT_EQ(s.ok(), true);
58 EXPECT_EQ(resp.message(), expected_resp);
61 void MakeServerStreamingCall(const std::shared_ptr<Channel>& channel) {
62 auto stub = grpc::testing::EchoTestService::NewStub(channel);
65 req.mutable_param()->set_echo_metadata(true);
66 ctx.AddMetadata("testkey", "testvalue");
67 req.set_message("Hello");
69 auto reader = stub->ResponseStream(&ctx, req);
71 while (reader->Read(&resp)) {
72 EXPECT_EQ(resp.message(), "Hello");
75 ASSERT_EQ(count, kNumStreamingMessages);
76 Status s = reader->Finish();
77 EXPECT_EQ(s.ok(), true);
80 void MakeBidiStreamingCall(const std::shared_ptr<Channel>& channel) {
81 auto stub = grpc::testing::EchoTestService::NewStub(channel);
85 ctx.AddMetadata("testkey", "testvalue");
86 req.mutable_param()->set_echo_metadata(true);
87 auto stream = stub->BidiStream(&ctx);
88 for (auto i = 0; i < kNumStreamingMessages; i++) {
89 req.set_message("Hello" + std::to_string(i));
92 EXPECT_EQ(req.message(), resp.message());
94 ASSERT_TRUE(stream->WritesDone());
95 Status s = stream->Finish();
96 EXPECT_EQ(s.ok(), true);
99 void MakeAsyncCQCall(const std::shared_ptr<Channel>& channel) {
100 auto stub = grpc::testing::EchoTestService::NewStub(channel);
102 EchoRequest send_request;
103 EchoResponse recv_response;
105 ClientContext cli_ctx;
107 send_request.set_message("Hello");
108 cli_ctx.AddMetadata("testkey", "testvalue");
109 std::unique_ptr<ClientAsyncResponseReader<EchoResponse>> response_reader(
110 stub->AsyncEcho(&cli_ctx, send_request, &cq));
111 response_reader->Finish(&recv_response, &recv_status, tag(1));
112 Verifier().Expect(1, true).Verify(&cq);
113 EXPECT_EQ(send_request.message(), recv_response.message());
114 EXPECT_TRUE(recv_status.ok());
117 void MakeAsyncCQClientStreamingCall(
118 const std::shared_ptr<Channel>& /*channel*/) {
119 // TODO(yashykt) : Fill this out
122 void MakeAsyncCQServerStreamingCall(const std::shared_ptr<Channel>& channel) {
123 auto stub = grpc::testing::EchoTestService::NewStub(channel);
125 EchoRequest send_request;
126 EchoResponse recv_response;
128 ClientContext cli_ctx;
130 cli_ctx.AddMetadata("testkey", "testvalue");
131 send_request.set_message("Hello");
132 std::unique_ptr<ClientAsyncReader<EchoResponse>> cli_stream(
133 stub->AsyncResponseStream(&cli_ctx, send_request, &cq, tag(1)));
134 Verifier().Expect(1, true).Verify(&cq);
135 // Read the expected number of messages
136 for (int i = 0; i < kNumStreamingMessages; i++) {
137 cli_stream->Read(&recv_response, tag(2));
138 Verifier().Expect(2, true).Verify(&cq);
139 ASSERT_EQ(recv_response.message(), send_request.message());
141 // The next read should fail
142 cli_stream->Read(&recv_response, tag(3));
143 Verifier().Expect(3, false).Verify(&cq);
145 cli_stream->Finish(&recv_status, tag(4));
146 Verifier().Expect(4, true).Verify(&cq);
147 EXPECT_TRUE(recv_status.ok());
150 void MakeAsyncCQBidiStreamingCall(const std::shared_ptr<Channel>& /*channel*/) {
151 // TODO(yashykt) : Fill this out
154 void MakeCallbackCall(const std::shared_ptr<Channel>& channel) {
155 auto stub = grpc::testing::EchoTestService::NewStub(channel);
159 std::condition_variable cv;
161 req.mutable_param()->set_echo_metadata(true);
162 ctx.AddMetadata("testkey", "testvalue");
163 req.set_message("Hello");
165 stub->experimental_async()->Echo(&ctx, &req, &resp,
166 [&resp, &mu, &done, &cv](Status s) {
167 EXPECT_EQ(s.ok(), true);
168 EXPECT_EQ(resp.message(), "Hello");
169 std::lock_guard<std::mutex> l(mu);
173 std::unique_lock<std::mutex> l(mu);
179 bool CheckMetadata(const std::multimap<grpc::string_ref, grpc::string_ref>& map,
180 const string& key, const string& value) {
181 for (const auto& pair : map) {
182 if (pair.first.starts_with(key) && pair.second.starts_with(value)) {
189 bool CheckMetadata(const std::multimap<std::string, std::string>& map,
190 const string& key, const string& value) {
191 for (const auto& pair : map) {
192 if (pair.first == key && pair.second == value) {
199 std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
200 CreateDummyClientInterceptors() {
201 std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
203 // Add 20 dummy interceptors before hijacking interceptor
204 creators.reserve(20);
205 for (auto i = 0; i < 20; i++) {
206 creators.push_back(std::unique_ptr<DummyInterceptorFactory>(
207 new DummyInterceptorFactory()));
212 } // namespace testing