forked from resque/resque-loner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loner_spec.rb
165 lines (125 loc) · 4.95 KB
/
loner_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
154
155
156
157
158
159
160
161
162
163
164
165
require File.dirname(__FILE__) + '/spec_helper'
#
# Resque-loner specific specs. I'm shooting right through the stack here and just
# test the outcomes, because the implementation will change soon and the tests run
# quite quickly.
#
class SomeJob
@queue = :some_queue
end
class SomeUniqueJob
include Resque::Plugins::UniqueJob
@queue = :other_queue
def self.perform(foo); end
end
class FailingUniqueJob
include Resque::Plugins::UniqueJob
@queue = :other_queue
def self.perform(foo)
raise "I beg to differ"
end
end
class DeprecatedUniqueJob < Resque::Plugins::Loner::UniqueJob
@queue = :other_queue
def self.perform(foo); end
end
describe "Resque" do
before(:each) do
Resque.redis.flushall
Resque.size(:other_queue).should == 0
Resque.size(:some_queue).should == 0
end
describe "Jobs" do
it "can put multiple normal jobs on a queue" do
Resque.enqueue SomeJob, "foo"
Resque.enqueue SomeJob, "foo"
Resque.size(:some_queue).should == 2
end
it "only one of the same job sits in a queue" do
Resque.enqueue SomeUniqueJob, "foo"
Resque.enqueue SomeUniqueJob, "foo"
Resque.size(:other_queue).should == 1
end
it "should support deprecated Resque::Plugins::Loner::UniqueJob class" do
Resque.enqueue DeprecatedUniqueJob, "foo"
Resque.enqueue DeprecatedUniqueJob, "foo"
Resque.size(:other_queue).should == 1
end
it "should allow the same jobs to be executed one after the other" do
Resque.enqueue SomeUniqueJob, "foo"
Resque.enqueue SomeUniqueJob, "foo"
Resque.size(:other_queue).should == 1
Resque.reserve(:other_queue)
Resque.size(:other_queue).should == 0
Resque.enqueue SomeUniqueJob, "foo"
Resque.enqueue SomeUniqueJob, "foo"
Resque.size(:other_queue).should == 1
end
it "should be robust regarding hash attributes" do
Resque.enqueue SomeUniqueJob, :bar => 1, :foo => 2
Resque.enqueue SomeUniqueJob, :foo => 2, :bar => 1
Resque.size(:other_queue).should == 1
end
it "should be robust regarding hash attributes (JSON does not distinguish between string and symbol)" do
Resque.enqueue SomeUniqueJob, :bar => 1, :foo => 1
Resque.enqueue SomeUniqueJob, :bar => 1, "foo" => 1
Resque.size(:other_queue).should == 1
end
it "should mark jobs as unqueued, when Job.destroy is killing them" do
Resque.enqueue SomeUniqueJob, "foo"
Resque.enqueue SomeUniqueJob, "foo"
Resque.size(:other_queue).should == 1
Resque::Job.destroy(:other_queue, SomeUniqueJob)
Resque.size(:other_queue).should == 0
Resque.enqueue SomeUniqueJob, "foo"
Resque.enqueue SomeUniqueJob, "foo"
Resque.size(:other_queue).should == 1
end
it "should mark jobs as unqueued, when they raise an exception during #perform" do
Resque.enqueue( FailingUniqueJob, "foo" ).should == 1
Resque.enqueue( FailingUniqueJob, "foo" ).should == "EXISTED"
Resque.size(:other_queue).should == 1
worker = Resque::Worker.new(:other_queue)
worker.work 0
Resque.size(:other_queue).should == 0
Resque.enqueue( FailingUniqueJob, "foo" ).should == 1 # Means that the job was not queued
Resque.enqueue( FailingUniqueJob, "foo" ).should == "EXISTED"
Resque.size(:other_queue).should == 1
end
it "should report if a job is queued or not" do
Resque.enqueue SomeUniqueJob, "foo"
Resque.enqueued?(SomeUniqueJob, "foo").should be_true
Resque.enqueued?(SomeUniqueJob, "bar").should be_false
end
it "should report if a job is in a special queue or not" do
Resque.enqueue_to :special_queue, SomeUniqueJob, "foo"
Resque.enqueued_in?( :special_queue, SomeUniqueJob, "foo").should be_true
Resque.enqueued?( SomeUniqueJob, "foo").should be_false
end
it "should not be able to report if a non-unique job was enqueued" do
Resque.enqueued?(SomeJob).should be_nil
end
it "should cleanup all loners when a queue is destroyed" do
Resque.enqueue SomeUniqueJob, "foo"
Resque.enqueue FailingUniqueJob, "foo"
Resque.remove_queue(:other_queue)
Resque.enqueue(SomeUniqueJob, "foo").should == 1
end
end
describe "Queues" do
it "should allow for jobs to be queued in other queues than their default" do
Resque.enqueue_to :yet_another_queue, SomeJob, 22
Resque.size(:some_queue).should == 0
Resque.size(:yet_another_queue).should ==1
end
it "should allow for jobs to be dequeued from other queues than their default" do
Resque.enqueue_to :yet_another_queue, SomeJob, 22
Resque.enqueue SomeJob, 22
Resque.size(:yet_another_queue).should == 1
Resque.size(:some_queue).should == 1
Resque.dequeue_from :yet_another_queue, SomeJob, 22
Resque.size(:yet_another_queue).should == 0
Resque.size(:some_queue).should == 1
end
end
end