rufus-scheduler 1.0

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