queue-bus 0.6.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +21 -0
  3. data/.rubocop.yml +35 -0
  4. data/CHANGELOG.md +38 -0
  5. data/Gemfile +4 -2
  6. data/README.mdown +16 -0
  7. data/Rakefile +2 -0
  8. data/lib/queue-bus.rb +15 -13
  9. data/lib/queue_bus/adapters/base.rb +4 -2
  10. data/lib/queue_bus/adapters/data.rb +12 -11
  11. data/lib/queue_bus/application.rb +13 -15
  12. data/lib/queue_bus/config.rb +64 -66
  13. data/lib/queue_bus/dispatch.rb +14 -12
  14. data/lib/queue_bus/dispatchers.rb +12 -5
  15. data/lib/queue_bus/driver.rb +15 -10
  16. data/lib/queue_bus/heartbeat.rb +32 -30
  17. data/lib/queue_bus/local.rb +9 -9
  18. data/lib/queue_bus/matcher.rb +36 -27
  19. data/lib/queue_bus/publisher.rb +7 -5
  20. data/lib/queue_bus/publishing.rb +31 -24
  21. data/lib/queue_bus/rider.rb +26 -22
  22. data/lib/queue_bus/subscriber.rb +20 -14
  23. data/lib/queue_bus/subscription.rb +25 -15
  24. data/lib/queue_bus/subscription_list.rb +30 -12
  25. data/lib/queue_bus/task_manager.rb +18 -16
  26. data/lib/queue_bus/tasks.rb +27 -11
  27. data/lib/queue_bus/util.rb +11 -8
  28. data/lib/queue_bus/version.rb +3 -1
  29. data/lib/queue_bus/worker.rb +3 -2
  30. data/queue-bus.gemspec +19 -17
  31. data/spec/adapter/publish_at_spec.rb +28 -25
  32. data/spec/adapter/support.rb +7 -1
  33. data/spec/adapter_spec.rb +4 -2
  34. data/spec/application_spec.rb +97 -97
  35. data/spec/config_spec.rb +116 -40
  36. data/spec/dispatch_spec.rb +48 -51
  37. data/spec/driver_spec.rb +60 -58
  38. data/spec/heartbeat_spec.rb +26 -24
  39. data/spec/integration_spec.rb +41 -40
  40. data/spec/matcher_spec.rb +104 -102
  41. data/spec/publish_spec.rb +46 -46
  42. data/spec/publisher_spec.rb +3 -1
  43. data/spec/rider_spec.rb +16 -14
  44. data/spec/spec_helper.rb +12 -7
  45. data/spec/subscriber_spec.rb +227 -227
  46. data/spec/subscription_list_spec.rb +31 -31
  47. data/spec/subscription_spec.rb +37 -36
  48. data/spec/worker_spec.rb +17 -15
  49. metadata +21 -6
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module QueueBus
4
6
  describe Publisher do
5
- it "should call publish as expected"
7
+ it 'should call publish as expected'
6
8
  end
7
9
  end
@@ -1,27 +1,29 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  module QueueBus
4
6
  describe Rider do
5
- it "should call execute" do
7
+ it 'should call execute' do
6
8
  expect(QueueBus).to receive(:dispatcher_execute)
7
- Rider.perform("bus_rider_app_key" => "app", "bus_rider_sub_key" => "sub", "ok" => true, "bus_event_type" => "event_name")
9
+ Rider.perform('bus_rider_app_key' => 'app', 'bus_rider_sub_key' => 'sub', 'ok' => true, 'bus_event_type' => 'event_name')
8
10
  end
9
11
 
10
- it "should change the value" do
11
- QueueBus.dispatch("r1") do
12
- subscribe "event_name" do |attributes|
12
+ it 'should change the value' do
13
+ QueueBus.dispatch('r1') do
14
+ subscribe 'event_name' do |attributes|
13
15
  Runner1.run(attributes)
14
16
  end
15
17
  end
16
18
  expect(Runner1.value).to eq(0)
17
- Rider.perform("bus_locale" => "en", "bus_timezone" => "PST", "bus_rider_app_key" => "r1", "bus_rider_sub_key" => "event_name", "ok" => true, "bus_event_type" => "event_name")
18
- Rider.perform("bus_rider_app_key" => "other", "bus_rider_sub_key" => "event_name", "ok" => true, "bus_event_type" => "event_name")
19
+ Rider.perform('bus_locale' => 'en', 'bus_timezone' => 'PST', 'bus_rider_app_key' => 'r1', 'bus_rider_sub_key' => 'event_name', 'ok' => true, 'bus_event_type' => 'event_name')
20
+ Rider.perform('bus_rider_app_key' => 'other', 'bus_rider_sub_key' => 'event_name', 'ok' => true, 'bus_event_type' => 'event_name')
19
21
  expect(Runner1.value).to eq(1)
20
22
  end
21
23
 
