queue-bus 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +21 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +4 -2
- data/Rakefile +2 -0
- data/lib/queue-bus.rb +15 -12
- data/lib/queue_bus/adapters/base.rb +4 -2
- data/lib/queue_bus/adapters/data.rb +12 -11
- data/lib/queue_bus/application.rb +13 -15
- data/lib/queue_bus/dispatch.rb +14 -12
- data/lib/queue_bus/dispatchers.rb +12 -5
- data/lib/queue_bus/driver.rb +15 -10
- data/lib/queue_bus/heartbeat.rb +32 -30
- data/lib/queue_bus/local.rb +9 -9
- data/lib/queue_bus/matcher.rb +36 -27
- data/lib/queue_bus/publisher.rb +7 -5
- data/lib/queue_bus/publishing.rb +31 -24
- data/lib/queue_bus/rider.rb +26 -22
- data/lib/queue_bus/subscriber.rb +20 -14
- data/lib/queue_bus/subscription.rb +25 -15
- data/lib/queue_bus/subscription_list.rb +30 -12
- data/lib/queue_bus/task_manager.rb +18 -16
- data/lib/queue_bus/tasks.rb +20 -15
- data/lib/queue_bus/util.rb +11 -8
- data/lib/queue_bus/version.rb +3 -1
- data/lib/queue_bus/worker.rb +3 -2
- data/queue-bus.gemspec +19 -18
- data/spec/adapter/publish_at_spec.rb +28 -25
- data/spec/adapter/support.rb +7 -1
- data/spec/adapter_spec.rb +4 -2
- data/spec/application_spec.rb +97 -97
- data/spec/dispatch_spec.rb +48 -51
- data/spec/driver_spec.rb +60 -58
- data/spec/heartbeat_spec.rb +26 -24
- data/spec/integration_spec.rb +41 -40
- data/spec/matcher_spec.rb +104 -102
- data/spec/publish_spec.rb +46 -46
- data/spec/publisher_spec.rb +3 -1
- data/spec/rider_spec.rb +16 -14
- data/spec/spec_helper.rb +2 -2
- data/spec/subscriber_spec.rb +227 -227
- data/spec/subscription_list_spec.rb +31 -31
- data/spec/subscription_spec.rb +37 -36
- data/spec/worker_spec.rb +17 -15
- metadata +7 -6
data/spec/publish_spec.rb
CHANGED
@@ -1,98 +1,98 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require 'spec_helper'
|
4
4
|
|
5
|
+
describe 'Publishing an event' do
|
5
6
|
before(:each) do
|
6
7
|
Timecop.freeze
|
7
|
-
allow(QueueBus).to receive(:generate_uuid).and_return(
|
8
|
+
allow(QueueBus).to receive(:generate_uuid).and_return('idfhlkj')
|
8
9
|
end
|
9
10
|
after(:each) do
|
10
11
|
Timecop.return
|
11
12
|
end
|
12
|
-
let(:bus_attrs)
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
let(:bus_attrs) do
|
14
|
+
{ 'bus_class_proxy' => 'QueueBus::Driver',
|
15
|
+
'bus_published_at' => Time.now.to_i,
|
16
|
+
'bus_id' => "#{Time.now.to_i}-idfhlkj",
|
17
|
+
'bus_app_hostname' => `hostname 2>&1`.strip.sub(/.local/, '') }
|
18
|
+
end
|
16
19
|
|
17
|
-
it
|
18
|
-
hash = {:one => 1,
|
19
|
-
event_name =
|
20
|
+
it 'should add it to Redis' do
|
21
|
+
hash = { :one => 1, 'two' => 'here', 'id' => 12 }
|
22
|
+
event_name = 'event_name'
|
20
23
|
|
21
|
-
val = QueueBus.redis { |redis| redis.lpop(
|
24
|
+
val = QueueBus.redis { |redis| redis.lpop('queue:bus_incoming') }
|
22
25
|
expect(val).to eq(nil)
|
23
26
|
|
24
27
|
QueueBus.publish(event_name, hash)
|
25
28
|
|
26
|
-
val = QueueBus.redis { |redis| redis.lpop(
|
29
|
+
val = QueueBus.redis { |redis| redis.lpop('queue:bus_incoming') }
|
27
30
|
hash = JSON.parse(val)
|
28
|
-
expect(hash[
|
29
|
-
expect(hash[
|
30
|
-
expect(JSON.parse(hash[
|
31
|
-
|
31
|
+
expect(hash['class']).to eq('QueueBus::Worker')
|
32
|
+
expect(hash['args'].size).to eq(1)
|
33
|
+
expect(JSON.parse(hash['args'].first)).to eq({ 'bus_event_type' => event_name, 'two' => 'here', 'one' => 1, 'id' => 12 }.merge(bus_attrs))
|
32
34
|
end
|
33
35
|
|
34
|
-
it
|
35
|
-
hash = {:one => 1,
|
36
|
-
event_name =
|
36
|
+
it 'should use the id if given' do
|
37
|
+
hash = { :one => 1, 'two' => 'here', 'bus_id' => 'app-given' }
|
38
|
+
event_name = 'event_name'
|
37
39
|
|
38
|
-
val = QueueBus.redis { |redis| redis.lpop(
|
40
|
+
val = QueueBus.redis { |redis| redis.lpop('queue:bus_incoming') }
|
39
41
|
expect(val).to eq(nil)
|
40
42
|
|
41
43
|
QueueBus.publish(event_name, hash)
|
42
44
|
|
43
|
-
val = QueueBus.redis { |redis| redis.lpop(
|
45
|
+
val = QueueBus.redis { |redis| redis.lpop('queue:bus_incoming') }
|
44
46
|
hash = JSON.parse(val)
|
45
|
-
expect(hash[
|
46
|
-
expect(hash[
|
47
|
-
expect(JSON.parse(hash[
|
47
|
+
expect(hash['class']).to eq('QueueBus::Worker')
|
48
|
+
expect(hash['args'].size).to eq(1)
|
49
|
+
expect(JSON.parse(hash['args'].first)).to eq({ 'bus_event_type' => event_name, 'two' => 'here', 'one' => 1 }.merge(bus_attrs).merge('bus_id' => 'app-given'))
|
48
50
|
end
|
49
51
|
|
50
|
-
it
|
52
|
+
it 'should add metadata via callback' do
|
51
53
|
myval = 0
|
52
54
|
QueueBus.before_publish = lambda { |att|
|
53
|
-
att[
|
55
|
+
att['mine'] = 4
|
54
56
|
myval += 1
|
55
57
|
}
|
56
58
|
|
57
|
-
hash = {:one => 1,
|
58
|
-
event_name =
|
59
|
+
hash = { :one => 1, 'two' => 'here', 'bus_id' => 'app-given' }
|
60
|
+
event_name = 'event_name'
|
59
61
|
|
60
|
-
val = QueueBus.redis { |redis| redis.lpop(
|
62
|
+
val = QueueBus.redis { |redis| redis.lpop('queue:bus_incoming') }
|
61
63
|
expect(val).to eq(nil)
|
62
64
|
|
63
65
|
QueueBus.publish(event_name, hash)
|
64
66
|
|
65
|
-
|
66
|
-
val = QueueBus.redis { |redis| redis.lpop("queue:bus_incoming") }
|
67
|
+
val = QueueBus.redis { |redis| redis.lpop('queue:bus_incoming') }
|
67
68
|
hash = JSON.parse(val)
|
68
|
-
att = JSON.parse(hash[
|
69
|
-
expect(att[
|
69
|
+
att = JSON.parse(hash['args'].first)
|
70
|
+
expect(att['mine']).to eq(4)
|
70
71
|
expect(myval).to eq(1)
|
71
72
|
end
|
72
73
|
|
73
|
-
it
|
74
|
+
it 'should set the timezone and locale if available' do
|
74
75
|
expect(defined?(I18n)).to be_nil
|
75
76
|
expect(Time.respond_to?(:zone)).to eq(false)
|
76
77
|
|
77
|
-
stub_const(
|
78
|
-
allow(I18n).to receive(:locale).and_return(
|
78
|
+
stub_const('I18n', Class.new)
|
79
|
+
allow(I18n).to receive(:locale).and_return('jp')
|
79
80
|
|
80
|
-
allow(Time).to receive(:zone).and_return(double('zone', :
|
81
|
+
allow(Time).to receive(:zone).and_return(double('zone', name: 'EST'))
|
81
82
|
|
82
|
-
hash = {:one => 1,
|
83
|
-
event_name =
|
83
|
+
hash = { :one => 1, 'two' => 'here', 'bus_id' => 'app-given' }
|
84
|
+
event_name = 'event_name'
|
84
85
|
|
85
|
-
val = QueueBus.redis { |redis| redis.lpop(
|
86
|
+
val = QueueBus.redis { |redis| redis.lpop('queue:bus_incoming') }
|
86
87
|
expect(val).to eq(nil)
|
87
88
|
|
88
89
|
QueueBus.publish(event_name, hash)
|
89
90
|
|
90
|
-
val = QueueBus.redis { |redis| redis.lpop(
|
91
|
+
val = QueueBus.redis { |redis| redis.lpop('queue:bus_incoming') }
|
91
92
|
hash = JSON.parse(val)
|
92
|
-
expect(hash[
|
93
|
-
att = JSON.parse(hash[
|
94
|
-
expect(att[
|
95
|
-
expect(att[
|
93
|
+
expect(hash['class']).to eq('QueueBus::Worker')
|
94
|
+
att = JSON.parse(hash['args'].first)
|
95
|
+
expect(att['bus_locale']).to eq('jp')
|
96
|
+
expect(att['bus_timezone']).to eq('EST')
|
96
97
|
end
|
97
|
-
|
98
98
|
end
|
data/spec/publisher_spec.rb
CHANGED
data/spec/rider_spec.rb
CHANGED
@@ -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
|
7
|
+
it 'should call execute' do
|
6
8
|
expect(QueueBus).to receive(:dispatcher_execute)
|
7
|
-
Rider.perform(
|
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
|
11
|
-
QueueBus.dispatch(
|
12
|
-
subscribe
|
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(
|
18
|
-
Rider.perform(
|
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
|
23
|
-
QueueBus.dispatch(
|
24
|
-
subscribe
|
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(
|
33
|
-
expect(I18n).to receive(:locale=).with(
|
34
|
-
expect(Time).to receive(:zone=).with(
|
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(
|
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
|
data/spec/spec_helper.rb
CHANGED
data/spec/subscriber_spec.rb
CHANGED
@@ -1,270 +1,270 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
let(:attributes) { {"x" => "y"} }
|
5
|
-
let(:bus_attrs) { {"bus_driven_at" => Time.now.to_i} }
|
3
|
+
require 'spec_helper'
|
6
4
|
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
9
|
+
before(:each) do
|
10
|
+
class SubscriberTest1
|
11
|
+
include QueueBus::Subscriber
|
12
|
+
@queue = 'myqueue'
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
application :my_thing
|
15
|
+
subscribe :thing_filter, x: 'y'
|
16
|
+
subscribe :event_sub
|
19
17
|
|
20
|
-
|
21
|
-
|
22
|
-
end
|
18
|
+
def event_sub(attributes)
|
19
|
+
QueueBus::Runner1.run(attributes)
|
23
20
|
end
|
24
21
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
transform :make_an_int
|
22
|
+
def thing_filter(attributes)
|
23
|
+
QueueBus::Runner2.run(attributes)
|
24
|
+
end
|
25
|
+
end
|
30
26
|
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
class SubscriberTest2
|
28
|
+
include QueueBus::Subscriber
|
29
|
+
application :test2
|
30
|
+
subscribe :test2, 'value' => :present
|
31
|
+
transform :make_an_int
|
34
32
|
|
35
|
-
|
36
|
-
|
37
|
-
end
|
33
|
+
def self.make_an_int(attributes)
|
34
|
+
attributes['value'].to_s.length
|
38
35
|
end
|
39
36
|
|
40
|
-
|
41
|
-
|
42
|
-
|
37
|
+
def test2(int)
|
38
|
+
QueueBus::Runner1.run('transformed' => int)
|
39
|
+
end
|
40
|
+
end
|
43
41
|
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
module SubModule
|
43
|
+
class SubscriberTest3
|
44
|
+
include QueueBus::Subscriber
|
47
45
|
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
53
|
-
|
54
|
-
end
|
50
|
+
def test3(attributes)
|
51
|
+
QueueBus::Runner1.run(attributes)
|
55
52
|
end
|
56
53
|
|
57
|
-
|
58
|
-
|
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
|
-
|
65
|
-
|
66
|
-
end
|
59
|
+
class SubscriberTest4
|
60
|
+
include QueueBus::Subscriber
|
67
61
|
|
68
|
-
|
69
|
-
|
62
|
+
subscribe_queue :sub_queue1, :test4
|
63
|
+
end
|
70
64
|
end
|
71
65
|
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
expect(all.size).to eq(1)
|
70
|
+
after(:each) do
|
71
|
+
Timecop.return
|
72
|
+
end
|
82
73
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
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
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
105
|
+
expect(QueueBus::Runner1.attributes).to eq('transformed' => 4)
|
107
106
|
|
108
|
-
|
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
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
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
|
-
|
121
|
-
|
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
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
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
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
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
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
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
|
-
|
253
|
-
|
254
|
-
|
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
|
-
|
267
|
-
|
268
|
-
end
|
266
|
+
expect_any_instance_of(SubscriberTest1).to receive(:event_sub)
|
267
|
+
SubscriberTest1.perform(attributes)
|
269
268
|
end
|
270
269
|
end
|
270
|
+
end
|