class NamedActions

defines methods corresponding to each interop test case.

Public Class Methods

new(pub, sub, args) click to toggle source

Initializes NamedActions

@param pub [Stub] a stub for accessing the publisher service @param sub [Stub] a stub for accessing the publisher service @param args [Args] provides access to the command line

# File src/ruby/bin/apis/pubsub_demo.rb, line 84
def initialize(pub, sub, args)
  @pub = pub
  @sub = sub
  @args = args
end

Public Instance Methods

check_exists() click to toggle source

Checks if a topics exists in a project

# File src/ruby/bin/apis/pubsub_demo.rb, line 120
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_topic() click to toggle source

Creates a test topic

# File src/ruby/bin/apis/pubsub_demo.rb, line 101
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_topics() click to toggle source

Lists topics in the project

# File src/ruby/bin/apis/pubsub_demo.rb, line 111
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_sub() click to toggle source

Publishes some messages

# File src/ruby/bin/apis/pubsub_demo.rb, line 131
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_topic() click to toggle source

Removes the test topic if it exists

# File src/ruby/bin/apis/pubsub_demo.rb, line 91
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

Private Instance Methods

create_topic_if_needed(name) click to toggle source
# File src/ruby/bin/apis/pubsub_demo.rb, line 181
def create_topic_if_needed(name)
  return if topic_exists?(name)
  @pub.create_topic(Topic.new(name: name))
end
list_project_topics() click to toggle source
# File src/ruby/bin/apis/pubsub_demo.rb, line 186
def list_project_topics
  q = "cloud.googleapis.com/project in (/projects/#{@args.project_id})"
  @pub.list_topics(ListTopicsRequest.new(query: q))
end
test_sub_name() click to toggle source

#test_sub_name is the subscription name to use in this test.

# File src/ruby/bin/apis/pubsub_demo.rb, line 167
def test_sub_name
  unless @args.sub_name.nil?
    return "/subscriptions/#{@args.project_id}/#{@args.sub_name}"
  end
  now_text = Time.now.utc.strftime('%Y%m%d%H%M%S%L')
  "/subscriptions/#{@args.project_id}/#{ENV['USER']}-#{now_text}"
end
test_topic_name() click to toggle source

#test_topic_name is the topic name to use in this test.

# File src/ruby/bin/apis/pubsub_demo.rb, line 158
def test_topic_name
  unless @args.topic_name.nil?
    return "/topics/#{@args.project_id}/#{@args.topic_name}"
  end
  now_text = Time.now.utc.strftime('%Y%m%d%H%M%S%L')
  "/topics/#{@args.project_id}/#{ENV['USER']}-#{now_text}"
end
topic_exists?(name) click to toggle source

determines if the topic name exists

# File src/ruby/bin/apis/pubsub_demo.rb, line 176
def topic_exists?(name)
  topics = list_project_topics.topic.map { |t| t.name }
  topics.include?(name)
end