22
- it "should set the timezone and locale if present" do
23
- QueueBus.dispatch("r1") do
24
- subscribe "event_name" do |attributes|
24
+ it 'should set the timezone and locale if present' do
25
+ QueueBus.dispatch('r1') do
26
+ subscribe 'event_name' do |attributes|
25
27
  Runner1.run(attributes)
26
28
  end
27
29
  end
@@ -29,11 +31,11 @@ module QueueBus
29
31
  expect(defined?(I18n)).to be_nil
30
32
  expect(Time.respond_to?(:zone)).to eq(false)
31
33
 
32
- stub_const("I18n", Class.new)
33
- expect(I18n).to receive(:locale=).with("en")
34
- expect(Time).to receive(:zone=).with("PST")
34
+ stub_const('I18n', Class.new)
35
+ expect(I18n).to receive(:locale=).with('en')
36
+ expect(Time).to receive(:zone=).with('PST')
35
37
 
36
- Rider.perform("bus_locale" => "en", "bus_timezone" => "PST", "bus_rider_app_key" => "r1", "bus_rider_sub_key" => "event_name", "ok" => true, "bus_event_type" => "event_name")
38
+ Rider.perform('bus_locale' => 'en', 'bus_timezone' => 'PST', 'bus_rider_app_key' => 'r1', 'bus_rider_sub_key' => 'event_name', 'ok' => true, 'bus_event_type' => 'event_name')
37
39
  end
38
40
  end
39
41
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'timecop'
2
4
  require 'queue-bus'
3
5
  require 'adapter/support'
@@ -10,8 +12,8 @@ module QueueBus
10
12
  @value ||= 0
11
13
  end
12
14
 
13
- def self.attributes
14
- @attributes
15
+ class << self
16
+ attr_reader :attributes
15
17
  end
16
18
 
17
19
  def self.run(attrs)
@@ -33,9 +35,9 @@ module QueueBus
33
35
  end
34
36
  end
35
37
 
36
- def test_sub(event_name, queue="default")
37
- matcher = {"bus_event_type" => event_name}
38
- QueueBus::Subscription.new(queue, event_name, "::QueueBus::Rider", matcher, nil)
38
+ def test_sub(event_name, queue = 'default')
39
+ matcher = { 'bus_event_type' => event_name }
40
+ QueueBus::Subscription.new(queue, event_name, '::QueueBus::Rider', matcher, nil)
39
41
  end
40
42
 
41
43
  def test_list(*args)
@@ -50,6 +52,7 @@ RSpec.configure do |config|
50
52
  config.mock_with :rspec do |c|
51
53
  c.syntax = :expect
52
54
  end
55
+
53
56
  config.expect_with :rspec do |c|
54
57
  c.syntax = :expect
55
58
  end
@@ -57,10 +60,12 @@ RSpec.configure do |config|
57
60
  config.before(:each) do
58
61
  reset_test_adapter
59
62
  end
63
+
60
64
  config.after(:each) do
61
65
  begin
62
- QueueBus.redis { |redis| redis.flushall }
63
- rescue
66
+ QueueBus.redis(&:flushall)
67
+ rescue RuntimeError # rubocop:disable Lint/HandleExceptions
68
+ # We raise if redis isn't there.
64
69
  end
65
70
  QueueBus.send(:reset)
66
71
  QueueBus::Runner1.reset
@@ -1,270 +1,270 @@
1
- require 'spec_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- describe QueueBus::Subscriber do
4
- let(:attributes) { {"x" => "y"} }
5
- let(:bus_attrs) { {"bus_driven_at" => Time.now.to_i} }
3
+ require 'spec_helper'
6
4
 
7
- before(:each) do
8
- class SubscriberTest1
9
- include QueueBus::Subscriber
10
- @queue = "myqueue"
5
+ describe QueueBus::Subscriber do
6
+ let(:attributes) { { 'x' => 'y' } }
7
+ let(:bus_attrs) { { 'bus_driven_at' => Time.now.to_i } }
11
8
 
12
- application :my_thing
13
- subscribe :thing_filter, :x => "y"
14
- subscribe :event_sub
9
+ before(:each) do
10
+ class SubscriberTest1
11
+ include QueueBus::Subscriber
12
+ @queue = 'myqueue'
15
13
 
16
- def event_sub(attributes)
17
- QueueBus::Runner1.run(attributes)
18
- end
14
+ application :my_thing
15
+ subscribe :thing_filter, x: 'y'
16
+ subscribe :event_sub
19
17
 
20
- def thing_filter(attributes)
21
- QueueBus::Runner2.run(attributes)
22
- end
18
+ def event_sub(attributes)
19
+ QueueBus::Runner1.run(attributes)
23
20
  end
24
21
 
25
- class SubscriberTest2
26
- include QueueBus::Subscriber
27
- application :test2
28
- subscribe :test2, "value" => :present
29
- transform :make_an_int
22
+ def thing_filter(attributes)
23
+ QueueBus::Runner2.run(attributes)
24
+ end
25
+ end
30
26
 
