resque-bus 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/.rbenv-version +1 -0
- data/.rvmrc +2 -0
- data/Gemfile +6 -0
- data/MIT-LICENSE +20 -0
- data/README.mdown +152 -0
- data/Rakefile +3 -0
- data/lib/resque-bus.rb +250 -0
- data/lib/resque_bus/application.rb +110 -0
- data/lib/resque_bus/dispatch.rb +59 -0
- data/lib/resque_bus/driver.rb +28 -0
- data/lib/resque_bus/local.rb +32 -0
- data/lib/resque_bus/matcher.rb +81 -0
- data/lib/resque_bus/publisher.rb +9 -0
- data/lib/resque_bus/rider.rb +52 -0
- data/lib/resque_bus/server/views/bus.erb +101 -0
- data/lib/resque_bus/server.rb +29 -0
- data/lib/resque_bus/subscriber.rb +60 -0
- data/lib/resque_bus/subscription.rb +50 -0
- data/lib/resque_bus/subscription_list.rb +49 -0
- data/lib/resque_bus/task_manager.rb +50 -0
- data/lib/resque_bus/tasks.rb +105 -0
- data/lib/resque_bus/util.rb +42 -0
- data/lib/resque_bus/version.rb +5 -0
- data/lib/tasks/resquebus.rake +2 -0
- data/resque-bus.gemspec +34 -0
- data/spec/application_spec.rb +152 -0
- data/spec/dispatch_spec.rb +76 -0
- data/spec/driver_spec.rb +100 -0
- data/spec/integration_spec.rb +53 -0
- data/spec/matcher_spec.rb +143 -0
- data/spec/publish_at_spec.rb +75 -0
- data/spec/publish_spec.rb +48 -0
- data/spec/redis_spec.rb +13 -0
- data/spec/rider_spec.rb +83 -0
- data/spec/spec_helper.rb +75 -0
- data/spec/subscriber_spec.rb +233 -0
- data/spec/subscription_list_spec.rb +43 -0
- data/spec/subscription_spec.rb +53 -0
- metadata +232 -0
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ResqueBus
|
4
|
+
describe Application do
|
5
|
+
describe ".all" do
|
6
|
+
it "should return empty array when none" do
|
7
|
+
Application.all.should == []
|
8
|
+
end
|
9
|
+
it "should return registered applications when there are some" do
|
10
|
+
Application.new("One").subscribe(test_list(test_sub("fdksjh")))
|
11
|
+
Application.new("Two").subscribe(test_list(test_sub("fdklhf")))
|
12
|
+
Application.new("Three").subscribe(test_list(test_sub("fkld")))
|
13
|
+
|
14
|
+
Application.all.collect(&:app_key).should =~ ["one", "two", "three"]
|
15
|
+
|
16
|
+
Application.new("two").unsubscribe
|
17
|
+
Application.all.collect(&:app_key).should =~ ["one", "three"]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".new" do
|
22
|
+
it "should have a key" do
|
23
|
+
Application.new("something").app_key.should == "something"
|
24
|
+
|
25
|
+
Application.new("some thing").app_key.should == "some_thing"
|
26
|
+
Application.new("some-thing").app_key.should == "some_thing"
|
27
|
+
Application.new("some_thing").app_key.should == "some_thing"
|
28
|
+
Application.new("Some Thing").app_key.should == "some_thing"
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should raise an error if not valid" do
|
32
|
+
lambda {
|
33
|
+
Application.new("")
|
34
|
+
}.should raise_error
|
35
|
+
|
36
|
+
lambda {
|
37
|
+
Application.new(nil)
|
38
|
+
}.should raise_error
|
39
|
+
|
40
|
+
lambda {
|
41
|
+
Application.new("/")
|
42
|
+
}.should raise_error
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#read_redis_hash" do
|
47
|
+
it "should handle old and new values" do
|
48
|
+
|
49
|
+
ResqueBus.redis.hset("resquebus_app:myapp", "new_one", Resque.encode("queue_name" => "x", "bus_event_type" => "event_name"))
|
50
|
+
ResqueBus.redis.hset("resquebus_app:myapp", "old_one", "oldqueue_name")
|
51
|
+
app = Application.new("myapp")
|
52
|
+
val = app.send(:read_redis_hash)
|
53
|
+
val.should == {"new_one" => {"queue_name" => "x", "bus_event_type" => "event_name"}, "old_one" => "oldqueue_name"}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#subscribe" do
|
58
|
+
let(:sub1) { test_sub("event_one", "default") }
|
59
|
+
let(:sub2) { test_sub("event_two", "default") }
|
60
|
+
let(:sub3) { test_sub("event_three", "other") }
|
61
|
+
it "should add array to redis" do
|
62
|
+
ResqueBus.redis.get("resquebus_app:myapp").should be_nil
|
63
|
+
Application.new("myapp").subscribe(test_list(sub1, sub2))
|
64
|
+
|
65
|
+
ResqueBus.redis.hgetall("resquebus_app:myapp").should == {"event_two"=>"{\"queue_name\":\"default\",\"key\":\"event_two\",\"class\":\"::ResqueBus::Rider\",\"matcher\":{\"bus_event_type\":\"event_two\"}}",
|
66
|
+
"event_one"=>"{\"queue_name\":\"default\",\"key\":\"event_one\",\"class\":\"::ResqueBus::Rider\",\"matcher\":{\"bus_event_type\":\"event_one\"}}"}
|
67
|
+
ResqueBus.redis.hkeys("resquebus_app:myapp").should =~ ["event_one", "event_two"]
|
68
|
+
ResqueBus.redis.smembers("resquebus_apps").should =~ ["myapp"]
|
69
|
+
end
|
70
|
+
it "should add string to redis" do
|
71
|
+
ResqueBus.redis.get("resquebus_app:myapp").should be_nil
|
72
|
+
Application.new("myapp").subscribe(test_list(sub1))
|
73
|
+
|
74
|
+
ResqueBus.redis.hgetall("resquebus_app:myapp").should == {"event_one"=>"{\"queue_name\":\"default\",\"key\":\"event_one\",\"class\":\"::ResqueBus::Rider\",\"matcher\":{\"bus_event_type\":\"event_one\"}}"}
|
75
|
+
ResqueBus.redis.hkeys("resquebus_app:myapp").should =~ ["event_one"]
|
76
|
+
ResqueBus.redis.smembers("resquebus_apps").should =~ ["myapp"]
|
77
|
+
end
|
78
|
+
it "should multiple queues to redis" do
|
79
|
+
ResqueBus.redis.get("resquebus_app:myapp").should be_nil
|
80
|
+
Application.new("myapp").subscribe(test_list(sub1, sub2, sub3))
|
81
|
+
ResqueBus.redis.hgetall("resquebus_app:myapp").should == {"event_two"=>"{\"queue_name\":\"default\",\"key\":\"event_two\",\"class\":\"::ResqueBus::Rider\",\"matcher\":{\"bus_event_type\":\"event_two\"}}", "event_one"=>"{\"queue_name\":\"default\",\"key\":\"event_one\",\"class\":\"::ResqueBus::Rider\",\"matcher\":{\"bus_event_type\":\"event_one\"}}",
|
82
|
+
"event_three"=>"{\"queue_name\":\"other\",\"key\":\"event_three\",\"class\":\"::ResqueBus::Rider\",\"matcher\":{\"bus_event_type\":\"event_three\"}}"}
|
83
|
+
ResqueBus.redis.hkeys("resquebus_app:myapp").should =~ ["event_three", "event_two", "event_one"]
|
84
|
+
ResqueBus.redis.smembers("resquebus_apps").should =~ ["myapp"]
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should do nothing if nil or empty" do
|
88
|
+
|
89
|
+
ResqueBus.redis.get("resquebus_app:myapp").should be_nil
|
90
|
+
|
91
|
+
Application.new("myapp").subscribe(nil)
|
92
|
+
ResqueBus.redis.get("resquebus_app:myapp").should be_nil
|
93
|
+
|
94
|
+
Application.new("myapp").subscribe([])
|
95
|
+
ResqueBus.redis.get("resquebus_app:myapp").should be_nil
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should call unsubscribe" do
|
99
|
+
app = Application.new("myapp")
|
100
|
+
app.should_receive(:unsubscribe)
|
101
|
+
app.subscribe([])
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "#unsubscribe" do
|
106
|
+
it "should remove items" do
|
107
|
+
ResqueBus.redis.sadd("resquebus_apps", "myapp")
|
108
|
+
ResqueBus.redis.sadd("resquebus_apps", "other")
|
109
|
+
ResqueBus.redis.hset("resquebus_app:myapp", "event_one", "myapp_default")
|
110
|
+
|
111
|
+
Application.new("myapp").unsubscribe
|
112
|
+
|
113
|
+
ResqueBus.redis.smembers("resquebus_apps").should == ["other"]
|
114
|
+
ResqueBus.redis.get("resquebus_app:myapp").should be_nil
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#subscription_matches" do
|
119
|
+
it "should return if it is there" do
|
120
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"three").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should == []
|
121
|
+
|
122
|
+
subs = test_list(test_sub("one_x"), test_sub("one_y"), test_sub("one"), test_sub("two"))
|
123
|
+
Application.new("myapp").subscribe(subs)
|
124
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"three").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should == []
|
125
|
+
|
126
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"two").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["myapp", "two", "default", "::ResqueBus::Rider"]]
|
127
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"one").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["myapp", "one", "default", "::ResqueBus::Rider"]]
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should handle * wildcards" do
|
131
|
+
subs = test_list(test_sub("one.+"), test_sub("one"), test_sub("one_.*"), test_sub("two"))
|
132
|
+
Application.new("myapp").subscribe(subs)
|
133
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"three").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should == []
|
134
|
+
|
135
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"onex").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["myapp", "one.+", "default", "::ResqueBus::Rider"]]
|
136
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"one").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["myapp", "one", "default", "::ResqueBus::Rider"]]
|
137
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"one_x").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["myapp", "one.+","default", "::ResqueBus::Rider"], ["myapp", "one_.*", "default", "::ResqueBus::Rider"]]
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should handle actual regular expressions" do
|
141
|
+
subs = test_list(test_sub(/one.+/), test_sub("one"), test_sub(/one_.*/), test_sub("two"))
|
142
|
+
Application.new("myapp").subscribe(subs)
|
143
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"three").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should == []
|
144
|
+
|
145
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"onex").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["myapp", "(?-mix:one.+)", "default", "::ResqueBus::Rider"]]
|
146
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"donex").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["myapp", "(?-mix:one.+)", "default", "::ResqueBus::Rider"]]
|
147
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"one").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["myapp", "one" ,"default", "::ResqueBus::Rider"]]
|
148
|
+
Application.new("myapp").subscription_matches("bus_event_type"=>"one_x").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["myapp", "(?-mix:one.+)", "default", "::ResqueBus::Rider"], ["myapp", "(?-mix:one_.*)", "default", "::ResqueBus::Rider"]]
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ResqueBus
|
4
|
+
describe Dispatch do
|
5
|
+
it "should not start with any applications" do
|
6
|
+
Dispatch.new("d").subscriptions.size.should == 0
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should register code to run and execute it" do
|
10
|
+
dispatch = Dispatch.new("d")
|
11
|
+
dispatch.subscribe("my_event") do |attrs|
|
12
|
+
Runner1.run(attrs)
|
13
|
+
end
|
14
|
+
sub = dispatch.subscriptions.key("my_event")
|
15
|
+
sub.send(:executor).is_a?(Proc).should == true
|
16
|
+
|
17
|
+
Runner.value.should == 0
|
18
|
+
dispatch.execute("my_event", {"bus_event_type" => "my_event", "ok" => true})
|
19
|
+
Runner1.value.should == 1
|
20
|
+
Runner1.attributes.should == {"bus_event_type" => "my_event", "ok" => true}
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should not crash if not there" do
|
25
|
+
lambda {
|
26
|
+
Dispatch.new("d").execute("fdkjh", "bus_event_type" => "fdkjh")
|
27
|
+
}.should_not raise_error
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "Top Level" do
|
31
|
+
before(:each) do
|
32
|
+
ResqueBus.dispatch("testit") do
|
33
|
+
subscribe "event1" do |attributes|
|
34
|
+
Runner2.run(attributes)
|
35
|
+
end
|
36
|
+
|
37
|
+
subscribe "event2" do
|
38
|
+
Runner2.run({})
|
39
|
+
end
|
40
|
+
|
41
|
+
high "event3" do
|
42
|
+
Runner2.run({})
|
43
|
+
end
|
44
|
+
|
45
|
+
low /^patt.+ern/ do
|
46
|
+
Runner.run({})
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should register and run" do
|
52
|
+
Runner2.value.should == 0
|
53
|
+
ResqueBus.dispatcher_execute("testit", "event2", "bus_event_type" => "event2")
|
54
|
+
Runner2.value.should == 1
|
55
|
+
ResqueBus.dispatcher_execute("testit", "event1", "bus_event_type" => "event1")
|
56
|
+
Runner2.value.should == 2
|
57
|
+
ResqueBus.dispatcher_execute("testit", "event1", "bus_event_type" => "event1")
|
58
|
+
Runner2.value.should == 3
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should return the subscriptions" do
|
62
|
+
dispatcher = ResqueBus.dispatcher_by_key("testit")
|
63
|
+
subs = dispatcher.subscriptions.all
|
64
|
+
tuples = subs.collect{ |sub| [sub.key, sub.queue_name]}
|
65
|
+
tuples.should =~ [ ["event1", "testit_default"],
|
66
|
+
["event2", "testit_default"],
|
67
|
+
["event3", "testit_high"],
|
68
|
+
[ "(?-mix:^patt.+ern)", "testit_low"]
|
69
|
+
]
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
data/spec/driver_spec.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ResqueBus
|
4
|
+
describe Driver do
|
5
|
+
before(:each) do
|
6
|
+
Application.new("app1").subscribe(test_list(test_sub("event1"), test_sub("event2"), test_sub("event3")))
|
7
|
+
Application.new("app2").subscribe(test_list(test_sub("event2","other"), test_sub("event4", "more")))
|
8
|
+
Application.new("app3").subscribe(test_list(test_sub("event[45]"), test_sub("event5"), test_sub("event6")))
|
9
|
+
Timecop.freeze
|
10
|
+
end
|
11
|
+
after(:each) do
|
12
|
+
Timecop.return
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:bus_attrs) { {"bus_driven_at" => Time.now.to_i, "bus_rider_class_name"=>"::ResqueBus::Rider"} }
|
16
|
+
|
17
|
+
describe ".subscription_matches" do
|
18
|
+
it "return empty array when none" do
|
19
|
+
Driver.subscription_matches("bus_event_type" => "else").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should == []
|
20
|
+
Driver.subscription_matches("bus_event_type" => "event").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should == []
|
21
|
+
end
|
22
|
+
it "should return a match" do
|
23
|
+
Driver.subscription_matches("bus_event_type" => "event1").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["app1", "event1", "default", "::ResqueBus::Rider"]]
|
24
|
+
Driver.subscription_matches("bus_event_type" => "event6").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["app3", "event6", "default", "::ResqueBus::Rider"]]
|
25
|
+
end
|
26
|
+
it "should match multiple apps" do
|
27
|
+
Driver.subscription_matches("bus_event_type" => "event2").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["app1", "event2", "default", "::ResqueBus::Rider"], ["app2", "event2", "other", "::ResqueBus::Rider"]]
|
28
|
+
end
|
29
|
+
it "should match multiple apps with patterns" do
|
30
|
+
Driver.subscription_matches("bus_event_type" => "event4").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["app3", "event[45]", "default", "::ResqueBus::Rider"], ["app2", "event4", "more", "::ResqueBus::Rider"]]
|
31
|
+
end
|
32
|
+
it "should match multiple events in same app" do
|
33
|
+
Driver.subscription_matches("bus_event_type" => "event5").collect{|s| [s.app_key, s.key, s.queue_name, s.class_name]}.should =~ [["app3", "event[45]", "default", "::ResqueBus::Rider"], ["app3", "event5", "default", "::ResqueBus::Rider"]]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe ".peform" do
|
38
|
+
let(:attributes) { {"x" => "y"} }
|
39
|
+
|
40
|
+
before(:each) do
|
41
|
+
ResqueBus.redis.smembers("queues").should == []
|
42
|
+
ResqueBus.redis.lpop("queue:app1_default").should be_nil
|
43
|
+
ResqueBus.redis.lpop("queue:app2_default").should be_nil
|
44
|
+
ResqueBus.redis.lpop("queue:app3_default").should be_nil
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should do nothing when empty" do
|
48
|
+
Driver.perform(attributes.merge("bus_event_type" => "else"))
|
49
|
+
ResqueBus.redis.smembers("queues").should == []
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should queue up the riders in redis" do
|
53
|
+
ResqueBus.redis.lpop("queue:app1_default").should be_nil
|
54
|
+
Driver.perform(attributes.merge("bus_event_type" => "event1"))
|
55
|
+
ResqueBus.redis.smembers("queues").should =~ ["default"]
|
56
|
+
|
57
|
+
hash = JSON.parse(ResqueBus.redis.lpop("queue:default"))
|
58
|
+
hash["class"].should == "::ResqueBus::Rider"
|
59
|
+
hash["args"].should == [ {"bus_rider_app_key"=>"app1", "x" => "y", "bus_event_type" => "event1", "bus_rider_sub_key"=>"event1", "bus_rider_queue" => "default"}.merge(bus_attrs) ]
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should queue up to multiple" do
|
63
|
+
Driver.perform(attributes.merge("bus_event_type" => "event4"))
|
64
|
+
ResqueBus.redis.smembers("queues").should =~ ["default", "more"]
|
65
|
+
|
66
|
+
hash = JSON.parse(ResqueBus.redis.lpop("queue:more"))
|
67
|
+
hash["class"].should == "::ResqueBus::Rider"
|
68
|
+
hash["args"].should == [ {"bus_rider_app_key"=>"app2", "x" => "y", "bus_event_type" => "event4", "bus_rider_sub_key"=>"event4", "bus_rider_queue" => "more"}.merge(bus_attrs) ]
|
69
|
+
|
70
|
+
hash = JSON.parse(ResqueBus.redis.lpop("queue:default"))
|
71
|
+
hash["class"].should == "::ResqueBus::Rider"
|
72
|
+
hash["args"].should == [ {"bus_rider_app_key"=>"app3", "x" => "y", "bus_event_type" => "event4", "bus_rider_sub_key"=>"event[45]", "bus_rider_queue" => "default"}.merge(bus_attrs) ]
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should queue up to the same" do
|
76
|
+
Driver.perform(attributes.merge("bus_event_type" => "event5"))
|
77
|
+
ResqueBus.redis.smembers("queues").should =~ ["default"]
|
78
|
+
|
79
|
+
ResqueBus.redis.llen("queue:default").should == 2
|
80
|
+
|
81
|
+
hash = JSON.parse(ResqueBus.redis.lpop("queue:default"))
|
82
|
+
hash["class"].should == "::ResqueBus::Rider"
|
83
|
+
hash["args"][0].should == {"bus_rider_app_key"=>"app3", "x" => "y", "bus_event_type" => "event5", "bus_rider_sub_key"=>"event5", "bus_rider_queue" => "default"}.merge(bus_attrs)
|
84
|
+
first = hash["args"][0]["bus_rider_sub_key"]
|
85
|
+
|
86
|
+
hash = JSON.parse(ResqueBus.redis.lpop("queue:default"))
|
87
|
+
hash["class"].should == "::ResqueBus::Rider"
|
88
|
+
hash["args"][0].should == {"bus_rider_app_key"=>"app3", "x" => "y", "bus_event_type" => "event5", "bus_rider_sub_key"=>"event[45]", "bus_rider_queue" => "default"}.merge(bus_attrs)
|
89
|
+
second= hash["args"][0]["bus_rider_sub_key"]
|
90
|
+
|
91
|
+
if first == "event[45]"
|
92
|
+
second.should == "event5"
|
93
|
+
else
|
94
|
+
first.should == "event5"
|
95
|
+
second.should == "event[45]"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ResqueBus
|
4
|
+
describe "Integration" do
|
5
|
+
it "should round trip attributes" do
|
6
|
+
write1 = Subscription.new("default", "key1", "MyClass1", {"bus_event_type" => "event_one"})
|
7
|
+
write2 = Subscription.new("else_ok", "key2", "MyClass2", {"bus_event_type" => /^[ab]here/}) #regex
|
8
|
+
|
9
|
+
write1.matches?("bus_event_type" => "event_one").should == true
|
10
|
+
write1.matches?("bus_event_type" => "event_one1").should == false
|
11
|
+
write1.matches?("bus_event_type" => "aevent_one").should == false
|
12
|
+
|
13
|
+
write2.matches?("bus_event_type" => "ahere").should == true
|
14
|
+
write2.matches?("bus_event_type" => "bhere").should == true
|
15
|
+
write2.matches?("bus_event_type" => "qhere").should == false
|
16
|
+
write2.matches?("bus_event_type" => "abhere").should == false
|
17
|
+
write2.matches?("bus_event_type" => "[ab]here").should == false
|
18
|
+
|
19
|
+
write = SubscriptionList.new
|
20
|
+
write.add(write1)
|
21
|
+
write.add(write2)
|
22
|
+
|
23
|
+
app = Application.new("test")
|
24
|
+
app.subscribe(write)
|
25
|
+
|
26
|
+
ResqueBus.send(:reset) # reset to make sure we read from redis
|
27
|
+
app = Application.new("test")
|
28
|
+
read = app.send(:subscriptions)
|
29
|
+
|
30
|
+
read.size.should == 2
|
31
|
+
read1 = read.key("key1")
|
32
|
+
read2 = read.key("key2")
|
33
|
+
read1.should_not be_nil
|
34
|
+
read2.should_not be_nil
|
35
|
+
|
36
|
+
read1.queue_name.should == "default"
|
37
|
+
read1.class_name.should == "MyClass1"
|
38
|
+
read2.queue_name.should == "else_ok"
|
39
|
+
read2.class_name.should == "MyClass2"
|
40
|
+
|
41
|
+
read1.matches?("bus_event_type" => "event_one").should == true
|
42
|
+
read1.matches?("bus_event_type" => "event_one1").should == false
|
43
|
+
read1.matches?("bus_event_type" => "aevent_one").should == false
|
44
|
+
|
45
|
+
read2.matches?("bus_event_type" => "ahere").should == true
|
46
|
+
read2.matches?("bus_event_type" => "bhere").should == true
|
47
|
+
read2.matches?("bus_event_type" => "qhere").should == false
|
48
|
+
read2.matches?("bus_event_type" => "abhere").should == false
|
49
|
+
read2.matches?("bus_event_type" => "[ab]here").should == false
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module ResqueBus
|
4
|
+
describe Matcher do
|
5
|
+
it "should already return false on empty filters" do
|
6
|
+
matcher = Matcher.new({})
|
7
|
+
matcher.matches?({}).should == false
|
8
|
+
matcher.matches?(nil).should == false
|
9
|
+
matcher.matches?("name" => "val").should == false
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should not crash if nil inputs" do
|
13
|
+
matcher = Matcher.new("name" => "val")
|
14
|
+
matcher.matches?(nil).should == false
|
15
|
+
end
|
16
|
+
|
17
|
+
it "string filter to/from redis" do
|
18
|
+
matcher = Matcher.new("name" => "val")
|
19
|
+
matcher.matches?("name" => "val").should == true
|
20
|
+
matcher.matches?("name" => " val").should == false
|
21
|
+
matcher.matches?("name" => "zval").should == false
|
22
|
+
end
|
23
|
+
|
24
|
+
it "regex filter" do
|
25
|
+
matcher = Matcher.new("name" => /^[cb]a+t/)
|
26
|
+
matcher.matches?("name" => "cat").should == true
|
27
|
+
matcher.matches?("name" => "bat").should == true
|
28
|
+
matcher.matches?("name" => "caaaaat").should == true
|
29
|
+
matcher.matches?("name" => "ct").should == false
|
30
|
+
matcher.matches?("name" => "bcat").should == false
|
31
|
+
end
|
32
|
+
|
33
|
+
it "present filter" do
|
34
|
+
matcher = Matcher.new("name" => :present)
|
35
|
+
matcher.matches?("name" => "").should == false
|
36
|
+
matcher.matches?("name" => "cat").should == true
|
37
|
+
matcher.matches?("name" => "bear").should == true
|
38
|
+
matcher.matches?("other" => "bear").should == false
|
39
|
+
end
|
40
|
+
|
41
|
+
it "blank filter" do
|
42
|
+
matcher = Matcher.new("name" => :blank)
|
43
|
+
matcher.matches?("name" => nil).should == true
|
44
|
+
matcher.matches?("other" => "bear").should == true
|
45
|
+
matcher.matches?("name" => "").should == true
|
46
|
+
matcher.matches?("name" => " ").should == true
|
47
|
+
matcher.matches?("name" => "bear").should == false
|
48
|
+
matcher.matches?("name" => " s ").should == false
|
49
|
+
end
|
50
|
+
|
51
|
+
it "nil filter" do
|
52
|
+
matcher = Matcher.new("name" => :nil)
|
53
|
+
matcher.matches?("name" => nil).should == true
|
54
|
+
matcher.matches?("other" => "bear").should == true
|
55
|
+
matcher.matches?("name" => "").should == false
|
56
|
+
matcher.matches?("name" => " ").should == false
|
57
|
+
matcher.matches?("name" => "bear").should == false
|
58
|
+
end
|
59
|
+
|
60
|
+
it "key filter" do
|
61
|
+
matcher = Matcher.new("name" => :key)
|
62
|
+
matcher.matches?("name" => nil).should == true
|
63
|
+
matcher.matches?("other" => "bear").should == false
|
64
|
+
matcher.matches?("name" => "").should == true
|
65
|
+
matcher.matches?("name" => " ").should == true
|
66
|
+
matcher.matches?("name" => "bear").should == true
|
67
|
+
end
|
68
|
+
|
69
|
+
it "empty filter" do
|
70
|
+
matcher = Matcher.new("name" => :empty)
|
71
|
+
matcher.matches?("name" => nil).should == false
|
72
|
+
matcher.matches?("other" => "bear").should == false
|
73
|
+
matcher.matches?("name" => "").should == true
|
74
|
+
matcher.matches?("name" => " ").should == false
|
75
|
+
matcher.matches?("name" => "bear").should == false
|
76
|
+
matcher.matches?("name" => " s ").should == false
|
77
|
+
end
|
78
|
+
|
79
|
+
it "value filter" do
|
80
|
+
matcher = Matcher.new("name" => :value)
|
81
|
+
matcher.matches?("name" => nil).should == false
|
82
|
+
matcher.matches?("other" => "bear").should == false
|
83
|
+
matcher.matches?("name" => "").should == true
|
84
|
+
matcher.matches?("name" => " ").should == true
|
85
|
+
matcher.matches?("name" => "bear").should == true
|
86
|
+
matcher.matches?("name" => " s ").should == true
|
87
|
+
end
|
88
|
+
|
89
|
+
it "multiple filters" do
|
90
|
+
matcher = Matcher.new("name" => /^[cb]a+t/, "state" => "sleeping")
|
91
|
+
matcher.matches?("state" => "sleeping", "name" => "cat").should == true
|
92
|
+
matcher.matches?("state" => "awake", "name" => "cat").should == false
|
93
|
+
matcher.matches?("state" => "sleeping", "name" => "bat").should == true
|
94
|
+
matcher.matches?("state" => "sleeping", "name" => "bear").should == false
|
95
|
+
matcher.matches?("state" => "awake", "name" => "bear").should == false
|
96
|
+
end
|
97
|
+
|
98
|
+
it "regex should go back and forth into redis" do
|
99
|
+
matcher = Matcher.new("name" => /^[cb]a+t/)
|
100
|
+
matcher.matches?("name" => "cat").should == true
|
101
|
+
matcher.matches?("name" => "bat").should == true
|
102
|
+
matcher.matches?("name" => "caaaaat").should == true
|
103
|
+
matcher.matches?("name" => "ct").should == false
|
104
|
+
matcher.matches?("name" => "bcat").should == false
|
105
|
+
|
106
|
+
ResqueBus.redis.set("temp1", Resque.encode(matcher.to_redis))
|
107
|
+
redis = ResqueBus.redis.get("temp1")
|
108
|
+
matcher = Matcher.new(Resque.decode(redis))
|
109
|
+
matcher.matches?("name" => "cat").should == true
|
110
|
+
matcher.matches?("name" => "bat").should == true
|
111
|
+
matcher.matches?("name" => "caaaaat").should == true
|
112
|
+
matcher.matches?("name" => "ct").should == false
|
113
|
+
matcher.matches?("name" => "bcat").should == false
|
114
|
+
|
115
|
+
ResqueBus.redis.set("temp2", Resque.encode(matcher.to_redis))
|
116
|
+
redis = ResqueBus.redis.get("temp2")
|
117
|
+
matcher = Matcher.new(Resque.decode(redis))
|
118
|
+
matcher.matches?("name" => "cat").should == true
|
119
|
+
matcher.matches?("name" => "bat").should == true
|
120
|
+
matcher.matches?("name" => "caaaaat").should == true
|
121
|
+
matcher.matches?("name" => "ct").should == false
|
122
|
+
matcher.matches?("name" => "bcat").should == false
|
123
|
+
end
|
124
|
+
|
125
|
+
it "special value should go back and forth into redis" do
|
126
|
+
matcher = Matcher.new("name" => :blank)
|
127
|
+
matcher.matches?("name" => "cat").should == false
|
128
|
+
matcher.matches?("name" => "").should == true
|
129
|
+
|
130
|
+
ResqueBus.redis.set("temp1", Resque.encode(matcher.to_redis))
|
131
|
+
redis = ResqueBus.redis.get("temp1")
|
132
|
+
matcher = Matcher.new(Resque.decode(redis))
|
133
|
+
matcher.matches?("name" => "cat").should == false
|
134
|
+
matcher.matches?("name" => "").should == true
|
135
|
+
|
136
|
+
ResqueBus.redis.set("temp2", Resque.encode(matcher.to_redis))
|
137
|
+
redis = ResqueBus.redis.get("temp2")
|
138
|
+
matcher = Matcher.new(Resque.decode(redis))
|
139
|
+
matcher.matches?("name" => "cat").should == false
|
140
|
+
matcher.matches?("name" => "").should == true
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Publishing an event in the future" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
Timecop.freeze(now)
|
7
|
+
ResqueBus.stub(:generate_uuid).and_return("idfhlkj")
|
8
|
+
end
|
9
|
+
after(:each) do
|
10
|
+
Timecop.return
|
11
|
+
end
|
12
|
+
let(:delayed_attrs) { {"bus_delayed_until" => future.to_i,
|
13
|
+
"bus_id" => "#{now.to_i}-idfhlkj",
|
14
|
+
"created_at" => now.to_i,
|
15
|
+
"bus_app_hostname" => `hostname 2>&1`.strip.sub(/.local/,'')} }
|
16
|
+
|
17
|
+
let(:bus_attrs) { delayed_attrs.merge({"bus_published_at" => worktime.to_i})}
|
18
|
+
let(:now) { Time.parse("01/01/2013 5:00")}
|
19
|
+
let(:future) { Time.at(now.to_i + 60) }
|
20
|
+
let(:worktime) {Time.at(future.to_i + 1)}
|
21
|
+
|
22
|
+
it "should add it to Redis" do
|
23
|
+
hash = {:one => 1, "two" => "here", "id" => 12 }
|
24
|
+
event_name = "event_name"
|
25
|
+
ResqueBus.publish_at(future, event_name, hash)
|
26
|
+
|
27
|
+
schedule = ResqueBus.redis.zrange("delayed_queue_schedule", 0, 1)
|
28
|
+
schedule.should == [future.to_i.to_s]
|
29
|
+
|
30
|
+
val = ResqueBus.redis.lpop("delayed:#{future.to_i}")
|
31
|
+
hash = JSON.parse(val)
|
32
|
+
|
33
|
+
hash["class"].should == "ResqueBus::Publisher"
|
34
|
+
hash["args"].should == [ "event_name", {"bus_event_type"=>"event_name", "two"=>"here", "one"=>1, "id" => 12}.merge(delayed_attrs) ]
|
35
|
+
hash["queue"].should == "resquebus_incoming"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should move it to the real queue when processing" do
|
39
|
+
hash = {:one => 1, "two" => "here", "id" => 12 }
|
40
|
+
event_name = "event_name"
|
41
|
+
|
42
|
+
val = ResqueBus.redis.lpop("queue:resquebus_incoming")
|
43
|
+
val.should == nil
|
44
|
+
|
45
|
+
ResqueBus.publish_at(future, event_name, hash)
|
46
|
+
|
47
|
+
val = ResqueBus.redis.lpop("queue:resquebus_incoming")
|
48
|
+
val.should == nil # nothing really added
|
49
|
+
|
50
|
+
# process sceduler now
|
51
|
+
Resque::Scheduler.handle_delayed_items
|
52
|
+
|
53
|
+
val = ResqueBus.redis.lpop("queue:resquebus_incoming")
|
54
|
+
val.should == nil # nothing added yet
|
55
|
+
|
56
|
+
# process scheduler in future
|
57
|
+
Timecop.freeze(worktime) do
|
58
|
+
Resque::Scheduler.handle_delayed_items
|
59
|
+
|
60
|
+
# added
|
61
|
+
val = ResqueBus.redis.lpop("queue:resquebus_incoming")
|
62
|
+
hash = JSON.parse(val)
|
63
|
+
hash["class"].should == "ResqueBus::Publisher"
|
64
|
+
hash["args"].should == [ "event_name", {"bus_event_type"=>"event_name", "two"=>"here", "one"=>1, "id" => 12}.merge(delayed_attrs) ]
|
65
|
+
|
66
|
+
ResqueBus::Publisher.perform(*hash["args"])
|
67
|
+
|
68
|
+
val = ResqueBus.redis.lpop("queue:resquebus_incoming")
|
69
|
+
hash = JSON.parse(val)
|
70
|
+
hash["class"].should == "ResqueBus::Driver"
|
71
|
+
hash["args"].should == [ {"bus_event_type"=>"event_name", "two"=>"here", "one"=>1, "id" => 12}.merge(bus_attrs) ]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Publishing an event" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
Timecop.freeze
|
7
|
+
ResqueBus.stub(:generate_uuid).and_return("idfhlkj")
|
8
|
+
end
|
9
|
+
after(:each) do
|
10
|
+
Timecop.return
|
11
|
+
end
|
12
|
+
let(:bus_attrs) { {"bus_published_at" => Time.now.to_i,
|
13
|
+
"created_at" => Time.now.to_i,
|
14
|
+
"bus_id"=>"#{Time.now.to_i}-idfhlkj",
|
15
|
+
"bus_app_hostname" => `hostname 2>&1`.strip.sub(/.local/,'')} }
|
16
|
+
|
17
|
+
it "should add it to Redis" do
|
18
|
+
hash = {:one => 1, "two" => "here", "id" => 12 }
|
19
|
+
event_name = "event_name"
|
20
|
+
|
21
|
+
val = ResqueBus.redis.lpop("queue:resquebus_incoming")
|
22
|
+
val.should == nil
|
23
|
+
|
24
|
+
ResqueBus.publish(event_name, hash)
|
25
|
+
|
26
|
+
val = ResqueBus.redis.lpop("queue:resquebus_incoming")
|
27
|
+
hash = JSON.parse(val)
|
28
|
+
hash["class"].should == "ResqueBus::Driver"
|
29
|
+
hash["args"].should == [ {"bus_event_type" => event_name, "two"=>"here", "one"=>1, "id" => 12}.merge(bus_attrs) ]
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should use the id if given" do
|
34
|
+
hash = {:one => 1, "two" => "here", "bus_id" => "app-given" }
|
35
|
+
event_name = "event_name"
|
36
|
+
|
37
|
+
val = ResqueBus.redis.lpop("queue:resquebus_incoming")
|
38
|
+
val.should == nil
|
39
|
+
|
40
|
+
ResqueBus.publish(event_name, hash)
|
41
|
+
|
42
|
+
val = ResqueBus.redis.lpop("queue:resquebus_incoming")
|
43
|
+
hash = JSON.parse(val)
|
44
|
+
hash["class"].should == "ResqueBus::Driver"
|
45
|
+
hash["args"].should == [ {"bus_event_type" => event_name, "two"=>"here", "one"=>1}.merge(bus_attrs).merge("bus_id" => 'app-given') ]
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|