Imported Upstream version 1.22.0
[platform/upstream/grpc.git] / test / cpp / end2end / interceptors_util.cc
1 /*
2  *
3  * Copyright 2018 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 #include "test/cpp/end2end/interceptors_util.h"
20
21 namespace grpc {
22 namespace testing {
23
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_;
27
28 void MakeCall(const std::shared_ptr<Channel>& channel) {
29   auto stub = grpc::testing::EchoTestService::NewStub(channel);
30   ClientContext ctx;
31   EchoRequest req;
32   req.mutable_param()->set_echo_metadata(true);
33   ctx.AddMetadata("testkey", "testvalue");
34   req.set_message("Hello");
35   EchoResponse resp;
36   Status s = stub->Echo(&ctx, req, &resp);
37   EXPECT_EQ(s.ok(), true);
38   EXPECT_EQ(resp.message(), "Hello");
39 }
40
41 void MakeClientStreamingCall(const std::shared_ptr<Channel>& channel) {
42   auto stub = grpc::testing::EchoTestService::NewStub(channel);
43   ClientContext ctx;
44   EchoRequest req;
45   req.mutable_param()->set_echo_metadata(true);
46   ctx.AddMetadata("testkey", "testvalue");
47   req.set_message("Hello");
48   EchoResponse resp;
49   string expected_resp = "";
50   auto writer = stub->RequestStream(&ctx, &resp);
51   for (int i = 0; i < kNumStreamingMessages; i++) {
52     writer->Write(req);
53     expected_resp += "Hello";
54   }
55   writer->WritesDone();
56   Status s = writer->Finish();
57   EXPECT_EQ(s.ok(), true);
58   EXPECT_EQ(resp.message(), expected_resp);
59 }
60
61 void MakeServerStreamingCall(const std::shared_ptr<Channel>& channel) {
62   auto stub = grpc::testing::EchoTestService::NewStub(channel);
63   ClientContext ctx;
64   EchoRequest req;
65   req.mutable_param()->set_echo_metadata(true);
66   ctx.AddMetadata("testkey", "testvalue");
67   req.set_message("Hello");
68   EchoResponse resp;
69   string expected_resp = "";
70   auto reader = stub->ResponseStream(&ctx, req);
71   int count = 0;
72   while (reader->Read(&resp)) {
73     EXPECT_EQ(resp.message(), "Hello");
74     count++;
75   }
76   ASSERT_EQ(count, kNumStreamingMessages);
77   Status s = reader->Finish();
78   EXPECT_EQ(s.ok(), true);
79 }
80
81 void MakeBidiStreamingCall(const std::shared_ptr<Channel>& channel) {
82   auto stub = grpc::testing::EchoTestService::NewStub(channel);
83   ClientContext ctx;
84   EchoRequest req;
85   EchoResponse resp;
86   ctx.AddMetadata("testkey", "testvalue");
87   auto stream = stub->BidiStream(&ctx);
88   for (auto i = 0; i < kNumStreamingMessages; i++) {
89     req.set_message("Hello" + std::to_string(i));
90     stream->Write(req);
91     stream->Read(&resp);
92     EXPECT_EQ(req.message(), resp.message());
93   }
94   ASSERT_TRUE(stream->WritesDone());
95   Status s = stream->Finish();
96   EXPECT_EQ(s.ok(), true);
97 }
98
99 void MakeCallbackCall(const std::shared_ptr<Channel>& channel) {
100   auto stub = grpc::testing::EchoTestService::NewStub(channel);
101   ClientContext ctx;
102   EchoRequest req;
103   std::mutex mu;
104   std::condition_variable cv;
105   bool done = false;
106   req.mutable_param()->set_echo_metadata(true);
107   ctx.AddMetadata("testkey", "testvalue");
108   req.set_message("Hello");
109   EchoResponse resp;
110   stub->experimental_async()->Echo(&ctx, &req, &resp,
111                                    [&resp, &mu, &done, &cv](Status s) {
112                                      // gpr_log(GPR_ERROR, "got the callback");
113                                      EXPECT_EQ(s.ok(), true);
114                                      EXPECT_EQ(resp.message(), "Hello");
115                                      std::lock_guard<std::mutex> l(mu);
116                                      done = true;
117                                      cv.notify_one();
118                                    });
119   std::unique_lock<std::mutex> l(mu);
120   while (!done) {
121     cv.wait(l);
122   }
123 }
124
125 bool CheckMetadata(const std::multimap<grpc::string_ref, grpc::string_ref>& map,
126                    const string& key, const string& value) {
127   for (const auto& pair : map) {
128     if (pair.first.starts_with(key) && pair.second.starts_with(value)) {
129       return true;
130     }
131   }
132   return false;
133 }
134
135 bool CheckMetadata(const std::multimap<grpc::string, grpc::string>& map,
136                    const string& key, const string& value) {
137   for (const auto& pair : map) {
138     if (pair.first == key && pair.second == value) {
139       return true;
140     }
141   }
142   return false;
143 }
144
145 std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
146 CreateDummyClientInterceptors() {
147   std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
148       creators;
149   // Add 20 dummy interceptors before hijacking interceptor
150   creators.reserve(20);
151   for (auto i = 0; i < 20; i++) {
152     creators.push_back(std::unique_ptr<DummyInterceptorFactory>(
153         new DummyInterceptorFactory()));
154   }
155   return creators;
156 }
157
158 }  // namespace testing
159 }  // namespace grpc