blob: 49738fcda624ab895b133a08dc867c0f28858e19 [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
Nicolas "Pixel" Noble3f36f5e2015-02-06 20:45:54 +010034#include <google/protobuf/stubs/common.h>
35
Chen Wang86af8cf2015-01-21 18:05:40 -080036#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>
Craig Tiller42bc87c2015-02-23 08:50:19 -080043#include <grpc++/server_credentials.h>
Chen Wang86af8cf2015-01-21 18:05:40 -080044#include <grpc++/status.h>
45#include <gtest/gtest.h>
46
Chen wang84232512015-02-12 17:29:18 -080047#include "examples/pubsub/subscriber.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080048#include "test/core/util/port.h"
49#include "test/core/util/test_config.h"
50
Chen Wang86af8cf2015-01-21 18:05:40 -080051namespace grpc {
52namespace testing {
53namespace {
54
55const char kTopic[] = "test topic";
Chen Wang04f1aa82015-01-30 18:26:16 -080056const char kSubscriptionName[] = "subscription name";
Chen Wang0010cda2015-02-01 20:44:33 -080057const char kData[] = "Message data";
Chen Wang86af8cf2015-01-21 18:05:40 -080058
Chen Wang04f1aa82015-01-30 18:26:16 -080059class SubscriberServiceImpl : public tech::pubsub::SubscriberService::Service {
Chen Wang86af8cf2015-01-21 18:05:40 -080060 public:
Chen Wang04f1aa82015-01-30 18:26:16 -080061 Status CreateSubscription(ServerContext* context,
62 const tech::pubsub::Subscription* request,
63 tech::pubsub::Subscription* response) override {
64 EXPECT_EQ(request->topic(), kTopic);
65 EXPECT_EQ(request->name(), kSubscriptionName);
Chen Wang86af8cf2015-01-21 18:05:40 -080066 return Status::OK;
67 }
Chen Wang04f1aa82015-01-30 18:26:16 -080068
69 Status GetSubscription(ServerContext* context,
70 const tech::pubsub::GetSubscriptionRequest* request,
71 tech::pubsub::Subscription* response) override {
72 EXPECT_EQ(request->subscription(), kSubscriptionName);
73 response->set_topic(kTopic);
74 return Status::OK;
75 }
76
Chen Wangb532ef82015-02-02 10:45:17 -080077 Status DeleteSubscription(
78 ServerContext* context,
79 const tech::pubsub::DeleteSubscriptionRequest* request,
80 proto2::Empty* response) override {
81 EXPECT_EQ(request->subscription(), kSubscriptionName);
82 return Status::OK;
83 }
84
Chen Wang0010cda2015-02-01 20:44:33 -080085 Status Pull(ServerContext* context,
86 const tech::pubsub::PullRequest* request,
87 tech::pubsub::PullResponse* response) override {
88 EXPECT_EQ(request->subscription(), kSubscriptionName);
89 response->set_ack_id("1");
90 response->mutable_pubsub_event()->mutable_message()->set_data(kData);
91 return Status::OK;
92 }
93
94 Status Acknowledge(ServerContext* context,
95 const tech::pubsub::AcknowledgeRequest* request,
96 proto2::Empty* response) override {
97 return Status::OK;
98 }
99
Chen Wang86af8cf2015-01-21 18:05:40 -0800100};
101
Chen Wang04f1aa82015-01-30 18:26:16 -0800102class SubscriberTest : public ::testing::Test {
Chen Wang86af8cf2015-01-21 18:05:40 -0800103 protected:
Chen Wang04f1aa82015-01-30 18:26:16 -0800104 // Setup a server and a client for SubscriberService.
Chen Wang86af8cf2015-01-21 18:05:40 -0800105 void SetUp() override {
106 int port = grpc_pick_unused_port_or_die();
107 server_address_ << "localhost:" << port;
Chen Wang86af8cf2015-01-21 18:05:40 -0800108 ServerBuilder builder;
Craig Tiller42bc87c2015-02-23 08:50:19 -0800109 builder.AddPort(server_address_.str(), grpc::InsecureServerCredentials());
Craig Tiller4d0fb5f2015-02-09 16:27:22 -0800110 builder.RegisterService(&service_);
Chen Wang86af8cf2015-01-21 18:05:40 -0800111 server_ = builder.BuildAndStart();
112
Craig Tiller42bc87c2015-02-23 08:50:19 -0800113 channel_ = CreateChannel(server_address_.str(), grpc::InsecureCredentials(), ChannelArguments());
Chen Wang04f1aa82015-01-30 18:26:16 -0800114
Chen wang84232512015-02-12 17:29:18 -0800115 subscriber_.reset(new grpc::examples::pubsub::Subscriber(channel_));
Chen Wang86af8cf2015-01-21 18:05:40 -0800116 }
117
Chen Wang04f1aa82015-01-30 18:26:16 -0800118 void TearDown() override {
119 server_->Shutdown();
120 subscriber_->Shutdown();
121 }
Chen Wang86af8cf2015-01-21 18:05:40 -0800122
Chen Wang86af8cf2015-01-21 18:05:40 -0800123 std::ostringstream server_address_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800124 std::unique_ptr<Server> server_;
125 SubscriberServiceImpl service_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800126
127 std::shared_ptr<ChannelInterface> channel_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800128
Chen wang84232512015-02-12 17:29:18 -0800129 std::unique_ptr<grpc::examples::pubsub::Subscriber> subscriber_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800130};
131
Chen Wang04f1aa82015-01-30 18:26:16 -0800132TEST_F(SubscriberTest, TestSubscriber) {
133 EXPECT_TRUE(subscriber_->CreateSubscription(kTopic,
134 kSubscriptionName).IsOk());
135
136 grpc::string topic;
137 EXPECT_TRUE(subscriber_->GetSubscription(kSubscriptionName,
138 &topic).IsOk());
139 EXPECT_EQ(topic, kTopic);
Chen Wang0010cda2015-02-01 20:44:33 -0800140
141 grpc::string data;
142 EXPECT_TRUE(subscriber_->Pull(kSubscriptionName,
143 &data).IsOk());
Chen Wangb532ef82015-02-02 10:45:17 -0800144
145 EXPECT_TRUE(subscriber_->DeleteSubscription(kSubscriptionName).IsOk());
Chen Wang86af8cf2015-01-21 18:05:40 -0800146}
147
148} // namespace
149} // namespace testing
150} // namespace grpc
151
152int main(int argc, char** argv) {
153 grpc_test_init(argc, argv);
154 grpc_init();
155 ::testing::InitGoogleTest(&argc, argv);
156 gpr_log(GPR_INFO, "Start test ...");
157 int result = RUN_ALL_TESTS();
158 grpc_shutdown();
159 return result;
Craig Tiller190d3602015-02-18 09:23:38 -0800160}