rufus-scheduler 3.0.0 → 3.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/error_spec.rb CHANGED
@@ -43,8 +43,8 @@ describe Rufus::Scheduler do
43
43
 
44
44
  sleep 2
45
45
 
46
- counter.should > 2
47
- $stderr.string.should match(/argh/)
46
+ expect(counter).to be > 2
47
+ expect($stderr.string).to match(/argh/)
48
48
  end
49
49
  end
50
50
 
@@ -69,8 +69,8 @@ describe Rufus::Scheduler do
69
69
 
70
70
  sleep 2
71
71
 
72
- mfh.counter.should > 2
73
- $stderr.string.should match(/ouch/)
72
+ expect(mfh.counter).to be > 2
73
+ expect($stderr.string).to match(/ouch/)
74
74
  end
75
75
  end
76
76
 
@@ -86,8 +86,31 @@ describe Rufus::Scheduler do
86
86
 
87
87
  sleep 0.5
88
88
 
89
- @scheduler.stderr.string.should match(/intercepted an error/)
90
- @scheduler.stderr.string.should match(/miserably/)
89
+ expect(@scheduler.stderr.string).to match(/intercepted an error/)
90
+ expect(@scheduler.stderr.string).to match(/miserably/)
91
+ end
92
+ end
93
+
94
+ context 'error information' do
95
+
96
+ it 'contains information about the error, the job and the scheduler' do
97
+
98
+ @scheduler.stderr = StringIO.new
99
+
100
+ @scheduler.in('0s') do
101
+ fail 'miserably'
102
+ end
103
+
104
+ sleep 0.5
105
+
106
+ s = @scheduler.stderr.string
107
+ #puts s
108
+
109
+ expect(s).to match(/ENV\['TZ'\]:/)
110
+ expect(s).to match(/down\?: false/)
111
+ expect(s).to match(/work_threads: 1/)
112
+ expect(s).to match(/running_jobs: 1/)
113
+ expect(s).to match(/uptime: \d/)
91
114
  end
92
115
  end
93
116
 
@@ -109,7 +132,7 @@ describe Rufus::Scheduler do
109
132
 
110
133
  sleep 0.5
111
134
 
112
- $message.should == 'Rufus::Scheduler::InJob 0s miserably'
135
+ expect($message).to eq('Rufus::Scheduler::InJob 0s miserably')
113
136
  end
114
137
  end
115
138
  end
@@ -22,7 +22,7 @@ describe Rufus::Scheduler::JobArray do
22
22
 
23
23
  @array.push(DummyJob.new('a', Time.local(0)))
24
24
 
25
- @array.to_a.collect(&:id).should == %w[ a ]
25
+ expect(@array.to_a.collect(&:id)).to eq(%w[ a ])
26
26
  end
27
27
 
28
28
  it 'pushes and remove duplicates' do
@@ -32,7 +32,7 @@ describe Rufus::Scheduler::JobArray do
32
32
  @array.push(j)
33
33
  @array.push(j)
34
34
 
35
- @array.to_a.collect(&:id).should == %w[ a ]
35
+ expect(@array.to_a.collect(&:id)).to eq(%w[ a ])
36
36
  end
37
37
  end
38
38
  end
data/spec/job_at_spec.rb CHANGED
@@ -28,7 +28,7 @@ describe Rufus::Scheduler::AtJob do
28
28
 
29
29
  sleep 0.4
30
30
 
31
- @scheduler.jobs.size.should == 0
31
+ expect(@scheduler.jobs.size).to eq(0)
32
32
  end
33
33
  end
34
34
 
@@ -38,8 +38,8 @@ describe Rufus::Scheduler::AtJob do
38
38
 
39
39
  job = @scheduler.schedule_at((t = Time.now) + 3600) {}
40
40
 
41
- job.scheduled_at.to_i.should >= t.to_i - 1
42
- job.scheduled_at.to_i.should <= t.to_i + 1
41
+ expect(job.scheduled_at.to_i).to be >= t.to_i - 1
42
+ expect(job.scheduled_at.to_i).to be <= t.to_i + 1
43
43
  end
44
44
  end
45
45
 
@@ -51,7 +51,7 @@ describe Rufus::Scheduler::AtJob do
51
51
 