31
- def self.make_an_int(attributes)
32
- attributes["value"].to_s.length
33
- end
27
+ class SubscriberTest2
28
+ include QueueBus::Subscriber
29
+ application :test2
30
+ subscribe :test2, 'value' => :present
31
+ transform :make_an_int
34
32
 
35
- def test2(int)
36
- QueueBus::Runner1.run("transformed"=>int)
37
- end
33
+ def self.make_an_int(attributes)
34
+ attributes['value'].to_s.length
38
35
  end
39
36
 
40
- module SubModule
41
- class SubscriberTest3
42
- include QueueBus::Subscriber
37
+ def test2(int)
38
+ QueueBus::Runner1.run('transformed' => int)
39
+ end
40
+ end
43
41
 
44
- subscribe_queue :sub_queue1, :test3, :bus_event_type => "the_event"
45
- subscribe_queue :sub_queue2, :the_event
46
- subscribe :other, :bus_event_type => "other_event"
42
+ module SubModule
43
+ class SubscriberTest3
44
+ include QueueBus::Subscriber
47
45
 
48
- def test3(attributes)
49
- QueueBus::Runner1.run(attributes)
50
- end
46
+ subscribe_queue :sub_queue1, :test3, bus_event_type: 'the_event'
47
+ subscribe_queue :sub_queue2, :the_event
48
+ subscribe :other, bus_event_type: 'other_event'
51
49
 
52
- def the_event(attributes)
53
- QueueBus::Runner2.run(attributes)
54
- end
50
+ def test3(attributes)
51
+ QueueBus::Runner1.run(attributes)
55
52
  end
56
53
 
57
- class SubscriberTest4
58
- include QueueBus::Subscriber
59
-
60
- subscribe_queue :sub_queue1, :test4
54
+ def the_event(attributes)
55
+ QueueBus::Runner2.run(attributes)
61
56
  end
62
57
  end
63
58
 
64
- Timecop.freeze
65
- QueueBus::TaskManager.new(false).subscribe!
66
- end
59
+ class SubscriberTest4
60
+ include QueueBus::Subscriber
67
61
 
68
- after(:each) do
69
- Timecop.return
62
+ subscribe_queue :sub_queue1, :test4
63
+ end
70
64
  end
71
65
 
72
- it "should have the application" do
73
- expect(SubscriberTest1.app_key).to eq("my_thing")
74
- expect(SubModule::SubscriberTest3.app_key).to eq("sub_module")
75
- expect(SubModule::SubscriberTest4.app_key).to eq("sub_module")
76
- end
66
+ Timecop.freeze
67
+ QueueBus::TaskManager.new(false).subscribe!
68
+ end
77
69
 
78
- it "should be able to transform the attributes" do
79
- dispatcher = QueueBus.dispatcher_by_key("test2")
80
- all = dispatcher.subscriptions.all
81
- expect(all.size).to eq(1)
70
+ after(:each) do
71
+ Timecop.return
72
+ end
82
73
 
83
- sub = all.first
84
- expect(sub.queue_name).to eq("test2_default")
85
- expect(sub.class_name).to eq("SubscriberTest2")
86
- expect(sub.key).to eq("SubscriberTest2.test2")
87
- expect(sub.matcher.filters).to eq({"value"=>"bus_special_value_present"})
74
+ it 'should have the application' do
75
+ expect(SubscriberTest1.app_key).to eq('my_thing')
76
+ expect(SubModule::SubscriberTest3.app_key).to eq('sub_module')
77
+ expect(SubModule::SubscriberTest4.app_key).to eq('sub_module')
78
+ end
88
79
 
89
- QueueBus::Driver.perform(attributes.merge("bus_event_type" => "something2", "value"=>"nice"))
80
+ it 'should be able to transform the attributes' do
81
+ dispatcher = QueueBus.dispatcher_by_key('test2')
82
+ all = dispatcher.subscriptions.all
83
+ expect(all.size).to eq(1)
90
84
 
91
- hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:test2_default") })
92
- expect(hash["class"]).to eq("QueueBus::Worker")
93
- expect(hash["args"].size).to eq(1)
94
- expect(JSON.parse(hash["args"].first)).to eq({"bus_class_proxy" => "SubscriberTest2", "bus_rider_app_key"=>"test2", "bus_rider_sub_key"=>"SubscriberTest2.test2", "bus_rider_queue" => "test2_default", "bus_rider_class_name"=>"SubscriberTest2",
95
- "bus_event_type" => "something2", "value"=>"nice", "x"=>"y"}.merge(bus_attrs))
85
+ sub = all.first
86
+ expect(sub.queue_name).to eq('test2_default')
87
+ expect(sub.class_name).to eq('SubscriberTest2')
88
+ expect(sub.key).to eq('SubscriberTest2.test2')
89
+ expect(sub.matcher.filters).to eq('value' => 'bus_special_value_present')
96
90
 
