Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 1 | /* |
| 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 <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> |
| 41 | #include <grpc++/status.h> |
| 42 | #include <gtest/gtest.h> |
| 43 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 44 | #include "examples/tips/subscriber.h" |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 45 | #include "test/core/util/port.h" |
| 46 | #include "test/core/util/test_config.h" |
| 47 | |
| 48 | using grpc::ChannelInterface; |
| 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 | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 56 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 57 | class SubscriberServiceImpl : public tech::pubsub::SubscriberService::Service { |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 58 | public: |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 59 | Status CreateSubscription(ServerContext* context, |
| 60 | const tech::pubsub::Subscription* request, |
| 61 | tech::pubsub::Subscription* response) override { |
| 62 | EXPECT_EQ(request->topic(), kTopic); |
| 63 | EXPECT_EQ(request->name(), kSubscriptionName); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 64 | return Status::OK; |
| 65 | } |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 66 | |
| 67 | Status GetSubscription(ServerContext* context, |
| 68 | const tech::pubsub::GetSubscriptionRequest* request, |
| 69 | tech::pubsub::Subscription* response) override { |
| 70 | EXPECT_EQ(request->subscription(), kSubscriptionName); |
| 71 | response->set_topic(kTopic); |
| 72 | return Status::OK; |
| 73 | } |
| 74 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 77 | class SubscriberTest : public ::testing::Test { |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 78 | protected: |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 79 | // Setup a server and a client for SubscriberService. |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 80 | void SetUp() override { |
| 81 | int port = grpc_pick_unused_port_or_die(); |
| 82 | server_address_ << "localhost:" << port; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 83 | ServerBuilder builder; |
| 84 | builder.AddPort(server_address_.str()); |
| 85 | builder.RegisterService(service_.service()); |
| 86 | server_ = builder.BuildAndStart(); |
| 87 | |
| 88 | channel_ = CreateChannel(server_address_.str(), ChannelArguments()); |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 89 | |
| 90 | subscriber_.reset(new grpc::examples::tips::Subscriber(channel_)); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 93 | void TearDown() override { |
| 94 | server_->Shutdown(); |
| 95 | subscriber_->Shutdown(); |
| 96 | } |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 97 | |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 98 | std::ostringstream server_address_; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 99 | std::unique_ptr<Server> server_; |
| 100 | SubscriberServiceImpl service_; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 101 | |
| 102 | std::shared_ptr<ChannelInterface> channel_; |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 103 | |
| 104 | std::unique_ptr<grpc::examples::tips::Subscriber> subscriber_; |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 105 | }; |
| 106 | |
Chen Wang | 04f1aa8 | 2015-01-30 18:26:16 -0800 | [diff] [blame^] | 107 | TEST_F(SubscriberTest, TestSubscriber) { |
| 108 | EXPECT_TRUE(subscriber_->CreateSubscription(kTopic, |
| 109 | kSubscriptionName).IsOk()); |
| 110 | |
| 111 | grpc::string topic; |
| 112 | EXPECT_TRUE(subscriber_->GetSubscription(kSubscriptionName, |
| 113 | &topic).IsOk()); |
| 114 | EXPECT_EQ(topic, kTopic); |
Chen Wang | 86af8cf | 2015-01-21 18:05:40 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | } // namespace |
| 118 | } // namespace testing |
| 119 | } // namespace grpc |
| 120 | |
| 121 | int main(int argc, char** argv) { |
| 122 | grpc_test_init(argc, argv); |
| 123 | grpc_init(); |
| 124 | ::testing::InitGoogleTest(&argc, argv); |
| 125 | gpr_log(GPR_INFO, "Start test ..."); |
| 126 | int result = RUN_ALL_TESTS(); |
| 127 | grpc_shutdown(); |
| 128 | return result; |
| 129 | } |