52
52
  job = @scheduler.schedule_at t do; end
53
53
 
54
- job.time.should == t
54
+ expect(job.time).to eq(t)
55
55
  end
56
56
  end
57
57
  end
@@ -25,7 +25,7 @@ describe Rufus::Scheduler::CronJob do
25
25
 
26
26
  sleep_until_next_minute
27
27
 
28
- (job.last_time.to_i % 10).should == 0
28
+ expect(job.last_time.to_i % 10).to eq(0)
29
29
  end
30
30
  end
31
31
 
@@ -59,8 +59,47 @@ describe Rufus::Scheduler::CronJob do
59
59
  #p [ job.last_time, job.last_time.to_f ]
60
60
  #p [ job.first_at, job.first_at.to_f ]
61
61
 
62
- job.first_at.should be_within_1s_of(t + 3)
63
- job.last_time.should == nil
62
+ expect(job.first_at).to be_within_1s_of(t + 3)
63
+ expect(job.last_time).to eq(nil)
64
+ end
65
+
66
+ it 'triggers for the first time at first_at' do
67
+
68
+ first_time = nil
69
+ t = Time.now
70
+
71
+ job = @scheduler.schedule_cron '* * * * * *', :first_in => '3s' do
72
+ first_time ||= Time.now
73
+ end
74
+ sleep 4.5
75
+
76
+ expect(job.first_at).to be_within_1s_of(t + 3)
77
+ expect(first_time).to be_within_1s_of(job.first_at)
78
+ end
79
+ end
80
+
81
+ context 'scheduling the cron itself' do
82
+
83
+ # for https://github.com/jmettraux/rufus-scheduler/issues/95
84
+ #
85
+ # schedule_cron takes more than 30 seconds, blocking...
86
+ #
87
+ it 'does not sit scheduling and blocking...' do
88
+
89
+ n = Time.now
90
+ first = nil
91
+
92
+ job = @scheduler.schedule_cron '*/2 * * * * *' do
93
+ first ||= Time.now
94
+ end
95
+
96
+ expect(Time.now - n).to be < 1.0
97
+
98
+ loop do
99
+ next unless first
100
+ expect(first - n).to be < 4.0
101
+ break
102
+ end
64
103
  end
65
104
  end
66
105
  end
@@ -27,7 +27,26 @@ describe Rufus::Scheduler::EveryJob do
27
27
 
28
28
  sleep 3.5
29
29
 
30
- [ 2, 3 ].should include(counter)
30
+ expect([ 2, 3 ]).to include(counter)
31
+ end
32
+
33
+ it 'lets its @next_time change in-flight' do
34
+
35
+ times = []
36
+
37
+ @scheduler.every '1s' do |job|
38
+ times << Time.now
39
+ job.next_time = Time.now + 3 if times.count == 2
40
+ end
41
+
42
+ sleep 0.3 while times.count < 3
43
+
44
+ #p [ times[1] - times[0], times[2] - times[1] ]
45
+
46
+ expect(times[1] - times[0]).to be > 1.0
47
+ expect(times[1] - times[0]).to be < 1.4
48
+ expect(times[2] - times[1]).to be > 3.0
49
+ expect(times[2] - times[1]).to be < 3.4
31
50
  end
32
51
 
33
52
  context 'first_at/in' do
@@ -44,8 +63,8 @@ describe Rufus::Scheduler::EveryJob do
44
63
  #p [ job.last_time, job.last_time.to_f ]
45
64
  #p [ job.first_at, job.first_at.to_f ]
46
65
 
47
- job.first_at.should be_within_1s_of(t + 2)
48
- job.last_time.should be_within_1s_of(job.first_at)
66
+ expect(job.first_at).to be_within_1s_of(t + 2)
67
+ expect(job.last_time).to be_within_1s_of(job.first_at)
49
68
  end
50
69
 
51
70
  describe '#first_at=' do
@@ -62,8 +81,8 @@ describe Rufus::Scheduler::EveryJob do
62
81
  fa1 = job.first_at
63
82
  nt1 = job.next_time
64
83
 
