dispatch-rider 1.9.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +50 -0
  3. data/.github/workflows/rubocop-challenger.yml +26 -0
  4. data/.gitignore +1 -0
  5. data/.rubocop.yml +31 -20
  6. data/.rubocop_todo.yml +904 -0
  7. data/CHANGELOG.md +12 -0
  8. data/README.md +2 -2
  9. data/dispatch-rider.gemspec +16 -10
  10. data/gemfiles/Gemfile-5-2 +8 -0
  11. data/gemfiles/Gemfile-6-0 +8 -0
  12. data/gemfiles/Gemfile-6-1 +8 -0
  13. data/gemfiles/Gemfile-7-0 +8 -0
  14. data/lib/dispatch-rider/callbacks/access.rb +0 -1
  15. data/lib/dispatch-rider/callbacks/storage.rb +0 -2
  16. data/lib/dispatch-rider/callbacks/support.rb +0 -2
  17. data/lib/dispatch-rider/command.rb +0 -2
  18. data/lib/dispatch-rider/demultiplexer.rb +0 -1
  19. data/lib/dispatch-rider/dispatcher.rb +0 -2
  20. data/lib/dispatch-rider/error_handlers.rb +0 -1
  21. data/lib/dispatch-rider/handlers/inheritance_tracking.rb +0 -2
  22. data/lib/dispatch-rider/handlers/named_process.rb +0 -2
  23. data/lib/dispatch-rider/integrations/appsignal.rb +0 -2
  24. data/lib/dispatch-rider/logging/json_formatter.rb +0 -2
  25. data/lib/dispatch-rider/logging/lifecycle_logger.rb +0 -1
  26. data/lib/dispatch-rider/logging/text_formatter.rb +1 -3
  27. data/lib/dispatch-rider/logging/translator/base_translator.rb +0 -2
  28. data/lib/dispatch-rider/logging/translator/complete_translator.rb +0 -2
  29. data/lib/dispatch-rider/logging/translator/error_handler_fail_translator.rb +0 -2
  30. data/lib/dispatch-rider/logging/translator/fail_translator.rb +0 -2
  31. data/lib/dispatch-rider/logging/translator/start_translator.rb +0 -2
  32. data/lib/dispatch-rider/logging/translator/stop_translator.rb +0 -2
  33. data/lib/dispatch-rider/logging/translator/success_translator.rb +0 -2
  34. data/lib/dispatch-rider/logging/translator.rb +0 -2
  35. data/lib/dispatch-rider/logging.rb +0 -1
  36. data/lib/dispatch-rider/notification_services/aws_sns.rb +4 -4
  37. data/lib/dispatch-rider/notification_services/base.rb +1 -1
  38. data/lib/dispatch-rider/notification_services/file_system/channel.rb +1 -2
  39. data/lib/dispatch-rider/notification_services/file_system/notifier.rb +0 -2
  40. data/lib/dispatch-rider/publisher/configuration/destination.rb +0 -2
  41. data/lib/dispatch-rider/publisher/configuration/notification_service.rb +0 -2
  42. data/lib/dispatch-rider/publisher/configuration.rb +0 -2
  43. data/lib/dispatch-rider/publisher/configuration_reader.rb +0 -3
  44. data/lib/dispatch-rider/publisher/configuration_support.rb +0 -2
  45. data/lib/dispatch-rider/queue_services/aws_sqs/sqs_received_message.rb +6 -7
  46. data/lib/dispatch-rider/queue_services/aws_sqs.rb +28 -20
  47. data/lib/dispatch-rider/queue_services/file_system/fs_received_message.rb +0 -1
  48. data/lib/dispatch-rider/queue_services/file_system.rb +2 -2
  49. data/lib/dispatch-rider/queue_services/received_message.rb +0 -2
  50. data/lib/dispatch-rider/registrars/base.rb +2 -2
  51. data/lib/dispatch-rider/registrars/sns_channel.rb +1 -1
  52. data/lib/dispatch-rider/runner.rb +0 -1
  53. data/lib/dispatch-rider/version.rb +1 -1
  54. data/lib/dispatch-rider.rb +2 -0
  55. data/lib/generators/dispatch_rider/install/install_generator.rb +0 -2
  56. data/lib/generators/dispatch_rider/job/dispatch_job_generator.rb +0 -2
  57. data/spec/factories/messages.rb +7 -6
  58. data/spec/lib/dispatch-rider/callbacks/access_spec.rb +2 -2
  59. data/spec/lib/dispatch-rider/callbacks/storage_spec.rb +3 -3
  60. data/spec/lib/dispatch-rider/configuration_spec.rb +1 -1
  61. data/spec/lib/dispatch-rider/default_error_handler_spec.rb +1 -0
  62. data/spec/lib/dispatch-rider/demultiplexer_spec.rb +2 -2
  63. data/spec/lib/dispatch-rider/notification_services/aws_sns_spec.rb +3 -3
  64. data/spec/lib/dispatch-rider/notification_services/base_spec.rb +7 -7
  65. data/spec/lib/dispatch-rider/notification_services/file_system/channel_spec.rb +5 -4
  66. data/spec/lib/dispatch-rider/publisher/base_spec.rb +2 -2
  67. data/spec/lib/dispatch-rider/publisher/configuration/destination_spec.rb +1 -2
  68. data/spec/lib/dispatch-rider/publisher/configuration/notification_service_spec.rb +1 -2
  69. data/spec/lib/dispatch-rider/publisher/configuration_spec.rb +2 -2
  70. data/spec/lib/dispatch-rider/publisher_spec.rb +2 -1
  71. data/spec/lib/dispatch-rider/queue_services/aws_sqs_spec.rb +24 -34
  72. data/spec/lib/dispatch-rider/queue_services/file_system_spec.rb +2 -2
  73. data/spec/lib/dispatch-rider/scheduled_job_spec.rb +12 -12
  74. data/spec/lib/dispatch-rider/subscriber_spec.rb +4 -0
  75. data/spec/spec_helper.rb +32 -4
  76. data/spec/support/integration_support.rb +0 -1
  77. metadata +92 -32
  78. data/.rubocop.hound.yml +0 -261
  79. data/spec/lib/dispatch-rider/queue_services_spec.rb +0 -6
