kiqit 0.0.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/.travis.yml +3 -0
- data/README.md +193 -0
- data/Rakefile +9 -1
- data/init.rb +2 -0
- data/kiqit.gemspec +12 -11
- data/lib/kiqit/args_parser.rb +56 -0
- data/lib/kiqit/config.rb +11 -0
- data/lib/kiqit/job_creator.rb +28 -0
- data/lib/kiqit/payload_helper.rb +20 -0
- data/lib/kiqit/plugins.rb +17 -0
- data/lib/kiqit/railtie.rb +5 -0
- data/lib/kiqit/version.rb +1 -1
- data/lib/kiqit/workers/active_record/lone_worker.rb +19 -0
- data/lib/kiqit/workers/active_record/worker.rb +15 -0
- data/lib/kiqit/workers/base.rb +21 -0
- data/lib/kiqit/workers/objects/lone_worker.rb +16 -0
- data/lib/kiqit/workers/objects/worker.rb +13 -0
- data/lib/kiqit.rb +30 -2
- data/lib/object_perform_later.rb +36 -0
- data/lib/resque_mailer_patch.rb +17 -0
- data/lib/sidekiq/plugins/later/method.rb +88 -0
- data/license +20 -0
- data/spec/lib/kiqit/args_parser_spec.rb +96 -0
- data/spec/lib/kiqit/config_spec.rb +11 -0
- data/spec/lib/kiqit/job_creator_spec.rb +23 -0
- data/spec/lib/kiqit/payload_helper_spec.rb +21 -0
- data/spec/lib/kiqit/plugins_spec.rb +33 -0
- data/spec/lib/kiqit/workers/active_record/worker_spec.rb +5 -0
- data/spec/lib/kiqit/workers/objects/worker_spec.rb +74 -0
- data/spec/lib/object_perform_later_spec.rb +141 -0
- data/spec/lib/perform_later_spec.rb +5 -0
- data/spec/lib/sidekiq/plugins/later/method_spec.rb +121 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/support/database_connection.rb +1 -0
- data/spec/support/database_models.rb +30 -0
- data/spec/support/db/.blank.sqlite3 +0 -0
- data/spec/support/db/test.sqlite3 +0 -0
- metadata +180 -110
- data/.rvmrc +0 -1
data/license
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) Avi Tzurel
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class CustomFinder
|
4
|
+
def self.find(klass, id)
|
5
|
+
klass.find(id)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Kiqit::ArgsParser do
|
10
|
+
subject { Kiqit::ArgsParser }
|
11
|
+
let(:user) { User.create }
|
12
|
+
|
13
|
+
context "Custom finder" do
|
14
|
+
it "should invoke the custom class finder method" do
|
15
|
+
CustomFinder.should_receive(:find).with(User, user.id.to_s)
|
16
|
+
Kiqit::Plugins.add_finder(CustomFinder)
|
17
|
+
|
18
|
+
subject.args_from_sidekiq(["AR:User:#{user.id}"])
|
19
|
+
|
20
|
+
Kiqit::Plugins.clear_finder!
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "args to sidekiq" do
|
25
|
+
|
26
|
+
it "should convert array of hashes correctly" do
|
27
|
+
arr = [
|
28
|
+
{ something: "aaa" },
|
29
|
+
{ something: "bbb" },
|
30
|
+
{ something: "ccc" },
|
31
|
+
{ something: "ddd" },
|
32
|
+
{ something: "eee" }
|
33
|
+
]
|
34
|
+
subject.args_to_sidekiq(arr).class.should == Array
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should translate array of hashes back and forth again" do
|
38
|
+
arr = [
|
39
|
+
{ something: "aaa" },
|
40
|
+
{ something: "bbb" },
|
41
|
+
{ something: "ccc" },
|
42
|
+
{ something: "ddd" },
|
43
|
+
{ something: "eee" }
|
44
|
+
]
|
45
|
+
to_sidekiq = subject.args_to_sidekiq(arr).to_json
|
46
|
+
a = JSON.parse(to_sidekiq)
|
47
|
+
from_sidekiq = subject.args_from_sidekiq(a)
|
48
|
+
|
49
|
+
from_sidekiq.should == arr
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should convert the AR object to the proper string" do
|
53
|
+
user_id = user.id
|
54
|
+
|
55
|
+
subject.args_to_sidekiq(user).should == "AR:User:#{user_id}"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should convert a hash into YAML string so that Sidekiq will be able to JSON convert it" do
|
59
|
+
hash = { name: "something", other: "something else" }
|
60
|
+
subject.args_to_sidekiq(hash)[0].class.name.should == "String"
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should be able to load a yaml from the string and translate it into the same hash again" do
|
64
|
+
hash = { name: "something", other: "something else" }
|
65
|
+
yaml = subject.args_to_sidekiq(hash)
|
66
|
+
|
67
|
+
loaded_yaml = YAML.load(yaml)
|
68
|
+
|
69
|
+
loaded_yaml[:name].should == "something"
|
70
|
+
loaded_yaml[:other].should == "something else"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should convert a class to the proper string representation" do
|
74
|
+
klass = User
|
75
|
+
subject.args_to_sidekiq(klass).should == "CLASS:User"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "args from sidekiq" do
|
80
|
+
it "should give me a hash back when I pass a yaml representation of it" do
|
81
|
+
hash = { name: "something", other: "something else" }
|
82
|
+
yaml = *hash.to_yaml
|
83
|
+
|
84
|
+
args = subject.args_from_sidekiq(yaml)
|
85
|
+
args[0].class.name.should == "Hash"
|
86
|
+
args[0][:name].should == "something"
|
87
|
+
args[0][:other].should == "something else"
|
88
|
+
end
|
89
|
+
|
90
|
+
it "Should give me a user model back when I pass the proper string" do
|
91
|
+
args_input = *"AR:User:#{user.id}"
|
92
|
+
args = subject.args_from_sidekiq(args_input)
|
93
|
+
args[0].should == user
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Kiqit::Config do
|
4
|
+
before(:each) { Kiqit.config.enabled = false }
|
5
|
+
|
6
|
+
it "should set the perform later mode" do
|
7
|
+
Kiqit.config.enabled?.should be_false
|
8
|
+
Kiqit.config.enabled = true
|
9
|
+
Kiqit.config.enabled?.should == true
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Kiqit::JobCreator do
|
4
|
+
|
5
|
+
let(:job) {Kiqit::JobCreator.new("some_queue", "WorkerClass", "Klass_name", 2, :the_method)}
|
6
|
+
let(:delay) {42}
|
7
|
+
|
8
|
+
describe :enqueue do
|
9
|
+
context "with :delay option" do
|
10
|
+
it "should enqueue job in Sidekiq with the given delay" do
|
11
|
+
Sidekiq::Client.should_receive(:push)
|
12
|
+
job.enqueue delay: delay
|
13
|
+
end
|
14
|
+
end
|
15
|
+
context "without :delay option" do
|
16
|
+
it "should create a regular sidekiq job if delay option isn't given" do
|
17
|
+
Sidekiq::Client.should_receive(:push)
|
18
|
+
job.enqueue
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Kiqit::PayloadHelper do
|
5
|
+
subject { Kiqit::PayloadHelper }
|
6
|
+
|
7
|
+
describe :get_digest do
|
8
|
+
it "should o something" do
|
9
|
+
user = User.create
|
10
|
+
|
11
|
+
digest = Digest::MD5.hexdigest({ :class => "DummyClass",
|
12
|
+
:method => :some_method.to_s,
|
13
|
+
:args => ["AR:User:#{user.id}"]
|
14
|
+
}.to_s)
|
15
|
+
digest = "loner:#{digest}"
|
16
|
+
|
17
|
+
args = Kiqit::ArgsParser.args_to_sidekiq(user)
|
18
|
+
subject.get_digest("DummyClass", :some_method, args).should == digest
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'kiqit/plugins'
|
2
|
+
|
3
|
+
class BadDummyFinder
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
class GoodDummyFinder
|
8
|
+
def self.find(runner_class, id)
|
9
|
+
runner_class.find(id)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Kiqit::Plugins do
|
14
|
+
subject { Kiqit::Plugins }
|
15
|
+
|
16
|
+
describe :finder_class do
|
17
|
+
it "should be nil" do
|
18
|
+
subject.finder_class.should == nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe :add_finder do
|
23
|
+
it "should not add the finder since it doens't have the proper method" do
|
24
|
+
subject.add_finder(BadDummyFinder)
|
25
|
+
subject.finder_class.should == nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should add the finder since it has the proper method" do
|
29
|
+
subject.add_finder(GoodDummyFinder)
|
30
|
+
subject.finder_class.should == GoodDummyFinder
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class DummyClass
|
4
|
+
def self.do_somthing_with_array_of_hashes(arr)
|
5
|
+
arr[0][:foo]
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.do_something_without_args
|
9
|
+
true
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.identity_function(data)
|
13
|
+
data
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.join(arg1, arg2)
|
17
|
+
"#{arg1}|#{arg2}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Kiqit::Workers::Objects::Worker do
|
22
|
+
subject { Kiqit::Workers::Objects::Worker.new }
|
23
|
+
|
24
|
+
it "should pass an array of hashes into the method" do
|
25
|
+
arr = [
|
26
|
+
{ foo: "bar" },
|
27
|
+
{ bar: "foo" }
|
28
|
+
]
|
29
|
+
arr = Kiqit::ArgsParser.args_to_sidekiq(arr)
|
30
|
+
subject.perform("DummyClass", :do_somthing_with_array_of_hashes, arr).should == "bar"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should pass no args to the method" do
|
34
|
+
subject.perform("DummyClass", :do_something_without_args).should == true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should pass a single argument (user)" do
|
38
|
+
user = User.create
|
39
|
+
args = Kiqit::ArgsParser.args_to_sidekiq(user)
|
40
|
+
subject.perform("DummyClass", :identity_function, args).should == user
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should pass a single argument (user) when translated args are passed in" do
|
44
|
+
user = User.create
|
45
|
+
user_arg = "AR:User:#{user.id}"
|
46
|
+
mock_user = double(:user, first: nil)
|
47
|
+
User.should_receive(:where).with(id: user.id.to_s).and_return { mock_user }
|
48
|
+
|
49
|
+
subject.perform("DummyClass", :identity_function, user_arg).should == nil
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should pass an array with one entry" do
|
53
|
+
users = [User.create]
|
54
|
+
args = Kiqit::ArgsParser.args_to_sidekiq(users)
|
55
|
+
subject.perform("DummyClass", :identity_function, args).should == users
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should pass multi dimension arrays" do
|
59
|
+
data = [1, 2, User.create, ["a", "b", "c"]]
|
60
|
+
args = Kiqit::ArgsParser.args_to_sidekiq(data)
|
61
|
+
subject.perform("DummyClass", :identity_function, args).should == data
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should pass AR and hash' do
|
65
|
+
user = User.create
|
66
|
+
arr = {
|
67
|
+
something_a: "aaa",
|
68
|
+
something_b: "bbb"
|
69
|
+
}
|
70
|
+
arg1 = Kiqit::ArgsParser.args_to_sidekiq(user)
|
71
|
+
arg2 = Kiqit::ArgsParser.args_to_sidekiq(arr)
|
72
|
+
subject.perform("DummyClass", :join, arg1, arg2).split("|")[1].should == "{:something_a=>\"aaa\", :something_b=>\"bbb\"}"
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class DummyClass
|
4
|
+
def self.do_something_really_heavy
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.do_something_with_string(value)
|
9
|
+
value
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.do_something_with_user(user)
|
13
|
+
user
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.do_something_with_multiple_args(a, b)
|
17
|
+
"#{a}, #{b}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.do_something_with_optional_hash(options = {})
|
21
|
+
options.blank?
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.do_something_with_array(arr)
|
25
|
+
arr
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ObjectKiqit do
|
30
|
+
it "should insert a task into sidekiq when the config is enabled" do
|
31
|
+
Sidekiq.redis = $redis
|
32
|
+
|
33
|
+
Kiqit.config.stub(:enabled?).and_return(true)
|
34
|
+
User.kiqit(:generic, :get_metadata)
|
35
|
+
|
36
|
+
Sidekiq::Queue.new(:generic).size.should == 1
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should send the method on the class when the config is disabled" do
|
40
|
+
Kiqit.config.stub(:enabled?).and_return(false)
|
41
|
+
|
42
|
+
User.should_receive(:get_metadata)
|
43
|
+
User.kiqit(:generic, :get_metadata)
|
44
|
+
|
45
|
+
Sidekiq::Queue.new(:generic).size.should == 0
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should only add the method a single time to the queue" do
|
49
|
+
Kiqit.config.stub(:enabled?).and_return(true)
|
50
|
+
|
51
|
+
DummyClass.kiqit!(:generic, :do_something_really_heavy)
|
52
|
+
DummyClass.kiqit!(:generic, :do_something_really_heavy)
|
53
|
+
DummyClass.kiqit!(:generic, :do_something_really_heavy)
|
54
|
+
DummyClass.kiqit!(:generic, :do_something_really_heavy)
|
55
|
+
|
56
|
+
Sidekiq::Queue.new(:generic).size.should == 1
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "When Enabled" do
|
60
|
+
let(:user) { User.create }
|
61
|
+
|
62
|
+
it "should pass no values" do
|
63
|
+
Kiqit.config.stub(:enabled?).and_return(true)
|
64
|
+
Sidekiq::Client.should_receive(:push).with("queue" => :generic, "class" => Kiqit::Workers::Objects::Worker, "args" => ["DummyClass", :do_something_with_array])
|
65
|
+
DummyClass.kiqit(:generic, :do_something_with_array)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should pass the correct value (array)" do
|
69
|
+
Kiqit.config.stub(:enabled?).and_return(true)
|
70
|
+
Sidekiq::Client.should_receive(:push).with("queue" => :generic, "class" => Kiqit::Workers::Objects::Worker, "args" => ["DummyClass", :do_something_with_array, [1,2,3,4,5]])
|
71
|
+
DummyClass.kiqit(:generic, :do_something_with_array, [1,2,3,4,5])
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should pass multiple args" do
|
75
|
+
Kiqit.config.stub(:enabled?).and_return(true)
|
76
|
+
Sidekiq::Client.should_receive(:push).with("queue" => :generic, "class" => Kiqit::Workers::Objects::Worker, "args" => ["DummyClass", :do_something_with_multiple_args, 1, 2])
|
77
|
+
DummyClass.kiqit(:generic, :do_something_with_multiple_args, 1, 2)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should pass AR and hash" do
|
81
|
+
Kiqit.config.stub(:enabled?).and_return(true)
|
82
|
+
Sidekiq::Client.should_receive(:push).with("queue" => :generic, "class" => Kiqit::Workers::Objects::Worker, "args" => ["DummyClass", :do_something_with_multiple_args, "AR:User:#{user.id}", "---\n:a: 2\n"])
|
83
|
+
DummyClass.kiqit(:generic, :do_something_with_multiple_args, user, {a: 2})
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe :kiqit do
|
88
|
+
before(:each) do
|
89
|
+
Kiqit.config.stub(:enabled?).and_return(false)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should pass the correct value (String)" do
|
93
|
+
DummyClass.kiqit(:generic, :do_something_with_string, "Avi Tzurel").should == "Avi Tzurel"
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should pass the correct value (AR object)" do
|
97
|
+
user = User.create
|
98
|
+
DummyClass.kiqit(:generic, :do_something_with_user, user).should == user
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should pass the correct value (optional hash)" do
|
102
|
+
DummyClass.kiqit(:generic, :do_something_with_optional_hash).should == true
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should pass multiple args" do
|
106
|
+
DummyClass.kiqit(:generic, :do_something_with_multiple_args, 1, 2).should == "1, 2"
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should pass AR and hash" do
|
110
|
+
u = User.create
|
111
|
+
DummyClass.kiqit(:generic, :do_something_with_multiple_args, u, {a: 2}).should == "#{u}, {:a=>2}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
describe :kiqit! do
|
116
|
+
before(:each) do
|
117
|
+
Kiqit.config.stub(:enabled?).and_return(false)
|
118
|
+
end
|
119
|
+
it "should pass the correct value (String)" do
|
120
|
+
DummyClass.kiqit!(:generic, :do_something_with_string, "Avi Tzurel").should == "Avi Tzurel"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should pass the correct value (AR object)" do
|
124
|
+
user = User.create
|
125
|
+
DummyClass.kiqit!(:generic, :do_something_with_user, user).should == user
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should pass the correct value (optional hash)" do
|
129
|
+
DummyClass.kiqit!(:generic, :do_something_with_optional_hash).should == true
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should pass multiple args" do
|
133
|
+
DummyClass.kiqit!(:generic, :do_something_with_multiple_args, 1, 2).should == "1, 2"
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should pass AR and hash" do
|
137
|
+
u = User.create
|
138
|
+
DummyClass.kiqit!(:generic, :do_something_with_multiple_args, u, {a: 2}).should == "#{u}, {:a=>2}"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Sidekiq::Plugins::Later::Method do
|
4
|
+
before(:each) { Kiqit.config.enabled = true }
|
5
|
+
before(:each) { Sidekiq.redis = $redis }
|
6
|
+
|
7
|
+
context "enabled" do
|
8
|
+
before(:each) do
|
9
|
+
Kiqit.config.stub(:enabled?).and_return(true)
|
10
|
+
User.later :long_running_method
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should insert a task into sidekiq when the config is enabled" do
|
14
|
+
user = User.create
|
15
|
+
user.long_running_method
|
16
|
+
Sidekiq::Queue.new(:generic).size.should == 1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "loner" do
|
21
|
+
before(:each) do
|
22
|
+
Kiqit.config.stub(:enabled?).and_return(true)
|
23
|
+
User.later :lonely_long_running_method, loner: true
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should only add a single method to the queue, since the config is with a loner" do
|
27
|
+
user = User.create
|
28
|
+
user.lonely_long_running_method
|
29
|
+
user.lonely_long_running_method
|
30
|
+
user.lonely_long_running_method
|
31
|
+
user.lonely_long_running_method
|
32
|
+
user.lonely_long_running_method
|
33
|
+
Sidekiq::Queue.new(:generic).size.should == 1
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should only add a single method to the queue, since the config is with a loner when using kiqit! method" do
|
37
|
+
user = User.create
|
38
|
+
user.kiqit!(:generic, :lonely_long_running_method)
|
39
|
+
user.kiqit!(:generic, :lonely_long_running_method)
|
40
|
+
user.kiqit!(:generic, :lonely_long_running_method)
|
41
|
+
user.kiqit!(:generic, :lonely_long_running_method)
|
42
|
+
user.kiqit!(:generic, :lonely_long_running_method)
|
43
|
+
Sidekiq::Queue.new(:generic).size.should == 1
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "disabled" do
|
48
|
+
it "should send the method on the class when the config is disabled" do
|
49
|
+
user = User.create
|
50
|
+
user.now_long_running_method
|
51
|
+
Sidekiq::Queue.new(:generic).size.should == 0
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'arguments to Sidekiq' do
|
56
|
+
it 'should send no args to sidekiq' do
|
57
|
+
user = User.create
|
58
|
+
Sidekiq::Client.should_receive(:push).with("queue" => :generic, "class" => Kiqit::Workers::ActiveRecord::Worker, "args" => ['User', user.id, :lonely_long_running_method])
|
59
|
+
user.kiqit(:generic, :lonely_long_running_method)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should send 1 arg to sidekiq' do
|
63
|
+
user = User.create
|
64
|
+
Sidekiq::Client.should_receive(:push).with("queue" => :generic, "class" => Kiqit::Workers::ActiveRecord::LoneWorker, "args" => ['User', user.id, :lonely_long_running_method, 1])
|
65
|
+
user.kiqit!(:generic, :lonely_long_running_method, 1)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it "shold define the correct method on the user model" do
|
70
|
+
user = User.create
|
71
|
+
user.should respond_to(:long_running_method)
|
72
|
+
user.should respond_to(:now_long_running_method)
|
73
|
+
end
|
74
|
+
|
75
|
+
describe :kiqit! do
|
76
|
+
it "should send the correct params on the method (with hash)" do
|
77
|
+
Kiqit.config.stub(:enabled?).and_return(false)
|
78
|
+
user = User.create
|
79
|
+
user.should_receive(:method_with_hash_as_option).with({:some_option => "Brown fox"})
|
80
|
+
user.kiqit!(:generic, :method_with_hash_as_option, :some_option => "Brown fox")
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should send the correct params on the method (with integer)" do
|
84
|
+
Kiqit.config.stub(:enabled?).and_return(false)
|
85
|
+
user = User.create
|
86
|
+
user.should_receive(:method_with_integer_option).with(1).and_return(1)
|
87
|
+
user.kiqit!(:generic, :method_with_integer_option, 1)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context "delay" do
|
92
|
+
|
93
|
+
let(:enqueue_in) {5}
|
94
|
+
let(:actual_enqueue) {Time.now + enqueue_in}
|
95
|
+
|
96
|
+
before(:each) do
|
97
|
+
Kiqit.config.stub(:enabled?).and_return(true)
|
98
|
+
User.later :delayed_long_running_method, :delay => enqueue_in
|
99
|
+
end
|
100
|
+
|
101
|
+
describe :delay do
|
102
|
+
it "should delay enqueuing for the duration of time given, if delay time is given" do
|
103
|
+
user = User.create
|
104
|
+
old_count = Sidekiq.redis{|i| i.zcount("schedule", "-inf", "+inf")}
|
105
|
+
user.delayed_long_running_method
|
106
|
+
new_count = Sidekiq.redis{|i| i.zcount("schedule", "-inf", "+inf")}
|
107
|
+
new_count.should == old_count + 1
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe :kiqit_in do
|
112
|
+
it "should delay enqueuing for the duration of delay time given" do
|
113
|
+
user = User.create
|
114
|
+
old_count = Sidekiq.redis{|i| i.zcount("schedule", "-inf", "+inf")}
|
115
|
+
user.kiqit_in(enqueue_in, :generic, :delayed_long_running_method)
|
116
|
+
new_count = Sidekiq.redis{|i| i.zcount("schedule", "-inf", "+inf")}
|
117
|
+
new_count.should == old_count + 1
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "uri"
|
3
|
+
require "kiqit"
|
4
|
+
require "rspec"
|
5
|
+
require "support/database_connection"
|
6
|
+
require "support/database_models"
|
7
|
+
require "redis"
|
8
|
+
require 'fakeredis/rspec'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.mock_with :rspec
|
12
|
+
|
13
|
+
config.before(:all) do
|
14
|
+
dir = File.join(File.dirname(__FILE__), 'support/db')
|
15
|
+
|
16
|
+
old_db = File.join(dir, 'test.sqlite3')
|
17
|
+
FileUtils.rm(old_db) if File.exists?(old_db)
|
18
|
+
FileUtils.cp(File.join(dir, '.blank.sqlite3'), File.join(dir, 'test.sqlite3'))
|
19
|
+
end
|
20
|
+
|
21
|
+
config.before(:suite) do
|
22
|
+
$redis = {}
|
23
|
+
$real_redis = Redis.new
|
24
|
+
Sidekiq.redis = $redis
|
25
|
+
end
|
26
|
+
|
27
|
+
config.before(:each) do
|
28
|
+
Sidekiq.redis{|i| i.flushdb}
|
29
|
+
end
|
30
|
+
|
31
|
+
config.after(:each) do
|
32
|
+
Kiqit::Plugins.clear_finder!
|
33
|
+
$real_redis.flushdb
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: File.join(File.dirname(__FILE__), "db/test.sqlite3")
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
def long_running_method
|
3
|
+
# Your code here
|
4
|
+
end
|
5
|
+
later :long_running_method
|
6
|
+
|
7
|
+
def long_running_method_2
|
8
|
+
# Your code here
|
9
|
+
end
|
10
|
+
later :long_running_method_2, queue: :some_queue_name
|
11
|
+
|
12
|
+
def lonely_long_running_method
|
13
|
+
# Your code here
|
14
|
+
end
|
15
|
+
later :lonely_long_running_method, :loner => true, queue: :some_queue_name
|
16
|
+
|
17
|
+
def delayed_long_running_method
|
18
|
+
# Your code here
|
19
|
+
end
|
20
|
+
later :delayed_long_running_method, :delay => 30, queue: :some_queue_name
|
21
|
+
|
22
|
+
def method_with_hash_as_option(options = {})
|
23
|
+
options[:some_option]
|
24
|
+
end
|
25
|
+
|
26
|
+
def method_with_integer_option(integer)
|
27
|
+
integer
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
Binary file
|
Binary file
|