65
- nt0.should == fa0
66
- nt1.should == fa1
84
+ expect(nt0).to eq(fa0)
85
+ expect(nt1).to eq(fa1)
67
86
  end
68
87
  end
69
88
  end
@@ -23,7 +23,7 @@ describe Rufus::Scheduler::IntervalJob do
23
23
 
24
24
  job = @scheduler.schedule_interval('1h') do; end
25
25
 
26
- job.interval.should == 3600
26
+ expect(job.interval).to eq(3600)
27
27
  end
28
28
  end
29
29
 
@@ -41,8 +41,8 @@ describe Rufus::Scheduler::IntervalJob do
41
41
  #p [ job.last_time, job.last_time.to_f ]
42
42
  #p [ job.first_at, job.first_at.to_f ]
43
43
 
44
- job.first_at.should be_within_1s_of(t + 2)
45
- job.last_time.should be_within_1s_of(job.first_at)
44
+ expect(job.first_at).to be_within_1s_of(t + 2)
45
+ expect(job.last_time).to be_within_1s_of(job.first_at)
46
46
  end
47
47
 
48
48
  describe '#first_at=' do
@@ -59,8 +59,8 @@ describe Rufus::Scheduler::IntervalJob do
59
59
  fa1 = job.first_at
60
60
  nt1 = job.next_time
61
61
 
62
- nt0.should == fa0
63
- nt1.should == fa1
62
+ expect(nt0).to eq(fa0)
63
+ expect(nt1).to eq(fa1)
64
64
  end
65
65
  end
66
66
  end
@@ -28,7 +28,7 @@ describe Rufus::Scheduler::RepeatJob do
28
28
  counter += 1
29
29
  end
30
30
 
31
- counter.should == 0
31
+ expect(counter).to eq(0)
32
32
 
33
33
  while counter < 1; sleep(0.1); end
34
34
 
@@ -36,7 +36,7 @@ describe Rufus::Scheduler::RepeatJob do
36
36
 
37
37
  sleep(1)
38
38
 
39
- counter.should == 1
39
+ expect(counter).to eq(1)
40
40
  end
41
41
  end
42
42
 
@@ -48,14 +48,14 @@ describe Rufus::Scheduler::RepeatJob do
48
48
 
49
49
  job.pause
50
50
 
51
- job.paused?.should == true
51
+ expect(job.paused?).to eq(true)
52
52
  end
53
53
 
54
54
  it 'returns false if the job is not paused' do
55
55
 
56
56
  job = @scheduler.schedule_every('10s') do; end
57
57
 
58
- job.paused?.should == false
58
+ expect(job.paused?).to eq(false)
59
59
  end
60
60
  end
61
61
 
@@ -75,7 +75,7 @@ describe Rufus::Scheduler::RepeatJob do
75
75
 
76
76
  sleep(1.5)
77
77
 
78
- counter.should > 1
78
+ expect(counter).to be > 1
79
79
  end
80
80
 
81
81
  it 'has no effect on a not paused job' do
@@ -84,7 +84,7 @@ describe Rufus::Scheduler::RepeatJob do
84
84
 
85
85
  job.resume
86
86
 
87
- job.paused?.should == false
87
+ expect(job.paused?).to eq(false)
88
88
  end
89
89
  end
90
90
 
@@ -101,7 +101,7 @@ describe Rufus::Scheduler::RepeatJob do
101
101
 
102
102
  sleep(2.6)
103
103
 
104
- counter.should == 3
104
+ expect(counter).to eq(3)
105
105
  end
106
106
 
107
107
  it 'is OK when passed a nil instead of an integer' do
@@ -115,14 +115,14 @@ describe Rufus::Scheduler::RepeatJob do
115
115
 
116
116
  sleep(2.5)
117
117
 
118
- counter.should > 3
118
+ expect(counter).to be > 3
119
119
  end
120
120
 
121
121
  it 'raises when passed something else than nil or an integer' do
122
122
 
123
- lambda {
123
+ expect {
124
124
  @scheduler.schedule_every '0.5s', :times => 'nada' do; end
125
- }.should raise_error(ArgumentError)
125
+ }.to raise_error(ArgumentError)
126
126
  end
127
127
  end
128
128
 
@@ -134,7 +134,7 @@ describe Rufus::Scheduler::RepeatJob do
134
134
 
