tanzeeb-rufus-scheduler 2.0.7.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,163 @@
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
+ require 'tzinfo'
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, nil ]
26
+ should '10-12 * * * *', [ [0], [10, 11, 12], nil, nil, nil, nil, nil ]
27
+ should '* * * * sun,mon', [ [0], nil, nil, nil, nil, [0, 1], nil ]
28
+ should '* * * * mon-wed', [ [0], nil, nil, nil, nil, [1, 2, 3], nil ]
29
+ should '* * * * 7', [ [0], nil, nil, nil, nil, [0], nil ]
30
+ should '* * * * 0', [ [0], nil, nil, nil, nil, [0], nil ]
31
+ should '* * * * 0,1', [ [0], nil, nil, nil, nil, [0,1], nil ]
32
+ should '* * * * 7,1', [ [0], nil, nil, nil, nil, [0,1], nil ]
33
+ should '* * * * 7,0', [ [0], nil, nil, nil, nil, [0], nil ]
34
+ should '* * * * sun,2-4', [ [0], nil, nil, nil, nil, [0, 2, 3, 4], nil ]
35
+
36
+ should '* * * * sun,mon-tue', [ [0], nil, nil, nil, nil, [0, 1, 2], nil ]
37
+
38
+ should '* * * * * *', [ nil, nil, nil, nil, nil, nil, nil ]
39
+ should '1 * * * * *', [ [1], nil, nil, nil, nil, nil, nil ]
40
+ should '7 10-12 * * * *', [ [7], [10, 11, 12], nil, nil, nil, nil, nil ]
41
+ should '1-5 * * * * *', [ [1,2,3,4,5], nil, nil, nil, nil, nil, nil ]
42
+
43
+ should '* * * * * : EST', [ [0], nil, nil, nil, nil, nil, 'EST' ]
44
+ should '* * * * * * : EST', [ nil, nil, nil, nil, nil, nil, 'EST' ]
45
+
46
+ lambda{cl('* * * * * : NotATimeZone')}.should.raise
47
+ lambda{cl('* * * * * * : NotATimeZone')}.should.raise
48
+ end
49
+ end
50
+
51
+ describe 'Rufus::CronLine#next_time' do
52
+
53
+ it 'should compute next occurence correctly' do
54
+
55
+ now = Time.utc(1970,1,1) # Time.at(0).utc # Thu Jan 01 00:00:00 UTC 1970
56
+
57
+ cl('* * * * *').next_time(now).should.equal( Time.utc(1970,1,1,0,1) )
58
+ cl('* * * * sun').next_time(now).should.equal( Time.utc(1970,1,4) )
59
+ cl('* * * * * *').next_time(now).should.equal( Time.utc(1970,1,1,0,0,1) )
60
+ cl('* * 13 * fri').next_time(now).should.equal( Time.utc(1970,2,13) )
61
+
62
+ cl('10 12 13 12 *').next_time(now).should.equal( Time.utc(1970,12,13,12,10) )
63
+ # this one is slow (1 year == 3 seconds)
64
+ cl('* * 1 6 *').next_time(now).should.equal( Time.utc(1970,6,1) )
65
+
66
+ cl('0 0 * * thu').next_time(now).should.equal( Time.utc(1970,1,8) )
67
+
68
+ now = Time.local(1970,1,1)
69
+
70
+ cl('* * * * *').next_time(now).should.equal( Time.local(1970,1,1,0,1) )
71
+ cl('* * * * sun').next_time(now).should.equal( Time.local(1970,1,4) )
72
+ cl('* * * * * *').next_time(now).should.equal( Time.local(1970,1,1,0,0,1) )
73
+ cl('* * 13 * fri').next_time(now).should.equal( Time.local(1970,2,13) )
74
+
75
+ cl('10 12 13 12 *').next_time(now).should.equal( Time.local(1970,12,13,12,10) )
76
+ # this one is slow (1 year == 3 seconds)
77
+ cl('* * 1 6 *').next_time(now).should.equal( Time.local(1970,6,1) )
78
+
79
+ cl('0 0 * * thu').next_time(now).should.equal( Time.local(1970,1,8) )
80
+ end
81
+
82
+ it 'should compute next occurence correctly with timezones' do
83
+ zone = 'Europe/Stockholm'
84
+ tz = TZInfo::Timezone.get(zone)
85
+ now = tz.local_to_utc(Time.utc(1970,1,1)).utc # Midnight in zone, UTC
86
+
87
+ cl("* * * * * : #{zone}").next_time(now).should.equal( Time.utc(1969,12,31,23,1) )
88
+ cl("* * * * sun : #{zone}").next_time(now).should.equal( Time.utc(1970,1,3,23) )
89
+ cl("* * * * * * : #{zone}").next_time(now).should.equal( Time.utc(1969,12,31,23,0,1) )
90
+ cl("* * 13 * fri : #{zone}").next_time(now).should.equal( Time.utc(1970,2,12,23) )
91
+
92
+ cl("10 12 13 12 * : #{zone}").next_time(now).should.equal( Time.utc(1970,12,13,11,10) )
93
+ # this one is slow (1 year == 3 seconds)
94
+ cl("* * 1 6 * : #{zone}").next_time(now).should.equal( Time.utc(1970,5,31,23) )
95
+
96
+ cl("0 0 * * thu : #{zone}").next_time(now).should.equal( Time.utc(1970,1,7,23) )
97
+
98
+ now = tz.local_to_utc(Time.utc(1970,1,1)).localtime # Midnight in zone, local time
99
+
100
+ cl("* * * * * : #{zone}").next_time(now).should.equal( Time.local(1969,12,31,18,1) )
101
+ cl("* * * * sun : #{zone}").next_time(now).should.equal( Time.local(1970,1,3,18) )
102
+ cl("* * * * * * : #{zone}").next_time(now).should.equal( Time.local(1969,12,31,18,0,1) )
103
+ cl("* * 13 * fri : #{zone}").next_time(now).should.equal( Time.local(1970,2,12,18) )
104
+
105
+ cl("10 12 13 12 * : #{zone}").next_time(now).should.equal( Time.local(1970,12,13,6,10) )
106
+ # this one is slow (1 year == 3 seconds)
107
+ cl("* * 1 6 * : #{zone}").next_time(now).should.equal( Time.local(1970,5,31,19) )
108
+
109
+ cl("0 0 * * thu : #{zone}").next_time(now).should.equal( Time.local(1970,1,7,18) )
110
+ end
111
+
112
+ end
113
+
114
+ describe "Rufus::Cronline#matches?" do
115
+
116
+ it 'should match occurrences' do
117
+
118
+ cl('* * * * *').matches?( Time.utc(1970,1,1,0,1) ).should.be.true
119
+ cl('* * * * sun').matches?( Time.utc(1970,1,4) ).should.be.true
120
+ cl('* * * * * *').matches?( Time.utc(1970,1,1,0,0,1) ).should.be.true
121
+ cl('* * 13 * fri').matches?( Time.utc(1970,2,13) ).should.be.true
122
+
123
+ cl('10 12 13 12 *').matches?( Time.utc(1970,12,13,12,10) ).should.be.true
124
+ cl('* * 1 6 *').matches?( Time.utc(1970,6,1) ).should.be.true
125
+
126
+ cl('0 0 * * thu').matches?( Time.utc(1970,1,8) ).should.be.true
127
+
128
+ cl('* * * * *').matches?( Time.local(1970,1,1,0,1) ).should.be.true
129
+ cl('* * * * sun').matches?( Time.local(1970,1,4) ).should.be.true
130
+ cl('* * * * * *').matches?( Time.local(1970,1,1,0,0,1) ).should.be.true
131
+ cl('* * 13 * fri').matches?( Time.local(1970,2,13) ).should.be.true
132
+
133
+ cl('10 12 13 12 *').matches?( Time.local(1970,12,13,12,10) ).should.be.true
134
+ cl('* * 1 6 *').matches?( Time.local(1970,6,1) ).should.be.true
135
+
136
+ cl('0 0 * * thu').matches?( Time.local(1970,1,8) ).should.be.true
137
+ end
138
+
139
+ it 'should match occurrences with timezones' do
140
+ zone = 'Europe/Stockholm'
141
+
142
+ cl("* * * * * : #{zone}").matches?( Time.utc(1969,12,31,23,1) ).should.be.true
143
+ cl("* * * * sun : #{zone}").matches?( Time.utc(1970,1,3,23) ).should.be.true
144
+ cl("* * * * * * : #{zone}").matches?( Time.utc(1969,12,31,23,0,1) ).should.be.true
145
+ cl("* * 13 * fri : #{zone}").matches?( Time.utc(1970,2,12,23) ).should.be.true
146
+
147
+ cl("10 12 13 12 * : #{zone}").matches?( Time.utc(1970,12,13,11,10) ).should.be.true
148
+ cl("* * 1 6 * : #{zone}").matches?( Time.utc(1970,5,31,23) ).should.be.true
149
+
150
+ cl("0 0 * * thu : #{zone}").matches?( Time.utc(1970,1,7,23) ).should.be.true
151
+
152
+ cl("* * * * * : #{zone}").matches?( Time.local(1969,12,31,18,1) ).should.be.true
153
+ cl("* * * * sun : #{zone}").matches?( Time.local(1970,1,3,18) ).should.be.true
154
+ cl("* * * * * * : #{zone}").matches?( Time.local(1969,12,31,18,0,1) ).should.be.true
155
+ cl("* * 13 * fri : #{zone}").matches?( Time.local(1970,2,12,18) ).should.be.true
156
+
157
+ cl("10 12 13 12 * : #{zone}").matches?( Time.local(1970,12,13,6,10) ).should.be.true
158
+ cl("* * 1 6 * : #{zone}").matches?( Time.local(1970,5,31,19) ).should.be.true
159
+
160
+ cl("0 0 * * thu : #{zone}").matches?( Time.local(1970,1,7,18) ).should.be.true
161
+ end
162
+
163
+ end
@@ -0,0 +1,229 @@
1
+
2
+ #
3
+ # Specifying rufus-scheduler
4
+ #
5
+ # Sun Mar 22 12:26:07 JST 2009
6
+ #
7
+
8
+ require File.join(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
+ it 'should list the "trigger threads"' do
173
+
174
+ @s.every '1s' do
175
+ sleep 10
176
+ end
177
+ sleep 5
178
+
179
+ @s.trigger_threads.size.should.equal(4)
180
+ end
181
+ end
182
+
183
+ describe Rufus::Scheduler::EveryJob do
184
+
185
+ before do
186
+ @s = start_scheduler
187
+ end
188
+ after do
189
+ stop_scheduler(@s)
190
+ end
191
+
192
+ it 'should respond to #next_time' do
193
+
194
+ t = Time.now + 3 * 3600
195
+
196
+ job = @s.every '3h' do
197
+ end
198
+
199
+ job.next_time.to_i.should.equal(t.to_i)
200
+ end
201
+
202
+
203
+ it 'should not allow a job to overlap execution if set !allow_overlapping' do
204
+ stack = []
205
+
206
+ @s.every '1s', :allow_overlapping => false do |job|
207
+ stack << 'ok'
208
+ sleep 2
209
+ end
210
+
211
+ sleep 5
212
+
213
+ stack.size.should.equal(2)
214
+ end
215
+
216
+ it 'should allow a job to overlap execution (backward compatability?)' do
217
+ stack = []
218
+
219
+ @s.every '1s' do |job|
220
+ stack << 'ok'
221
+ sleep 2
222
+ end
223
+
224
+ sleep 5
225
+
226
+ stack.size.should.equal(4)
227
+ end
228
+ end
229
+
@@ -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
+