@@ -10,24 +10,25 @@ module DispatchRider
10
10
  class VisibilityTimeoutExceeded < RuntimeError; end
11
11
 
12
12
  def assign_storage(attrs)
13
- begin
14
- sqs = AWS::SQS.new(logger: nil, region: attrs[:region])
15
- if attrs[:name]
16
- sqs.queues.named(attrs[:name])
17
- elsif attrs[:url]
18
- sqs.queues[attrs[:url]]
19
- else
20
- raise RecordInvalid.new(self, ["Either name or url have to be specified"])
21
- end
22
- rescue NameError
23
- raise AdapterNotFoundError.new(self.class.name, 'aws-sdk')
13
+ sqs = Aws::SQS::Client.new(logger: nil)
14
+ if attrs[:name].present?
15
+ url = sqs.list_queues({queue_name_prefix: attrs[:name]}).queue_urls.first
16
+ set_visibility_timeout(sqs,url)
17
+ Aws::SQS::Queue.new(url: url, client: sqs)
18
+ elsif attrs[:url].present?
19
+ set_visibility_timeout(sqs,attrs[:url])
20
+ Aws::SQS::Queue.new(url: attrs[:url], client: sqs)
21
+ else
22
+ raise RecordInvalid.new(self, ["Either name or url have to be specified"])
24
23
  end
24
+ rescue NameError
25
+ raise AdapterNotFoundError.new(self.class.name, 'aws-sdk')
25
26
  end
26
27
 
27
28
  def pop
28
- raw_item = queue.receive_message
29
+ raw_item = queue.receive_messages({max_number_of_messages: 1}).first
29
30
  if raw_item.present?
