From 7f68344dc3147d4e21a09069f5142cacc42f041e Mon Sep 17 00:00:00 2001 From: Eduardo Hattori Date: Fri, 15 Apr 2022 14:14:41 -0300 Subject: [PATCH] feat: add test to poller success --- lib/pipefy_message/providers/aws_broker.rb | 1 + spec/providers/aws_broker_spec.rb | 45 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/lib/pipefy_message/providers/aws_broker.rb b/lib/pipefy_message/providers/aws_broker.rb index 07bdc4e..6f17cab 100644 --- a/lib/pipefy_message/providers/aws_broker.rb +++ b/lib/pipefy_message/providers/aws_broker.rb @@ -6,6 +6,7 @@ module Providers class AwsBroker < Broker def initialize(queue_name, opts={}) @config = build_options(opts) + # require 'pry'; binding.pry Aws.config.update(@config) @sqs = Aws::SQS::Client.new diff --git a/spec/providers/aws_broker_spec.rb b/spec/providers/aws_broker_spec.rb index 68ca585..7667c0a 100644 --- a/spec/providers/aws_broker_spec.rb +++ b/spec/providers/aws_broker_spec.rb @@ -7,6 +7,36 @@ before do stub_const("ENV", ENV.to_hash.merge("AWS_CLI_STUB_RESPONSE" => "true")) end + + describe "#poller" do + mocked_poller = nil + + before do + mocked_message = { message_id: "44c44782-fee1-6784-d614-43b73c0bda8d", + receipt_handle: "2312dasdas1231221312321adsads", + body: "{\"Message\": {\"foo\": \"bar\"}}" } + mocked_poller = Aws::SQS::QueuePoller.new("http://localhost:4566/000000000000/my_queue", + { skip_delete: true }) + mocked_poller.before_request { |stats| throw :stop_polling if stats.received_message_count > 0 } + + mocked_element = Aws::SQS::Types::Message.new(mocked_message) + mocked_list = Aws::Xml::DefaultList.new + mocked_list.append(mocked_element) + mocked_poller.client.stub_responses(:receive_message, messages: mocked_list) + end + it "should consume message" do + worker = PipefyMessage::Providers::AwsBroker.new("my_queue") + worker.instance_variable_set(:@poller, mocked_poller) + + result = nil + expected_result = { "Message" => { "foo" => "bar" } } + worker.poller do |message| + result = message + end + expect(result).to eq expected_result + end + end + describe "should raise Errors" do it "QueueNonExistError" do allow_any_instance_of(Aws::SQS::Client) @@ -24,6 +54,21 @@ end.to raise_error(PipefyMessage::Providers::Errors::ResourceError, /The specified queue my_queue does not exist for this wsdl version/) end + it "NetworkingError" do + allow_any_instance_of(Aws::SQS::Client) + .to receive(:get_queue_url) + .with({ queue_name: "my_queue" }) + .and_raise( + Seahorse::Client::NetworkingError.new( + Errno::ECONNREFUSED.new(""), + "Failed to open TCP connection" + ) + ) + + expect do + PipefyMessage::Providers::AwsBroker.new("my_queue") + end.to raise_error(PipefyMessage::Providers::Errors::ResourceError) + end end end end