97
- expect(QueueBus::Runner1.value).to eq(0)
98
- expect(QueueBus::Runner2.value).to eq(0)
99
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
100
- expect(QueueBus::Runner1.value).to eq(1)
101
- expect(QueueBus::Runner2.value).to eq(0)
91
+ QueueBus::Driver.perform(attributes.merge('bus_event_type' => 'something2', 'value' => 'nice'))
102
92
 
103
- expect(QueueBus::Runner1.attributes).to eq({"transformed" => 4})
93
+ hash = JSON.parse(QueueBus.redis { |redis| redis.lpop('queue:test2_default') })
94
+ expect(hash['class']).to eq('QueueBus::Worker')
95
+ expect(hash['args'].size).to eq(1)
96
+ expect(JSON.parse(hash['args'].first)).to eq({ 'bus_class_proxy' => 'SubscriberTest2', 'bus_rider_app_key' => 'test2', 'bus_rider_sub_key' => 'SubscriberTest2.test2', 'bus_rider_queue' => 'test2_default', 'bus_rider_class_name' => 'SubscriberTest2',
97
+ 'bus_event_type' => 'something2', 'value' => 'nice', 'x' => 'y' }.merge(bus_attrs))
104
98
 
99
+ expect(QueueBus::Runner1.value).to eq(0)
100
+ expect(QueueBus::Runner2.value).to eq(0)
101
+ QueueBus::Util.constantize(hash['class']).perform(*hash['args'])
102
+ expect(QueueBus::Runner1.value).to eq(1)
103
+ expect(QueueBus::Runner2.value).to eq(0)
105
104
 
106
- QueueBus::Driver.perform(attributes.merge("bus_event_type" => "something2", "value"=>"12"))
105
+ expect(QueueBus::Runner1.attributes).to eq('transformed' => 4)
107
106
 
108
- hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:test2_default") })
109
- expect(hash["class"]).to eq("QueueBus::Worker")
110
- expect(hash["args"].size).to eq(1)
111
- expect(JSON.parse(hash["args"].first)).to eq({"bus_class_proxy" => "SubscriberTest2", "bus_rider_app_key"=>"test2", "bus_rider_sub_key"=>"SubscriberTest2.test2", "bus_rider_queue" => "test2_default", "bus_rider_class_name"=>"SubscriberTest2",
112
- "bus_event_type" => "something2", "value"=>"12", "x"=>"y"}.merge(bus_attrs))
107
+ QueueBus::Driver.perform(attributes.merge('bus_event_type' => 'something2', 'value' => '12'))
113
108
 
114
- expect(QueueBus::Runner1.value).to eq(1)
115
- expect(QueueBus::Runner2.value).to eq(0)
116
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
117
- expect(QueueBus::Runner1.value).to eq(2)
118
- expect(QueueBus::Runner2.value).to eq(0)
109
+ hash = JSON.parse(QueueBus.redis { |redis| redis.lpop('queue:test2_default') })
110
+ expect(hash['class']).to eq('QueueBus::Worker')
111
+ expect(hash['args'].size).to eq(1)
112
+ expect(JSON.parse(hash['args'].first)).to eq({ 'bus_class_proxy' => 'SubscriberTest2', 'bus_rider_app_key' => 'test2', 'bus_rider_sub_key' => 'SubscriberTest2.test2', 'bus_rider_queue' => 'test2_default', 'bus_rider_class_name' => 'SubscriberTest2',
113
+ 'bus_event_type' => 'something2', 'value' => '12', 'x' => 'y' }.merge(bus_attrs))
119
114
 
120
- expect(QueueBus::Runner1.attributes).to eq({"transformed" => 2})
121
- end
115
+ expect(QueueBus::Runner1.value).to eq(1)
116
+ expect(QueueBus::Runner2.value).to eq(0)
117
+ QueueBus::Util.constantize(hash['class']).perform(*hash['args'])
118
+ expect(QueueBus::Runner1.value).to eq(2)
119
+ expect(QueueBus::Runner2.value).to eq(0)
122
120
 
