1 //
2 //
3 // Copyright 2015 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 <time.h>
20
21 #include <mutex>
22 #include <thread>
23
24 #include <gtest/gtest.h>
25
26 #include <grpc/grpc.h>
27 #include <grpc/support/atm.h>
28 #include <grpc/support/log.h>
29 #include <grpc/support/time.h>
30 #include <grpcpp/channel.h>
31 #include <grpcpp/client_context.h>
32 #include <grpcpp/create_channel.h>
33 #include <grpcpp/security/credentials.h>
34 #include <grpcpp/security/server_credentials.h>
35 #include <grpcpp/server.h>
36 #include <grpcpp/server_builder.h>
37 #include <grpcpp/server_context.h>
38
39 #include "src/core/lib/gprpp/crash.h"
40 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
41 #include "src/proto/grpc/testing/echo.grpc.pb.h"
42 #include "test/core/util/port.h"
43 #include "test/core/util/test_config.h"
44
45 using grpc::testing::EchoRequest;
46 using grpc::testing::EchoResponse;
47
48 const char* kLargeString =
49 "("
50 "To be, or not to be- that is the question:"
51 "Whether 'tis nobler in the mind to suffer"
52 "The slings and arrows of outrageous fortune"
53 "Or to take arms against a sea of troubles,"
54 "And by opposing end them. To die- to sleep-"
55 "No more; and by a sleep to say we end"
56 "The heartache, and the thousand natural shock"
57 "That flesh is heir to. 'Tis a consummation"
58 "Devoutly to be wish'd. To die- to sleep."
59 "To sleep- perchance to dream: ay, there's the rub!"
60 "For in that sleep of death what dreams may come"
61 "When we have shuffled off this mortal coil,"
62 "Must give us pause. There's the respect"
63 "That makes calamity of so long life."
64 "For who would bear the whips and scorns of time,"
65 "Th' oppressor's wrong, the proud man's contumely,"
66 "The pangs of despis'd love, the law's delay,"
67 "The insolence of office, and the spurns"
68 "That patient merit of th' unworthy takes,"
69 "When he himself might his quietus make"
70 "With a bare bodkin? Who would these fardels bear,"
71 "To grunt and sweat under a weary life,"
72 "But that the dread of something after death-"
73 "The undiscover'd country, from whose bourn"
74 "No traveller returns- puzzles the will,"
75 "And makes us rather bear those ills we have"
76 "Than fly to others that we know not of?"
77 "Thus conscience does make cowards of us all,"
78 "And thus the native hue of resolution"
79 "Is sicklied o'er with the pale cast of thought,"
80 "And enterprises of great pith and moment"
81 "With this regard their currents turn awry"
82 "And lose the name of action.- Soft you now!"
83 "The fair Ophelia!- Nymph, in thy orisons"
84 "Be all my sins rememb'red.";
85
86 namespace grpc {
87 namespace testing {
88
89 class TestServiceImpl : public grpc::testing::EchoTestService::Service {
90 public:
BidiStream_Sender(ServerReaderWriter<EchoResponse,EchoRequest> * stream,gpr_atm * should_exit)91 static void BidiStream_Sender(
92 ServerReaderWriter<EchoResponse, EchoRequest>* stream,
93 gpr_atm* should_exit) {
94 EchoResponse response;
95 response.set_message(kLargeString);
96 while (gpr_atm_acq_load(should_exit) == gpr_atm{0}) {
97 struct timespec tv = {0, 1000000}; // 1 ms
98 struct timespec rem;
99 // TODO (vpai): Mark this blocking
100 while (nanosleep(&tv, &rem) != 0) {
101 tv = rem;
102 };
103
104 stream->Write(response);
105 }
106 }
107
108 // Only implement the one method we will be calling for brevity.
BidiStream(ServerContext *,ServerReaderWriter<EchoResponse,EchoRequest> * stream)109 Status BidiStream(
110 ServerContext* /*context*/,
111 ServerReaderWriter<EchoResponse, EchoRequest>* stream) override {
112 EchoRequest request;
113 gpr_atm should_exit;
114 gpr_atm_rel_store(&should_exit, gpr_atm{0});
115
116 std::thread sender(
117 std::bind(&TestServiceImpl::BidiStream_Sender, stream, &should_exit));
118
119 while (stream->Read(&request)) {
120 struct timespec tv = {0, 3000000}; // 3 ms
121 struct timespec rem;
122 // TODO (vpai): Mark this blocking
123 while (nanosleep(&tv, &rem) != 0) {
124 tv = rem;
125 };
126 }
127 gpr_atm_rel_store(&should_exit, gpr_atm{1});
128 sender.join();
129 return Status::OK;
130 }
131 };
132
133 class End2endTest : public ::testing::Test {
134 protected:
SetUp()135 void SetUp() override {
136 int port = grpc_pick_unused_port_or_die();
137 server_address_ << "localhost:" << port;
138 // Setup server
139 ServerBuilder builder;
140 builder.AddListeningPort(server_address_.str(),
141 InsecureServerCredentials());
142 builder.RegisterService(&service_);
143 server_ = builder.BuildAndStart();
144 }
145
TearDown()146 void TearDown() override { server_->Shutdown(); }
147
ResetStub()148 void ResetStub() {
149 std::shared_ptr<Channel> channel = grpc::CreateChannel(
150 server_address_.str(), InsecureChannelCredentials());
151 stub_ = grpc::testing::EchoTestService::NewStub(channel);
152 }
153
154 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
155 std::unique_ptr<Server> server_;
156 std::ostringstream server_address_;
157 TestServiceImpl service_;
158 };
159
Drainer(ClientReaderWriter<EchoRequest,EchoResponse> * reader)160 static void Drainer(ClientReaderWriter<EchoRequest, EchoResponse>* reader) {
161 EchoResponse response;
162 while (reader->Read(&response)) {
163 // Just drain out the responses as fast as possible.
164 }
165 }
166
TEST_F(End2endTest,StreamingThroughput)167 TEST_F(End2endTest, StreamingThroughput) {
168 ResetStub();
169 grpc::ClientContext context;
170 auto stream = stub_->BidiStream(&context);
171
172 auto reader = stream.get();
173 std::thread receiver(std::bind(Drainer, reader));
174
175 for (int i = 0; i < 10000; i++) {
176 EchoRequest request;
177 request.set_message(kLargeString);
178 ASSERT_TRUE(stream->Write(request));
179 if (i % 1000 == 0) {
180 gpr_log(GPR_INFO, "Send count = %d", i);
181 }
182 }
183 stream->WritesDone();
184 receiver.join();
185 }
186
187 } // namespace testing
188 } // namespace grpc
189
main(int argc,char ** argv)190 int main(int argc, char** argv) {
191 grpc::testing::TestEnvironment env(&argc, argv);
192 ::testing::InitGoogleTest(&argc, argv);
193 return RUN_ALL_TESTS();
194 }
195