sidekiq-cron 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,157 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require './test/test_helper'
3
+
4
+ class CronPollerTest < Test::Unit::TestCase
5
+
6
+ context 'Cron Poller' do
7
+ setup do
8
+ Sidekiq.redis = REDIS
9
+ Sidekiq.redis do |conn|
10
+ conn.flushdb
11
+ end
12
+
13
+ #clear all previous saved data from redis
14
+ Sidekiq.redis do |conn|
15
+ conn.keys("cron_job*").each do |key|
16
+ conn.del(key)
17
+ end
18
+ end
19
+
20
+
21
+ @args = {
22
+ name: "Test",
23
+ cron: "*/2 * * * *",
24
+ klass: "CronTestClass"
25
+ }
26
+ @args2 = @args.merge(name: 'with_queue', klass: 'CronTestClassWithQueue', cron: "*/10 * * * *")
27
+
28
+ @poller = Sidekiq::Cron::Poller.new
29
+ end
30
+
31
+
32
+ should 'not enqueue any job - new jobs' do
33
+ now = Time.now
34
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 5, 1)
35
+ Time.stubs(:now).returns(enqueued_time)
36
+ #new jobs!
37
+ Sidekiq::Cron::Job.create(@args)
38
+ Sidekiq::Cron::Job.create(@args2)
39
+
40
+ @poller.poll
41
+
42
+ Sidekiq.redis do |conn|
43
+ assert_equal 0, conn.llen("queue:default")
44
+ assert_equal 0, conn.llen("queue:super")
45
+ end
46
+
47
+ #30 seconds after!
48
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 5, 30)
49
+ Time.stubs(:now).returns(enqueued_time)
50
+ @poller.poll
51
+
52
+ Sidekiq.redis do |conn|
53
+ assert_equal 0, conn.llen("queue:default")
54
+ assert_equal 0, conn.llen("queue:super")
55
+ end
56
+ end
57
+
58
+ should 'should enqueue only job with cron */2' do
59
+ now = Time.now
60
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 5, 1)
61
+ Time.stubs(:now).returns(enqueued_time)
62
+ #new jobs!
63
+ Sidekiq::Cron::Job.create(@args)
64
+ Sidekiq::Cron::Job.create(@args2)
65
+
66
+ @poller.poll
67
+
68
+ Sidekiq.redis do |conn|
69
+ assert_equal 0, conn.llen("queue:default")
70
+ assert_equal 0, conn.llen("queue:super")
71
+ end
72
+
73
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 6, 1)
74
+ Time.stubs(:now).returns(enqueued_time)
75
+ @poller.poll
76
+
77
+ Sidekiq.redis do |conn|
78
+ assert_equal 1, conn.llen("queue:default")
79
+ assert_equal 0, conn.llen("queue:super")
80
+ end
81
+ end
82
+
83
+ should 'should enqueue both jobs' do
84
+ now = Time.now
85
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 8, 1)
86
+ Time.stubs(:now).returns(enqueued_time)
87
+ #new jobs!
88
+ Sidekiq::Cron::Job.create(@args)
89
+ Sidekiq::Cron::Job.create(@args2)
90
+
91
+ @poller.poll
92
+
93
+ Sidekiq.redis do |conn|
94
+ assert_equal 0, conn.llen("queue:default")
95
+ assert_equal 0, conn.llen("queue:super")
96
+ end
97
+
98
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 10, 5)
99
+ Time.stubs(:now).returns(enqueued_time)
100
+ @poller.poll
101
+
102
+ Sidekiq.redis do |conn|
103
+ assert_equal 1, conn.llen("queue:default")
104
+ assert_equal 1, conn.llen("queue:super")
105
+ end
106
+ end
107
+
108
+ should 'should enqueue both jobs but only one time each' do
109
+ now = Time.now
110
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 8, 1)
111
+ Time.stubs(:now).returns(enqueued_time)
112
+ #new jobs!
113
+ Sidekiq::Cron::Job.create(@args)
114
+ Sidekiq::Cron::Job.create(@args2)
115
+
116
+ @poller.poll
117
+
118
+ Sidekiq.redis do |conn|
119
+ assert_equal 0, conn.llen("queue:default")
120
+ assert_equal 0, conn.llen("queue:super")
121
+ end
122
+
123
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 20, 1)
124
+ Time.stubs(:now).returns(enqueued_time)
125
+ @poller.poll false
126
+ Sidekiq.redis do |conn|
127
+ assert_equal 1, conn.llen("queue:default")
128
+ assert_equal 1, conn.llen("queue:super")
129
+ end
130
+
131
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 20, 2)
132
+ Time.stubs(:now).returns(enqueued_time)
133
+ @poller.poll false
134
+ Sidekiq.redis do |conn|
135
+ assert_equal 1, conn.llen("queue:default")
136
+ assert_equal 1, conn.llen("queue:super")
137
+ end
138
+
139
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 20, 20)
140
+ Time.stubs(:now).returns(enqueued_time)
141
+ @poller.poll false
142
+ Sidekiq.redis do |conn|
143
+ assert_equal 1, conn.llen("queue:default")
144
+ assert_equal 1, conn.llen("queue:super")
145
+ end
146
+
147
+ enqueued_time = Time.new(now.year, now.month, now.day, now.hour + 1, 20, 50)
148
+ Time.stubs(:now).returns(enqueued_time)
149
+ @poller.poll false
150
+ Sidekiq.redis do |conn|
151
+ assert_equal 1, conn.llen("queue:default")
152
+ assert_equal 1, conn.llen("queue:super")
153
+ end
154
+ end
155
+
156
+ end
157
+ end
@@ -0,0 +1,92 @@
1
+ require './test/test_helper'
2
+
3
+ class CronWebExtensionTest < Test::Unit::TestCase
4
+ context 'Cron web' do
5
+ include Rack::Test::Methods
6
+
7
+ def app
8
+ Sidekiq::Web
9
+ end
10
+
11
+ setup do
12
+ Sidekiq.redis = REDIS
13
+ Sidekiq.redis {|c| c.flushdb }
14
+
15
+ #clear all previous saved data from redis
16
+ Sidekiq.redis do |conn|
17
+ conn.keys("cron_job*").each do |key|
18
+ conn.del(key)
19
+ end
20
+ end
21
+
22
+ @args = {
23
+ name: "TestNameOfCronJob",
24
+ cron: "*/2 * * * *",
25
+ klass: "CronTestClass"
26
+ }
27
+
28
+ end
29
+
30
+ should 'display cron web' do
31
+ get '/cron'
32
+ assert_equal 200, last_response.status
33
+ end
34
+
35
+ should 'display cron web with message - no cron jobs' do
36
+ get '/cron'
37
+ assert last_response.body.include?('No cron jobs found')
38
+ end
39
+
40
+ should 'display cron web with cron jobs table' do
41
+ Sidekiq::Cron::Job.create(@args)
42
+ get '/cron'
43
+ refute last_response.body.include?('No cron jobs found')
44
+ assert last_response.body.include?('table')
45
+ assert last_response.body.include?("TestNameOfCronJob")
46
+ end
47
+
48
+ context "work with cron job" do
49
+
50
+ setup do
51
+ @job = Sidekiq::Cron::Job.new(@args.merge(status: "enabled"))
52
+ @job.save
53
+ @name = "TestNameOfCronJob"
54
+ end
55
+
56
+ should "disable and enable cron job" do
57
+ post "/cron/#{@name}/disable"
58
+ assert_equal Sidekiq::Cron::Job.find(@name).status, "disabled"
59
+
60
+ post "/cron/#{@name}/enable"
61
+ assert_equal Sidekiq::Cron::Job.find(@name).status, "enabled"
62
+ end
63
+
64
+ should "enqueue job" do
65
+ Sidekiq.redis do |conn|
66
+ assert_equal 0, conn.llen("queue:default"), "Queue should have no jobs"
67
+ end
68
+
69
+ post "/cron/#{@name}/enque"
70
+
71
+ Sidekiq.redis do |conn|
72
+ assert_equal 1, conn.llen("queue:default"), "Queue should have 1 job"
73
+ end
74
+
75
+ #should enqueue more times
76
+ post "/cron/#{@name}/enque"
77
+
78
+ Sidekiq.redis do |conn|
79
+ assert_equal 2, conn.llen("queue:default"), "Queue should have 2 job"
80
+ end
81
+ end
82
+
83
+ should "destroy job" do
84
+ assert_equal Sidekiq::Cron::Job.all.size, 1, "Should have 1 job"
85
+ post "/cron/#{@name}/delete"
86
+ assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have zero jobs"
87
+ end
88
+
89
+
90
+ end
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,313 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-cron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ondrej Bartas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sidekiq
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.13.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.13.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: parse-cron
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.1.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.1.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: shoulda-context
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: turn
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rack
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rack-test
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: jeweler
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 1.8.3
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 1.8.3
158
+ - !ruby/object:Gem::Dependency
159
+ name: sdoc
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: slim
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ - !ruby/object:Gem::Dependency
191
+ name: sinatra
192
+ requirement: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ - !ruby/object:Gem::Dependency
207
+ name: mocha
208
+ requirement: !ruby/object:Gem::Requirement
209
+ none: false
210
+ requirements:
211
+ - - '>='
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
214
+ type: :development
215
+ prerelease: false
216
+ version_requirements: !ruby/object:Gem::Requirement
217
+ none: false
218
+ requirements:
219
+ - - '>='
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ - !ruby/object:Gem::Dependency
223
+ name: coveralls
224
+ requirement: !ruby/object:Gem::Requirement
225
+ none: false
226
+ requirements:
227
+ - - '>='
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ none: false
234
+ requirements:
235
+ - - '>='
236
+ - !ruby/object:Gem::Version
237
+ version: '0'
238
+ - !ruby/object:Gem::Dependency
239
+ name: shotgun
240
+ requirement: !ruby/object:Gem::Requirement
241
+ none: false
242
+ requirements:
243
+ - - '>='
244
+ - !ruby/object:Gem::Version
245
+ version: '0'
246
+ type: :development
247
+ prerelease: false
248
+ version_requirements: !ruby/object:Gem::Requirement
249
+ none: false
250
+ requirements:
251
+ - - '>='
252
+ - !ruby/object:Gem::Version
253
+ version: '0'
254
+ description: Enables to set jobs to be run in specified time (using CRON notation)
255
+ email: ondrej@bartas.cz
256
+ executables: []
257
+ extensions: []
258
+ extra_rdoc_files:
259
+ - LICENSE.txt
260
+ - README.md
261
+ files:
262
+ - .document
263
+ - .travis.yml
264
+ - Changes.md
265
+ - Gemfile
266
+ - LICENSE.txt
267
+ - README.md
268
+ - Rakefile
269
+ - VERSION
270
+ - config.ru
271
+ - examples/web-cron-ui.png
272
+ - lib/sidekiq-cron.rb
273
+ - lib/sidekiq/cron.rb
274
+ - lib/sidekiq/cron/job.rb
275
+ - lib/sidekiq/cron/locales/en.yml
276
+ - lib/sidekiq/cron/poller.rb
277
+ - lib/sidekiq/cron/views/cron.slim
278
+ - lib/sidekiq/cron/web_extension.rb
279
+ - lib/sidekiq/launcher.rb
280
+ - sidekiq-cron.gemspec
281
+ - test/test_helper.rb
282
+ - test/unit/job_test.rb
283
+ - test/unit/poller_test.rb
284
+ - test/unit/web_extesion_test.rb
285
+ homepage: http://github.com/ondrejbartas/sidekiq-cron
286
+ licenses:
287
+ - MIT
288
+ post_install_message:
289
+ rdoc_options: []
290
+ require_paths:
291
+ - lib
292
+ required_ruby_version: !ruby/object:Gem::Requirement
293
+ none: false
294
+ requirements:
295
+ - - '>='
296
+ - !ruby/object:Gem::Version
297
+ version: '0'
298
+ segments:
299
+ - 0
300
+ hash: -3914560018168738406
301
+ required_rubygems_version: !ruby/object:Gem::Requirement
302
+ none: false
303
+ requirements:
304
+ - - '>='
305
+ - !ruby/object:Gem::Version
306
+ version: '0'
307
+ requirements: []
308
+ rubyforge_project:
309
+ rubygems_version: 1.8.25
310
+ signing_key:
311
+ specification_version: 3
312
+ summary: Sidekiq Cron helps to add repeated scheduled jobs
313
+ test_files: []