121
+ expect(QueueBus::Runner1.attributes).to eq('transformed' => 2)
122
+ end
123
+
124
+ it 'should put in a different queue' do
125
+ dispatcher = QueueBus.dispatcher_by_key('sub_module')
126
+ all = dispatcher.subscriptions.all
127
+ expect(all.size).to eq(4)
128
+
129
+ sub = all.select { |s| s.key == 'SubModule::SubscriberTest3.test3' }.first
130
+ expect(sub.queue_name).to eq('sub_queue1')
131
+ expect(sub.class_name).to eq('SubModule::SubscriberTest3')
132
+ expect(sub.key).to eq('SubModule::SubscriberTest3.test3')
133
+ expect(sub.matcher.filters).to eq('bus_event_type' => 'the_event')
134
+
135
+ sub = all.select { |s| s.key == 'SubModule::SubscriberTest3.the_event' }.first
136
+ expect(sub.queue_name).to eq('sub_queue2')
137
+ expect(sub.class_name).to eq('SubModule::SubscriberTest3')
138
+ expect(sub.key).to eq('SubModule::SubscriberTest3.the_event')
139
+ expect(sub.matcher.filters).to eq('bus_event_type' => 'the_event')
140
+
141
+ sub = all.select { |s| s.key == 'SubModule::SubscriberTest3.other' }.first
142
+ expect(sub.queue_name).to eq('sub_module_default')
143
+ expect(sub.class_name).to eq('SubModule::SubscriberTest3')
144
+ expect(sub.key).to eq('SubModule::SubscriberTest3.other')
145
+ expect(sub.matcher.filters).to eq('bus_event_type' => 'other_event')
146
+
147
+ sub = all.select { |s| s.key == 'SubModule::SubscriberTest4.test4' }.first
148
+ expect(sub.queue_name).to eq('sub_queue1')
149
+ expect(sub.class_name).to eq('SubModule::SubscriberTest4')
150
+ expect(sub.key).to eq('SubModule::SubscriberTest4.test4')
151
+ expect(sub.matcher.filters).to eq('bus_event_type' => 'test4')
152
+
153
+ QueueBus::Driver.perform(attributes.merge('bus_event_type' => 'the_event'))
154
+
155
+ hash = JSON.parse(QueueBus.redis { |redis| redis.lpop('queue:sub_queue1') })
156
+ expect(hash['class']).to eq('QueueBus::Worker')
157
+ expect(hash['args'].size).to eq(1)
158
+ expect(JSON.parse(hash['args'].first)).to eq({ 'bus_class_proxy' => 'SubModule::SubscriberTest3', 'bus_rider_app_key' => 'sub_module', 'bus_rider_sub_key' => 'SubModule::SubscriberTest3.test3', 'bus_rider_queue' => 'sub_queue1', 'bus_rider_class_name' => 'SubModule::SubscriberTest3',
159
+ 'bus_event_type' => 'the_event', 'x' => 'y' }.merge(bus_attrs))
160
+
161
+ expect(QueueBus::Runner1.value).to eq(0)
162
+ expect(QueueBus::Runner2.value).to eq(0)
163
+ QueueBus::Util.constantize(hash['class']).perform(*hash['args'])
164
+ expect(QueueBus::Runner1.value).to eq(1)
165
+ expect(QueueBus::Runner2.value).to eq(0)
166
+
167
+ hash = JSON.parse(QueueBus.redis { |redis| redis.lpop('queue:sub_queue2') })
168
+ expect(hash['class']).to eq('QueueBus::Worker')
169
+ expect(hash['args'].size).to eq(1)
170
+ expect(JSON.parse(hash['args'].first)).to eq({ 'bus_class_proxy' => 'SubModule::SubscriberTest3', 'bus_rider_app_key' => 'sub_module', 'bus_rider_sub_key' => 'SubModule::SubscriberTest3.the_event', 'bus_rider_queue' => 'sub_queue2', 'bus_rider_class_name' => 'SubModule::SubscriberTest3',
171
+ 'bus_event_type' => 'the_event', 'x' => 'y' }.merge(bus_attrs))
172
+
173
+ expect(QueueBus::Runner1.value).to eq(1)
174
+ expect(QueueBus::Runner2.value).to eq(0)
175
+ QueueBus::Util.constantize(hash['class']).perform(*hash['args'])
176
+ expect(QueueBus::Runner1.value).to eq(1)
177
+ expect(QueueBus::Runner2.value).to eq(1)
178
+ end
123
179
 
