rufus-scheduler 2.0.3 → 2.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+
2
+ #
3
+ # Specifying rufus-scheduler
4
+ #
5
+ # Sat Mar 21 17:36:36 JST 2009
6
+ #
7
+
8
+ require File.dirname(__FILE__) + '/spec_base'
9
+
10
+
11
+ describe SCHEDULER_CLASS do
12
+
13
+ before do
14
+ @s = start_scheduler
15
+ end
16
+ after do
17
+ stop_scheduler(@s)
18
+ end
19
+
20
+ JOB = Proc.new do |x|
21
+ begin
22
+ $var << "a#{x}"
23
+ sleep 0.500
24
+ $var << "b#{x}"
25
+ rescue Exception => e
26
+ puts '=' * 80
27
+ p e
28
+ puts '=' * 80
29
+ end
30
+ end
31
+
32
+ it 'should not block when :blocking => nil' do
33
+
34
+ $var = []
35
+ @s.in('1s') { JOB.call(1) }
36
+ @s.in('1s') { JOB.call(2) }
37
+
38
+ sleep 5.0
39
+
40
+ [ %w{ a1 a2 b1 b2 }, %w{ a1 a2 b2 b1 } ].should.include($var)
41
+ end
42
+
43
+ it 'should block when :blocking => true' do
44
+
45
+ $var = []
46
+ @s.in('1s', :blocking => true) { JOB.call(8) }
47
+ @s.in('1s', :blocking => true) { JOB.call(9) }
48
+
49
+ sleep 4.5
50
+
51
+ $var.should.equal(%w{ a8 b8 a9 b9 })
52
+ end
53
+ end
54
+
data/spec/cron_spec.rb ADDED
@@ -0,0 +1,122 @@
1
+
2
+ #
3
+ # Specifying rufus-scheduler
4
+ #
5
+ # Sun Mar 22 19:59:12 JST 2009
6
+ #
7
+
8
+ require File.dirname(__FILE__) + '/spec_base'
9
+
10
+
11
+ describe "#{SCHEDULER_CLASS}#cron" do
12
+
13
+ before do
14
+ @s = start_scheduler
15
+ end
16
+ after do
17
+ stop_scheduler(@s)
18
+ end
19
+
20
+ it 'should have job ids with the class name in it' do
21
+
22
+ j0 = @s.cron('7 10-12 * * * *') {}
23
+ j0.job_id.should.match(/Rufus::Scheduler::CronJob/)
24
+ end
25
+
26
+ it 'should cron every second' do
27
+
28
+ seconds = []
29
+
30
+ job = @s.cron '* * * * * *' do |job|
31
+ seconds << job.last.sec
32
+ end
33
+ sleep 4.990
34
+
35
+ job.unschedule
36
+
37
+ seconds.uniq.size.should.equal(seconds.size)
38
+ end
39
+
40
+ it 'should unschedule' do
41
+
42
+ second = nil
43
+
44
+ job = @s.cron '* * * * * *' do |job|
45
+ second = job.last.sec
46
+ end
47
+
48
+ second.should.be.nil
49
+
50
+ sleep 2
51
+
52
+ second.should.not.be.nil
53
+
54
+ job.unschedule
55
+
56
+ after = second
57
+
58
+ sleep 2
59
+
60
+ second.should.equal(after)
61
+ end
62
+
63
+ it 'should keep track of cron jobs' do
64
+
65
+ j0 = @s.cron '7 10-12 * * * *' do
66
+ end
67
+ j1 = @s.cron '7 10-12 * * * *' do
68
+ end
69
+
70
+ @s.cron_jobs.keys.sort.should.equal([ j0.job_id, j1.job_id ].sort)
71
+ end
72
+
73
+ it 'should accept tags for jobs' do
74
+
75
+ job = @s.cron '* * * * * *', :tags => 'spec' do
76
+ end
77
+
78
+ wait_next_tick
79
+
80
+ @s.find_by_tag('spec').size.should.equal(1)
81
+ @s.find_by_tag('spec').first.job_id.should.equal(job.job_id)
82
+ end
83
+
84
+ it 'should accept job.unschedule within the job' do
85
+
86
+ stack = []
87
+
88
+ @s.cron '* * * * * *' do |job|
89
+ if stack.size > 2
90
+ stack << 'done'
91
+ job.unschedule
92
+ else
93
+ stack << 'ok'
94
+ end
95
+ end
96
+
97
+ sleep 4
98
+
99
+ @s.jobs.size.should.equal(0)
100
+ stack.should.equal(%w[ ok ok ok done ])
101
+ end
102
+
103
+ end
104
+
105
+ describe Rufus::Scheduler::CronJob do
106
+
107
+ before do
108
+ @s = start_scheduler
109
+ end
110
+ after do
111
+ stop_scheduler(@s)
112
+ end
113
+
114
+ it 'should respond to #next_time' do
115
+
116
+ job = @s.cron '* * * * *' do
117
+ end
118
+
119
+ (job.next_time.to_i - Time.now.to_i).should.satisfy { |v| v < 60 }
120
+ end
121
+ end
122
+
@@ -0,0 +1,67 @@
1
+
2
+ #
3
+ # Specifying rufus-scheduler
4
+ #
5
+ # Sat Mar 21 12:55:27 JST 2009
6
+ #
7
+
8
+ require File.dirname(__FILE__) + '/spec_base'
9
+
10
+
11
+ def cl (cronline_string)
12
+ Rufus::CronLine.new(cronline_string)
13
+ end
14
+
15
+
16
+ describe Rufus::CronLine do
17
+
18
+ def should (line, array)
19
+
20
+ cl(line).to_array.should.equal(array)
21
+ end
22
+
23
+ it 'should interpret cron strings correctly' do
24
+
25
+ should '* * * * *', [ [0], nil, nil, nil, nil, nil ]
26
+ should '10-12 * * * *', [ [0], [10, 11, 12], nil, nil, nil, nil ]
27
+ should '* * * * sun,mon', [ [0], nil, nil, nil, nil, [0, 1] ]
28
+ should '* * * * mon-wed', [ [0], nil, nil, nil, nil, [1, 2, 3] ]
29
+ should '* * * * 7', [ [0], nil, nil, nil, nil, [0] ]
30
+ should '* * * * 0', [ [0], nil, nil, nil, nil, [0] ]
31
+ should '* * * * 0,1', [ [0], nil, nil, nil, nil, [0,1] ]
32
+ should '* * * * 7,1', [ [0], nil, nil, nil, nil, [0,1] ]
33
+ should '* * * * 7,0', [ [0], nil, nil, nil, nil, [0] ]
34
+ should '* * * * sun,2-4', [ [0], nil, nil, nil, nil, [0, 2, 3, 4] ]
35
+
36
+ should '* * * * sun,mon-tue', [ [0], nil, nil, nil, nil, [0, 1, 2] ]
37
+
38
+ should '* * * * * *', [ nil, nil, nil, nil, nil, nil ]
39
+ should '1 * * * * *', [ [1], nil, nil, nil, nil, nil ]
40
+ should '7 10-12 * * * *', [ [7], [10, 11, 12], nil, nil, nil, nil ]
41
+ should '1-5 * * * * *', [ [1,2,3,4,5], nil, nil, nil, nil, nil ]
42
+ end
43
+ end
44
+
45
+ describe 'Rufus::CronLine#next_time' do
46
+
47
+ it 'should compute next occurence correctly' do
48
+
49
+ now = Time.at(0).utc # Thu Jan 01 00:00:00 UTC 1970
50
+
51
+ cl('* * * * *').next_time(now).should.equal(now + 60)
52
+ cl('* * * * sun').next_time(now).should.equal(now + 259200)
53
+ cl('* * * * * *').next_time(now).should.equal(now + 1)
54
+ cl('* * 13 * fri').next_time(now).should.equal(now + 3715200)
55
+
56
+ cl('10 12 13 12 *').next_time(now).should.equal(now + 29938200)
57
+ # this one is slow (1 year == 3 seconds)
58
+
59
+ cl('0 0 * * thu').next_time(now).should.equal(now + 604800)
60
+
61
+ now = Time.local(2008, 12, 31, 23, 59, 59, 0)
62
+
63
+ cl('* * * * *').next_time(now).should.equal(now + 1)
64
+ end
65
+
66
+ end
67
+
@@ -0,0 +1,193 @@
1
+
2
+ #
3
+ # Specifying rufus-scheduler
4
+ #
5
+ # Sun Mar 22 12:26:07 JST 2009
6
+ #
7
+
8
+ require File.dirname(__FILE__) + '/spec_base'
9
+
10
+
11
+ describe "#{SCHEDULER_CLASS}#every" do
12
+
13
+ before do
14
+ @s = start_scheduler
15
+ end
16
+ after do
17
+ stop_scheduler(@s)
18
+ end
19
+
20
+ it 'should have job ids with the class name in it' do
21
+
22
+ j0 = @s.every(1) {}
23
+ j0.job_id.should.match(/Rufus::Scheduler::EveryJob/)
24
+ end
25
+
26
+ it 'should compute frequency' do
27
+
28
+ job = @s.every '2h1m' do
29
+ end
30
+ job.frequency.should.equal(7260)
31
+ end
32
+
33
+ it 'should schedule every 1s' do
34
+
35
+ var = 0
36
+
37
+ job = @s.every '1s' do
38
+ var += 1
39
+ end
40
+
41
+ sleep 3.7
42
+
43
+ var.should.equal(3)
44
+ end
45
+
46
+ it 'should be punctilious' do
47
+
48
+ hits = []
49
+
50
+ job = @s.every '1s' do
51
+ hits << Time.now.to_f
52
+ end
53
+
54
+ sleep 9.9
55
+
56
+ hh = nil
57
+ deltas = []
58
+ hits.each { |h| f = h; deltas << (f - hh) if hh; hh = f }
59
+
60
+ #puts; p deltas
61
+ deltas.max.should.satisfy { |d| d < 1.200 }
62
+ end
63
+
64
+ it 'should unschedule' do
65
+
66
+ var = 0
67
+
68
+ job = @s.every '1s' do
69
+ var += 1
70
+ end
71
+
72
+ sleep 2.7
73
+
74
+ @s.unschedule(job.job_id)
75
+
76
+ var.should.equal(2)
77
+
78
+ sleep 1.7
79
+
80
+ var.should.equal(2)
81
+ end
82
+
83
+ it 'should accept tags for jobs' do
84
+
85
+ job = @s.every '1s', :tags => 'spec' do
86
+ end
87
+
88
+ wait_next_tick
89
+
90
+ @s.find_by_tag('spec').size.should.equal(1)
91
+ @s.find_by_tag('spec').first.job_id.should.equal(job.job_id)
92
+ end
93
+
94
+ it 'should honour :first_at' do
95
+
96
+ counter = 0
97
+
98
+ job = @s.every '1s', :first_at => Time.now + 2 do
99
+ counter += 1
100
+ end
101
+
102
+ sleep 1
103
+ counter.should.equal(0)
104
+
105
+ sleep 2.5
106
+ counter.should.equal(2)
107
+ end
108
+
109
+ it 'should honour :first_in' do
110
+
111
+ counter = 0
112
+
113
+ job = @s.every '1s', :first_in => 2 do
114
+ counter += 1
115
+ end
116
+
117
+ sleep 1
118
+ counter.should.equal(0)
119
+
120
+ sleep 2.5
121
+ counter.should.equal(2)
122
+ end
123
+
124
+ #it 'should honour :dont_reschedule' do
125
+ # stack = []
126
+ # @s.every 0.400 do |job|
127
+ # if stack.size > 3
128
+ # stack << 'done'
129
+ # job.params[:dont_reschedule] = true
130
+ # else
131
+ # stack << 'ok'
132
+ # end
133
+ # end
134
+ # sleep 4
135
+ # @s.jobs.size.should.equal(0)
136
+ # stack.should.equal(%w[ ok ok ok ok done ])
137
+ #end
138
+
139
+ it 'should accept job.unschedule within the job' do
140
+
141
+ stack = []
142
+
143
+ @s.every 0.400 do |job|
144
+ if stack.size > 3
145
+ stack << 'done'
146
+ job.unschedule
147
+ else
148
+ stack << 'ok'
149
+ end
150
+ end
151
+
152
+ sleep 4
153
+
154
+ @s.jobs.size.should.equal(0)
155
+ stack.should.equal(%w[ ok ok ok ok done ])
156
+ end
157
+
158
+ it 'should respect :blocking => true' do
159
+
160
+ stack = []
161
+
162
+ @s.every '1s', :blocking => true do |job|
163
+ stack << 'ok'
164
+ sleep 2
165
+ end
166
+
167
+ sleep 5
168
+
169
+ stack.should.equal(%w[ ok ok ])
170
+ end
171
+
172
+ end
173
+
174
+ describe Rufus::Scheduler::EveryJob do
175
+
176
+ before do
177
+ @s = start_scheduler
178
+ end
179
+ after do
180
+ stop_scheduler(@s)
181
+ end
182
+
183
+ it 'should respond to #next_time' do
184
+
185
+ t = Time.now + 3 * 3600
186
+
187
+ job = @s.every '3h' do
188
+ end
189
+
190
+ job.next_time.to_i.should.equal(t.to_i)
191
+ end
192
+ end
193
+
@@ -0,0 +1,77 @@
1
+
2
+ #
3
+ # Specifying rufus-scheduler
4
+ #
5
+ # Mon May 4 17:07:17 JST 2009
6
+ #
7
+
8
+ require File.dirname(__FILE__) + '/spec_base'
9
+
10
+
11
+ describe SCHEDULER_CLASS do
12
+
13
+ before do
14
+ @s = start_scheduler
15
+ end
16
+ after do
17
+ stop_scheduler(@s)
18
+ end
19
+
20
+ it 'should emit exception messages to stdout' do
21
+
22
+ require 'stringio' unless defined?(StringIO) # ruby 1.9
23
+
24
+ stdout = $stdout
25
+ s = StringIO.new
26
+ $stdout = s
27
+
28
+ @s.in 0.400 do
29
+ raise 'Houston we have a problem'
30
+ end
31
+
32
+ sleep 0.500
33
+ sleep 0.500
34
+ $stdout = stdout
35
+ s.close
36
+
37
+ s.string.should.match(/Houston we have a problem/)
38
+ end
39
+
40
+ it 'should accept custom handling of exceptions' do
41
+
42
+ $job = nil
43
+
44
+ def @s.handle_exception (j, e)
45
+ $job = j
46
+ end
47
+
48
+ @s.in 0.400 do
49
+ raise 'Houston we have a problem'
50
+ end
51
+
52
+ sleep 0.500
53
+ sleep 0.500
54
+
55
+ $job.class.should.equal(Rufus::Scheduler::InJob)
56
+ end
57
+
58
+ it 'should accept overriding #log_exception' do
59
+
60
+ $e = nil
61
+
62
+ def @s.log_exception (e)
63
+ $e = e
64
+ end
65
+
66
+ @s.in 0.400 do
67
+ raise 'Houston we have a problem'
68
+ end
69
+
70
+ sleep 0.500
71
+ sleep 0.500
72
+
73
+ $e.to_s.should.equal('Houston we have a problem')
74
+ end
75
+
76
+ end
77
+