30
- obj = SqsReceivedMessage.new(construct_message_from(raw_item), raw_item, queue)
31
+ obj = SqsReceivedMessage.new(construct_message_from(raw_item), raw_item, queue, visibility_timeout)
31
32
 
32
33
  visibility_timeout_shield(obj) do
33
34
  raise AbortExecution, "false received from handler" unless yield(obj)
@@ -56,18 +57,25 @@ module DispatchRider
56
57
  queue.approximate_number_of_messages
57
58
  end
58
59
 
60
+ attr_reader :visibility_timeout
61
+
59
62
  private
60
63
 
64
+ def set_visibility_timeout(client,url)
65
+ resp = client.get_queue_attributes(queue_url: url, attribute_names: ["VisibilityTimeout"])
66
+ @visibility_timeout = resp.attributes["VisibilityTimeout"]
67
+ end
68
+
61
69
  def visibility_timeout_shield(message)
62
- begin
63
- yield
64
- ensure
65
- duration = Time.now - message.start_time
66
- timeout = message.total_timeout
67
- raise VisibilityTimeoutExceeded, "message: #{message.subject}, #{message.body.inspect} took #{duration} seconds while the timeout was #{timeout}" if duration > timeout
70
+ yield
71
+ ensure
72
+ duration = Time.now - message.start_time
73
+ timeout = message.total_timeout
74
+ if duration > timeout
75
+ message = "message: #{message.subject}, #{message.body.inspect} took #{duration} seconds while the timeout was #{timeout}"
76
+ raise VisibilityTimeoutExceeded, message
68
77
  end
69
78
  end
70
-
71
79
  end
72
80
  end
73
81
  end
@@ -28,7 +28,6 @@ module DispatchRider
28
28
  def queue_name
29
29
  File.basename(File.dirname(@item.path))
30
30
  end
31
-
32
31
  end
33
32
  end
34
33
  end
@@ -1,6 +1,6 @@
1
1
  # This is a rudementary queue service that uses file system instead of
2
- # AWS::SQS or SimpleQueue. It addresses SimpleQueue's inability to be used
3
- # by only one application instance while avoiding the cost of setting up AWS::SQS.
2
+ # Aws::SQS or SimpleQueue. It addresses SimpleQueue's inability to be used
3
+ # by only one application instance while avoiding the cost of setting up Aws::SQS.
4
4
  # This is ideal to be used in development mode between multiple applications.
5
5
  module DispatchRider
6
6
  module QueueServices
@@ -3,7 +3,6 @@ require 'delegate'
3
3
  module DispatchRider
4
4
  module QueueServices
5
5
  class ReceivedMessage < ::SimpleDelegator
6
-
7
6
  #Item is the raw message item as returned by the queue implementor
8
7
  #it's contents will depend on the queue being used
9
8
  attr_reader :item
@@ -36,7 +35,6 @@ module DispatchRider
36
35
  def queue_name
37
36
  raise NotImplementedError
38
37
  end
39
-
40
38
  end
41
39
  end
42
40
  end
@@ -15,7 +15,7 @@ module DispatchRider
15
15
  store[name.to_sym] = value(name, options)
16
16
  self
17
17
  rescue NameError
18
- raise NotFound.new(name)
18
+ raise NotFound, name
19
19
  end
20
20
 
21
21
  def value(name, options = {})
@@ -31,7 +31,7 @@ module DispatchRider
31
31
  begin
32
32
  store.fetch(name.to_sym)
33
33
  rescue IndexError
34
- raise NotRegistered.new(name)
34
+ raise NotRegistered, name
35
35
  end
36
36
  end
37
37
  end
@@ -1,4 +1,4 @@
1
- # This is the registrar for the AWS SNS channels.
1
+ # This is the registrar for the Aws SNS channels.
2
2
  module DispatchRider
3
3
  module Registrars
4
4
  class SnsChannel < Base
@@ -44,6 +44,5 @@ module DispatchRider
44
44
  @subscriber.register_queue(kind, info)
45
45
  @subscriber.setup_demultiplexer(kind, config.error_handler)