124
- it "should put in a different queue" do
125
- dispatcher = QueueBus.dispatcher_by_key("sub_module")
126
- all = dispatcher.subscriptions.all
127
- expect(all.size).to eq(4)
128
-
129
- sub = all.select{ |s| s.key == "SubModule::SubscriberTest3.test3"}.first
130
- expect(sub.queue_name).to eq("sub_queue1")
131
- expect(sub.class_name).to eq("SubModule::SubscriberTest3")
132
- expect(sub.key).to eq("SubModule::SubscriberTest3.test3")
133
- expect(sub.matcher.filters).to eq({"bus_event_type"=>"the_event"})
134
-
135
- sub = all.select{ |s| s.key == "SubModule::SubscriberTest3.the_event"}.first
136
- expect(sub.queue_name).to eq("sub_queue2")
137
- expect(sub.class_name).to eq("SubModule::SubscriberTest3")
138
- expect(sub.key).to eq("SubModule::SubscriberTest3.the_event")
139
- expect(sub.matcher.filters).to eq({"bus_event_type"=>"the_event"})
140
-
141
- sub = all.select{ |s| s.key == "SubModule::SubscriberTest3.other"}.first
142
- expect(sub.queue_name).to eq("sub_module_default")
143
- expect(sub.class_name).to eq("SubModule::SubscriberTest3")
144
- expect(sub.key).to eq("SubModule::SubscriberTest3.other")
145
- expect(sub.matcher.filters).to eq({"bus_event_type"=>"other_event"})
146
-
147
- sub = all.select{ |s| s.key == "SubModule::SubscriberTest4.test4"}.first
148
- expect(sub.queue_name).to eq("sub_queue1")
149
- expect(sub.class_name).to eq("SubModule::SubscriberTest4")
150
- expect(sub.key).to eq("SubModule::SubscriberTest4.test4")
151
- expect(sub.matcher.filters).to eq({"bus_event_type"=>"test4"})
152
-
153
- QueueBus::Driver.perform(attributes.merge("bus_event_type" => "the_event"))
154
-
155
- hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:sub_queue1") })
156
- expect(hash["class"]).to eq("QueueBus::Worker")
157
- expect(hash["args"].size).to eq(1)
158
- expect(JSON.parse(hash["args"].first)).to eq({"bus_class_proxy" => "SubModule::SubscriberTest3", "bus_rider_app_key"=>"sub_module", "bus_rider_sub_key"=>"SubModule::SubscriberTest3.test3", "bus_rider_queue" => "sub_queue1", "bus_rider_class_name"=>"SubModule::SubscriberTest3",
159
- "bus_event_type" => "the_event", "x" => "y"}.merge(bus_attrs))
160
-
161
- expect(QueueBus::Runner1.value).to eq(0)
162
- expect(QueueBus::Runner2.value).to eq(0)
163
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
164
- expect(QueueBus::Runner1.value).to eq(1)
165
- expect(QueueBus::Runner2.value).to eq(0)
166
-
167
- hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:sub_queue2") })
168
- expect(hash["class"]).to eq("QueueBus::Worker")
169
- expect(hash["args"].size).to eq(1)
170
- expect(JSON.parse(hash["args"].first)).to eq({"bus_class_proxy" => "SubModule::SubscriberTest3", "bus_rider_app_key"=>"sub_module", "bus_rider_sub_key"=>"SubModule::SubscriberTest3.the_event", "bus_rider_queue" => "sub_queue2", "bus_rider_class_name"=>"SubModule::SubscriberTest3",
171
- "bus_event_type" => "the_event", "x" => "y"}.merge(bus_attrs))
172
-
173
- expect(QueueBus::Runner1.value).to eq(1)
174
- expect(QueueBus::Runner2.value).to eq(0)
175
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
176
- expect(QueueBus::Runner1.value).to eq(1)
177
- expect(QueueBus::Runner2.value).to eq(1)
180
+ it 'should subscribe to default and attributes' do
181
+ dispatcher = QueueBus.dispatcher_by_key('my_thing')
182
+ all = dispatcher.subscriptions.all
183
+
184
+ sub = all.select { |s| s.key == 'SubscriberTest1.event_sub' }.first
185
+ expect(sub.queue_name).to eq('myqueue')
186
+ expect(sub.class_name).to eq('SubscriberTest1')
187
+ expect(sub.key).to eq('SubscriberTest1.event_sub')
188
+ expect(sub.matcher.filters).to eq('bus_event_type' => 'event_sub')
189
+
190
+ sub = all.select { |s| s.key == 'SubscriberTest1.thing_filter' }.first
191
+ expect(sub.queue_name).to eq('myqueue')
192
+ expect(sub.class_name).to eq('SubscriberTest1')
193
+ expect(sub.key).to eq('SubscriberTest1.thing_filter')
194
+ expect(sub.matcher.filters).to eq('x' => 'y')
195
+
196
+ QueueBus::Driver.perform(attributes.merge('bus_event_type' => 'event_sub'))
197
+ expect(QueueBus.redis { |redis| redis.smembers('queues') }).to match_array(['myqueue'])
198
+
199
+ pop1 = JSON.parse(QueueBus.redis { |redis| redis.lpop('queue:myqueue') })
200
+ pop2 = JSON.parse(QueueBus.redis { |redis| redis.lpop('queue:myqueue') })
201
+
202
+ if JSON.parse(pop1['args'].first)['bus_rider_sub_key'] == 'SubscriberTest1.thing_filter'
203
+ hash1 = pop1
204
+ hash2 = pop2
205
+ else
206
+ hash1 = pop2
207
+ hash2 = pop1
178
208
  end
179
209
 
