blob: ef2b53f36f3930b8b1a5e7aff335385bd2c33716 [file] [log] [blame]
Rakesh Iyer4abe76e2015-08-26 11:43:53 -07001/*
2 *
murgatroid99ace28d32016-01-14 10:12:10 -08003 * Copyright 2015-2016, Google Inc.
Rakesh Iyer4abe76e2015-08-26 11:43:53 -07004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080034#include <time.h>
Rakesh Iyer4abe76e2015-08-26 11:43:53 -070035#include <mutex>
36#include <thread>
37
Rakesh Iyer4abe76e2015-08-26 11:43:53 -070038#include <grpc++/channel.h>
39#include <grpc++/client_context.h>
40#include <grpc++/create_channel.h>
Craig Tiller93ddc612015-09-02 07:57:48 -070041#include <grpc++/security/credentials.h>
42#include <grpc++/security/server_credentials.h>
Rakesh Iyer4abe76e2015-08-26 11:43:53 -070043#include <grpc++/server.h>
44#include <grpc++/server_builder.h>
45#include <grpc++/server_context.h>
Craig Tiller93ddc612015-09-02 07:57:48 -070046#include <grpc/grpc.h>
Vijay Pai84232032015-09-29 23:16:08 +000047#include <grpc/support/atm.h>
Craig Tiller93ddc612015-09-02 07:57:48 -070048#include <grpc/support/thd.h>
49#include <grpc/support/time.h>
Rakesh Iyer4abe76e2015-08-26 11:43:53 -070050#include <gtest/gtest.h>
51
Craig Tiller1b4e3302015-12-17 16:35:00 -080052#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
53#include "src/proto/grpc/testing/echo.grpc.pb.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080054#include "test/core/util/port.h"
55#include "test/core/util/test_config.h"
Rakesh Iyer4abe76e2015-08-26 11:43:53 -070056
Craig Tiller1b4e3302015-12-17 16:35:00 -080057using grpc::testing::EchoRequest;
58using grpc::testing::EchoResponse;
Rakesh Iyer4abe76e2015-08-26 11:43:53 -070059using std::chrono::system_clock;
60
Craig Tiller71a0f9d2015-09-28 17:22:01 -070061const char* kLargeString =
62 "("
63 "To be, or not to be- that is the question:"
64 "Whether 'tis nobler in the mind to suffer"
65 "The slings and arrows of outrageous fortune"
66 "Or to take arms against a sea of troubles,"
67 "And by opposing end them. To die- to sleep-"
68 "No more; and by a sleep to say we end"
69 "The heartache, and the thousand natural shock"
70 "That flesh is heir to. 'Tis a consummation"
71 "Devoutly to be wish'd. To die- to sleep."
72 "To sleep- perchance to dream: ay, there's the rub!"
73 "For in that sleep of death what dreams may come"
74 "When we have shuffled off this mortal coil,"
75 "Must give us pause. There's the respect"
76 "That makes calamity of so long life."
77 "For who would bear the whips and scorns of time,"
78 "Th' oppressor's wrong, the proud man's contumely,"
79 "The pangs of despis'd love, the law's delay,"
80 "The insolence of office, and the spurns"
81 "That patient merit of th' unworthy takes,"
82 "When he himself might his quietus make"
83 "With a bare bodkin? Who would these fardels bear,"
84 "To grunt and sweat under a weary life,"
85 "But that the dread of something after death-"
86 "The undiscover'd country, from whose bourn"
87 "No traveller returns- puzzles the will,"
88 "And makes us rather bear those ills we have"
89 "Than fly to others that we know not of?"
90 "Thus conscience does make cowards of us all,"
91 "And thus the native hue of resolution"
92 "Is sicklied o'er with the pale cast of thought,"
93 "And enterprises of great pith and moment"
94 "With this regard their currents turn awry"
95 "And lose the name of action.- Soft you now!"
96 "The fair Ophelia!- Nymph, in thy orisons"
97 "Be all my sins rememb'red.";
Rakesh Iyer4abe76e2015-08-26 11:43:53 -070098
99namespace grpc {
100namespace testing {
101
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800102class TestServiceImpl : public ::grpc::testing::EchoTestService::Service {
Rakesh Iyer4abe76e2015-08-26 11:43:53 -0700103 public:
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700104 static void BidiStream_Sender(
105 ServerReaderWriter<EchoResponse, EchoRequest>* stream,
Craig Tillerb7a59772015-10-01 08:01:58 -0700106 gpr_atm* should_exit) {
Craig Tillera4517a22015-08-31 07:29:12 -0700107 EchoResponse response;
108 response.set_message(kLargeString);
Vijay Pai84232032015-09-29 23:16:08 +0000109 while (gpr_atm_acq_load(should_exit) == static_cast<gpr_atm>(0)) {
Craig Tillerb7a59772015-10-01 08:01:58 -0700110 struct timespec tv = {0, 1000000}; // 1 ms
Vijay Pai84232032015-09-29 23:16:08 +0000111 struct timespec rem;
112 // TODO (vpai): Mark this blocking
113 while (nanosleep(&tv, &rem) != 0) {
Craig Tillerb7a59772015-10-01 08:01:58 -0700114 tv = rem;
Vijay Pai84232032015-09-29 23:16:08 +0000115 };
116
Craig Tillera4517a22015-08-31 07:29:12 -0700117 stream->Write(response);
118 }
119 }
120
Rakesh Iyer4abe76e2015-08-26 11:43:53 -0700121 // Only implement the one method we will be calling for brevity.
122 Status BidiStream(ServerContext* context,
123 ServerReaderWriter<EchoResponse, EchoRequest>* stream)
124 GRPC_OVERRIDE {
125 EchoRequest request;
Vijay Pai84232032015-09-29 23:16:08 +0000126 gpr_atm should_exit;
127 gpr_atm_rel_store(&should_exit, static_cast<gpr_atm>(0));
128
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700129 std::thread sender(
130 std::bind(&TestServiceImpl::BidiStream_Sender, stream, &should_exit));
Rakesh Iyer4abe76e2015-08-26 11:43:53 -0700131
132 while (stream->Read(&request)) {
Craig Tillerb7a59772015-10-01 08:01:58 -0700133 struct timespec tv = {0, 3000000}; // 3 ms
Vijay Pai84232032015-09-29 23:16:08 +0000134 struct timespec rem;
135 // TODO (vpai): Mark this blocking
136 while (nanosleep(&tv, &rem) != 0) {
Craig Tillerb7a59772015-10-01 08:01:58 -0700137 tv = rem;
Vijay Pai84232032015-09-29 23:16:08 +0000138 };
Rakesh Iyer4abe76e2015-08-26 11:43:53 -0700139 }
Vijay Pai84232032015-09-29 23:16:08 +0000140 gpr_atm_rel_store(&should_exit, static_cast<gpr_atm>(1));
Rakesh Iyer4abe76e2015-08-26 11:43:53 -0700141 sender.join();
142 return Status::OK;
143 }
144};
145
146class End2endTest : public ::testing::Test {
147 protected:
148 void SetUp() GRPC_OVERRIDE {
149 int port = grpc_pick_unused_port_or_die();
150 server_address_ << "localhost:" << port;
151 // Setup server
152 ServerBuilder builder;
153 builder.AddListeningPort(server_address_.str(),
154 InsecureServerCredentials());
155 builder.RegisterService(&service_);
156 server_ = builder.BuildAndStart();
157 }
158
159 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
160
161 void ResetStub() {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700162 std::shared_ptr<Channel> channel =
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700163 CreateChannel(server_address_.str(), InsecureChannelCredentials());
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800164 stub_ = grpc::testing::EchoTestService::NewStub(channel);
Rakesh Iyer4abe76e2015-08-26 11:43:53 -0700165 }
166
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800167 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Rakesh Iyer4abe76e2015-08-26 11:43:53 -0700168 std::unique_ptr<Server> server_;
169 std::ostringstream server_address_;
170 TestServiceImpl service_;
171};
172
Craig Tiller93ddc612015-09-02 07:57:48 -0700173static void Drainer(ClientReaderWriter<EchoRequest, EchoResponse>* reader) {
Craig Tillera4517a22015-08-31 07:29:12 -0700174 EchoResponse response;
175 while (reader->Read(&response)) {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700176 // Just drain out the responses as fast as possible.
Craig Tillera4517a22015-08-31 07:29:12 -0700177 }
178}
179
Rakesh Iyer4abe76e2015-08-26 11:43:53 -0700180TEST_F(End2endTest, StreamingThroughput) {
181 ResetStub();
182 grpc::ClientContext context;
183 auto stream = stub_->BidiStream(&context);
184
185 auto reader = stream.get();
Craig Tillera4517a22015-08-31 07:29:12 -0700186 std::thread receiver(std::bind(Drainer, reader));
Rakesh Iyer4abe76e2015-08-26 11:43:53 -0700187
188 for (int i = 0; i < 10000; i++) {
189 EchoRequest request;
190 request.set_message(kLargeString);
191 ASSERT_TRUE(stream->Write(request));
192 if (i % 1000 == 0) {
193 gpr_log(GPR_INFO, "Send count = %d", i);
194 }
195 }
196 stream->WritesDone();
197 receiver.join();
198}
199
200} // namespace testing
201} // namespace grpc
202
203int main(int argc, char** argv) {
204 grpc_test_init(argc, argv);
205 ::testing::InitGoogleTest(&argc, argv);
206 return RUN_ALL_TESTS();
murgatroid99ace28d32016-01-14 10:12:10 -0800207}