nogara-resque-loner 1.2.1

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.
@@ -0,0 +1,116 @@
1
+ require 'test_helper'
2
+
3
+ context "Resque::Plugin finding hooks" do
4
+ module SimplePlugin
5
+ extend self
6
+ def before_perform1; end
7
+ def before_perform; end
8
+ def before_perform2; end
9
+ def after_perform1; end
10
+ def after_perform; end
11
+ def after_perform2; end
12
+ def perform; end
13
+ def around_perform1; end
14
+ def around_perform; end
15
+ def around_perform2; end
16
+ def on_failure1; end
17
+ def on_failure; end
18
+ def on_failure2; end
19
+ end
20
+
21
+ test "before_perform hooks are found and sorted" do
22
+ assert_equal ["before_perform", "before_perform1", "before_perform2"], Resque::Plugin.before_hooks(SimplePlugin).map {|m| m.to_s}
23
+ end
24
+
25
+ test "after_perform hooks are found and sorted" do
26
+ assert_equal ["after_perform", "after_perform1", "after_perform2"], Resque::Plugin.after_hooks(SimplePlugin).map {|m| m.to_s}
27
+ end
28
+
29
+ test "around_perform hooks are found and sorted" do
30
+ assert_equal ["around_perform", "around_perform1", "around_perform2"], Resque::Plugin.around_hooks(SimplePlugin).map {|m| m.to_s}
31
+ end
32
+
33
+ test "on_failure hooks are found and sorted" do
34
+ assert_equal ["on_failure", "on_failure1", "on_failure2"], Resque::Plugin.failure_hooks(SimplePlugin).map {|m| m.to_s}
35
+ end
36
+ end
37
+
38
+ context "Resque::Plugin linting" do
39
+ module ::BadBefore
40
+ def self.before_perform; end
41
+ end
42
+ module ::BadAfter
43
+ def self.after_perform; end
44
+ end
45
+ module ::BadAround
46
+ def self.around_perform; end
47
+ end
48
+ module ::BadFailure
49
+ def self.on_failure; end
50
+ end
51
+
52
+ test "before_perform must be namespaced" do
53
+ begin
54
+ Resque::Plugin.lint(BadBefore)
55
+ assert false, "should have failed"
56
+ rescue Resque::Plugin::LintError => e
57
+ assert_equal "BadBefore.before_perform is not namespaced", e.message
58
+ end
59
+ end
60
+
61
+ test "after_perform must be namespaced" do
62
+ begin
63
+ Resque::Plugin.lint(BadAfter)
64
+ assert false, "should have failed"
65
+ rescue Resque::Plugin::LintError => e
66
+ assert_equal "BadAfter.after_perform is not namespaced", e.message
67
+ end
68
+ end
69
+
70
+ test "around_perform must be namespaced" do
71
+ begin
72
+ Resque::Plugin.lint(BadAround)
73
+ assert false, "should have failed"
74
+ rescue Resque::Plugin::LintError => e
75
+ assert_equal "BadAround.around_perform is not namespaced", e.message
76
+ end
77
+ end
78
+
79
+ test "on_failure must be namespaced" do
80
+ begin
81
+ Resque::Plugin.lint(BadFailure)
82
+ assert false, "should have failed"
83
+ rescue Resque::Plugin::LintError => e
84
+ assert_equal "BadFailure.on_failure is not namespaced", e.message
85
+ end
86
+ end
87
+
88
+ module GoodBefore
89
+ def self.before_perform1; end
90
+ end
91
+ module GoodAfter
92
+ def self.after_perform1; end
93
+ end
94
+ module GoodAround
95
+ def self.around_perform1; end
96
+ end
97
+ module GoodFailure
98
+ def self.on_failure1; end
99
+ end
100
+
101
+ test "before_perform1 is an ok name" do
102
+ Resque::Plugin.lint(GoodBefore)
103
+ end
104
+
105
+ test "after_perform1 is an ok name" do
106
+ Resque::Plugin.lint(GoodAfter)
107
+ end
108
+
109
+ test "around_perform1 is an ok name" do
110
+ Resque::Plugin.lint(GoodAround)
111
+ end
112
+
113
+ test "on_failure1 is an ok name" do
114
+ Resque::Plugin.lint(GoodFailure)
115
+ end
116
+ end
@@ -0,0 +1,115 @@
1
+ # Redis configuration file example
2
+
3
+ # By default Redis does not run as a daemon. Use 'yes' if you need it.
4
+ # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
5
+ daemonize yes
6
+
7
+ # When run as a daemon, Redis write a pid file in /var/run/redis.pid by default.
8
+ # You can specify a custom pid file location here.
9
+ pidfile ./test/redis-test.pid
10
+
11
+ # Accept connections on the specified port, default is 6379
12
+ port 9736
13
+
14
+ # If you want you can bind a single interface, if the bind option is not
15
+ # specified all the interfaces will listen for connections.
16
+ #
17
+ # bind 127.0.0.1
18
+
19
+ # Close the connection after a client is idle for N seconds (0 to disable)
20
+ timeout 300
21
+
22
+ # Save the DB on disk:
23
+ #
24
+ # save <seconds> <changes>
25
+ #
26
+ # Will save the DB if both the given number of seconds and the given
27
+ # number of write operations against the DB occurred.
28
+ #
29
+ # In the example below the behaviour will be to save:
30
+ # after 900 sec (15 min) if at least 1 key changed
31
+ # after 300 sec (5 min) if at least 10 keys changed
32
+ # after 60 sec if at least 10000 keys changed
33
+ save 900 1
34
+ save 300 10
35
+ save 60 10000
36
+
37
+ # The filename where to dump the DB
38
+ dbfilename dump.rdb
39
+
40
+ # For default save/load DB in/from the working directory
41
+ # Note that you must specify a directory not a file name.
42
+ dir ./test/
43
+
44
+ # Set server verbosity to 'debug'
45
+ # it can be one of:
46
+ # debug (a lot of information, useful for development/testing)
47
+ # notice (moderately verbose, what you want in production probably)
48
+ # warning (only very important / critical messages are logged)
49
+ loglevel debug
50
+
51
+ # Specify the log file name. Also 'stdout' can be used to force
52
+ # the demon to log on the standard output. Note that if you use standard
53
+ # output for logging but daemonize, logs will be sent to /dev/null
54
+ logfile stdout
55
+
56
+ # Set the number of databases. The default database is DB 0, you can select
57
+ # a different one on a per-connection basis using SELECT <dbid> where
58
+ # dbid is a number between 0 and 'databases'-1
59
+ databases 16
60
+
61
+ ################################# REPLICATION #################################
62
+
63
+ # Master-Slave replication. Use slaveof to make a Redis instance a copy of
64
+ # another Redis server. Note that the configuration is local to the slave
65
+ # so for example it is possible to configure the slave to save the DB with a
66
+ # different interval, or to listen to another port, and so on.
67
+
68
+ # slaveof <masterip> <masterport>
69
+
70
+ ################################## SECURITY ###################################
71
+
72
+ # Require clients to issue AUTH <PASSWORD> before processing any other
73
+ # commands. This might be useful in environments in which you do not trust
74
+ # others with access to the host running redis-server.
75
+ #
76
+ # This should stay commented out for backward compatibility and because most
77
+ # people do not need auth (e.g. they run their own servers).
78
+
79
+ # requirepass foobared
80
+
81
+ ################################### LIMITS ####################################
82
+
83
+ # Set the max number of connected clients at the same time. By default there
84
+ # is no limit, and it's up to the number of file descriptors the Redis process
85
+ # is able to open. The special value '0' means no limts.
86
+ # Once the limit is reached Redis will close all the new connections sending
87
+ # an error 'max number of clients reached'.
88
+
89
+ # maxclients 128
90
+
91
+ # Don't use more memory than the specified amount of bytes.
92
+ # When the memory limit is reached Redis will try to remove keys with an
93
+ # EXPIRE set. It will try to start freeing keys that are going to expire
94
+ # in little time and preserve keys with a longer time to live.
95
+ # Redis will also try to remove objects from free lists if possible.
96
+ #
97
+ # If all this fails, Redis will start to reply with errors to commands
98
+ # that will use more memory, like SET, LPUSH, and so on, and will continue
99
+ # to reply to most read-only commands like GET.
100
+ #
101
+ # WARNING: maxmemory can be a good idea mainly if you want to use Redis as a
102
+ # 'state' server or cache, not as a real DB. When Redis is used as a real
103
+ # database the memory usage will grow over the weeks, it will be obvious if
104
+ # it is going to use too much memory in the long run, and you'll have the time
105
+ # to upgrade. With maxmemory after the limit is reached you'll start to get
106
+ # errors for write operations, and this may even lead to DB inconsistency.
107
+
108
+ # maxmemory <bytes>
109
+
110
+ ############################### ADVANCED CONFIG ###############################
111
+
112
+ # Glue small output buffers together in order to send small replies in a
113
+ # single TCP packet. Uses a bit more CPU but most of the times it is a win
114
+ # in terms of number of queries per second. Use 'yes' if unsure.
115
+ glueoutputbuf yes
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+ require 'resque/server/test_helper'
3
+
4
+ # Root path test
5
+ context "on GET to /" do
6
+ setup { get "/" }
7
+
8
+ test "redirect to overview" do
9
+ follow_redirect!
10
+ end
11
+ end
12
+
13
+ # Global overview
14
+ context "on GET to /overview" do
15
+ setup { get "/overview" }
16
+
17
+ test "should at least display 'queues'" do
18
+ assert last_response.body.include?('Queues')
19
+ end
20
+ end
21
+
22
+ # Working jobs
23
+ context "on GET to /working" do
24
+ setup { get "/working" }
25
+
26
+ should_respond_with_success
27
+ end
28
+
29
+ # Failed
30
+ context "on GET to /failed" do
31
+ setup { get "/failed" }
32
+
33
+ should_respond_with_success
34
+ end
35
+
36
+ # Stats
37
+ context "on GET to /stats/resque" do
38
+ setup { get "/stats/resque" }
39
+
40
+ should_respond_with_success
41
+ end
42
+
43
+ context "on GET to /stats/redis" do
44
+ setup { get "/stats/redis" }
45
+
46
+ should_respond_with_success
47
+ end
48
+
49
+ context "on GET to /stats/resque" do
50
+ setup { get "/stats/keys" }
51
+
52
+ should_respond_with_success
53
+ end
@@ -0,0 +1,259 @@
1
+ require 'test_helper'
2
+
3
+ context "Resque" do
4
+ setup do
5
+ Resque.redis.flushall
6
+
7
+ Resque.push(:people, { 'name' => 'chris' })
8
+ Resque.push(:people, { 'name' => 'bob' })
9
+ Resque.push(:people, { 'name' => 'mark' })
10
+ end
11
+
12
+ test "can set a namespace through a url-like string" do
13
+ assert Resque.redis
14
+ assert_equal :resque, Resque.redis.namespace
15
+ Resque.redis = 'localhost:9736/namespace'
16
+ assert_equal 'namespace', Resque.redis.namespace
17
+ end
18
+
19
+ test "redis= works correctly with a Redis::Namespace param" do
20
+ new_redis = Redis.new(:host => "localhost", :port => 9736)
21
+ new_namespace = Redis::Namespace.new("namespace", :redis => new_redis)
22
+ Resque.redis = new_namespace
23
+ assert_equal new_namespace, Resque.redis
24
+
25
+ Resque.redis = 'localhost:9736/namespace'
26
+ end
27
+
28
+ test "can put jobs on a queue" do
29
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
30
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
31
+ end
32
+
33
+ test "can grab jobs off a queue" do
34
+ Resque::Job.create(:jobs, 'some-job', 20, '/tmp')
35
+
36
+ job = Resque.reserve(:jobs)
37
+
38
+ assert_kind_of Resque::Job, job
39
+ assert_equal SomeJob, job.payload_class
40
+ assert_equal 20, job.args[0]
41
+ assert_equal '/tmp', job.args[1]
42
+ end
43
+
44
+ test "can re-queue jobs" do
45
+ Resque::Job.create(:jobs, 'some-job', 20, '/tmp')
46
+
47
+ job = Resque.reserve(:jobs)
48
+ job.recreate
49
+
50
+ assert_equal job, Resque.reserve(:jobs)
51
+ end
52
+
53
+ test "can put jobs on a queue by way of an ivar" do
54
+ assert_equal 0, Resque.size(:ivar)
55
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
56
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
57
+
58
+ job = Resque.reserve(:ivar)
59
+
60
+ assert_kind_of Resque::Job, job
61
+ assert_equal SomeIvarJob, job.payload_class
62
+ assert_equal 20, job.args[0]
63
+ assert_equal '/tmp', job.args[1]
64
+
65
+ assert Resque.reserve(:ivar)
66
+ assert_equal nil, Resque.reserve(:ivar)
67
+ end
68
+
69
+ test "can remove jobs from a queue by way of an ivar" do
70
+ assert_equal 0, Resque.size(:ivar)
71
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
72
+ assert Resque.enqueue(SomeIvarJob, 30, '/tmp')
73
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
74
+ assert Resque::Job.create(:ivar, 'blah-job', 20, '/tmp')
75
+ assert Resque.enqueue(SomeIvarJob, 20, '/tmp')
76
+ assert_equal 5, Resque.size(:ivar)
77
+
78
+ assert Resque.dequeue(SomeIvarJob, 30, '/tmp')
79
+ assert_equal 4, Resque.size(:ivar)
80
+ assert Resque.dequeue(SomeIvarJob)
81
+ assert_equal 1, Resque.size(:ivar)
82
+ end
83
+
84
+ test "jobs have a nice #inspect" do
85
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
86
+ job = Resque.reserve(:jobs)
87
+ assert_equal '(Job{jobs} | SomeJob | [20, "/tmp"])', job.inspect
88
+ end
89
+
90
+ test "jobs can be destroyed" do
91
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
92
+ assert Resque::Job.create(:jobs, 'BadJob', 20, '/tmp')
93
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
94
+ assert Resque::Job.create(:jobs, 'BadJob', 30, '/tmp')
95
+ assert Resque::Job.create(:jobs, 'BadJob', 20, '/tmp')
96
+
97
+ assert_equal 5, Resque.size(:jobs)
98
+ assert_equal 2, Resque::Job.destroy(:jobs, 'SomeJob')
99
+ assert_equal 3, Resque.size(:jobs)
100
+ assert_equal 1, Resque::Job.destroy(:jobs, 'BadJob', 30, '/tmp')
101
+ assert_equal 2, Resque.size(:jobs)
102
+ end
103
+
104
+ test "jobs can test for equality" do
105
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
106
+ assert Resque::Job.create(:jobs, 'some-job', 20, '/tmp')
107
+ assert_equal Resque.reserve(:jobs), Resque.reserve(:jobs)
108
+
109
+ assert Resque::Job.create(:jobs, 'SomeMethodJob', 20, '/tmp')
110
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
111
+ assert_not_equal Resque.reserve(:jobs), Resque.reserve(:jobs)
112
+
113
+ assert Resque::Job.create(:jobs, 'SomeJob', 20, '/tmp')
114
+ assert Resque::Job.create(:jobs, 'SomeJob', 30, '/tmp')
115
+ assert_not_equal Resque.reserve(:jobs), Resque.reserve(:jobs)
116
+ end
117
+
118
+ test "can put jobs on a queue by way of a method" do
119
+ assert_equal 0, Resque.size(:method)
120
+ assert Resque.enqueue(SomeMethodJob, 20, '/tmp')
121
+ assert Resque.enqueue(SomeMethodJob, 20, '/tmp')
122
+
123
+ job = Resque.reserve(:method)
124
+
125
+ assert_kind_of Resque::Job, job
126
+ assert_equal SomeMethodJob, job.payload_class
127
+ assert_equal 20, job.args[0]
128
+ assert_equal '/tmp', job.args[1]
129
+
130
+ assert Resque.reserve(:method)
131
+ assert_equal nil, Resque.reserve(:method)
132
+ end
133
+
134
+ test "needs to infer a queue with enqueue" do
135
+ assert_raises Resque::NoQueueError do
136
+ Resque.enqueue(SomeJob, 20, '/tmp')
137
+ end
138
+ end
139
+
140
+ test "validates job for queue presence" do
141
+ assert_raises Resque::NoQueueError do
142
+ Resque.validate(SomeJob)
143
+ end
144
+ end
145
+
146
+ test "can put items on a queue" do
147
+ assert Resque.push(:people, { 'name' => 'jon' })
148
+ end
149
+
150
+ test "can pull items off a queue" do
151
+ assert_equal({ 'name' => 'chris' }, Resque.pop(:people))
152
+ assert_equal({ 'name' => 'bob' }, Resque.pop(:people))
153
+ assert_equal({ 'name' => 'mark' }, Resque.pop(:people))
154
+ assert_equal nil, Resque.pop(:people)
155
+ end
156
+
157
+ test "knows how big a queue is" do
158
+ assert_equal 3, Resque.size(:people)
159
+
160
+ assert_equal({ 'name' => 'chris' }, Resque.pop(:people))
161
+ assert_equal 2, Resque.size(:people)
162
+
163
+ assert_equal({ 'name' => 'bob' }, Resque.pop(:people))
164
+ assert_equal({ 'name' => 'mark' }, Resque.pop(:people))
165
+ assert_equal 0, Resque.size(:people)
166
+ end
167
+
168
+ test "can peek at a queue" do
169
+ assert_equal({ 'name' => 'chris' }, Resque.peek(:people))
170
+ assert_equal 3, Resque.size(:people)
171
+ end
172
+
173
+ test "can peek multiple items on a queue" do
174
+ assert_equal({ 'name' => 'bob' }, Resque.peek(:people, 1, 1))
175
+
176
+ assert_equal([{ 'name' => 'bob' }, { 'name' => 'mark' }], Resque.peek(:people, 1, 2))
177
+ assert_equal([{ 'name' => 'chris' }, { 'name' => 'bob' }], Resque.peek(:people, 0, 2))
178
+ assert_equal([{ 'name' => 'chris' }, { 'name' => 'bob' }, { 'name' => 'mark' }], Resque.peek(:people, 0, 3))
179
+ assert_equal({ 'name' => 'mark' }, Resque.peek(:people, 2, 1))
180
+ assert_equal nil, Resque.peek(:people, 3)
181
+ assert_equal [], Resque.peek(:people, 3, 2)
182
+ end
183
+
184
+ test "knows what queues it is managing" do
185
+ assert_equal %w( people ), Resque.queues
186
+ Resque.push(:cars, { 'make' => 'bmw' })
187
+ assert_equal %w( cars people ), Resque.queues
188
+ end
189
+
190
+ test "queues are always a list" do
191
+ Resque.redis.flushall
192
+ assert_equal [], Resque.queues
193
+ end
194
+
195
+ test "can delete a queue" do
196
+ Resque.push(:cars, { 'make' => 'bmw' })
197
+ assert_equal %w( cars people ), Resque.queues
198
+ Resque.remove_queue(:people)
199
+ assert_equal %w( cars ), Resque.queues
200
+ assert_equal nil, Resque.pop(:people)
201
+ end
202
+
203
+ test "keeps track of resque keys" do
204
+ assert_equal ["queue:people", "queues"], Resque.keys
205
+ end
206
+
207
+ test "badly wants a class name, too" do
208
+ assert_raises Resque::NoClassError do
209
+ Resque::Job.create(:jobs, nil)
210
+ end
211
+ end
212
+
213
+ test "keeps stats" do
214
+ Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
215
+ Resque::Job.create(:jobs, BadJob)
216
+ Resque::Job.create(:jobs, GoodJob)
217
+
218
+ Resque::Job.create(:others, GoodJob)
219
+ Resque::Job.create(:others, GoodJob)
220
+
221
+ stats = Resque.info
222
+ assert_equal 8, stats[:pending]
223
+
224
+ @worker = Resque::Worker.new(:jobs)
225
+ @worker.register_worker
226
+ 2.times { @worker.process }
227
+
228
+ job = @worker.reserve
229
+ @worker.working_on job
230
+
231
+ stats = Resque.info
232
+ assert_equal 1, stats[:working]
233
+ assert_equal 1, stats[:workers]
234
+
235
+ @worker.done_working
236
+
237
+ stats = Resque.info
238
+ assert_equal 3, stats[:queues]
239
+ assert_equal 3, stats[:processed]
240
+ assert_equal 1, stats[:failed]
241
+ assert_equal [Resque.redis.respond_to?(:server) ? 'localhost:9736' : 'redis://localhost:9736/0'], stats[:servers]
242
+ end
243
+
244
+ test "decode bad json" do
245
+ assert_raises Resque::Helpers::DecodeException do
246
+ Resque.decode("{\"error\":\"Module not found \\u002\"}")
247
+ end
248
+ end
249
+
250
+ test "inlining jobs" do
251
+ begin
252
+ Resque.inline = true
253
+ Resque.enqueue(SomeIvarJob, 20, '/tmp')
254
+ assert_equal 0, Resque.size(:ivar)
255
+ ensure
256
+ Resque.inline = false
257
+ end
258
+ end
259
+ end