blob: 9c46718308bf8cf66b555de5b5e278e6ddbcba23 [file] [log] [blame]
Chen Wang86af8cf2015-01-21 18:05:40 -08001/*
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 Wang04f1aa82015-01-30 18:26:16 -080044#include "examples/tips/subscriber.h"
Chen Wang86af8cf2015-01-21 18:05:40 -080045#include "test/core/util/port.h"
46#include "test/core/util/test_config.h"
47
48using grpc::ChannelInterface;
49
Chen Wang86af8cf2015-01-21 18:05:40 -080050namespace grpc {
51namespace testing {
52namespace {
53
54const char kTopic[] = "test topic";
Chen Wang04f1aa82015-01-30 18:26:16 -080055const char kSubscriptionName[] = "subscription name";
Chen Wang0010cda2015-02-01 20:44:33 -080056const char kData[] = "Message data";
Chen Wang86af8cf2015-01-21 18:05:40 -080057
Chen Wang04f1aa82015-01-30 18:26:16 -080058class SubscriberServiceImpl : public tech::pubsub::SubscriberService::Service {
Chen Wang86af8cf2015-01-21 18:05:40 -080059 public:
Chen Wang04f1aa82015-01-30 18:26:16 -080060 Status CreateSubscription(ServerContext* context,
61 const tech::pubsub::Subscription* request,
62 tech::pubsub::Subscription* response) override {
63 EXPECT_EQ(request->topic(), kTopic);
64 EXPECT_EQ(request->name(), kSubscriptionName);
Chen Wang86af8cf2015-01-21 18:05:40 -080065 return Status::OK;
66 }
Chen Wang04f1aa82015-01-30 18:26:16 -080067
68 Status GetSubscription(ServerContext* context,
69 const tech::pubsub::GetSubscriptionRequest* request,
70 tech::pubsub::Subscription* response) override {
71 EXPECT_EQ(request->subscription(), kSubscriptionName);
72 response->set_topic(kTopic);
73 return Status::OK;
74 }
75
Chen Wangb532ef82015-02-02 10:45:17 -080076 Status DeleteSubscription(
77 ServerContext* context,
78 const tech::pubsub::DeleteSubscriptionRequest* request,
79 proto2::Empty* response) override {
80 EXPECT_EQ(request->subscription(), kSubscriptionName);
81 return Status::OK;
82 }
83
Chen Wang0010cda2015-02-01 20:44:33 -080084 Status Pull(ServerContext* context,
85 const tech::pubsub::PullRequest* request,
86 tech::pubsub::PullResponse* response) override {
87 EXPECT_EQ(request->subscription(), kSubscriptionName);
88 response->set_ack_id("1");
89 response->mutable_pubsub_event()->mutable_message()->set_data(kData);
90 return Status::OK;
91 }
92
93 Status Acknowledge(ServerContext* context,
94 const tech::pubsub::AcknowledgeRequest* request,
95 proto2::Empty* response) override {
96 return Status::OK;
97 }
98
Chen Wang86af8cf2015-01-21 18:05:40 -080099};
100
Chen Wang04f1aa82015-01-30 18:26:16 -0800101class SubscriberTest : public ::testing::Test {
Chen Wang86af8cf2015-01-21 18:05:40 -0800102 protected:
Chen Wang04f1aa82015-01-30 18:26:16 -0800103 // Setup a server and a client for SubscriberService.
Chen Wang86af8cf2015-01-21 18:05:40 -0800104 void SetUp() override {
105 int port = grpc_pick_unused_port_or_die();
106 server_address_ << "localhost:" << port;
Chen Wang86af8cf2015-01-21 18:05:40 -0800107 ServerBuilder builder;
108 builder.AddPort(server_address_.str());
109 builder.RegisterService(service_.service());
110 server_ = builder.BuildAndStart();
111
112 channel_ = CreateChannel(server_address_.str(), ChannelArguments());
Chen Wang04f1aa82015-01-30 18:26:16 -0800113
114 subscriber_.reset(new grpc::examples::tips::Subscriber(channel_));
Chen Wang86af8cf2015-01-21 18:05:40 -0800115 }
116
Chen Wang04f1aa82015-01-30 18:26:16 -0800117 void TearDown() override {
118 server_->Shutdown();
119 subscriber_->Shutdown();
120 }
Chen Wang86af8cf2015-01-21 18:05:40 -0800121
Chen Wang86af8cf2015-01-21 18:05:40 -0800122 std::ostringstream server_address_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800123 std::unique_ptr<Server> server_;
124 SubscriberServiceImpl service_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800125
126 std::shared_ptr<ChannelInterface> channel_;
Chen Wang04f1aa82015-01-30 18:26:16 -0800127
128 std::unique_ptr<grpc::examples::tips::Subscriber> subscriber_;
Chen Wang86af8cf2015-01-21 18:05:40 -0800129};
130
Chen Wang04f1aa82015-01-30 18:26:16 -0800131TEST_F(SubscriberTest, TestSubscriber) {
132 EXPECT_TRUE(subscriber_->CreateSubscription(kTopic,
133 kSubscriptionName).IsOk());
134
Chen Wang0010cda2015-02-01 20:44:33 -0800135
Chen Wang04f1aa82015-01-30 18:26:16 -0800136 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;
160}