rufus-scheduler 3.1.9 → 3.1.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,128 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-scheduler
4
- #
5
- # Wed Apr 17 06:00:59 JST 2013
6
- #
7
-
8
- require 'pp'
9
-
10
- puts "RUBY_VERSION: #{RUBY_VERSION}"
11
- puts "RUBY_PLATFORM: #{RUBY_PLATFORM}"
12
-
13
- Thread.abort_on_exception = true
14
-
15
- require 'stringio'
16
- require 'rufus-scheduler'
17
-
18
- #
19
- # misc helper methods
20
-
21
- def ruby18?
22
-
23
- !! RUBY_VERSION.match(/^1\.8\./)
24
- end
25
-
26
- def jruby?
27
-
28
- !! RUBY_PLATFORM.match(/java/)
29
- end
30
-
31
- def local(*args)
32
-
33
- Time.local(*args)
34
- end
35
- alias lo local
36
-
37
- def utc(*args)
38
-
39
- Time.utc(*args)
40
- end
41
-
42
- def ltz(tz, *args)
43
-
44
- in_zone(tz) { Time.local(*args) }
45
- end
46
-
47
- def ltu(tz, *args)
48
-
49
- in_zone(tz) { Time.local(*args) }.getutc
50
- end
51
-
52
- def sleep_until_next_minute
53
-
54
- min = Time.now.min
55
- while Time.now.min == min; sleep 2; end
56
- end
57
-
58
- def sleep_until_next_second
59
-
60
- sec = Time.now.sec
61
- while Time.now.sec == sec; sleep 0.2; end
62
- end
63
-
64
- def in_zone(zone_name, &block)
65
-
66
- prev_tz = ENV['TZ']
67
- ENV['TZ'] = zone_name
68
-
69
- block.call
70
-
71
- ensure
72
-
73
- ENV['TZ'] = prev_tz
74
- end
75
-
76
- def with_chronic(&block)
77
-
78
- require 'chronic'
79
-
80
- Object.const_set(:Khronic, Chronic) unless defined?(Khronic)
81
- Object.const_set(:Chronic, Khronic) unless defined?(Chronic)
82
-
83
- block.call
84
-
85
- ensure
86
-
87
- Object.send(:remove_const, :Chronic)
88
- end
89
-
90
- def without_chronic(&block) # for quick counter-tests ;-)
91
-
92
- block.call
93
- end
94
-
95
-
96
- #
97
- # matchers
98
-
99
- #require 'rspec/expectations'
100
-
101
- RSpec::Matchers.define :be_within_1s_of do |expected|
102
-
103
- match do |actual|
104
-
105
- if actual.respond_to?(:asctime)
106
- (actual.to_f - expected.to_f).abs <= 1.0
107
- else
108
- false
109
- end
110
- end
111
-
112
- failure_message do |actual|
113
-
114
- if actual.respond_to?(:asctime)
115
- "expected #{actual.inspect} to be within 1 second of #{expected}"
116
- else
117
- "expected Time instance, got a #{actual.inspect}"
118
- end
119
- end
120
- end
121
-
122
-
123
- #
124
- # configure
125
-
126
- #RSpec.configure do |config|
127
- #end
128
-
@@ -1,96 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-scheduler
4
- #
5
- # Thu Jul 25 05:53:51 JST 2013
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Rufus::Scheduler do
12
-
13
- before :each do
14
- @scheduler = Rufus::Scheduler.new
15
- end
16
- after :each do
17
- @scheduler.shutdown
18
- end
19
-
20
- context 'thread pool' do
21
-
22
- it 'starts with an empty thread pool' do
23
-
24
- expect(@scheduler.work_threads.size).to eq(0)
25
- end
26
-
27
- it 'does not cross the max_work_threads threshold' do
28
-
29
- #@scheduler.min_work_threads = 2
30
- @scheduler.max_work_threads = 5
31
-
32
- 10.times do
33
- @scheduler.in '0s' do
34
- sleep 5
35
- end
36
- end
37
-
38
- sleep 0.5
39
-
40
- #@scheduler.job_threads.each do |t|
41
- # p t.keys
42
- # p t[:rufus_scheduler_job].class
43
- #end
44
-
45
- expect(@scheduler.work_threads.size).to eq(5)
46
- end
47
-
48
- it 'does not cross the max_work_threads threshold (overlap: false)' do
49
-
50
- #@scheduler.min_work_threads = 2
51
- @scheduler.max_work_threads = 5
52
-
53
- 10.times do
54
- @scheduler.in '0s', :overlap => false do
55
- sleep 5
56
- end
57
- end
58
-
59
- sleep 0.5
60
-
61
- #@scheduler.job_threads.each do |t|
62
- # p t.keys
63
- # p t[:rufus_scheduler_job].class
64
- #end
65
-
66
- expect(@scheduler.work_threads.size).to eq(5)
67
- end
68
-
69
- it 'does not execute unscheduled jobs' do
70
-
71
- @scheduler.max_work_threads = 1
72
-
73
- counter = 0
74
-
75
- job0 =
76
- @scheduler.schedule_in '0.3s' do
77
- counter += 1
78
- sleep 1
79
- end
80
- job1 =
81
- @scheduler.schedule_in '0.35s' do
82
- counter += 1
83
- sleep 1
84
- end
85
-
86
- sleep(0.1) while counter < 1
87
- sleep(0.1) while @scheduler.work_queue.size < 1
88
- job1.unschedule
89
-
90
- sleep(2)
91
-
92
- expect(counter).to eq(1)
93
- end
94
- end
95
- end
96
-
@@ -1,391 +0,0 @@
1
-
2
- #
3
- # Specifying rufus-scheduler
4
- #
5
- # Wed Mar 11 21:17:36 JST 2015, quatre ans...
6
- #
7
-
8
- require 'spec_helper'
9
-
10
-
11
- describe Rufus::Scheduler::ZoTime do
12
-
13
- describe '.new' do
14
-
15
- it 'accepts an integer' do
16
-
17
- zt = Rufus::Scheduler::ZoTime.new(1234567890, 'America/Los_Angeles')
18
-
19
- expect(zt.seconds.to_i).to eq(1234567890)
20
- end
21
-
22
- it 'accepts a float' do
23
-
24
- zt = Rufus::Scheduler::ZoTime.new(1234567890.1234, 'America/Los_Angeles')
25
-
26
- expect(zt.seconds.to_i).to eq(1234567890)
27
- end
28
-
29
- it 'accepts a Time instance' do
30
-
31
- zt =
32
- Rufus::Scheduler::ZoTime.new(
33
- Time.utc(2007, 11, 1, 15, 25, 0),
34
- 'America/Los_Angeles')
35
-
36
- expect(zt.seconds.to_i).to eq(1193930700)
37
- end
38
- end
39
-
40
- #it "flips burgers" do
41
- # puts "---"
42
- # t0 = ltz('America/New_York', 2004, 10, 31, 0, 30, 0)
43
- # t1 = ltz('America/New_York', 2004, 10, 31, 1, 30, 0)
44
- # p t0
45
- # p t1
46
- # puts "---"
47
- # zt0 = Rufus::Scheduler::ZoTime.new(t0, 'America/New_York')
48
- # zt1 = Rufus::Scheduler::ZoTime.new(t1, 'America/New_York')
49
- # p zt0.time
50
- # p zt1.time
51
- # puts "---"
52
- # zt0.add(3600)
53
- # p [ zt0.time, zt0.time.zone ]
54
- # p [ zt1.time, zt1.time.zone ]
55
- # #puts "---"
56
- # #zt0.add(3600)
57
- # #zt1.add(3600)
58
- # #p [ zt0.time, zt0.time.zone ]
59
- # #p [ zt1.time, zt1.time.zone ]
60
- #end
61
-
62
- describe '#time' do
63
-
64
- it 'returns a Time instance in with the right offset' do
65
-
66
- zt = Rufus::Scheduler::ZoTime.new(1193898300, 'America/Los_Angeles')
67
- t = zt.time
68
-
69
- expect(t.strftime('%Y/%m/%d %H:%M:%S %Z')
70
- ).to eq('2007/10/31 23:25:00 PDT')
71
- end
72
-
73
- # New York EST: UTC-5
74
- # summer (dst) EDT: UTC-4
75
-
76
- it 'chooses the DST time when there is ambiguity' do
77
-
78
- t = ltz('America/New_York', 2004, 10, 31, 0, 30, 0)
79
- zt = Rufus::Scheduler::ZoTime.new(t, 'America/New_York')
80
- zt.add(3600)
81
- ztt = zt.time
82
-
83
- expect(ztt.to_i).to eq(1099204200 - 3600)
84
-
85
- expect(ztt.strftime('%Y/%m/%d %H:%M:%S %Z %z')
86
- ).to eq('2004/10/31 01:30:00 EDT -0400')
87
- end
88
- end
89
-
90
- describe '#utc' do
91
-
92
- it 'returns an UTC Time instance' do
93
-
94
- zt = Rufus::Scheduler::ZoTime.new(1193898300, 'America/Los_Angeles')
95
- t = zt.utc
96
-
97
- expect(t.to_i).to eq(1193898300)
98
-
99
- if ruby18?
100
- expect(t.strftime('%Y/%m/%d %H:%M:%S %Z %z')
101
- ).to eq('2007/11/01 06:25:00 GMT +0000')
102
- else
103
- expect(t.strftime('%Y/%m/%d %H:%M:%S %Z %z')
104
- ).to eq('2007/11/01 06:25:00 UTC +0000')
105
- end
106
- end
107
- end
108
-
109
- describe '#add' do
110
-
111
- it 'adds seconds' do
112
-
113
- zt = Rufus::Scheduler::ZoTime.new(1193898300, 'Europe/Paris')
114
- zt.add(111)
115
-
116
- expect(zt.seconds).to eq(1193898300 + 111)
117
- end
118
-
119
- it 'goes into DST' do
120
-
121
- zt =
122
- Rufus::Scheduler::ZoTime.new(
123
- Time.gm(2015, 3, 8, 9, 59, 59),
124
- 'America/Los_Angeles')
125
-
126
- t0 = zt.time
127
- zt.add(1)
128
- t1 = zt.time
129
-
130
- st0 = t0.strftime('%Y/%m/%d %H:%M:%S %Z') + " #{t0.isdst}"
131
- st1 = t1.strftime('%Y/%m/%d %H:%M:%S %Z') + " #{t1.isdst}"
132
-
133
- expect(t0.to_i).to eq(1425808799)
134
- expect(t1.to_i).to eq(1425808800)
135
- expect(st0).to eq('2015/03/08 01:59:59 PST false')
136
- expect(st1).to eq('2015/03/08 03:00:00 PDT true')
137
- end
138
-
139
- it 'goes out of DST' do
140
-
141
- zt =
142
- Rufus::Scheduler::ZoTime.new(
143
- ltz('Europe/Berlin', 2014, 10, 26, 01, 59, 59),
144
- 'Europe/Berlin')
145
-
146
- t0 = zt.time
147
- zt.add(1)
148
- t1 = zt.time
149
- zt.add(3600)
150
- t2 = zt.time
151
- zt.add(1)
152
- t3 = zt.time
153
-
154
- st0 = t0.strftime('%Y/%m/%d %H:%M:%S %Z') + " #{t0.isdst}"
155
- st1 = t1.strftime('%Y/%m/%d %H:%M:%S %Z') + " #{t1.isdst}"
156
- st2 = t2.strftime('%Y/%m/%d %H:%M:%S %Z') + " #{t2.isdst}"
157
- st3 = t3.strftime('%Y/%m/%d %H:%M:%S %Z') + " #{t3.isdst}"
158
-
159
- expect(t0.to_i).to eq(1414281599)
160
- expect(t1.to_i).to eq(1414285200 - 3600)
161
- expect(t2.to_i).to eq(1414285200)
162
- expect(t3.to_i).to eq(1414285201)
163
-
164
- expect(st0).to eq('2014/10/26 01:59:59 CEST true')
165
- expect(st1).to eq('2014/10/26 02:00:00 CEST true')
166
- expect(st2).to eq('2014/10/26 02:00:00 CET false')
167
- expect(st3).to eq('2014/10/26 02:00:01 CET false')
168
-
169
- expect(t1 - t0).to eq(1)
170
- expect(t2 - t1).to eq(3600)
171
- expect(t3 - t2).to eq(1)
172
- end
173
- end
174
-
175
- describe '#to_f' do
176
-
177
- it 'returns the @seconds' do
178
-
179
- zt = Rufus::Scheduler::ZoTime.new(1193898300, 'Europe/Paris')
180
-
181
- expect(zt.to_f).to eq(1193898300)
182
- end
183
- end
184
-
185
- describe '.envtzable?' do
186
-
187
- def etza?(s); Rufus::Scheduler::ZoTime.envtzable?(s); end
188
-
189
- it 'matches' do
190
-
191
- expect(etza?('Asia/Tokyo')).to eq(true)
192
- expect(etza?('America/Los_Angeles')).to eq(true)
193
- expect(etza?('Europe/Paris')).to eq(true)
194
- expect(etza?('UTC')).to eq(true)
195
-
196
- expect(etza?('Japan')).to eq(true)
197
- expect(etza?('Turkey')).to eq(true)
198
- end
199
-
200
- it 'does not match' do
201
-
202
- expect(etza?('14:00')).to eq(false)
203
- expect(etza?('14:00:14')).to eq(false)
204
- expect(etza?('2014/12/11')).to eq(false)
205
- expect(etza?('2014-12-11')).to eq(false)
206
- expect(etza?('+25:00')).to eq(false)
207
-
208
- expect(etza?('+09:00')).to eq(false)
209
- expect(etza?('-01:30')).to eq(false)
210
- expect(etza?('-0200')).to eq(false)
211
-
212
- expect(etza?('Wed')).to eq(false)
213
- expect(etza?('Sun')).to eq(false)
214
- expect(etza?('Nov')).to eq(false)
215
-
216
- expect(etza?('PST')).to eq(false)
217
- expect(etza?('Z')).to eq(false)
218
-
219
- expect(etza?('YTC')).to eq(false)
220
- expect(etza?('Asia/Paris')).to eq(false)
221
- expect(etza?('Nada/Nada')).to eq(false)
222
- end
223
-
224
- #it 'returns true for all entries in the tzinfo list' do
225
- # File.readlines(
226
- # File.join(File.dirname(__FILE__), '../misc/tz_all.txt')
227
- # ).each do |tz|
228
- # tz = tz.strip
229
- # if tz.length > 0 && tz.match(/^[^#]/)
230
- # p tz
231
- # expect(llat?(tz)).to eq(true)
232
- # end
233
- # end
234
- #end
235
- end
236
-
237
- describe '.is_timezone?' do
238
-
239
- def is_timezone?(o); Rufus::Scheduler::ZoTime.is_timezone?(o); end
240
-
241
- it 'returns true when passed a string describing a timezone' do
242
-
243
- expect(is_timezone?('Asia/Tokyo')).to eq(true)
244
- expect(is_timezone?('Europe/Paris')).to eq(true)
245
- expect(is_timezone?('UTC')).to eq(true)
246
- expect(is_timezone?('GMT')).to eq(true)
247
- expect(is_timezone?('Z')).to eq(true)
248
- expect(is_timezone?('Zulu')).to eq(true)
249
- expect(is_timezone?('PST')).to eq(true)
250
- expect(is_timezone?('+09:00')).to eq(true)
251
- expect(is_timezone?('-01:30')).to eq(true)
252
- expect(is_timezone?('Japan')).to eq(true)
253
- expect(is_timezone?('Turkey')).to eq(true)
254
- end
255
-
256
- it 'returns false when it cannot make sense of the timezone' do
257
-
258
- expect(is_timezone?('Asia/Paris')).to eq(false)
259
- #expect(is_timezone?('YTC')).to eq(false)
260
- expect(is_timezone?('Nada/Nada')).to eq(false)
261
- expect(is_timezone?('7')).to eq(false)
262
- expect(is_timezone?('06')).to eq(false)
263
- expect(is_timezone?('sun#3')).to eq(false)
264
- end
265
-
266
- #it 'returns true for all entries in the tzinfo list' do
267
- # File.readlines(
268
- # File.join(File.dirname(__FILE__), '../misc/tz_all.txt')
269
- # ).each do |tz|
270
- # tz = tz.strip
271
- # if tz.length > 0 && tz.match(/^[^#]/)
272
- # #p tz
273
- # expect(is_timezone?(tz)).to eq(true)
274
- # end
275
- # end
276
- #end
277
- end
278
-
279
- describe '.parse' do
280
-
281
- it 'parses a time string without a timezone' do
282
-
283
- zt =
284
- in_zone('Europe/Moscow') {
285
- Rufus::Scheduler::ZoTime.parse('2015/03/08 01:59:59')
286
- }
287
-
288
- t = zt.time
289
- u = zt.utc
290
-
291
- expect(t.to_i).to eq(1425769199)
292
- expect(u.to_i).to eq(1425769199)
293
-
294
- expect(t.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{t.isdst}"
295
- ).to eq('2015/03/08 01:59:59 MSK +0300 false')
296
-
297
- if ruby18?
298
- expect(u.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{u.isdst}"
299
- ).to eq('2015/03/07 22:59:59 GMT +0000 false')
300
- else
301
- expect(u.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{u.isdst}"
302
- ).to eq('2015/03/07 22:59:59 UTC +0000 false')
303
- end
304
- end
305
-
306
- it 'parses a time string with a full name timezone' do
307
-
308
- zt =
309
- Rufus::Scheduler::ZoTime.parse(
310
- '2015/03/08 01:59:59 America/Los_Angeles')
311
-
312
- t = zt.time
313
- u = zt.utc
314
-
315
- expect(t.to_i).to eq(1425808799)
316
- expect(u.to_i).to eq(1425808799)
317
-
318
- expect(t.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{t.isdst}"
319
- ).to eq('2015/03/08 01:59:59 PST -0800 false')
320
-
321
- if ruby18?
322
- expect(u.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{u.isdst}"
323
- ).to eq('2015/03/08 09:59:59 GMT +0000 false')
324
- else
325
- expect(u.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{u.isdst}"
326
- ).to eq('2015/03/08 09:59:59 UTC +0000 false')
327
- end
328
- end
329
-
330
- it 'parses a time string with a delta timezone' do
331
-
332
- zt =
333
- in_zone('Europe/Berlin') {
334
- Rufus::Scheduler::ZoTime.parse('2015-12-13 12:30 -0200')
335
- }
336
-
337
- t = zt.time
338
- u = zt.utc
339
-
340
- expect(t.to_i).to eq(1450017000)
341
- expect(u.to_i).to eq(1450017000)
342
-
343
- expect(t.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{t.isdst}"
344
- ).to eq('2015/12/13 15:30:00 CET +0100 false')
345
-
346
- if ruby18?
347
- expect(u.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{u.isdst}"
348
- ).to eq('2015/12/13 14:30:00 GMT +0000 false')
349
- else
350
- expect(u.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{u.isdst}"
351
- ).to eq('2015/12/13 14:30:00 UTC +0000 false')
352
- end
353
- end
354
-
355
- it 'parses a time string with a delta (:) timezone' do
356
-
357
- zt =
358
- in_zone('Europe/Berlin') {
359
- Rufus::Scheduler::ZoTime.parse('2015-12-13 12:30 -02:00')
360
- }
361
-
362
- t = zt.time
363
- u = zt.utc
364
-
365
- expect(t.to_i).to eq(1450017000)
366
- expect(u.to_i).to eq(1450017000)
367
-
368
- expect(t.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{t.isdst}"
369
- ).to eq('2015/12/13 15:30:00 CET +0100 false')
370
-
371
- if ruby18?
372
- expect(u.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{u.isdst}"
373
- ).to eq('2015/12/13 14:30:00 GMT +0000 false')
374
- else
375
- expect(u.strftime('%Y/%m/%d %H:%M:%S %Z %z') + " #{u.isdst}"
376
- ).to eq('2015/12/13 14:30:00 UTC +0000 false')
377
- end
378
- end
379
-
380
- it 'takes the local TZ when it does not know the timezone' do
381
-
382
- in_zone 'Europe/Moscow' do
383
-
384
- zt = Rufus::Scheduler::ZoTime.parse('2015/03/08 01:59:59 Nada/Nada')
385
-
386
- expect(zt.time.zone).to eq('MSK')
387
- end
388
- end
389
- end
390
- end
391
-