rufus-scheduler 1.0.12 → 1.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,356 +0,0 @@
1
-
2
- #
3
- # Testing Rufus
4
- #
5
- # John Mettraux at openwfe.org
6
- #
7
- # Sun Oct 29 16:18:25 JST 2006
8
- #
9
-
10
- $:.unshift(File.dirname(__FILE__) + '/../lib')
11
-
12
- require 'test/unit'
13
- require 'rufus/scheduler'
14
-
15
-
16
- #
17
- # testing otime and the scheduler
18
- #
19
- class Scheduler0Test < Test::Unit::TestCase
20
-
21
- def test_scheduler_0
22
-
23
- #Thread.abort_on_exception = true
24
-
25
- $var = nil
26
-
27
- scheduler = Rufus::Scheduler.start_new
28
-
29
- sid = scheduler.schedule_in('2s', :schedulable => TestSchedulable.new)
30
-
31
- assert \
32
- sid,
33
- "scheduler_0 did not return a job id"
34
-
35
- assert \
36
- (not $var),
37
- "scheduler_0 is blocking but should not"
38
-
39
- sleep 2.5
40
-
41
- assert_equal 0, scheduler.cron_job_count
42
- assert_equal 0, scheduler.every_job_count
43
- assert_equal 0, scheduler.at_job_count
44
- assert_equal 0, scheduler.pending_job_count
45
-
46
- scheduler.sstop
47
-
48
- #puts ">#{$var}<"
49
-
50
- assert_equal "ok", $var
51
- end
52
-
53
- def test_scheduler_1
54
-
55
- $var = nil
56
-
57
- scheduler = Rufus::Scheduler.new
58
- scheduler.start
59
-
60
- sid = scheduler.in('1s') do
61
- $var = "ok..1"
62
- end
63
-
64
- assert \
65
- sid,
66
- "scheduler_1 did not return a job id"
67
-
68
- assert \
69
- (not $var),
70
- "scheduler_1 is blocking but should not"
71
-
72
- sleep 2
73
- scheduler.sstop
74
-
75
- #puts ">#{$var}<"
76
-
77
- assert "ok..1", $var
78
- end
79
-
80
- #
81
- # test idea by ara.t.howard on the ruby-talk ml
82
- #
83
- def test_scheduler_2
84
-
85
- text = ""
86
-
87
- scheduler = Rufus::Scheduler.new()
88
- scheduler.start
89
-
90
- scheduler.schedule_in("1s") do
91
- text << "one"
92
- sleep(2)
93
- end
94
- scheduler.schedule_in("1s") do
95
- text << "two"
96
- end
97
-
98
- sleep(2)
99
-
100
- scheduler.sstop
101
-
102
- #puts text
103
-
104
- assert_equal text, "onetwo"
105
- end
106
-
107
- #
108
- # Testing schedule_every()
109
- #
110
- def test_scheduler_4
111
-
112
- Thread.abort_on_exception = true
113
-
114
- scheduler = Rufus::Scheduler.start_new
115
-
116
- #
117
- # phase 0
118
-
119
- count = 0
120
-
121
- job_id = scheduler.schedule_every("1s") do
122
- count += 1
123
- end
124
-
125
- #puts "0 job_id : " + job_id.to_s
126
-
127
- sleep 3.5
128
-
129
- assert_equal 3, count
130
-
131
- job = scheduler.get_job job_id
132
-
133
- assert_equal "1s", job.schedule_info
134
-
135
- assert_job_count scheduler, 0, 0, 1
136
-
137
- #
138
- # phase 1
139
-
140
- es = EverySchedulable.new
141
-
142
- job_id = scheduler.every "500", es
143
-
144
- #puts "1 job_id : " + job_id.to_s
145
-
146
- #sleep(3.4) # was a bit soonish for JRuby...
147
- sleep 3.5
148
-
149
- #assert_job_count scheduler, 0, 0, 2
150
- assert_equal 6, es.count
151
-
152
- scheduler.unschedule job_id
153
-
154
- sleep 1
155
-
156
- assert_equal 6, es.count
157
-
158
- assert_nil scheduler.get_job(job_id)
159
- assert_equal 1, scheduler.every_job_count # one left from before
160
-
161
- # done
162
-
163
- scheduler.sstop
164
- end
165
-
166
- #
167
- # testing to see if the scheduler immediately executes schedule_in(t)
168
- # requests where t < scheduler.frequency.
169
- # (100ms < 250ms)
170
- #
171
- def test_scheduler_5
172
-
173
- scheduler = Rufus::Scheduler.new
174
- scheduler.start
175
-
176
- touched = false
177
-
178
- scheduler.schedule_in "100" do
179
- touched = true
180
- end
181
-
182
- assert touched
183
-
184
- scheduler.sstop
185
- end
186
-
187
- #
188
- # Testing to see if a second job with the same id discards the first one.
189
- #
190
- def test_scheduler_6
191
-
192
- scheduler = Rufus::Scheduler.new
193
- scheduler.start
194
-
195
- #class << scheduler
196
- # attr_reader :pending_jobs
197
- #end
198
-
199
- value = nil
200
-
201
- scheduler.schedule_in "3s", :job_id => "job" do
202
- value = 0
203
- end
204
- scheduler.schedule_in "2s", :job_id => "job" do
205
- value = 1
206
- end
207
-
208
- sleep 0.5
209
-
210
- #puts scheduler.pending_jobs.collect { |j| j.job_id }.inspect
211
- assert_job_count scheduler, 0, 1, 0
212
-
213
- assert_nil value
214
-
215
- sleep 2.0
216
-
217
- #puts scheduler.pending_jobs.collect { |j| j.job_id }.inspect
218
- assert_job_count scheduler, 0, 0, 0
219
-
220
- assert_equal 1, value
221
-
222
- sleep 4
223
-
224
- assert_equal 1, value
225
-
226
- scheduler.stop
227
- end
228
-
229
- #
230
- # Testing custom precision.
231
- #
232
- def test_scheduler_7
233
-
234
- scheduler = Rufus::Scheduler.new(:scheduler_precision => 0.100)
235
-
236
- assert_equal 0.100, scheduler.precision
237
- end
238
-
239
- #
240
- # Making sure that a job scheduled in the past is executed immediately
241
- # and not scheduled.
242
- #
243
- # This test also makes sure that schedule_at() understands the
244
- # time.to_s format.
245
- #
246
- def test_8
247
-
248
- scheduler = Rufus::Scheduler.new
249
- scheduler.start
250
-
251
- var = false
252
-
253
- job_id = scheduler.schedule_at Time.now.to_s do
254
- var = true
255
- end
256
-
257
- assert_equal var, true
258
- assert_nil job_id
259
- end
260
-
261
- #
262
- # Scheduling in the past, with :discard_past set to true.
263
- #
264
- def test_8b
265
-
266
- scheduler = Rufus::Scheduler.start_new
267
-
268
- var = nil
269
-
270
- job_id = scheduler.at(Time.now.to_s, :discard_past => true) do
271
- var = "something"
272
- end
273
-
274
- assert_nil var
275
- assert_nil job_id
276
-
277
- scheduler.stop
278
- end
279
-
280
- #
281
- # Testing restarting the scheduler.
282
- #
283
- def test_9
284
-
285
- scheduler = Rufus::Scheduler.new
286
- scheduler.start
287
-
288
- value = nil
289
-
290
- scheduler.schedule_in "2s" do
291
- value = 0
292
- end
293
-
294
- assert_nil value
295
-
296
- scheduler.stop
297
-
298
- sleep 0.5
299
-
300
- scheduler.start
301
-
302
- assert_nil value
303
-
304
- sleep 2
305
-
306
- assert_equal value, 0
307
-
308
- scheduler.stop
309
- end
310
-
311
- def test_10
312
-
313
- e = nil
314
- begin
315
- Rufus::Scheduler.new.precision = 10
316
- rescue Exception => e
317
- end
318
-
319
- assert_not_nil e, "exception not caught"
320
- end
321
-
322
- protected
323
-
324
- class TestSchedulable
325
- include Rufus::Schedulable
326
-
327
- def trigger (params)
328
- $var = "ok"
329
- end
330
- end
331
-
332
- class EverySchedulable
333
- include Rufus::Schedulable
334
-
335
- attr_accessor :job_id, :count
336
-
337
- def initialize
338
- @job_id = -1
339
- @count = 0
340
- end
341
-
342
- def trigger (params)
343
- #puts "toto"
344
- @count += 1
345
- end
346
- end
347
-
348
- def assert_job_count (scheduler, cron, at, every)
349
-
350
- assert_equal cron, scheduler.cron_job_count
351
- assert_equal at, scheduler.at_job_count
352
- assert_equal every, scheduler.every_job_count
353
- assert_equal at + every, scheduler.pending_job_count
354
- end
355
-
356
- end
@@ -1,82 +0,0 @@
1
-
2
- #
3
- # Testing Rufus
4
- #
5
- # John Mettraux at openwfe.org
6
- #
7
- # Sun Oct 29 16:18:25 JST 2006
8
- #
9
-
10
- $:.unshift(File.dirname(__FILE__) + '/../lib')
11
-
12
- require 'test/unit'
13
- require 'openwfe/util/scheduler'
14
-
15
-
16
- #
17
- # testing otime and the scheduler
18
- #
19
- class Scheduler1Test < Test::Unit::TestCase
20
-
21
- def test_0
22
-
23
- scheduler = Rufus::Scheduler.new
24
- scheduler.start
25
-
26
- job_id = scheduler.schedule_every "500", :tags => "Avery" do
27
- # don't do a thing
28
- end
29
-
30
- sleep 0.300
31
-
32
- count = nil
33
-
34
- 200_000.times do |i|
35
- break if scheduler.get_job(job_id) == nil
36
- count = i + 1
37
- end
38
-
39
- scheduler.sstop
40
-
41
- assert_equal 200_000, count
42
- end
43
-
44
- def test_1
45
-
46
- scheduler = Rufus::Scheduler.start_new
47
-
48
- job_id = scheduler.schedule_every "500", :tags => "Avery" do
49
- # don't do a thing
50
- end
51
-
52
- sleep 0.300
53
-
54
- count = 1
55
-
56
- 200_000.times do
57
- count = scheduler.find_jobs("Avery").size
58
- #p scheduler.instance_variable_get(:@non_cron_jobs).keys if count != 1
59
- break if count != 1
60
- end
61
-
62
- scheduler.sstop
63
-
64
- assert_equal 1, count
65
- end
66
-
67
- #
68
- # testing "deviation", if I may call it like that...
69
- #
70
- def _test_2
71
-
72
- scheduler = Rufus::Scheduler.start_new
73
- last = nil
74
- job_id = scheduler.schedule_every "1s" do
75
- t = Time.now
76
- puts t.to_f
77
- end
78
- sleep 4 * 60
79
- scheduler.sstop
80
- end
81
-
82
- end
@@ -1,119 +0,0 @@
1
-
2
- #
3
- # Testing Rufus
4
- #
5
- # John Mettraux at openwfe.org
6
- #
7
- # Sun Oct 29 16:18:25 JST 2006
8
- #
9
-
10
- $:.unshift(File.dirname(__FILE__) + '/../lib')
11
-
12
- require 'test/unit'
13
- require 'openwfe/util/scheduler'
14
-
15
-
16
- #
17
- # testing otime and the scheduler
18
- #
19
- class Scheduler2Test < Test::Unit::TestCase
20
-
21
- def test_0
22
-
23
- scheduler = Rufus::Scheduler.new
24
- scheduler.start
25
-
26
- counter = 0
27
- $error_counter = 0
28
-
29
- def scheduler.lwarn (&block)
30
- #puts block.call
31
- $error_counter += 1
32
- end
33
-
34
- job_id = scheduler.schedule_every "500" do
35
- counter += 1
36
- raise "exception!"
37
- end
38
-
39
- sleep 2.300
40
-
41
- scheduler.sstop
42
-
43
- assert_equal 4, counter, "execution count wrong"
44
- assert_equal 4, $error_counter, "error count wrong"
45
- end
46
-
47
- def test_1
48
-
49
- # repeating myself
50
-
51
- scheduler = Rufus::Scheduler.new
52
- scheduler.start
53
-
54
- counter = 0
55
- $error_counter = 0
56
-
57
- def scheduler.lwarn (&block)
58
- #puts block.call
59
- $error_counter += 1
60
- end
61
-
62
- job_id = scheduler.schedule_every "500", :try_again => false do
63
- counter += 1
64
- raise "exception?"
65
- end
66
-
67
- sleep 2.300
68
-
69
- scheduler.sstop
70
-
71
- assert_equal 1, counter, "execution count wrong"
72
- assert_equal 1, $error_counter, "error count wrong"
73
- end
74
-
75
- def test_2
76
-
77
- scheduler = Rufus::Scheduler.new
78
- scheduler.start
79
-
80
- def scheduler.lwarn (&block)
81
- puts block.call
82
- end
83
-
84
- counter = 0
85
-
86
- job_id = scheduler.schedule_every "500" do |job_id, at, params|
87
- counter += 1
88
- params[:dont_reschedule] = true if counter == 2
89
- end
90
-
91
- sleep 3.000
92
-
93
- assert_equal 2, counter
94
- end
95
-
96
- def test_3
97
-
98
- # repeating myself ...
99
-
100
- scheduler = Rufus::Scheduler.new
101
- scheduler.start
102
-
103
- def scheduler.lwarn (&block)
104
- puts block.call
105
- end
106
-
107
- counter = 0
108
-
109
- job_id = scheduler.schedule_every "500" do |job_id, at, params|
110
- counter += 1
111
- params[:every] = "1s" if counter == 2
112
- end
113
-
114
- sleep 5.000
115
-
116
- assert_equal 2 + 3, counter
117
- end
118
-
119
- end
@@ -1,72 +0,0 @@
1
-
2
- #
3
- # Testing Rufus
4
- #
5
- # John Mettraux at openwfe.org
6
- #
7
- # Sun Oct 29 16:18:25 JST 2006
8
- #
9
-
10
- $:.unshift(File.dirname(__FILE__) + '/../lib')
11
-
12
- require 'test/unit'
13
- require 'openwfe/util/scheduler'
14
-
15
-
16
- class Scheduler3Test < Test::Unit::TestCase
17
-
18
- #
19
- # Testing tags
20
- #
21
- def test_0
22
-
23
- scheduler = Rufus::Scheduler.new
24
- scheduler.start
25
-
26
- value = nil
27
-
28
- scheduler.schedule_in "3s", :tags => "fish" do
29
- value = "fish"
30
- end
31
-
32
- sleep 0.300 # let the job get really scheduled
33
-
34
- assert_equal [], scheduler.find_jobs('deer')
35
- assert_equal 1, scheduler.find_jobs('fish').size
36
-
37
- scheduler.schedule "* * * * *", :tags => "fish" do
38
- value = "cron-fish"
39
- end
40
- scheduler.cron "* * * * *", :tags => "vegetable" do
41
- value = "daikon"
42
- end
43
-
44
- sleep 0.300 # let the jobs get really scheduled
45
-
46
- assert_equal 2, scheduler.find_jobs('fish').size
47
- #puts scheduler.find_jobs('fish')
48
-
49
- assert_equal(
50
- 3,
51
- scheduler.all_jobs.size)
52
- assert_equal(
53
- [ "Rufus::CronJob", "Rufus::CronJob", "Rufus::AtJob" ],
54
- scheduler.all_jobs.collect { |j| j.class.name })
55
-
56
- scheduler.find_jobs('fish').each do |job|
57
- scheduler.unschedule(job.job_id)
58
- end
59
-
60
- sleep 0.300 # give it some time to unschedule
61
-
62
- assert_equal [], scheduler.find_jobs('fish')
63
- assert_equal 1, scheduler.find_jobs('vegetable').size
64
-
65
- scheduler.find_jobs('vegetable')[0].unschedule
66
-
67
- sleep 0.300 # give it some time to unschedule
68
-
69
- assert_equal 0, scheduler.find_jobs('vegetable').size
70
- end
71
-
72
- end
@@ -1,77 +0,0 @@
1
-
2
- #
3
- # Testing the 'rufus-scheduler'
4
- #
5
- # John Mettraux at openwfe.org
6
- #
7
- # Tue Jan 8 13:46:17 JST 2008
8
- #
9
-
10
- $:.unshift(File.dirname(__FILE__) + '/../lib')
11
-
12
- require 'test/unit'
13
- require 'rufus/scheduler'
14
-
15
-
16
- class Scheduler4Test < Test::Unit::TestCase
17
-
18
- #
19
- # Checking that a sleep in a schedule won't raise any exception
20
- #
21
- def test_0
22
-
23
- s = Rufus::Scheduler.new
24
- s.start
25
-
26
- $exception = nil
27
-
28
- class << s
29
- def lwarn (&block)
30
- $exception = block.call
31
- end
32
- end
33
-
34
- counters = Counters.new
35
-
36
- s.schedule_every "2s" do
37
- counters.inc :a
38
- sleep 4
39
- counters.inc :b
40
- end
41
- s.schedule_every "3s" do
42
- counters.inc :c
43
- end
44
- #p Time.now.to_f
45
-
46
- sleep 10.600
47
-
48
- s.stop
49
-
50
- assert_equal({ :a => 3, :b => 2, :c => 3 }, counters.counters)
51
- assert_nil $exception
52
- end
53
-
54
- protected
55
-
56
- class Counters
57
-
58
- attr_reader :counters
59
-
60
- def initialize
61
-
62
- @counters = {}
63
- end
64
-
65
- def inc (counter)
66
-
67
- @counters[counter] ||= 0
68
- @counters[counter] += 1
69
-
70
- #puts(
71
- # "#{counter} _ " +
72
- # "#{Time.now.to_f} #{@counters.inspect} " +
73
- # "(#{Thread.current.object_id})")
74
- end
75
- end
76
-
77
- end