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