blob: 9ab60ed6a760eabf34c946da8cafb74f623b0f71 [file] [log] [blame]
Chen Wang86af8cf2015-01-21 18:05:40 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Chen Wang86af8cf2015-01-21 18:05:40 -08004 * 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 Tiller42bc87c2015-02-23 08:50:19 -080041#include <grpc++/server_credentials.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080042#include <grpc++/status.h>
43#include <gtest/gtest.h>
44
Chen wang84232512015-02-12 17:29:18 -080045#include "examples/pubsub/subscriber.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080046#include "test/core/util/port.h"
47#include "test/core/util/test_config.h"
48
Chen Wang86af8cf2015-01-21 18:05:40 -080049namespace grpc {
50namespace testing {
51namespace {
52
53const char kTopic[] = "test topic";
Chen Wang04f1aa82015-01-30 18:26:16 -080054const char kSubscriptionName[] = "subscription name";
Chen Wang0010cda2015-02-01 20:44:33 -080055const char kData[] = "Message data";
Chen Wang86af8cf2015-01-21 18:05:40 -080056
Chen Wang04f1aa82015-01-30 18:26:16 -080057class SubscriberServiceImpl : public tech::pubsub::SubscriberService::Service {
Chen Wang86af8cf2015-01-21 18:05:40 -080058 public:
Craig Tillercf133f42015-02-26 14:05:56 -080059 Status CreateSubscription(
60 ServerContext* context, const tech::pubsub::Subscription* request,
61 tech::pubsub::Subscription* response) GRPC_OVERRIDE {
Chen Wang04f1aa82015-01-30 18:26:16 -080062 EXPECT_EQ(request->topic(), kTopic);
63 EXPECT_EQ(request->name(), kSubscriptionName);
Chen Wang86af8cf2015-01-21 18:05:40 -080064 return Status::OK;
65 }
Chen Wang04f1aa82015-01-30 18:26:16 -080066
67 Status GetSubscription(ServerContext* context,
68 const tech::pubsub::GetSubscriptionRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -080069 tech::pubsub::Subscription* response) GRPC_OVERRIDE {
Chen Wang04f1aa82015-01-30 18:26:16 -080070 EXPECT_EQ(request->subscription(), kSubscriptionName);
71 response->set_topic(kTopic);
72 return Status::OK;
73 }
74
Chen Wangb532ef82015-02-02 10:45:17 -080075 Status DeleteSubscription(
76 ServerContext* context,
77 const tech::pubsub::DeleteSubscriptionRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -080078 proto2::Empty* response) GRPC_OVERRIDE {
Chen Wangb532ef82015-02-02 10:45:17 -080079 EXPECT_EQ(request->subscription(), kSubscriptionName);
80 return Status::OK;
81 }
82
Craig Tillercf133f42015-02-26 14:05:56 -080083 Status Pull(ServerContext* context, const tech::pubsub::PullRequest* request,
84 tech::pubsub::PullResponse* response) GRPC_OVERRIDE {
Chen Wang0010cda2015-02-01 20:44:33 -080085 EXPECT_EQ(request->subscription(), kSubscriptionName);
86 response->set_ack_id("1");
87 response->mutable_pubsub_event()->mutable_message()->set_data(kData);
88 return Status::OK;
89 }
90
91 Status Acknowledge(ServerContext* context,
92 const tech::pubsub::AcknowledgeRequest* request,
Craig Tillercf133f42015-02-26 14:05:56 -080093 proto2::Empty* response) GRPC_OVERRIDE {
Chen Wang0010cda2015-02-01 20:44:33 -080094 return Status::OK;
95 }
Chen Wang86af8cf2015-01-21 18:05:40 -080096};
97
Chen Wang04f1aa82015-01-30 18:26:16 -080098class SubscriberTest : public ::testing::Test {
Chen Wang86af8cf2015-01-21 18:05:40 -080099 protected:
Chen Wang04f1aa82015-01-30 18:26:16 -0800100 // Setup a server and a client for SubscriberService.
Craig Tillercf133f42015-02-26 14:05:56 -0800101 void SetUp() GRPC_OVERRIDE {
Chen Wang86af8cf2015-01-21 18:05:40 -0800102 int port = grpc_pick_unused_port_or_die();
103 server_address_ << "localhost:" << port;
Chen Wang86af8cf2015-01-21 18:05:40 -0800104 ServerBuilder builder;
Yang Gao884ed082015-03-25 10:45:23 -0700105 builder.AddListeningPort(server_address_.str(),
106 grpc::InsecureServerCredentials());
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800107 builder.RegisterService(&service_);
Chen Wang86af8cf2015-01-21 18:05:40 -0800108 server_ = builder.BuildAndStart();
109
Yang Gao884ed082015-03-25 10:45:23 -0700110 channel_ = CreateChannel(server_address_.str(), grpc::InsecureCredentials(),
111 ChannelArguments());
Chen Wang04f1aa82015-01-30 18:26:16 -0800112
Chen wang84232512015-02-12 17:29:18 -0800113 subscriber_.reset(new grpc::examples::pubsub::Subscriber(channel_));
Chen Wang86af8cf2015-01-21 18:05:40 -0800114 }
115
Craig Tillercf133f42015-02-26 14:05:56 -0800116 void TearDown() GRPC_OVERRIDE {
Chen Wang04f1aa82015-01-30 18:26:16 -0800117 server_->Shutdown();
118 subscriber_->Shutdown();
119 }
Chen Wang86af8cf2015-01-21 18:05:40 -0800120
Chen Wang86af8cf2015-01-21 18:05:40 -0800121 std::ostringstream server_address_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800122 std::unique_ptr<Server> server_;
123 SubscriberServiceImpl service_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800124
125 std::shared_ptr<ChannelInterface> channel_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800126
Chen wang84232512015-02-12 17:29:18 -0800127 std::unique_ptr<grpc::examples::pubsub::Subscriber> subscriber_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800128};
129
Chen Wang04f1aa82015-01-30 18:26:16 -0800130TEST_F(SubscriberTest, TestSubscriber) {
Yang Gao884ed082015-03-25 10:45:23 -0700131 EXPECT_TRUE(
132 subscriber_->CreateSubscription(kTopic, kSubscriptionName).IsOk());
Chen Wang04f1aa82015-01-30 18:26:16 -0800133
134 grpc::string topic;
Yang Gao884ed082015-03-25 10:45:23 -0700135 EXPECT_TRUE(subscriber_->GetSubscription(kSubscriptionName, &topic).IsOk());
Chen Wang04f1aa82015-01-30 18:26:16 -0800136 EXPECT_EQ(topic, kTopic);
Chen Wang0010cda2015-02-01 20:44:33 -0800137
138 grpc::string data;
Yang Gao884ed082015-03-25 10:45:23 -0700139 EXPECT_TRUE(subscriber_->Pull(kSubscriptionName, &data).IsOk());
Chen Wangb532ef82015-02-02 10:45:17 -0800140
141 EXPECT_TRUE(subscriber_->DeleteSubscription(kSubscriptionName).IsOk());
Chen Wang86af8cf2015-01-21 18:05:40 -0800142}
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 gpr_log(GPR_INFO, "Start test ...");
153 int result = RUN_ALL_TESTS();
154 grpc_shutdown();
155 return result;
Craig Tiller190d3602015-02-18 09:23:38 -0800156}