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 | |
Nicolas "Pixel" Noble | 3f36f5e | 2015-02-06 20:45:54 +0100 | [diff] [blame] | 34 | #include <google/protobuf/stubs/common.h> |
| 35 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 36 | #include <grpc++/channel_arguments.h> |
| 37 | #include <grpc++/channel_interface.h> |
| 38 | #include <grpc++/client_context.h> |
| 39 | #include <grpc++/create_channel.h> |
| 40 | #include <grpc++/server.h> |
| 41 | #include <grpc++/server_builder.h> |
| 42 | #include <grpc++/server_context.h> |
| 43 | #include <grpc++/status.h> |
| 44 | #include <gtest/gtest.h> |
| 45 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 46 | #include "examples/pubsub/subscriber.h" |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 47 | #include "test/core/util/port.h" |
| 48 | #include "test/core/util/test_config.h" |
| 49 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 50 | namespace grpc { |
| 51 | namespace testing { |
| 52 | namespace { |
| 53 | |
| 54 | const char kTopic[] = "test topic"; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 55 | const char kSubscriptionName[] = "subscription name"; |
Chen Wang | 0010cda | 2015-02-01 20:44:33 -0800 | [diff] [blame] | 56 | const char kData[] = "Message data"; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 57 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 58 | class SubscriberServiceImpl : public tech::pubsub::SubscriberService::Service { |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 59 | public: |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame^] | 60 | Status CreateSubscription( |
| 61 | ServerContext* context, const tech::pubsub::Subscription* request, |
| 62 | tech::pubsub::Subscription* response) GRPC_OVERRIDE { |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 63 | EXPECT_EQ(request->topic(), kTopic); |
| 64 | EXPECT_EQ(request->name(), kSubscriptionName); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 65 | return Status::OK; |
| 66 | } |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 67 | |
| 68 | Status GetSubscription(ServerContext* context, |
| 69 | const tech::pubsub::GetSubscriptionRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame^] | 70 | tech::pubsub::Subscription* response) GRPC_OVERRIDE { |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 71 | EXPECT_EQ(request->subscription(), kSubscriptionName); |
| 72 | response->set_topic(kTopic); |
| 73 | return Status::OK; |
| 74 | } |
| 75 | |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 76 | Status DeleteSubscription( |
| 77 | ServerContext* context, |
| 78 | const tech::pubsub::DeleteSubscriptionRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame^] | 79 | proto2::Empty* response) GRPC_OVERRIDE { |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 80 | EXPECT_EQ(request->subscription(), kSubscriptionName); |
| 81 | return Status::OK; |
| 82 | } |
| 83 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame^] | 84 | Status Pull(ServerContext* context, const tech::pubsub::PullRequest* request, |
| 85 | tech::pubsub::PullResponse* response) GRPC_OVERRIDE { |
Chen Wang | 0010cda | 2015-02-01 20:44:33 -0800 | [diff] [blame] | 86 | EXPECT_EQ(request->subscription(), kSubscriptionName); |
| 87 | response->set_ack_id("1"); |
| 88 | response->mutable_pubsub_event()->mutable_message()->set_data(kData); |
| 89 | return Status::OK; |
| 90 | } |
| 91 | |
| 92 | Status Acknowledge(ServerContext* context, |
| 93 | const tech::pubsub::AcknowledgeRequest* request, |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame^] | 94 | proto2::Empty* response) GRPC_OVERRIDE { |
Chen Wang | 0010cda | 2015-02-01 20:44:33 -0800 | [diff] [blame] | 95 | return Status::OK; |
| 96 | } |
| 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 SubscriberTest : 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 SubscriberService. |
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; |
| 107 | builder.AddPort(server_address_.str()); |
Craig Tiller | 4d0fb5f | 2015-02-09 16:27:22 -0800 | [diff] [blame] | 108 | builder.RegisterService(&service_); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 109 | server_ = builder.BuildAndStart(); |
| 110 | |
Craig Tiller | e4fe844 | 2015-02-23 13:55:15 -0800 | [diff] [blame] | 111 | channel_ = |
| 112 | CreateChannelDeprecated(server_address_.str(), ChannelArguments()); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 113 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 114 | subscriber_.reset(new grpc::examples::pubsub::Subscriber(channel_)); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame^] | 117 | void TearDown() GRPC_OVERRIDE { |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 118 | server_->Shutdown(); |
| 119 | subscriber_->Shutdown(); |
| 120 | } |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 121 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 122 | std::ostringstream server_address_; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 123 | std::unique_ptr<Server> server_; |
| 124 | SubscriberServiceImpl service_; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 125 | |
| 126 | std::shared_ptr<ChannelInterface> channel_; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 127 | |
Chen wang | 8423251 | 2015-02-12 17:29:18 -0800 | [diff] [blame] | 128 | std::unique_ptr<grpc::examples::pubsub::Subscriber> subscriber_; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame] | 131 | TEST_F(SubscriberTest, TestSubscriber) { |
| 132 | EXPECT_TRUE(subscriber_->CreateSubscription(kTopic, |
| 133 | kSubscriptionName).IsOk()); |
| 134 | |
| 135 | grpc::string topic; |
| 136 | EXPECT_TRUE(subscriber_->GetSubscription(kSubscriptionName, |
| 137 | &topic).IsOk()); |
| 138 | EXPECT_EQ(topic, kTopic); |
Chen Wang | 0010cda | 2015-02-01 20:44:33 -0800 | [diff] [blame] | 139 | |
| 140 | grpc::string data; |
| 141 | EXPECT_TRUE(subscriber_->Pull(kSubscriptionName, |
| 142 | &data).IsOk()); |
Chen Wang | b532ef8 | 2015-02-02 10:45:17 -0800 | [diff] [blame] | 143 | |
| 144 | EXPECT_TRUE(subscriber_->DeleteSubscription(kSubscriptionName).IsOk()); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace |
| 148 | } // namespace testing |
| 149 | } // namespace grpc |
| 150 | |
| 151 | int main(int argc, char** argv) { |
| 152 | grpc_test_init(argc, argv); |
| 153 | grpc_init(); |
| 154 | ::testing::InitGoogleTest(&argc, argv); |
| 155 | gpr_log(GPR_INFO, "Start test ..."); |
| 156 | int result = RUN_ALL_TESTS(); |
| 157 | grpc_shutdown(); |
| 158 | return result; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 159 | } |