Class: NamedActions

Inherits:
Object
  • Object
show all
Includes:
Tech::Pubsub
Defined in:
src/ruby/bin/apis/pubsub_demo.rb

Overview

defines methods corresponding to each interop test case.

Constant Summary

Constants included from Tech::Pubsub

Tech::Pubsub::AcknowledgeRequest, Tech::Pubsub::DeleteSubscriptionRequest, Tech::Pubsub::DeleteTopicRequest, Tech::Pubsub::GetSubscriptionRequest, Tech::Pubsub::GetTopicRequest, Tech::Pubsub::ListSubscriptionsRequest, Tech::Pubsub::ListSubscriptionsResponse, Tech::Pubsub::ListTopicsRequest, Tech::Pubsub::ListTopicsResponse, Tech::Pubsub::ModifyAckDeadlineRequest, Tech::Pubsub::ModifyPushConfigRequest, Tech::Pubsub::NackRequest, Tech::Pubsub::PublishBatchRequest, Tech::Pubsub::PublishBatchResponse, Tech::Pubsub::PublishRequest, Tech::Pubsub::PubsubEvent, Tech::Pubsub::PubsubMessage, Tech::Pubsub::PullBatchRequest, Tech::Pubsub::PullBatchResponse, Tech::Pubsub::PullRequest, Tech::Pubsub::PullResponse, Tech::Pubsub::PushConfig, Tech::Pubsub::Subscription, Tech::Pubsub::Topic, Tech::Pubsub::TruncateSubscriptionRequest

Instance Method Summary collapse

Constructor Details

#initialize(pub, sub, args) ⇒ NamedActions

Initializes NamedActions

Parameters:

  • pub (Stub)

    a stub for accessing the publisher service

  • sub (Stub)

    a stub for accessing the publisher service

  • args (Args)

    provides access to the command line



99
100
101
102
103
# File 'src/ruby/bin/apis/pubsub_demo.rb', line 99

def initialize(pub, sub, args)
  @pub = pub
  @sub = sub
  @args = args
end

Instance Method Details

#check_existsObject

Checks if a topics exists in a project



135
136
137
138
139
140
141
142
143
# File 'src/ruby/bin/apis/pubsub_demo.rb', line 135

def check_exists
  name = test_topic_name
  p "... checking for topic #{name}"
  exists = topic_exists?(name)
  p "#{name} is a topic" if exists
  p "#{name} is not a topic" unless exists
rescue GRPC::BadStatus => e
  p "Could not check for a topics: rpc failed with '#{e}'"
end

#create_topicObject

Creates a test topic



116
117
118
119
120
121
122
123
# File 'src/ruby/bin/apis/pubsub_demo.rb', line 116

def create_topic
  name = test_topic_name
  p "... creating Topic #{name}"
  resp = @pub.create_topic(Topic.new(name: name))
  p "created Topic: #{resp.name} OK"
rescue GRPC::BadStatus => e
  p "Could not create a topics: rpc failed with '#{e}'"
end

#list_some_topicsObject

Lists topics in the project



126
127
128
129
130
131
132
# File 'src/ruby/bin/apis/pubsub_demo.rb', line 126

def list_some_topics
  p 'Listing topics'
  p '-------------_'
  list_project_topics.topic.each { |t| p t.name }
rescue GRPC::BadStatus => e
  p "Could not list topics: rpc failed with '#{e}'"
end

#random_pub_subObject

Publishes some messages



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'src/ruby/bin/apis/pubsub_demo.rb', line 146

def random_pub_sub
  topic_name, sub_name = test_topic_name, test_sub_name
  create_topic_if_needed(topic_name)
  @sub.create_subscription(Subscription.new(name: sub_name,
                                            topic: topic_name))
  msg_count = rand(10..30)
  msg_count.times do |x|
    msg = PubsubMessage.new(data: "message #{x}")
    @pub.publish(PublishRequest.new(topic: topic_name, message: msg))
  end
  p "Sent #{msg_count} messages to #{topic_name}, checking for them now."
  batch = @sub.pull_batch(PullBatchRequest.new(subscription: sub_name,
                                               max_events: msg_count))
  ack_ids = batch.pull_responses.map { |x| x.ack_id }
  p "Got #{ack_ids.size} messages; acknowledging them.."
  @sub.acknowledge(AcknowledgeRequest.new(subscription: sub_name,
                                          ack_id: ack_ids))
  p "Test messages were acknowledged OK, deleting the subscription"
  del_req = DeleteSubscriptionRequest.new(subscription: sub_name)
  @sub.delete_subscription(del_req)
rescue GRPC::BadStatus => e
  p "Could not do random pub sub: rpc failed with '#{e}'"
end

#remove_topicObject

Removes the test topic if it exists



106
107
108
109
110
111
112
113
# File 'src/ruby/bin/apis/pubsub_demo.rb', line 106

def remove_topic
  name = test_topic_name
  p "... removing Topic #{name}"
  @pub.delete_topic(DeleteTopicRequest.new(topic: name))
  p "removed Topic: #{name} OK"
rescue GRPC::BadStatus => e
  p "Could not delete a topics: rpc failed with '#{e}'"
end