135
135
  job = @scheduler.schedule_every '0.5s', :first => t do; end
136
136
 
137
- job.first_at.should == t
137
+ expect(job.first_at).to eq(t)
138
138
  end
139
139
 
140
140
  it 'accepts a time string' do
@@ -143,8 +143,8 @@ describe Rufus::Scheduler::RepeatJob do
143
143
 
144
144
  job = @scheduler.schedule_every '0.5s', :first => t.to_s do; end
145
145
 
146
- job.first_at.to_s.should == t.to_s
147
- job.first_at.zone.should == t.zone
146
+ expect(job.first_at.to_s).to eq(t.to_s)
147
+ expect(job.first_at.zone).to eq(t.zone)
148
148
  end
149
149
 
150
150
  it 'only lets the job trigger after the :first' do
@@ -159,20 +159,43 @@ describe Rufus::Scheduler::RepeatJob do
159
159
 
160
160
  sleep(1)
161
161
 
162
- counter.should == 0
162
+ expect(counter).to eq(0)
163
163
 
164
164
  sleep(1)
165
165
 
166
- counter.should > 0
166
+ expect(counter).to be > 0
167
167
  end
168
168
 
169
169
  it 'raises on points in the past' do
170
170
 
171
- lambda {
171
+ expect {
172
172
 
173
173
  @scheduler.schedule_every '0.5s', :first => Time.now - 60 do; end
174
174
 
175
- }.should raise_error(ArgumentError)
175
+ }.to raise_error(ArgumentError)
176
+ end
177
+
178
+ context ':first_time => :now/:immediately' do
179
+
180
+ it 'schedules the first execution immediately' do
181
+
182
+ n = Time.now
183
+ ft = nil
184
+
185
+ job =
186
+ @scheduler.schedule_every '7s', :first => :now do
187
+ ft ||= Time.now
188
+ end
189
+
190
+ sleep 0.7
191
+
192
+ #p n.to_f
193
+ #p job.first_at.to_f
194
+ #p ft.to_f
195
+
196
+ expect(job.first_at).to be < n + 0.7
197
+ expect(ft).to be < job.first_at + @scheduler.frequency + 0.1
198
+ end
176
199
  end
177
200
  end
178
201
 
@@ -184,8 +207,8 @@ describe Rufus::Scheduler::RepeatJob do
184
207
 
185
208
  job = @scheduler.schedule_every '0.5s', :first => '1h' do; end
186
209
 
187
- job.first_at.should >= t + 3600
188
- job.first_at.should < t + 3601
210
+ expect(job.first_at).to be >= t + 3600
211
+ expect(job.first_at).to be < t + 3601
189
212
  end
190
213
 
191
214
  it 'accepts a duration in seconds (integer)' do
@@ -194,15 +217,15 @@ describe Rufus::Scheduler::RepeatJob do
194
217
 
195
218
  job = @scheduler.schedule_every '0.5s', :first => 3600 do; end
196
219
 
197
- job.first_at.should >= t + 3600
198
- job.first_at.should < t + 3601
220
+ expect(job.first_at).to be >= t + 3600
221
+ expect(job.first_at).to be < t + 3601
199
222
  end
200
223
 
201
224
  it 'raises if the argument cannot be used' do
202
225
 
203
- lambda {
226
+ expect {
204
227
  @scheduler.every '0.5s', :first => :nada do; end
205
- }.should raise_error(ArgumentError)
228
+ }.to raise_error(ArgumentError)
206
229
  end
207
230
  end
208
231
 
@@ -213,7 +236,7 @@ describe Rufus::Scheduler::RepeatJob do
213
236
  job = @scheduler.schedule_every '0.5s', :first => 3600 do; end
214
237
  job.first_at = '2030-12-12 12:00:30'
215
238
 
216
- job.first_at.strftime('%c').should == 'Thu Dec 12 12:00:30 2030'
239
+ expect(job.first_at.strftime('%c')).to eq('Thu Dec 12 12:00:30 2030')
217
240
  end
218
241
  end
219
242
 
@@ -225,23 +248,28 @@ describe Rufus::Scheduler::RepeatJob do
225
248
 
