2015-06-24 20:48:14 +00:00
|
|
|
require 'spec_helper'
|
2015-12-18 22:37:21 +00:00
|
|
|
require 'google/apis/pubsub_v1'
|
2015-06-24 20:48:14 +00:00
|
|
|
require 'googleauth'
|
|
|
|
|
2015-12-18 22:37:21 +00:00
|
|
|
Pubsub = Google::Apis::PubsubV1
|
2015-06-24 20:48:14 +00:00
|
|
|
|
2015-12-18 22:37:21 +00:00
|
|
|
RSpec.describe Google::Apis::PubsubV1, :if => run_integration_tests? do
|
2015-06-24 20:48:14 +00:00
|
|
|
|
|
|
|
before(:context) do
|
|
|
|
WebMock.allow_net_connect!
|
|
|
|
project = ENV['GOOGLE_PROJECT_ID']
|
|
|
|
@topic_name = "projects/#{project}/topics/test"
|
|
|
|
@subscription_name = "projects/#{project}/subscriptions/test"
|
|
|
|
|
|
|
|
@pubsub = Pubsub::PubsubService.new
|
|
|
|
@pubsub.authorization = Google::Auth.get_application_default([Pubsub::AUTH_PUBSUB])
|
|
|
|
@pubsub.create_topic(@topic_name)
|
|
|
|
@pubsub.create_subscription(@subscription_name, Pubsub::Subscription.new(topic: @topic_name))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should publish & receive messages' do
|
|
|
|
# Publish messages
|
|
|
|
request = Pubsub::PublishRequest.new(messages: [])
|
|
|
|
request.messages << Pubsub::Message.new(attributes: { "language" => "en" }, data: 'Hello')
|
|
|
|
request.messages << Pubsub::Message.new(attributes: { "language" => "en" }, data: 'World')
|
2015-12-15 01:42:53 +00:00
|
|
|
@pubsub.publish_topic(@topic_name, request)
|
2015-06-24 20:48:14 +00:00
|
|
|
|
|
|
|
# Pull messages
|
2015-12-15 01:42:53 +00:00
|
|
|
response = @pubsub.pull_subscription(@subscription_name, Pubsub::PullRequest.new(max_messages: 5))
|
2015-06-24 20:48:14 +00:00
|
|
|
response.received_messages.each do |received_message|
|
|
|
|
data = received_message.message.data
|
|
|
|
puts "Received #{data}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Acknowledge receipt
|
|
|
|
ack_ids = response.received_messages.map{ |msg| msg.ack_id }
|
2015-12-15 01:42:53 +00:00
|
|
|
@pubsub.acknowledge_subscription(@subscription_name, Pubsub::AcknowledgeRequest.new(ack_ids: ack_ids))
|
2015-06-24 20:48:14 +00:00
|
|
|
|
|
|
|
expect(response.received_messages.length).to eq 2
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:context) do
|
|
|
|
@pubsub.delete_subscription(@subscription_name)
|
|
|
|
@pubsub.delete_topic(@topic_name)
|
|
|
|
WebMock.disable_net_connect!
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|