46
46
  end
47
-
48
47
  end
49
48
  end
@@ -1,4 +1,4 @@
1
1
  # This file specifies the current version of the gem.
2
2
  module DispatchRider
3
- VERSION = '1.9.0'
3
+ VERSION = '2.0.0'
4
4
  end
@@ -5,6 +5,8 @@ require "active_support/hash_with_indifferent_access"
5
5
  require "active_support/core_ext/hash/indifferent_access"
6
6
  require "active_support/inflector"
7
7
  require "active_support/json"
8
+ # Local testing fails without this
9
+ #require "active_support/isolated_execution_state"
8
10
  require "active_support/core_ext/array/conversions"
9
11
  require "active_model"
10
12
 
@@ -1,12 +1,10 @@
1
1
  module DispatchRider
2
2
  class InstallGenerator < ::Rails::Generators::Base
3
-
4
3
  source_root File.expand_path("../templates", __FILE__)
5
4
 
6
5
  def create_scripts
7
6
  copy_file "script/dispatch_rider", "script/dispatch_rider"
8
7
  chmod 'script/dispatch_rider', 0755
9
8
  end
10
-
11
9
  end
12
10
  end
@@ -1,5 +1,4 @@
1
1
  class DispatchJob < Rails::Generators::Base
2
-
3
2
  class Publisher < Rails::Generators::Base
4
3
  source_root File.expand_path('../templates/publisher', __FILE__)
5
4
  argument :handler_name, type: :string, required: true
@@ -56,5 +55,4 @@ class DispatchJob < Rails::Generators::Base
56
55
  hook_for :handler do |handler|
57
56
  invoke handler, [handler_name]
58
57
  end
59
-
60
58
  end
@@ -1,10 +1,11 @@
1
- FactoryGirl.define do
1
+ FactoryBot.define do
2
2
  factory :message, class: 'DispatchRider::Message' do
3
- subject 'sample_handler'
4
- body(
5
- 'key' => 'value',
6
- 'guid' => DispatchRider::Debug::PUBLISHER_MESSAGE_GUID,
7
- )
3
+ subject { 'sample_handler' }
4
+ body {
5
+ {
6
+ 'key' => 'value',
7
+ 'guid' => DispatchRider::Debug::PUBLISHER_MESSAGE_GUID,
8
+ }}
8
9
  initialize_with { DispatchRider::Message.new(attributes) }
9
10
  end
10
11
  end
@@ -2,6 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe DispatchRider::Callbacks::Access do
4
4
  describe "#invoke" do
5
+ subject { described_class.new(storage) }
6
+
5
7
  let(:callback_a1) { proc { |x| x.call } }
6
8
  let(:callback_a2) { proc { |x| x.call } }
7
9
  let(:callback_a3) { proc { |x| x.call } }
@@ -13,8 +15,6 @@ describe DispatchRider::Callbacks::Access do
13
15
  let(:storage) { DispatchRider::Callbacks::Storage.new }
14
16
  let(:action) { proc {} }
15
17
 
16
- subject { described_class.new(storage) }
17
-
18
18
  before do
19
19
  callbacks_a.each do |cb|
20
20
  storage.around :event1, cb
@@ -15,7 +15,7 @@ describe DispatchRider::Callbacks::Storage do
15
15
  log << :aafter
16
16
  end
17
17
  subject.for(:initialize).first[actual]
18
- expect(log).to eq [:abefore, :actual, :aafter]
18
+ expect(log).to eq %i[abefore actual aafter]
19
19
  end
20
20
  end
21
21
 
@@ -23,7 +23,7 @@ describe DispatchRider::Callbacks::Storage do
23
23
  example do
24
24
  subject.before(:initialize) { log << :before }
25
25
  subject.for(:initialize).first[actual]
26
- expect(log).to eq [:before, :actual]
26
+ expect(log).to eq %i[before actual]
27
27
  end
28
28
  end
29
29
 
@@ -31,7 +31,7 @@ describe DispatchRider::Callbacks::Storage do
31
31
  example do