180
- it "should subscribe to default and attributes" do
181
- dispatcher = QueueBus.dispatcher_by_key("my_thing")
182
- all = dispatcher.subscriptions.all
183
-
184
- sub = all.select{ |s| s.key == "SubscriberTest1.event_sub"}.first
185
- expect(sub.queue_name).to eq("myqueue")
186
- expect(sub.class_name).to eq("SubscriberTest1")
187
- expect(sub.key).to eq("SubscriberTest1.event_sub")
188
- expect(sub.matcher.filters).to eq({"bus_event_type"=>"event_sub"})
189
-
190
- sub = all.select{ |s| s.key == "SubscriberTest1.thing_filter"}.first
191
- expect(sub.queue_name).to eq("myqueue")
192
- expect(sub.class_name).to eq("SubscriberTest1")
193
- expect(sub.key).to eq("SubscriberTest1.thing_filter")
194
- expect(sub.matcher.filters).to eq({"x"=>"y"})
195
-
196
- QueueBus::Driver.perform(attributes.merge("bus_event_type" => "event_sub"))
197
- expect(QueueBus.redis { |redis| redis.smembers("queues") }).to match_array(["myqueue"])
198
-
199
- pop1 = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:myqueue") })
200
- pop2 = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:myqueue") })
201
-
202
- if JSON.parse(pop1["args"].first)["bus_rider_sub_key"] == "SubscriberTest1.thing_filter"
203
- hash1 = pop1
204
- hash2 = pop2
205
- else
206
- hash1 = pop2
207
- hash2 = pop1
208
- end
210
+ expect(hash1['class']).to eq('QueueBus::Worker')
211
+ expect(JSON.parse(hash1['args'].first)).to eq({ 'bus_class_proxy' => 'SubscriberTest1', 'bus_rider_app_key' => 'my_thing', 'bus_rider_sub_key' => 'SubscriberTest1.thing_filter', 'bus_rider_queue' => 'myqueue', 'bus_rider_class_name' => 'SubscriberTest1',
212
+ 'bus_event_type' => 'event_sub', 'x' => 'y' }.merge(bus_attrs))
213
+
214
+ expect(QueueBus::Runner1.value).to eq(0)
215
+ expect(QueueBus::Runner2.value).to eq(0)
216
+ QueueBus::Util.constantize(hash1['class']).perform(*hash1['args'])
217
+ expect(QueueBus::Runner1.value).to eq(0)
218
+ expect(QueueBus::Runner2.value).to eq(1)
219
+
220
+ expect(hash2['class']).to eq('QueueBus::Worker')
221
+ expect(hash2['args'].size).to eq(1)
222
+ expect(JSON.parse(hash2['args'].first)).to eq({ 'bus_class_proxy' => 'SubscriberTest1', 'bus_rider_app_key' => 'my_thing', 'bus_rider_sub_key' => 'SubscriberTest1.event_sub', 'bus_rider_queue' => 'myqueue', 'bus_rider_class_name' => 'SubscriberTest1',
223
+ 'bus_event_type' => 'event_sub', 'x' => 'y' }.merge(bus_attrs))
224
+
225
+ expect(QueueBus::Runner1.value).to eq(0)
226
+ expect(QueueBus::Runner2.value).to eq(1)
227
+ QueueBus::Util.constantize(hash2['class']).perform(*hash2['args'])
228
+ expect(QueueBus::Runner1.value).to eq(1)
229
+ expect(QueueBus::Runner2.value).to eq(1)
230
+
231
+ QueueBus::Driver.perform(attributes.merge('bus_event_type' => 'event_sub_other'))
232
+ expect(QueueBus.redis { |redis| redis.smembers('queues') }).to match_array(['myqueue'])
233
+
234
+ hash = JSON.parse(QueueBus.redis { |redis| redis.lpop('queue:myqueue') })
235
+ expect(hash['class']).to eq('QueueBus::Worker')
236
+ expect(hash['args'].size).to eq(1)
237
+ expect(JSON.parse(hash['args'].first)).to eq({ 'bus_class_proxy' => 'SubscriberTest1', 'bus_rider_app_key' => 'my_thing', 'bus_rider_sub_key' => 'SubscriberTest1.thing_filter', 'bus_rider_queue' => 'myqueue', 'bus_rider_class_name' => 'SubscriberTest1',
238
+ 'bus_event_type' => 'event_sub_other', 'x' => 'y' }.merge(bus_attrs))
239
+
240
+ expect(QueueBus::Runner1.value).to eq(1)
241
+ expect(QueueBus::Runner2.value).to eq(1)
242
+ QueueBus::Util.constantize(hash['class']).perform(*hash['args'])
243
+ expect(QueueBus::Runner1.value).to eq(1)
244
+ expect(QueueBus::Runner2.value).to eq(2)
245
+
246
+ QueueBus::Driver.perform({ 'x' => 'z' }.merge('bus_event_type' => 'event_sub_other'))
247
+ expect(QueueBus.redis { |redis| redis.smembers('queues') }).to match_array(['myqueue'])
248
+
249
+ expect(QueueBus.redis { |redis| redis.lpop('queue:myqueue') }).to be_nil
250
+ end
209
251
 
