fixed typos and add ListTopics implementation.
diff --git a/examples/tips/main.cc b/examples/tips/main.cc
index edccd69..df9d984 100644
--- a/examples/tips/main.cc
+++ b/examples/tips/main.cc
@@ -69,7 +69,7 @@
} // namespace
grpc::string GetServiceAccountJsonKey() {
- static grpc::string json_key;
+ grpc::string json_key;
if (json_key.empty()) {
std::ifstream json_key_file(FLAGS_service_account_key_file);
std::stringstream key_stream;
@@ -116,7 +116,7 @@
ss << FLAGS_project_id << "/" << kSubscriptionName;
grpc::string subscription_name = ss.str();
- // Clean up test topic and subcription.
+ // Clean up test topic and subcription if they exist before.
grpc::string subscription_topic;
if (subscriber.GetSubscription(
subscription_name, &subscription_topic).IsOk()) {
@@ -134,6 +134,18 @@
s.code(), s.details().c_str());
GPR_ASSERT(s.IsOk());
+ std::vector<grpc::string> topics;
+ s = publisher.ListTopics(FLAGS_project_id, &topics);
+ gpr_log(GPR_INFO, "List topic returns code %d, %s",
+ s.code(), s.details().c_str());
+ bool topic_found = false;
+ for (unsigned int i = 0; i < topics.size(); i++) {
+ if (topics[i] == topic) topic_found = true;
+ gpr_log(GPR_INFO, "topic: %s", topics[i].c_str());
+ }
+ GPR_ASSERT(s.IsOk());
+ GPR_ASSERT(topic_found);
+
s = subscriber.CreateSubscription(topic, subscription_name);
gpr_log(GPR_INFO, "create subscrption returns code %d, %s",
s.code(), s.details().c_str());
diff --git a/examples/tips/publisher.cc b/examples/tips/publisher.cc
index 027c157..085e6c5 100644
--- a/examples/tips/publisher.cc
+++ b/examples/tips/publisher.cc
@@ -31,6 +31,8 @@
*
*/
+#include <sstream>
+
#include <grpc++/client_context.h>
#include "examples/tips/publisher.h"
@@ -56,7 +58,7 @@
stub_.reset();
}
-Status Publisher::CreateTopic(const string& topic) {
+Status Publisher::CreateTopic(const grpc::string& topic) {
Topic request;
Topic response;
request.set_name(topic);
@@ -65,15 +67,28 @@
return stub_->CreateTopic(&context, request, &response);
}
-Status Publisher::ListTopics() {
+Status Publisher::ListTopics(const grpc::string& project_id,
+ std::vector<grpc::string>* topics) {
ListTopicsRequest request;
ListTopicsResponse response;
ClientContext context;
- return stub_->ListTopics(&context, request, &response);
+ std::stringstream ss;
+ ss << "cloud.googleapis.com/project in (/projects/" << project_id << ")";
+ request.set_query(ss.str());
+
+ Status s = stub_->ListTopics(&context, request, &response);
+
+ tech::pubsub::Topic topic;
+ for (int i = 0; i < response.topic_size(); i++) {
+ topic = response.topic(i);
+ topics->push_back(topic.name());
+ }
+
+ return s;
}
-Status Publisher::GetTopic(const string& topic) {
+Status Publisher::GetTopic(const grpc::string& topic) {
GetTopicRequest request;
Topic response;
ClientContext context;
@@ -83,7 +98,7 @@
return stub_->GetTopic(&context, request, &response);
}
-Status Publisher::DeleteTopic(const string& topic) {
+Status Publisher::DeleteTopic(const grpc::string& topic) {
DeleteTopicRequest request;
proto2::Empty response;
ClientContext context;
@@ -93,7 +108,7 @@
return stub_->DeleteTopic(&context, request, &response);
}
-Status Publisher::Publish(const string& topic, const string& data) {
+Status Publisher::Publish(const grpc::string& topic, const grpc::string& data) {
PublishRequest request;
proto2::Empty response;
ClientContext context;
diff --git a/examples/tips/publisher.h b/examples/tips/publisher.h
index b1be4d9..d8d7353 100644
--- a/examples/tips/publisher.h
+++ b/examples/tips/publisher.h
@@ -48,12 +48,13 @@
Publisher(std::shared_ptr<ChannelInterface> channel);
void Shutdown();
- Status CreateTopic(const string& topic);
- Status GetTopic(const string& topic);
- Status DeleteTopic(const string& topic);
- Status ListTopics();
+ Status CreateTopic(const grpc::string& topic);
+ Status GetTopic(const grpc::string& topic);
+ Status DeleteTopic(const grpc::string& topic);
+ Status ListTopics(const grpc::string& project_id,
+ std::vector<grpc::string>* topics);
- Status Publish(const string& topic, const string& data);
+ Status Publish(const grpc::string& topic, const grpc::string& data);
private:
std::unique_ptr<tech::pubsub::PublisherService::Stub> stub_;
diff --git a/examples/tips/publisher_test.cc b/examples/tips/publisher_test.cc
index 7f845fe..cb949e9 100644
--- a/examples/tips/publisher_test.cc
+++ b/examples/tips/publisher_test.cc
@@ -51,6 +51,7 @@
namespace testing {
namespace {
+const char kProjectId[] = "project id";
const char kTopic[] = "test topic";
const char kMessageData[] = "test message data";
@@ -80,7 +81,11 @@
Status ListTopics(ServerContext* context,
const ::tech::pubsub::ListTopicsRequest* request,
::tech::pubsub::ListTopicsResponse* response) override {
- return Status::OK;
+ std::stringstream ss;
+ ss << "cloud.googleapis.com/project in (/projects/" << kProjectId << ")";
+ EXPECT_EQ(request->query(), ss.str());
+ response->add_topic()->set_name(kTopic);
+ return Status::OK;
}
Status DeleteTopic(ServerContext* context,
@@ -124,9 +129,15 @@
TEST_F(PublisherTest, TestPublisher) {
EXPECT_TRUE(publisher_->CreateTopic(kTopic).IsOk());
+
EXPECT_TRUE(publisher_->Publish(kTopic, kMessageData).IsOk());
+
EXPECT_TRUE(publisher_->GetTopic(kTopic).IsOk());
- EXPECT_TRUE(publisher_->ListTopics().IsOk());
+
+ std::vector<grpc::string> topics;
+ EXPECT_TRUE(publisher_->ListTopics(kProjectId, &topics).IsOk());
+ EXPECT_EQ(topics.size(), 1);
+ EXPECT_EQ(topics[0], kTopic);
}
} // namespace
diff --git a/examples/tips/pubsub.proto b/examples/tips/pubsub.proto
index 15ada60..a2dd2f5 100644
--- a/examples/tips/pubsub.proto
+++ b/examples/tips/pubsub.proto
@@ -1,4 +1,4 @@
-// This file will be moved to new location.
+// This file will be moved to a new location.
// Specification of the Pubsub API.
diff --git a/examples/tips/subscriber.cc b/examples/tips/subscriber.cc
index 6dae3ce..c067322 100644
--- a/examples/tips/subscriber.cc
+++ b/examples/tips/subscriber.cc
@@ -56,7 +56,8 @@
stub_.reset();
}
-Status Subscriber::CreateSubscription(const string& topic, const string& name) {
+Status Subscriber::CreateSubscription(const grpc::string& topic,
+ const grpc::string& name) {
tech::pubsub::Subscription request;
tech::pubsub::Subscription response;
ClientContext context;
@@ -67,7 +68,8 @@
return stub_->CreateSubscription(&context, request, &response);
}
-Status Subscriber::GetSubscription(const string& name, string* topic) {
+Status Subscriber::GetSubscription(const grpc::string& name,
+ grpc::string* topic) {
tech::pubsub::GetSubscriptionRequest request;
tech::pubsub::Subscription response;
ClientContext context;
@@ -79,7 +81,7 @@
return s;
}
-Status Subscriber::DeleteSubscription(const string& name) {
+Status Subscriber::DeleteSubscription(const grpc::string& name) {
tech::pubsub::DeleteSubscriptionRequest request;
proto2::Empty response;
ClientContext context;
@@ -89,7 +91,7 @@
return stub_->DeleteSubscription(&context, request, &response);
}
-Status Subscriber::Pull(const string& name, string* data) {
+Status Subscriber::Pull(const grpc::string& name, grpc::string* data) {
tech::pubsub::PullRequest request;
tech::pubsub::PullResponse response;
ClientContext context;
diff --git a/examples/tips/subscriber.h b/examples/tips/subscriber.h
index d991114..ed706ff 100644
--- a/examples/tips/subscriber.h
+++ b/examples/tips/subscriber.h
@@ -48,14 +48,14 @@
Subscriber(std::shared_ptr<ChannelInterface> channel);
void Shutdown();
- Status CreateSubscription(const string& topic,
- const string& name);
+ Status CreateSubscription(const grpc::string& topic,
+ const grpc::string& name);
- Status GetSubscription(const string& name, string* topic);
+ Status GetSubscription(const grpc::string& name, grpc::string* topic);
- Status DeleteSubscription(const string& name);
+ Status DeleteSubscription(const grpc::string& name);
- Status Pull(const string& name, string* data);
+ Status Pull(const grpc::string& name, grpc::string* data);
private:
std::unique_ptr<tech::pubsub::SubscriberService::Stub> stub_;
diff --git a/examples/tips/subscriber_test.cc b/examples/tips/subscriber_test.cc
index 9c46718..595a6a1 100644
--- a/examples/tips/subscriber_test.cc
+++ b/examples/tips/subscriber_test.cc
@@ -45,8 +45,6 @@
#include "test/core/util/port.h"
#include "test/core/util/test_config.h"
-using grpc::ChannelInterface;
-
namespace grpc {
namespace testing {
namespace {
@@ -132,7 +130,6 @@
EXPECT_TRUE(subscriber_->CreateSubscription(kTopic,
kSubscriptionName).IsOk());
-
grpc::string topic;
EXPECT_TRUE(subscriber_->GetSubscription(kSubscriptionName,
&topic).IsOk());