polling 0.0.7 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +61 -3
- data/lib/polling/engine.rb +71 -0
- data/lib/polling/sleep.rb +1 -2
- data/lib/polling/utils.rb +21 -0
- data/lib/polling/validate.rb +23 -0
- data/lib/polling/version.rb +1 -1
- data/lib/polling.rb +48 -16
- data/test/helper.rb +1 -0
- data/test/run.rb +14 -0
- data/test/test_engine.rb +103 -0
- data/test/test_main.rb +10 -0
- data/test/test_sleep.rb +5 -5
- data/test/test_utils.rb +27 -0
- data/test/test_validate.rb +35 -0
- metadata +15 -10
- data/lib/polling/confirm.rb +0 -51
- data/lib/polling/target.rb +0 -27
- data/test/test_confirm.rb +0 -34
- data/test/test_run.rb +0 -15
- data/test/test_target.rb +0 -31
data/README.md
CHANGED
@@ -14,10 +14,10 @@ Or install it yourself as:
|
|
14
14
|
|
15
15
|
## Usage
|
16
16
|
|
17
|
-
|
17
|
+
New Interface
|
18
18
|
|
19
|
-
|
20
|
-
Polling
|
19
|
+
require 'polling'
|
20
|
+
Polling.start 10 do
|
21
21
|
puts "test"
|
22
22
|
sleep 2
|
23
23
|
end
|
@@ -40,6 +40,64 @@ result
|
|
40
40
|
sleep 7.998055934906006seconds (until 2012-11-16 19:49:00 +0900)
|
41
41
|
test
|
42
42
|
sleep 7.998072147369385seconds (until 2012-11-16 19:49:10 +0900)
|
43
|
+
|
44
|
+
set offset
|
45
|
+
|
46
|
+
require 'polling'
|
47
|
+
Polling.setting offset: 30
|
48
|
+
Polling.start 5 do
|
49
|
+
puts "exec sleep 2"
|
50
|
+
sleep 2
|
51
|
+
end
|
52
|
+
|
53
|
+
result
|
54
|
+
|
55
|
+
start: 2013-03-24 23:38:25 +0900
|
56
|
+
sleep 4.086651802062988seconds (until 2013-03-24 23:38:30 +0900)
|
57
|
+
2013-03-24 23:38:30 +0900
|
58
|
+
exec sleep 2
|
59
|
+
sleep 2.9990789890289307seconds (until 2013-03-24 23:38:35 +0900)
|
60
|
+
2013-03-24 23:38:35 +0900
|
61
|
+
exec sleep 2
|
62
|
+
sleep 2.9979379177093506seconds (until 2013-03-24 23:38:40 +0900)
|
63
|
+
2013-03-24 23:38:40 +0900
|
64
|
+
exec sleep 2
|
65
|
+
sleep 2.9986801147460938seconds (until 2013-03-24 23:38:45 +0900)
|
66
|
+
2013-03-24 23:38:45 +0900
|
67
|
+
exec sleep 2
|
68
|
+
sleep 2.998732089996338seconds (until 2013-03-24 23:38:50 +0900)
|
69
|
+
|
70
|
+
debug
|
71
|
+
|
72
|
+
require 'polling'
|
73
|
+
Polling.setting offset: 5, debug: true
|
74
|
+
Polling.start 5 do
|
75
|
+
puts "test"
|
76
|
+
sleep 2
|
77
|
+
end
|
78
|
+
|
79
|
+
or
|
80
|
+
|
81
|
+
require 'polling'
|
82
|
+
Polling.start 5,true do
|
83
|
+
puts "test"
|
84
|
+
sleep 2
|
85
|
+
end
|
86
|
+
|
87
|
+
Time which can be set up
|
88
|
+
|
89
|
+
- 1 or other integer
|
90
|
+
- "5s" or like string type [s|m|h|d]
|
91
|
+
- [0,10,20,30,40,50] array class
|
92
|
+
|
93
|
+
Support legacy interface.
|
94
|
+
It starts at per minute 0 second.
|
95
|
+
|
96
|
+
time = [0,10,20,30,40,50]
|
97
|
+
Polling::run(time,debug=true) do
|
98
|
+
puts "test"
|
99
|
+
sleep 2
|
100
|
+
end
|
43
101
|
|
44
102
|
The multiple of 60 is set up.
|
45
103
|
|
@@ -0,0 +1,71 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
module Polling
|
3
|
+
class Engine
|
4
|
+
attr_accessor :target, :offset
|
5
|
+
def initialize
|
6
|
+
@target = @offset = 0
|
7
|
+
@init_time = 60
|
8
|
+
end
|
9
|
+
|
10
|
+
def increment! interval
|
11
|
+
@init_time = interval if interval > 60
|
12
|
+
@target += interval
|
13
|
+
end
|
14
|
+
|
15
|
+
def substitution! interval
|
16
|
+
@init_time = interval if interval > 60
|
17
|
+
@target = interval
|
18
|
+
end
|
19
|
+
|
20
|
+
def stime opts={}
|
21
|
+
opts = {target: @target, init_time: @init_time}.merge opts
|
22
|
+
|
23
|
+
case
|
24
|
+
when target(opts[:target]) < opts[:init_time]
|
25
|
+
stime = make_sleep(opts[:target],now_sec_to_f)
|
26
|
+
stime += opts[:init_time] if opts[:target].eql? 0
|
27
|
+
when target(opts[:target]) >= opts[:init_time]
|
28
|
+
decrement!
|
29
|
+
stime = make_sleep(opts[:target],now_sec_to_f) + opts[:init_time]
|
30
|
+
end
|
31
|
+
|
32
|
+
stime += @offset
|
33
|
+
stime -= opts[:init_time] if stime > opts[:init_time]
|
34
|
+
|
35
|
+
if stime < 0
|
36
|
+
stime = opts[:init_time] - stime.abs
|
37
|
+
end
|
38
|
+
|
39
|
+
debug debug: opts[:debug], stime: stime
|
40
|
+
stime
|
41
|
+
rescue => ex
|
42
|
+
$stderr.puts ex.to_s
|
43
|
+
return opts[:init_time]
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def target target=@target, offset=@offset
|
49
|
+
target + offset
|
50
|
+
end
|
51
|
+
|
52
|
+
def now_sec_to_f init_time=@init_time
|
53
|
+
Time.now.to_f % init_time
|
54
|
+
end
|
55
|
+
|
56
|
+
def make_sleep target=@target, now
|
57
|
+
target - now
|
58
|
+
end
|
59
|
+
|
60
|
+
def decrement! init_time=@init_time
|
61
|
+
@target -= init_time
|
62
|
+
end
|
63
|
+
|
64
|
+
def debug opts={}
|
65
|
+
if opts[:debug]
|
66
|
+
until_time = Time.at(Time.now.to_f + opts[:stime])
|
67
|
+
$stdout.print "sleep #{opts[:stime]}seconds (until #{until_time})\n"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/polling/sleep.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Polling
|
2
|
+
class Utils
|
3
|
+
class << self
|
4
|
+
def convert(value)
|
5
|
+
case value
|
6
|
+
when /(\d+)s/
|
7
|
+
value = $1.to_i
|
8
|
+
when /(\d+)m/
|
9
|
+
value = $1.to_i*60
|
10
|
+
when /(\d+)h/
|
11
|
+
value = $1.to_i*60*60
|
12
|
+
when /(\d+)d/
|
13
|
+
value = $1.to_i*60*60*24
|
14
|
+
when String
|
15
|
+
value = value.to_i
|
16
|
+
end
|
17
|
+
return value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),"utils")
|
2
|
+
module Polling
|
3
|
+
module Validate
|
4
|
+
def self.value value
|
5
|
+
case value
|
6
|
+
when Array
|
7
|
+
if value.size.eql? 1
|
8
|
+
value = value[0]
|
9
|
+
if value.class == String
|
10
|
+
value = Utils.convert value
|
11
|
+
end
|
12
|
+
elsif value.size > 1
|
13
|
+
if value[0].class == String
|
14
|
+
value = value.map {|i| i.to_i}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
when String
|
18
|
+
value = Utils.convert value
|
19
|
+
end
|
20
|
+
value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/polling/version.rb
CHANGED
data/lib/polling.rb
CHANGED
@@ -1,23 +1,55 @@
|
|
1
|
-
require 'polling/
|
2
|
-
require 'polling/confirm'
|
1
|
+
require 'polling/engine'
|
3
2
|
require 'polling/sleep'
|
4
|
-
require 'polling/
|
3
|
+
require 'polling/validate'
|
4
|
+
require 'polling/utils'
|
5
5
|
|
6
6
|
module Polling
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
yield
|
7
|
+
class << self
|
8
|
+
def setting opts={}
|
9
|
+
set_instance_variables opts
|
10
|
+
end
|
11
|
+
|
12
|
+
def run interval=@interval, debug=false
|
13
|
+
@offset ||= 0
|
14
|
+
@debug ||= debug
|
15
|
+
e = Engine.new
|
16
|
+
e.offset = @offset
|
17
|
+
|
18
|
+
exec = lambda do |time|
|
19
|
+
yield if block_given?
|
20
|
+
e.target = e.increment! time
|
21
|
+
Sleep.exec e.stime(:debug=>@debug)
|
22
|
+
end
|
23
|
+
|
24
|
+
exec_arr = lambda do |time_arr|
|
25
|
+
time_arr.each do |time|
|
26
|
+
e.target = e.substitution! time
|
27
|
+
Sleep.exec e.stime(:debug=>@debug)
|
28
|
+
yield if block_given?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
interval = Validate.value interval
|
33
|
+
|
34
|
+
case interval
|
35
|
+
when Array
|
36
|
+
loop { exec_arr.call interval }
|
37
|
+
else
|
38
|
+
Sleep.exec e.stime(:debug=>@debug)
|
39
|
+
loop { exec.call interval }
|
40
|
+
end
|
41
|
+
rescue => ex
|
42
|
+
$stderr.puts ex.message
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def set_instance_variables variables
|
48
|
+
variables.each do |key,var|
|
49
|
+
instance_variable_set "@#{key}", var
|
20
50
|
end
|
21
51
|
end
|
52
|
+
|
53
|
+
alias start run
|
22
54
|
end
|
23
55
|
end
|
data/test/helper.rb
CHANGED
data/test/run.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
|
+
|
3
|
+
class Polling::Test < Test::Unit::TestCase
|
4
|
+
def test_run
|
5
|
+
puts "start: #{Time.now}"
|
6
|
+
Polling.setting offset: 5, debug: true
|
7
|
+
#Polling::run [0,10,20,30,40,50] do
|
8
|
+
Polling::run 10 do
|
9
|
+
puts Time.now
|
10
|
+
puts "exec sleep 2"
|
11
|
+
sleep 2
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/test/test_engine.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
|
+
|
3
|
+
class Polling::Engine::Test < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@e = Polling::Engine.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_engine_initialize
|
9
|
+
assert_equal 0, @e.offset
|
10
|
+
assert_equal 5, @e.target = 5
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_increment!
|
14
|
+
@e.target = 0
|
15
|
+
assert_equal 10, @e.increment!(10)
|
16
|
+
@e.target = 60
|
17
|
+
assert_equal 130, @e.increment!(70)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_substitution!
|
21
|
+
@e.target = 0
|
22
|
+
assert_equal 10, @e.substitution!(10)
|
23
|
+
@e.target = 60
|
24
|
+
assert_equal 70, @e.substitution!(70)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_stime
|
28
|
+
Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:00")
|
29
|
+
assert_equal 60, @e.stime(target: 0)
|
30
|
+
|
31
|
+
arr = [1,5,10,20,30,40,50,58,60,120,300,500]
|
32
|
+
arr.each do |time|
|
33
|
+
assert_equal time, @e.stime(target: time)
|
34
|
+
end
|
35
|
+
|
36
|
+
assert_equal 10, @e.stime(target: @e.substitution!(10))
|
37
|
+
assert_equal 20, @e.stime(target: @e.substitution!(20))
|
38
|
+
assert_equal 30, @e.stime(target: @e.substitution!(30))
|
39
|
+
|
40
|
+
@e.target = 10
|
41
|
+
assert_equal 20, @e.stime(target: @e.increment!(10))
|
42
|
+
assert_equal 40, @e.stime(target: @e.increment!(20))
|
43
|
+
assert_equal 70, @e.stime(target: @e.increment!(30))
|
44
|
+
|
45
|
+
Time.stubs(:now).returns(Time.parse "2012/12/31 23:50:20")
|
46
|
+
arr = [30,40,50,60,70,80,90,60,120,300,500]
|
47
|
+
arr.each do |time|
|
48
|
+
assert_equal time - 20, @e.stime(target: time)
|
49
|
+
end
|
50
|
+
|
51
|
+
Time.stubs(:now).returns(Time.parse "2012/12/31 23:59:50")
|
52
|
+
@e.target = 0
|
53
|
+
assert_equal 10, @e.stime(target: 0)
|
54
|
+
@e.target = 60
|
55
|
+
assert_equal 70, @e.stime(target: @e.increment!(60))
|
56
|
+
@e.target = 120
|
57
|
+
assert_equal 130, @e.stime(target: @e.increment!(120))
|
58
|
+
@e.target = 600
|
59
|
+
assert_equal 610, @e.stime(target: @e.increment!(300))
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_target
|
63
|
+
assert_equal 0, @e.__send__(:target, 0, 0)
|
64
|
+
assert_equal 10, @e.__send__(:target, 10, 0)
|
65
|
+
assert_equal 5, @e.__send__(:target, 0, 5)
|
66
|
+
assert_equal 15, @e.__send__(:target, 10, 5)
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_now_sec_to_f
|
70
|
+
Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:00")
|
71
|
+
assert_equal 0, @e.__send__(:now_sec_to_f, 60)
|
72
|
+
Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:30")
|
73
|
+
assert_equal 30, @e.__send__(:now_sec_to_f, 60)
|
74
|
+
Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:33")
|
75
|
+
assert_equal 33, @e.__send__(:now_sec_to_f, 60)
|
76
|
+
Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:30")
|
77
|
+
assert_equal 30, @e.__send__(:now_sec_to_f, 120)
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_make_sleep
|
81
|
+
Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:30")
|
82
|
+
assert_equal -20, @e.__send__(:make_sleep, 10, @e.__send__(:now_sec_to_f,60))
|
83
|
+
assert_equal -10, @e.__send__(:make_sleep, 20, @e.__send__(:now_sec_to_f,60))
|
84
|
+
assert_equal 0, @e.__send__(:make_sleep, 30, @e.__send__(:now_sec_to_f,60))
|
85
|
+
assert_equal 10, @e.__send__(:make_sleep, 40, @e.__send__(:now_sec_to_f,60))
|
86
|
+
assert_equal 90, @e.__send__(:make_sleep, 120, @e.__send__(:now_sec_to_f,60))
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_decrement!
|
90
|
+
@e.target = 70
|
91
|
+
assert_equal 10, @e.__send__(:decrement!, 60)
|
92
|
+
assert_equal -110, @e.__send__(:decrement!, 120)
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_debug
|
96
|
+
Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:30")
|
97
|
+
opts = {
|
98
|
+
debug: true,
|
99
|
+
stime: 40
|
100
|
+
}
|
101
|
+
assert_nil @e.__send__(:debug, opts)
|
102
|
+
end
|
103
|
+
end
|
data/test/test_main.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
|
+
|
3
|
+
class Polling::Test < Test::Unit::TestCase
|
4
|
+
def test_setting
|
5
|
+
hash = {:offset=>0}
|
6
|
+
assert_equal hash, Polling.setting(offset:0)
|
7
|
+
hash = {:offset=>30,:debug=>true}
|
8
|
+
assert_equal hash, Polling.setting(offset:30,debug:true)
|
9
|
+
end
|
10
|
+
end
|
data/test/test_sleep.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
2
|
|
3
|
-
class
|
4
|
-
def
|
5
|
-
assert_equal false, Polling::Sleep
|
6
|
-
assert_equal false, Polling::Sleep
|
7
|
-
|
3
|
+
class Polling::Sleep::Test < Test::Unit::TestCase
|
4
|
+
def test_sleep_exec
|
5
|
+
assert_equal false, Polling::Sleep.exec(-1)
|
6
|
+
assert_equal false, Polling::Sleep.exec(0)
|
7
|
+
assert_equal 1, Polling::Sleep.exec(1)
|
8
8
|
end
|
9
9
|
end
|
data/test/test_utils.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
|
+
|
3
|
+
class Polling::Utils::Test < Test::Unit::TestCase
|
4
|
+
def test_utils_convert
|
5
|
+
assert_equal 0, Polling::Utils.convert(0)
|
6
|
+
assert_equal 60, Polling::Utils.convert(60)
|
7
|
+
|
8
|
+
assert_equal 0, Polling::Utils.convert("0")
|
9
|
+
assert_equal 60, Polling::Utils.convert("60")
|
10
|
+
assert_equal 120, Polling::Utils.convert("120")
|
11
|
+
|
12
|
+
assert_equal 0, Polling::Utils.convert("0s")
|
13
|
+
assert_equal 10, Polling::Utils.convert("10s")
|
14
|
+
assert_equal 60, Polling::Utils.convert("60s")
|
15
|
+
assert_equal 60, Polling::Utils.convert("1m")
|
16
|
+
assert_equal 300, Polling::Utils.convert("5m")
|
17
|
+
assert_equal 1800, Polling::Utils.convert("30m")
|
18
|
+
assert_equal 3600, Polling::Utils.convert("1h")
|
19
|
+
assert_equal 18000, Polling::Utils.convert("5h")
|
20
|
+
assert_equal 86400, Polling::Utils.convert("1d")
|
21
|
+
assert_equal 432000, Polling::Utils.convert("5d")
|
22
|
+
|
23
|
+
assert_equal [0], Polling::Utils.convert([0])
|
24
|
+
assert_equal [60], Polling::Utils.convert([60])
|
25
|
+
assert_equal [0,10,20,30,40,50], Polling::Utils.convert([0,10,20,30,40,50])
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
|
+
|
3
|
+
class Polling::Validate::Test < Test::Unit::TestCase
|
4
|
+
def test_validate_value
|
5
|
+
assert_equal 0, Polling::Validate.value(0)
|
6
|
+
assert_equal 60, Polling::Validate.value(60)
|
7
|
+
|
8
|
+
assert_equal 0, Polling::Validate.value("0")
|
9
|
+
assert_equal 60, Polling::Validate.value("60")
|
10
|
+
assert_equal 120, Polling::Validate.value("120")
|
11
|
+
|
12
|
+
assert_equal 0, Polling::Validate.value("0s")
|
13
|
+
assert_equal 10, Polling::Validate.value("10s")
|
14
|
+
assert_equal 60, Polling::Validate.value("60s")
|
15
|
+
assert_equal 60, Polling::Validate.value("1m")
|
16
|
+
assert_equal 300, Polling::Validate.value("5m")
|
17
|
+
assert_equal 1800, Polling::Validate.value("30m")
|
18
|
+
assert_equal 3600, Polling::Validate.value("1h")
|
19
|
+
assert_equal 18000, Polling::Validate.value("5h")
|
20
|
+
assert_equal 86400, Polling::Validate.value("1d")
|
21
|
+
assert_equal 432000, Polling::Validate.value("5d")
|
22
|
+
|
23
|
+
assert_equal 0, Polling::Validate.value([0])
|
24
|
+
assert_equal 60, Polling::Validate.value([60])
|
25
|
+
assert_equal [0,10,20,30,40,50], Polling::Validate.value([0,10,20,30,40,50])
|
26
|
+
assert_equal [0,10,20,30,40,50], Polling::Validate.value(["0","10","20","30","40","50"])
|
27
|
+
assert_equal [5,15,25,35,45,55], Polling::Validate.value(["5","15","25","35","45","55"])
|
28
|
+
|
29
|
+
assert_equal 0, Polling::Validate.value(["0"])
|
30
|
+
assert_equal 60, Polling::Validate.value(["60"])
|
31
|
+
assert_equal 10, Polling::Validate.value(["10s"])
|
32
|
+
assert_equal 60, Polling::Validate.value(["60s"])
|
33
|
+
assert_equal 60, Polling::Validate.value(["1m"])
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polling
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-03-26 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: polling
|
15
15
|
email:
|
@@ -24,16 +24,19 @@ files:
|
|
24
24
|
- README.md
|
25
25
|
- Rakefile
|
26
26
|
- lib/polling.rb
|
27
|
-
- lib/polling/
|
27
|
+
- lib/polling/engine.rb
|
28
28
|
- lib/polling/sleep.rb
|
29
|
-
- lib/polling/
|
29
|
+
- lib/polling/utils.rb
|
30
|
+
- lib/polling/validate.rb
|
30
31
|
- lib/polling/version.rb
|
31
32
|
- polling.gemspec
|
32
33
|
- test/helper.rb
|
33
|
-
- test/
|
34
|
-
- test/
|
34
|
+
- test/run.rb
|
35
|
+
- test/test_engine.rb
|
36
|
+
- test/test_main.rb
|
35
37
|
- test/test_sleep.rb
|
36
|
-
- test/
|
38
|
+
- test/test_utils.rb
|
39
|
+
- test/test_validate.rb
|
37
40
|
homepage: https://github.com/hiro-su/polling
|
38
41
|
licenses: []
|
39
42
|
post_install_message:
|
@@ -60,7 +63,9 @@ specification_version: 3
|
|
60
63
|
summary: polling
|
61
64
|
test_files:
|
62
65
|
- test/helper.rb
|
63
|
-
- test/
|
64
|
-
- test/
|
66
|
+
- test/run.rb
|
67
|
+
- test/test_engine.rb
|
68
|
+
- test/test_main.rb
|
65
69
|
- test/test_sleep.rb
|
66
|
-
- test/
|
70
|
+
- test/test_utils.rb
|
71
|
+
- test/test_validate.rb
|
data/lib/polling/confirm.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
module Polling
|
2
|
-
module Confirm
|
3
|
-
module_function
|
4
|
-
def check_value(value)
|
5
|
-
value = value.to_i
|
6
|
-
raise TypeError, "Please set a value that is multiple of 60." if (value % 60 != 0) && value > 60
|
7
|
-
return value
|
8
|
-
end
|
9
|
-
|
10
|
-
def check_arr(arr)
|
11
|
-
if arr.count == 1
|
12
|
-
time = convert(arr[0])
|
13
|
-
#arr = time < 60 ? create_arr(time) : [time]
|
14
|
-
arr = create_arr(time)
|
15
|
-
raise TypeError, "Please set a value that is divisible by 60." if (60 % time != 0) && time < 60
|
16
|
-
end
|
17
|
-
return arr
|
18
|
-
end
|
19
|
-
|
20
|
-
def convert(value)
|
21
|
-
case value
|
22
|
-
when /(\d+)s/
|
23
|
-
value = $1.to_i
|
24
|
-
when /(\d+)m/
|
25
|
-
value = $1.to_i*60
|
26
|
-
when /(\d+)h/
|
27
|
-
value = $1.to_i*60*60
|
28
|
-
when /(\d+)d/
|
29
|
-
value = $1.to_i*60*60*24
|
30
|
-
when String
|
31
|
-
value = value.to_i
|
32
|
-
end
|
33
|
-
return value
|
34
|
-
end
|
35
|
-
|
36
|
-
def create_arr(time)
|
37
|
-
arr = Array.new
|
38
|
-
if time < 60
|
39
|
-
arr << 0 if time != 0
|
40
|
-
result = time
|
41
|
-
while time < 60
|
42
|
-
arr << time
|
43
|
-
time += result
|
44
|
-
end
|
45
|
-
else
|
46
|
-
arr << time
|
47
|
-
end
|
48
|
-
return arr
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
data/lib/polling/target.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'time'
|
2
|
-
|
3
|
-
module Polling
|
4
|
-
module Target
|
5
|
-
module_function
|
6
|
-
|
7
|
-
def interval(interval,*args)
|
8
|
-
init = 60
|
9
|
-
now_to_f = Time.now.to_f
|
10
|
-
stime = interval - (now_to_f % init)
|
11
|
-
|
12
|
-
args.each do |arg|
|
13
|
-
@start = arg[:start]
|
14
|
-
@debug = arg[:debug]
|
15
|
-
end
|
16
|
-
|
17
|
-
if !@start && stime < 0
|
18
|
-
stime = init - stime.abs
|
19
|
-
end
|
20
|
-
|
21
|
-
### debug
|
22
|
-
print "sleep #{stime}seconds (until #{Time.at(now_to_f + stime)})\n" if @debug && stime > 0
|
23
|
-
return stime
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
data/test/test_confirm.rb
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
|
-
|
3
|
-
class PollingTest < Test::Unit::TestCase
|
4
|
-
def test_check_value
|
5
|
-
assert_equal 0, Polling::Confirm::check_value(0)
|
6
|
-
assert_equal 0, Polling::Confirm::check_value("0")
|
7
|
-
assert_equal 60, Polling::Confirm::check_value(60)
|
8
|
-
assert_equal 5, Polling::Confirm::check_value("5")
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_convert
|
12
|
-
assert_equal 5, Polling::Confirm::convert(5)
|
13
|
-
assert_equal 5, Polling::Confirm::convert("5")
|
14
|
-
assert_equal 5, Polling::Confirm::convert("5s")
|
15
|
-
assert_equal 300, Polling::Confirm::convert("5m")
|
16
|
-
assert_equal 18000, Polling::Confirm::convert("5h")
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_check_arr
|
20
|
-
assert_equal ["0","10","20","30","40","50"], Polling::Confirm::check_arr(["0","10","20","30","40","50"])
|
21
|
-
assert_equal [0,10,20,30,40,50], Polling::Confirm::check_arr([0,10,20,30,40,50])
|
22
|
-
assert_equal [0,10,20,30,40,50], Polling::Confirm::check_arr(["10s"])
|
23
|
-
assert_equal [60], Polling::Confirm::check_arr(["1m"])
|
24
|
-
assert_equal [120], Polling::Confirm::check_arr(["2m"])
|
25
|
-
assert_equal [1800], Polling::Confirm::check_arr(["30m"])
|
26
|
-
assert_equal [0,10,20,30,40,50], Polling::Confirm::check_arr(["10"])
|
27
|
-
assert_equal [0,10,20,30,40,50], Polling::Confirm::check_arr([10])
|
28
|
-
assert_equal [0,30], Polling::Confirm::check_arr([30])
|
29
|
-
assert_equal [60], Polling::Confirm::check_arr([60])
|
30
|
-
assert_equal [60], Polling::Confirm::check_arr(["60"])
|
31
|
-
assert_equal [60], Polling::Confirm::check_arr(["60s"])
|
32
|
-
assert_equal [120], Polling::Confirm::check_arr(["120s"])
|
33
|
-
end
|
34
|
-
end
|
data/test/test_run.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
|
-
|
3
|
-
class PollingTest < Test::Unit::TestCase
|
4
|
-
#def test_run
|
5
|
-
# puts "start: #{Time.now}"
|
6
|
-
# #arr = [5,15,25,35,45,55]
|
7
|
-
# #arr = ["0","10","20","30","40","50"]
|
8
|
-
# #arr = ["10s"]
|
9
|
-
# arr = ["3m"]
|
10
|
-
# Polling::run(arr,true) do
|
11
|
-
# puts Time.now
|
12
|
-
# sleep 2
|
13
|
-
# end
|
14
|
-
#end
|
15
|
-
end
|
data/test/test_target.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__)) + "/helper.rb"
|
2
|
-
require 'time'
|
3
|
-
|
4
|
-
class PollingTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_target_time
|
7
|
-
Time.stubs(:now).returns(Time.parse "2012/12/31 23:59:59")
|
8
|
-
assert_equal 1, Polling::Target::interval(0)
|
9
|
-
assert_equal 6, Polling::Target::interval(5)
|
10
|
-
assert_equal 0, Polling::Target::interval(59)
|
11
|
-
assert_equal 1, Polling::Target::interval(60)
|
12
|
-
assert_equal 2, Polling::Target::interval(61)
|
13
|
-
|
14
|
-
arr = [0,1,5,10,20,30,40,50,58]
|
15
|
-
arr.each do |time|
|
16
|
-
assert_equal time+1, Polling::Target::interval(time)
|
17
|
-
end
|
18
|
-
|
19
|
-
debug = {:debug => true}
|
20
|
-
|
21
|
-
Time.stubs(:now).returns(Time.parse "2012/01/01 00:00:00")
|
22
|
-
assert_equal 180, Polling::Target::interval(180,debug)
|
23
|
-
assert_equal 300, Polling::Target::interval(300,debug)
|
24
|
-
assert_equal 300, Polling::Target::interval(300,debug)
|
25
|
-
assert_equal 3600, Polling::Target::interval(3600,debug)
|
26
|
-
|
27
|
-
Time.stubs(:now).returns(Time.parse "2012/11/19 10:16:00")
|
28
|
-
assert_equal 300, Polling::Target::interval(300,debug)
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|