210
- expect(hash1["class"]).to eq("QueueBus::Worker")
211
- expect(JSON.parse(hash1["args"].first)).to eq({"bus_class_proxy" => "SubscriberTest1", "bus_rider_app_key"=>"my_thing", "bus_rider_sub_key"=>"SubscriberTest1.thing_filter", "bus_rider_queue" => "myqueue", "bus_rider_class_name"=>"SubscriberTest1",
212
- "bus_event_type" => "event_sub", "x" => "y"}.merge(bus_attrs))
213
-
214
- expect(QueueBus::Runner1.value).to eq(0)
215
- expect(QueueBus::Runner2.value).to eq(0)
216
- QueueBus::Util.constantize(hash1["class"]).perform(*hash1["args"])
217
- expect(QueueBus::Runner1.value).to eq(0)
218
- expect(QueueBus::Runner2.value).to eq(1)
219
-
220
- expect(hash2["class"]).to eq("QueueBus::Worker")
221
- expect(hash2["args"].size).to eq(1)
222
- expect(JSON.parse(hash2["args"].first)).to eq({"bus_class_proxy" => "SubscriberTest1", "bus_rider_app_key"=>"my_thing", "bus_rider_sub_key"=>"SubscriberTest1.event_sub", "bus_rider_queue" => "myqueue", "bus_rider_class_name"=>"SubscriberTest1",
223
- "bus_event_type" => "event_sub", "x" => "y"}.merge(bus_attrs))
224
-
225
- expect(QueueBus::Runner1.value).to eq(0)
226
- expect(QueueBus::Runner2.value).to eq(1)
227
- QueueBus::Util.constantize(hash2["class"]).perform(*hash2["args"])
228
- expect(QueueBus::Runner1.value).to eq(1)
229
- expect(QueueBus::Runner2.value).to eq(1)
230
-
231
- QueueBus::Driver.perform(attributes.merge("bus_event_type" => "event_sub_other"))
232
- expect(QueueBus.redis { |redis| redis.smembers("queues") }).to match_array(["myqueue"])
233
-
234
- hash = JSON.parse(QueueBus.redis { |redis| redis.lpop("queue:myqueue") })
235
- expect(hash["class"]).to eq("QueueBus::Worker")
236
- expect(hash["args"].size).to eq(1)
237
- expect(JSON.parse(hash["args"].first)).to eq({"bus_class_proxy" => "SubscriberTest1", "bus_rider_app_key"=>"my_thing", "bus_rider_sub_key"=>"SubscriberTest1.thing_filter", "bus_rider_queue" => "myqueue", "bus_rider_class_name"=>"SubscriberTest1",
238
- "bus_event_type" => "event_sub_other", "x" => "y"}.merge(bus_attrs))
239
-
240
- expect(QueueBus::Runner1.value).to eq(1)
241
- expect(QueueBus::Runner2.value).to eq(1)
242
- QueueBus::Util.constantize(hash["class"]).perform(*hash["args"])
243
- expect(QueueBus::Runner1.value).to eq(1)
244
- expect(QueueBus::Runner2.value).to eq(2)
245
-
246
- QueueBus::Driver.perform({"x"=>"z"}.merge("bus_event_type" => "event_sub_other"))
247
- expect(QueueBus.redis { |redis| redis.smembers("queues") }).to match_array(["myqueue"])
248
-
249
- expect(QueueBus.redis { |redis| redis.lpop("queue:myqueue") }).to be_nil
252
+ describe '.perform' do
253
+ let(:attributes) { { 'bus_rider_sub_key' => 'SubscriberTest1.event_sub', 'bus_locale' => 'en', 'bus_timezone' => 'PST' } }
254
+ it 'should call the method based on key' do
255
+ expect_any_instance_of(SubscriberTest1).to receive(:event_sub)
256
+ SubscriberTest1.perform(attributes)
250
257
  end
258
+ it 'should set the timezone and locale if present' do
259
+ expect(defined?(I18n)).to be_nil
260
+ expect(Time.respond_to?(:zone)).to eq(false)
251
261
 
252
- describe ".perform" do
253
- let(:attributes) { {"bus_rider_sub_key"=>"SubscriberTest1.event_sub", "bus_locale" => "en", "bus_timezone" => "PST"} }
254
- it "should call the method based on key" do
255
- expect_any_instance_of(SubscriberTest1).to receive(:event_sub)
256
- SubscriberTest1.perform(attributes)
257
- end
258
- it "should set the timezone and locale if present" do
259
- expect(defined?(I18n)).to be_nil
260
- expect(Time.respond_to?(:zone)).to eq(false)
261
-
262
- stub_const("I18n", Class.new)
263
- expect(I18n).to receive(:locale=).with("en")
264
- expect(Time).to receive(:zone=).with("PST")
262
+ stub_const('I18n', Class.new)
263
+ expect(I18n).to receive(:locale=).with('en')
264
+ expect(Time).to receive(:zone=).with('PST')
265
265
 
266
- expect_any_instance_of(SubscriberTest1).to receive(:event_sub)
267
- SubscriberTest1.perform(attributes)
268
- end
266
+ expect_any_instance_of(SubscriberTest1).to receive(:event_sub)
267
+ SubscriberTest1.perform(attributes)
269
268
  end
270
269
  end
270
+ end