Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 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 <grpc++/channel_arguments.h> |
| 35 | #include <grpc++/channel_interface.h> |
| 36 | #include <grpc++/client_context.h> |
| 37 | #include <grpc++/create_channel.h> |
| 38 | #include <grpc++/server.h> |
| 39 | #include <grpc++/server_builder.h> |
| 40 | #include <grpc++/server_context.h> |
Craig Tiller | 42bc87c | 2015-02-23 08:50:19 -0800 | [diff] [blame] | 41 | #include <grpc++/server_credentials.h> |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 42 | #include <grpc++/status.h> |
| 43 | #include <gtest/gtest.h> |
| 44 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 45 | #include "examples/pubsub/publisher.h" |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 46 | #include "test/core/util/port.h" |
| 47 | #include "test/core/util/test_config.h" |
| 48 | |
| 49 | using grpc::ChannelInterface; |
| 50 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 51 | namespace grpc { |
| 52 | namespace testing { |
| 53 | namespace { |
| 54 | |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 55 | const char kProjectId[] = "project id"; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 56 | const char kTopic[] = "test topic"; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 57 | const char kMessageData[] = "test message data"; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 58 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 59 | class PublisherServiceImpl : public tech::pubsub::PublisherService::Service { |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 60 | public: |
| 61 | Status CreateTopic(::grpc::ServerContext* context, |
| 62 | const ::tech::pubsub::Topic* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 63 | ::tech::pubsub::Topic* response) GRPC_OVERRIDE { |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 64 | EXPECT_EQ(request->name(), kTopic); |
| 65 | return Status::OK; |
| 66 | } |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 67 | |
| 68 | Status Publish(ServerContext* context, |
| 69 | const ::tech::pubsub::PublishRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 70 | ::proto2::Empty* response) GRPC_OVERRIDE { |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 71 | EXPECT_EQ(request->message().data(), kMessageData); |
| 72 | return Status::OK; |
| 73 | } |
| 74 | |
| 75 | Status GetTopic(ServerContext* context, |
| 76 | const ::tech::pubsub::GetTopicRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 77 | ::tech::pubsub::Topic* response) GRPC_OVERRIDE { |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 78 | EXPECT_EQ(request->topic(), kTopic); |
| 79 | return Status::OK; |
| 80 | } |
| 81 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 82 | Status ListTopics( |
| 83 | ServerContext* context, const ::tech::pubsub::ListTopicsRequest* request, |
| 84 | ::tech::pubsub::ListTopicsResponse* response) GRPC_OVERRIDE { |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 85 | std::ostringstream ss; |
| 86 | ss << "cloud.googleapis.com/project in (/projects/" << kProjectId << ")"; |
| 87 | EXPECT_EQ(request->query(), ss.str()); |
| 88 | response->add_topic()->set_name(kTopic); |
| 89 | return Status::OK; |
| 90 | } |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 91 | |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 92 | Status DeleteTopic(ServerContext* context, |
| 93 | const ::tech::pubsub::DeleteTopicRequest* request, |
| 94 | ::proto2::Empty* response) GRPC_OVERRIDE { |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 95 | EXPECT_EQ(request->topic(), kTopic); |
| 96 | return Status::OK; |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 97 | } |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 98 | }; |
| 99 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 100 | class PublisherTest : public ::testing::Test { |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 101 | protected: |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 102 | // Setup a server and a client for PublisherService. |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 103 | void SetUp() GRPC_OVERRIDE { |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 104 | int port = grpc_pick_unused_port_or_die(); |
| 105 | server_address_ << "localhost:" << port; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 106 | ServerBuilder builder; |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 107 | builder.AddListeningPort(server_address_.str(), |
| 108 | grpc::InsecureServerCredentials()); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 109 | builder.RegisterService(&service_); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 110 | server_ = builder.BuildAndStart(); |
| 111 | |
Yang Gao | 884ed08 | 2015-03-25 10:45:23 -0700 | [diff] [blame] | 112 | channel_ = CreateChannel(server_address_.str(), grpc::InsecureCredentials(), |
| 113 | ChannelArguments()); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 114 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 115 | publisher_.reset(new grpc::examples::pubsub::Publisher(channel_)); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 118 | void TearDown() GRPC_OVERRIDE { |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 119 | server_->Shutdown(); |
| 120 | publisher_->Shutdown(); |
| 121 | } |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 122 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 123 | std::ostringstream server_address_; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 124 | std::unique_ptr<Server> server_; |
| 125 | PublisherServiceImpl service_; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 126 | |
| 127 | std::shared_ptr<ChannelInterface> channel_; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 128 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 129 | std::unique_ptr<grpc::examples::pubsub::Publisher> publisher_; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 132 | TEST_F(PublisherTest, TestPublisher) { |
| 133 | EXPECT_TRUE(publisher_->CreateTopic(kTopic).IsOk()); |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 134 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 135 | EXPECT_TRUE(publisher_->Publish(kTopic, kMessageData).IsOk()); |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 136 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 137 | EXPECT_TRUE(publisher_->GetTopic(kTopic).IsOk()); |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 138 | |
| 139 | std::vector<grpc::string> topics; |
| 140 | EXPECT_TRUE(publisher_->ListTopics(kProjectId, &topics).IsOk()); |
vjpai | dbbdda5 | 2015-02-19 17:02:54 -0800 | [diff] [blame] | 141 | EXPECT_EQ(topics.size(), static_cast<size_t>(1)); |
Chen Wang | 405392c | 2015-02-03 10:28:39 -0800 | [diff] [blame] | 142 | EXPECT_EQ(topics[0], kTopic); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace |
| 146 | } // namespace testing |
| 147 | } // namespace grpc |
| 148 | |
| 149 | int main(int argc, char** argv) { |
| 150 | grpc_test_init(argc, argv); |
| 151 | grpc_init(); |
| 152 | ::testing::InitGoogleTest(&argc, argv); |
| 153 | gpr_log(GPR_INFO, "Start test ..."); |
| 154 | int result = RUN_ALL_TESTS(); |
| 155 | grpc_shutdown(); |
| 156 | return result; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 157 | } |