226
249
  job = @scheduler.schedule_every '0.5s', :last => t do; end
227
250
 
228
- job.last_at.should == t
251
+ expect(job.last_at).to eq(t)
229
252
  end
230
253
 
231
254
  it 'unschedules the job after the last_at time' do
232
255
 
233
256
  t = Time.now + 2
257
+
234
258
  counter = 0
259
+ tt = nil
235
260
 
236
261
  job =
237
262
  @scheduler.schedule_every '0.5s', :last => t do
238
263
  counter = counter + 1
264
+ tt = Time.now
239
265
  end
240
266
 
241
267
  sleep 3
242
268
 
243
- counter.should == 3
244
- @scheduler.jobs.should_not include(job)
269
+ #counter.should == 3
270
+ expect([ 3, 4 ]).to include(counter)
271
+ expect(tt).to be < t
272
+ expect(@scheduler.jobs).not_to include(job)
245
273
  end
246
274
 
247
275
  it 'accepts a time string' do
@@ -250,17 +278,17 @@ describe Rufus::Scheduler::RepeatJob do
250
278
 
251
279
  job = @scheduler.schedule_every '0.5s', :last => t.to_s do; end
252
280
 
253
- job.last_at.to_s.should == t.to_s
254
- job.last_at.zone.should == t.zone
281
+ expect(job.last_at.to_s).to eq(t.to_s)
282
+ expect(job.last_at.zone).to eq(t.zone)
255
283
  end
256
284
 
257
285
  it 'raises on a point in the past' do
258
286
 
259
- lambda {
287
+ expect {
260
288
 
261
289
  @scheduler.every '0.5s', :last => Time.now - 60 do; end
262
290
 
263
- }.should raise_error(ArgumentError)
291
+ }.to raise_error(ArgumentError)
264
292
  end
265
293
  end
266
294
 
@@ -272,8 +300,8 @@ describe Rufus::Scheduler::RepeatJob do
272
300
 
273
301
  job = @scheduler.schedule_every '0.5s', :last_in => '2s' do; end
274
302
 
275
- job.last_at.should >= t + 2
276
- job.last_at.should < t + 2.5
303
+ expect(job.last_at).to be >= t + 2
304
+ expect(job.last_at).to be < t + 2.5
277
305
  end
278
306
 
279
307
  it 'accepts a duration in seconds (integer)' do
@@ -282,15 +310,15 @@ describe Rufus::Scheduler::RepeatJob do
282
310
 
283
311
  job = @scheduler.schedule_every '0.5s', :last_in => 2.0 do; end
284
312
 
285
- job.last_at.should >= t + 2
286
- job.last_at.should < t + 2.5
313
+ expect(job.last_at).to be >= t + 2
314
+ expect(job.last_at).to be < t + 2.5
287
315
  end
288
316
 
289
317
  it 'raises if the argument is worthless' do
290
318
 
291
- lambda {
319
+ expect {
292
320
  @scheduler.every '0.5s', :last => :nada do; end
293
- }.should raise_error(ArgumentError)
321
+ }.to raise_error(ArgumentError)
294
322
  end
295
323
  end
296
324
 
@@ -301,7 +329,27 @@ describe Rufus::Scheduler::RepeatJob do
301
329
  job = @scheduler.schedule_every '0.5s', :last_in => 10.0 do; end
302
330
  job.last_at = '2030-12-12 12:00:30'
303
331
 
304
- job.last_at.strftime('%c').should == 'Thu Dec 12 12:00:30 2030'
332
+ expect(job.last_at.strftime('%c')).to eq('Thu Dec 12 12:00:30 2030')
333
+ end
334
+ end
335
+
336
+ describe '#count' do
337
+
338
+ it 'starts at 0' do
339
+
340
+ job = @scheduler.schedule_every '5m' do; end
341
+
342
+ expect(job.count).to eq(0)
343
+ end
344
+
345
+ it 'keeps track of how many times the job fired' do
346
+
347
+ job = @scheduler.schedule_every '0.5s' do; end
348
+
349
+ sleep(2.0)
350
+
351
+ expect(job.count).to be >= 3
352
+ expect(job.count).to be <= 4
305
353
  end
306
354
  end
307
355
  end