blob: 52fb80e8db32ea4ea65b4caaded0f1ff726c8440 [file] [log] [blame]
Craig Tiller0220cf12015-02-12 17:39:26 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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
34#include <chrono>
35#include <thread>
36
37#include "test/core/util/test_config.h"
38#include "test/cpp/util/echo_duplicate.pb.h"
39#include "test/cpp/util/echo.pb.h"
40#include "src/cpp/util/time.h"
41#include <grpc++/channel_arguments.h>
42#include <grpc++/channel_interface.h>
43#include <grpc++/client_context.h>
44#include <grpc++/create_channel.h>
45#include <grpc++/credentials.h>
46#include <grpc++/server.h>
47#include <grpc++/server_builder.h>
48#include <grpc++/server_context.h>
49#include <grpc++/status.h>
50#include <grpc++/stream.h>
51#include "test/core/util/port.h"
52#include <gtest/gtest.h>
53
54#include <grpc/grpc.h>
55#include <grpc/support/thd.h>
56#include <grpc/support/time.h>
57
58using grpc::cpp::test::util::EchoRequest;
59using grpc::cpp::test::util::EchoResponse;
60using std::chrono::system_clock;
61
62namespace grpc {
63namespace testing {
64
65namespace {
66
67class End2endTest : public ::testing::Test {
68 protected:
69 End2endTest() : service_(&cq_) {}
70
71 void SetUp() override {
72 int port = grpc_pick_unused_port_or_die();
73 server_address_ << "localhost:" << port;
74 // Setup server
75 ServerBuilder builder;
76 builder.AddPort(server_address_.str());
77 builder.RegisterAsyncService(&service_);
78 server_ = builder.BuildAndStart();
79 }
80
81 void TearDown() override { server_->Shutdown(); }
82
83 void ResetStub() {
84 std::shared_ptr<ChannelInterface> channel =
85 CreateChannel(server_address_.str(), ChannelArguments());
86 stub_.reset(grpc::cpp::test::util::TestService::NewStub(channel));
87 }
88
89 CompletionQueue cq_;
90 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
91 std::unique_ptr<Server> server_;
92 grpc::cpp::test::util::TestService::AsyncService service_;
93 std::ostringstream server_address_;
94};
95
96void* tag(int i) {
97 return (void*)(gpr_intptr)i;
98}
99
100TEST_F(End2endTest, SimpleRpc) {
101 ResetStub();
102
103 EchoRequest send_request;
104 EchoRequest recv_request;
105 EchoResponse send_response;
106 EchoResponse recv_response;
107 Status recv_status;
108 ClientContext cli_ctx;
109 ServerContext srv_ctx;
110 grpc::ServerAsyncResponseWriter<EchoResponse> response_writer(&srv_ctx);
111
112 send_request.set_message("Hello");
113 stub_->Echo(&cli_ctx, send_request, &recv_response, &recv_status, &cq_, tag(1));
114
115 service_.RequestEcho(&srv_ctx, &recv_request, &response_writer, &cq_, tag(2));
116
117 void *got_tag;
118 bool ok;
119 EXPECT_TRUE(cq_.Next(&got_tag, &ok));
120 EXPECT_TRUE(ok);
121 EXPECT_EQ(got_tag, tag(2));
122 EXPECT_EQ(recv_request.message(), "Hello");
123
124 send_response.set_message(recv_request.message());
125 response_writer.Finish(send_response, Status::OK, tag(3));
126
127 EXPECT_TRUE(cq_.Next(&got_tag, &ok));
128 EXPECT_TRUE(ok);
129 if (got_tag == tag(3)) {
130 EXPECT_TRUE(cq_.Next(&got_tag, &ok));
131 EXPECT_TRUE(ok);
132 EXPECT_EQ(got_tag, tag(1));
133 } else {
134 EXPECT_EQ(got_tag, tag(1));
135 EXPECT_TRUE(cq_.Next(&got_tag, &ok));
136 EXPECT_TRUE(ok);
137 EXPECT_EQ(got_tag, tag(3));
138 }
139
140 EXPECT_EQ(recv_response.message(), "Hello");
141 EXPECT_TRUE(recv_status.IsOk());
142}
143
144} // namespace
145} // namespace testing
146} // namespace grpc
147
148int main(int argc, char** argv) {
149 grpc_test_init(argc, argv);
150 grpc_init();
151 ::testing::InitGoogleTest(&argc, argv);
152 int result = RUN_ALL_TESTS();
153 grpc_shutdown();
154 google::protobuf::ShutdownProtobufLibrary();
155 return result;
156}