32
32
  subject.after(:initialize) { log << :after }
33
33
  subject.for(:initialize).first[actual]
34
- expect(log).to eq [:actual, :after]
34
+ expect(log).to eq %i[actual after]
35
35
  end
36
36
  end
37
37
  end
@@ -54,7 +54,7 @@ describe DispatchRider::Configuration do
54
54
  describe "#logger" do
55
55
 
56
56
  describe "default" do
57
- example { expect(subject.logger).to be_kind_of(Logger) }
57
+ example { expect(subject.logger).to be_a(Logger) }
58
58
  end
59
59
 
60
60
  example { expect(subject).to respond_to(:logger) }
@@ -4,6 +4,7 @@ describe DispatchRider::DefaultErrorHandler do
4
4
 
5
5
  describe ".call" do
6
6
  let(:exception) { Exception.new("Something went terribly wrong") }
7
+
7
8
  example do
8
9
  expect {
9
10
  described_class.call("Error", exception)
@@ -7,6 +7,8 @@ describe DispatchRider::Demultiplexer, nodb: true do
7
7
  end
8
8
  end
9
9
 
10
+ subject(:demultiplexer) { DispatchRider::Demultiplexer.new(queue, dispatcher, error_handler) }
11
+
10
12
  let(:dispatcher) do
11
13
  dispatcher = DispatchRider::Dispatcher.new
12
14
  dispatcher.register(:test_handler)
@@ -30,8 +32,6 @@ describe DispatchRider::Demultiplexer, nodb: true do
30
32
 
31
33
  let(:error_handler) { ->(_message, exception) { raise exception } }
32
34
 
33
- subject(:demultiplexer) { DispatchRider::Demultiplexer.new(queue, dispatcher, error_handler) }
34
-
35
35
  describe "#initialize" do
36
36
  it "should assign the queue" do
37
37
  expect(demultiplexer.queue).to be_empty
@@ -5,7 +5,7 @@ describe DispatchRider::NotificationServices::AwsSns do
5
5
 
6
6
  describe "#notifier_builder" do
7
7
  it "returns the notifier builder" do
8
- expect(subject.notifier_builder).to eq(AWS::SNS)
8
+ expect(subject.notifier_builder).to eq(Aws::SNS::Client)
9
9
  end
10
10
  end
11
11
 
@@ -21,12 +21,12 @@ describe DispatchRider::NotificationServices::AwsSns do
21
21
 
22
22
  # @note: This is tested this way cause you don't really wanna post a message to the actual service.
23
23
  it "publishes the message to the channels" do
24
- expect(channel).to receive(:publish).with(kind_of String) do |serialized_message|
24
+ expect(channel).to receive(:publish).with(kind_of Hash) do |serialized_message|
25
25
  expected = {
26
26
  "subject" => "test_handler",
27
27
  "body" => { "bar" => "baz" }
28
28
  }
29
- expect(JSON.parse(serialized_message)).to eq(expected)
29
+ expect(JSON.parse(serialized_message[:message])).to eq(expected)
30
30
  end
31
31
 
32
32
  subject.publish_to_channel(channel, message: message)
@@ -1,11 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DispatchRider::NotificationServices::Base do
4
+ subject do
5
+ DispatchRider::NotificationServices::Base.new(topics: {})
6
+ end
7
+
4
8
  let :channel do
5
9
  channel = OpenStruct.new
6
10
  class << channel
7
11
  def publish(msg)
8
- throw :published, JSON.parse(msg)["body"]["bar"]
12
+ throw :published, JSON.parse(msg[:message])["body"]["bar"]
9
13
  end
10
14
  end
11
15
  channel
@@ -20,10 +24,6 @@ describe DispatchRider::NotificationServices::Base do
20
24
  end
21
25
  end
22
26
 
23
- subject do
24
- DispatchRider::NotificationServices::Base.new(topics: {})
25
- end
26
-
27
27
  describe "#initialize" do
28
28
  it "assigns the notifier" do
29
29
  expect(subject.notifier).to respond_to(:topics)
@@ -60,8 +60,8 @@ describe DispatchRider::NotificationServices::Base do
60
60
 
61
61
  # @note: This is tested this way cause you don't really wanna post a message to the actual service.
62
62
  it "publishes the message to the channels" do
63
- expect(channel).to receive(:publish).with(kind_of String) { |serialized_message|
64
- parsed_message = JSON.parse(serialized_message)
63
+ expect(channel).to receive(:publish).with(kind_of Hash) { |serialized_message|
64
+ parsed_message = JSON.parse(serialized_message[:message])
65
65
  expect(parsed_message).to eq(expected_message)
66
66
  }
67
67
 
@@ -1,25 +1,26 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DispatchRider::NotificationServices::FileSystem::Channel do
4
+ subject { described_class.new(path) }
5
+
4
6
  let(:path) { File.expand_path("tmp/test/channel") }
5
7
  let(:published_message) { File.new(Dir["#{path}/*.ready"].first).read }
6
8
 
7
9
  before { FileUtils.mkdir_p(path) }
8
- after { FileUtils.rm_rf(path) }
9
10
 
10
- subject { described_class.new(path) }
11
+ after { FileUtils.rm_rf(path) }
11
12
 
12
13
  describe "#publish" do
13
14
  let(:message) { { subject: "foo", body: "bar" }.to_json }
14
15
 
15
16
  it "adds a file to the path folder" do
16
17
  expect {
17
- subject.publish(message)
18
+ subject.publish(message: message)
18
19
  }.to change { Dir["#{path}/*"].length }.by(1)
19
20
  end
20
21
 
21
22
  it "writes the message to the file" do
22
- subject.publish(message)
23
+ subject.publish(message: message)
23
24
 
24
25
  expect(published_message).to eq(message)
25
26
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe DispatchRider::Publisher::Base do
4
4
 
5
5
  class DummyPublisher < DispatchRider::Publisher::Base
6
- destinations [:sns_message_queue, :file_system_queue]
6
+ destinations %i[sns_message_queue file_system_queue]
7
7
  subject "Loud Cheering"
8
8
 
9
9
  def self.publish(cheer)
@@ -38,7 +38,7 @@ describe DispatchRider::Publisher::Base do
38
38
  context "in a derived class with publish implemented" do
39
39
  let(:message) do
40
40
  {
41
- destinations: [:sns_message_queue, :file_system_queue],
41
+ destinations: %i[sns_message_queue file_system_queue],
42
42
  message: {
43
43
  subject: "Loud Cheering",
44
44
  body: {
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DispatchRider::Publisher::Configuration::Destination do
4
+ subject { described_class.new("employee", attributes) }
4
5
  let(:attributes) do
5
6
  {
6
7
  "service" => "aws_sns",
@@ -17,8 +18,6 @@ describe DispatchRider::Publisher::Configuration::Destination do
17
18
  }
18
19
  end
19
20
 
20
- subject { described_class.new("employee", attributes) }
21
-
22
21
  describe "#name" do
23
22
  describe '#name' do
24
23
  subject { super().name }
@@ -2,14 +2,13 @@ require 'spec_helper'
2
2
 
3
3
  describe DispatchRider::Publisher::Configuration::NotificationService do
4
4
 
5
+ subject { described_class.new("file_system", options) }
5
6
  let(:options) do
6
7
  {
7
8
  "default_folder" => "/tmp/dispatch_rider"
8
9
  }
9
10
  end
10
11
 
11
- subject { described_class.new("file_system", options) }
12
-
13
12
  describe "#name" do
14
13
  describe '#name' do
15
14
  subject { super().name }
@@ -2,6 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  describe DispatchRider::Publisher::Configuration do
4
4
 
5
+ subject { described_class.new(configuration_hash) }
6
+
5
7
  let(:configuration_hash) do
6
8
  {
7
9
  "notification_services" => {
@@ -31,8 +33,6 @@ describe DispatchRider::Publisher::Configuration do
31
33
  }
32
34
  end
33
35
 
34
- subject { described_class.new(configuration_hash) }
35
-
36
36
  describe "#notification services" do
37
37
  let(:file_system) do
38
38
  DispatchRider::Publisher::Configuration::NotificationService.new(
@@ -45,7 +45,7 @@ describe DispatchRider::Publisher do
45
45
  it "registers a notification service" do
46
46
  subject.register_notification_service(:aws_sns)
47
47
  result = subject.notification_service_registrar.fetch(:aws_sns)
48
- expect(result.notifier).to respond_to(:topics)
48
+ expect(result.notifier).to respond_to(:list_topics)
49
49
  expect(result.channel_registrar.store).to be_empty
50
50
  end
51
51
 
@@ -146,6 +146,7 @@ describe DispatchRider::Publisher do
146
146
  }
147
147
 
148
148
  before { DispatchRider.config.callbacks.for(:publish) << publish_callback }
149
+
149
150
  after { DispatchRider.config.callbacks.for(:publish).delete publish_callback }
150
151
 
151
152
  example do
@@ -2,25 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  describe DispatchRider::QueueServices::AwsSqs do
4
4
 
5
- let(:visibility_timeout) { 100 }
6
-
7
- let(:fake_response) do
8
- AWS::SQS::Client.new.stub_for(:get_queue_url).tap { |response|
9
- response.data[:queue_url] = "the.queue.url"
10
- response.data[:attributes] = { "VisibilityTimeout" => visibility_timeout }
11
- }
5
+ subject(:aws_sqs_queue) do
6
+ DispatchRider::QueueServices::AwsSqs.new(:name => "normal_priority")
12
7
  end
13
8
 
14
- before do
15
- AWS.config(stub_requests: true)
16
- allow_any_instance_of(AWS::SQS::Client).to receive(:client_request).and_return(fake_response)
17
- end
9
+ let(:visibility_timeout) { 100 }
18
10
 
19
- subject(:aws_sqs_queue) do
20
- DispatchRider::QueueServices::AwsSqs.new(:name => "normal_priority")
11
+ before do
12
+ allow_any_instance_of(Aws::SQS::Client).to receive(:list_queues).and_return(OpenStruct.new({queue_urls:["the.queue.url"]}))
13
+ allow_any_instance_of(Aws::SQS::Client).to receive(:get_queue_attributes).and_return(
14
+ OpenStruct.new({attributes:{"VisibilityTimeout"=>visibility_timeout}})
15
+ )
21
16
  end
22
17
 
23
18
  describe "#assign_storage" do
19
+
24
20
  context "when the aws gem is installed" do
25
21
 
26
22
  context "when the name of the queue is passed in the options" do
@@ -65,20 +61,18 @@ describe DispatchRider::QueueServices::AwsSqs do
65
61
  end
66
62
 
67
63
  let(:response_message) do
68
- {
69
- message_id: 12345,
64
+ OpenStruct.new({
65
+ message_id: "12345",
70
66
  md5_of_body: "mmmddd555",
71
67
  body: { subject: "foo", body: { bar: "baz" } }.to_json,
72
68
  receipt_handle: "HANDLE",
73
69
  attributes: response_attributes,
74
- }
70
+ visibility_timeout: visibility_timeout
71
+ })
75
72
  end
76
73
 
77
- before :each do
78
- response = AWS::SQS::Client.new.stub_for(:receive_message)
79
- response.data[:messages] = [response_message]
80
- allow_any_instance_of(AWS::SQS::Client::V20121105).to receive(:receive_message).and_return(response)
81
- allow_any_instance_of(AWS::SQS::Queue).to receive(:verify_receive_message_checksum).and_return([])
74
+ before do
75
+ allow_any_instance_of(Aws::SQS::Queue).to receive(:receive_messages).and_return(OpenStruct.new({first: response_message }))
82
76
  end
83
77
 
84
78
  context "when the block runs faster than the timeout" do
@@ -95,7 +89,7 @@ describe DispatchRider::QueueServices::AwsSqs do
95
89
 
96
90
  it "should raise" do
97
91
  expect {
98
- aws_sqs_queue.pop do |message|
92
+ aws_sqs_queue.pop do |_message|
99
93
  sleep(1.1)
100
94
  end
101
95
  }.to raise_exception(/message: foo,.+ took .+ seconds while the timeout was 1/)
@@ -106,7 +100,7 @@ describe DispatchRider::QueueServices::AwsSqs do
106
100
 
107
101
  context "when the sqs queue is empty" do
108
102
  before :each do
109
- allow(aws_sqs_queue.queue).to receive(:receive_message).and_return(nil)
103
+ allow_any_instance_of(Aws::SQS::Queue).to receive(:receive_messages).and_return(OpenStruct.new({first: nil }))
110
104
  end
111
105
 
112
106
  it "should not yield" do
@@ -129,25 +123,21 @@ describe DispatchRider::QueueServices::AwsSqs do
129
123
  end
130
124
 
131
125
  let(:response_message) do
132
- {
126
+ OpenStruct.new({
133
127
  message_id: 12345,
134
128
  md5_of_body: "mmmddd555",
135
129
  body: { subject: "foo", body: { bar: "baz" } }.to_json,
136
130
  receipt_handle: "HANDLE",
137
131
  attributes: response_attributes,
138
- }
132
+ visibility_timeout: visibility_timeout
133
+ })
139
134
  end
140
135
 
141
- before :each do
142
- response = AWS::SQS::Client.new.stub_for(:receive_message)
143
- response.data[:messages] = [response_message]
144
- allow_any_instance_of(AWS::SQS::Client::V20121105).to receive(:receive_message).and_return(response)
145
- allow_any_instance_of(AWS::SQS::Queue).to receive(:verify_receive_message_checksum).and_return([])
136
+ before do
137
+ allow_any_instance_of(Aws::SQS::Queue).to receive(:receive_messages).and_return(OpenStruct.new({first: response_message }))
146
138
  end
147
139
 
148
140
  it "should set the visibility timeout when extend is called" do
149
- expect_any_instance_of(AWS::SQS::ReceivedMessage).to receive(:visibility_timeout=).with(10)
150
- expect_any_instance_of(AWS::SQS::ReceivedMessage).to receive(:visibility_timeout=).with(0)
151
141
  aws_sqs_queue.pop do |message|
152
142
  message.extend_timeout(10)
153
143
  expect(message.total_timeout).to eq(10)
@@ -158,7 +148,7 @@ describe DispatchRider::QueueServices::AwsSqs do
158
148
  end
159
149
 
160
150
  describe "#construct_message_from" do
161
- context "when the item is directly published to AWS::SQS" do
151
+ context "when the item is directly published to Aws::SQS" do
162
152
  let(:sqs_message) { OpenStruct.new(body: { 'subject' => 'foo', 'body' => 'bar' }.to_json) }
163
153
 
164
154
  it "should return a message" do
@@ -168,7 +158,7 @@ describe DispatchRider::QueueServices::AwsSqs do
168
158
  end
169
159
  end
170
160
 
171
- context "when the item is published through AWS::SNS" do
161
+ context "when the item is published through Aws::SNS" do
172
162
  let(:sqs_message) do
173
163
  message = { 'subject' => 'foo', 'body' => 'bar' }
174
164
  body = { "Type" => "Notification", "Message" => message.to_json }.to_json
@@ -1,12 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe DispatchRider::QueueServices::FileSystem do
4
- let(:queue_path) { "tmp/queue" }
5
-
6
4
  subject(:file_system_queue) do
7
5
  DispatchRider::QueueServices::FileSystem.new(:path => queue_path)
8
6
  end
9
7
 
8
+ let(:queue_path) { "tmp/queue" }
9
+
10
10
  before { file_system_queue.send(:queue).send(:file_paths).each { |file| File.unlink(file) } }
11
11
 
12
12
  describe "#assign_storage" do