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