rolling_timeout 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +8 -0
- data/examples/example.rb +21 -0
- data/lib/rolling_timeout.rb +39 -0
- data/lib/rolling_timeout/version.rb +3 -0
- data/rolling_timeout.gemspec +24 -0
- data/test/minitest_helper.rb +4 -0
- data/test/test_rolling_timeout.rb +57 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab0add27f838cf73a1d0a0708e3957cc3c5a266e
|
4
|
+
data.tar.gz: b1ed033e339c193b0fc4a9ac5d6514c1003b5d21
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 523cfe37628ebfe04a0af7648bcffbe88d677fc879469053d8e9cd4bbaf3e770c89c40d59046e3d415f1e97eae4edd7039519dccd005aa74e66328b5f4f99db6
|
7
|
+
data.tar.gz: a5d0bd06b8096ac4681c1b922e91ea500807b76e26f7786bf60a391b5f5dc71eefcc657cdb4503753279ac2941a63f8b9d54dab6f3ee76965d0ba93147fef29b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Will Jessop
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# RollingTimeout
|
2
|
+
|
3
|
+
A timeout class with a rolling window. Call reset to reset the timer back to 0, stop and start do what you might expect.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rolling_timeout'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rolling_timeout
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
````ruby
|
22
|
+
require 'rolling_timeout'
|
23
|
+
|
24
|
+
begin
|
25
|
+
RollingTimeout.new(1) {|timer| # 1 second timeout
|
26
|
+
sleep(0.9)
|
27
|
+
timer.reset # Back to 1 second on the timeout clock
|
28
|
+
sleep(0.9)
|
29
|
+
timer.stop # The timer is stopped, we can do what we want for as long as we want
|
30
|
+
sleep(0.2) # This would have taken us over the 1 second timeout if the timer was running
|
31
|
+
timer.start # restart the timer
|
32
|
+
sleep(100) # This will now timeout quickly
|
33
|
+
}
|
34
|
+
rescue Timeout::Error
|
35
|
+
puts "Timed out"
|
36
|
+
end
|
37
|
+
````
|
38
|
+
|
39
|
+
## Requirements
|
40
|
+
|
41
|
+
Works with ruby 2.0, probably 1.9.3 too. 1.8.7 YMMV.
|
42
|
+
|
43
|
+
## Issues
|
44
|
+
|
45
|
+
It's probably not a very accurate (relatively speaking) timer. How the VM schedules the code after the timer thread wakes up will affect how long after the actual time limit expires that the code thread is stopped and an exception raised. Also, if the code finishes at the same time the timeout thread wakes up it's a race to return.
|
46
|
+
|
47
|
+
That said, this was accurate enough for my needs. If you need something more accurate please feel free to submit a patch.
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
56
|
+
|
57
|
+
## Author
|
58
|
+
|
59
|
+
* Will Jessop (will@willj.net)
|
data/Rakefile
ADDED
data/examples/example.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
$:.unshift File.join(File.basename(__FILE__), "../lib")
|
2
|
+
require 'rolling_timeout'
|
3
|
+
|
4
|
+
begin
|
5
|
+
RollingTimeout.new(1) {|timer|
|
6
|
+
sleep(0.5)
|
7
|
+
puts "slept for 0.5 seconds"
|
8
|
+
timer.reset
|
9
|
+
sleep(0.5)
|
10
|
+
puts "slept for 0.5 seconds"
|
11
|
+
timer.reset
|
12
|
+
sleep(0.5)
|
13
|
+
puts "slept for 0.5 seconds"
|
14
|
+
timer.reset
|
15
|
+
sleep(0.5)
|
16
|
+
puts "Slept for 0.5 seconds, 2 total, will now timeout."
|
17
|
+
sleep(100)
|
18
|
+
}
|
19
|
+
rescue Timeout::Error
|
20
|
+
puts "Timed out"
|
21
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "rolling_timeout/version"
|
2
|
+
require 'timeout'
|
3
|
+
require 'thread'
|
4
|
+
|
5
|
+
class RollingTimeout
|
6
|
+
def initialize(time_in_seconds, &block)
|
7
|
+
@time_in_seconds = time_in_seconds
|
8
|
+
start
|
9
|
+
@timed_out = false
|
10
|
+
@code_thread = Thread.new{ yield self }
|
11
|
+
@code_thread.join
|
12
|
+
@timer_thread.join if @timed_out
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def reset
|
17
|
+
stop
|
18
|
+
start
|
19
|
+
end
|
20
|
+
|
21
|
+
def stop
|
22
|
+
@timer_thread.kill
|
23
|
+
end
|
24
|
+
|
25
|
+
def start
|
26
|
+
@timer_thread = new_timer_thread
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def new_timer_thread
|
32
|
+
Thread.new {
|
33
|
+
sleep(@time_in_seconds)
|
34
|
+
@timed_out = true
|
35
|
+
@code_thread.kill
|
36
|
+
raise Timeout::Error
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rolling_timeout/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rolling_timeout"
|
8
|
+
spec.version = RollingTimeout::VERSION
|
9
|
+
spec.authors = ["Will Jessop"]
|
10
|
+
spec.email = ["will@willj.net"]
|
11
|
+
spec.description = %q{a timeout with a rolling window that can be moved forward}
|
12
|
+
spec.summary = %q{a timeout with a rolling window that can be moved forward}
|
13
|
+
spec.homepage = "https://github.com/wjessop/rolling_timeout"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'minitest_helper'
|
2
|
+
|
3
|
+
class TestRollingTimeout < MiniTest::Unit::TestCase
|
4
|
+
def test_that_it_has_a_version_number
|
5
|
+
refute_nil ::RollingTimeout::VERSION
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_it_times_out
|
9
|
+
assert_raises(Timeout::Error) {
|
10
|
+
RollingTimeout.new(0.001) {|timer|
|
11
|
+
sleep(0.002)
|
12
|
+
}
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_it_completes_with_no_timeout_when_code_completes_within_time_limit
|
17
|
+
marker = false
|
18
|
+
RollingTimeout.new(0.001) {|timer|
|
19
|
+
marker = true
|
20
|
+
}
|
21
|
+
assert(marker)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_it_rolls_the_timeout_window
|
25
|
+
marker = false
|
26
|
+
RollingTimeout.new(0.003) {|timer|
|
27
|
+
sleep(0.002)
|
28
|
+
timer.reset
|
29
|
+
sleep(0.002)
|
30
|
+
# Now outside the original timeout window
|
31
|
+
marker = true
|
32
|
+
}
|
33
|
+
assert(marker)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_timer_stop
|
37
|
+
marker = false
|
38
|
+
RollingTimeout.new(0.001) {|timer|
|
39
|
+
timer.stop
|
40
|
+
sleep(0.002)
|
41
|
+
# Now outside the original timeout window
|
42
|
+
timer.start
|
43
|
+
marker = true
|
44
|
+
}
|
45
|
+
assert(marker)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_timer_start
|
49
|
+
assert_raises(Timeout::Error) {
|
50
|
+
RollingTimeout.new(0.001) {|timer|
|
51
|
+
timer.stop
|
52
|
+
timer.start
|
53
|
+
sleep(0.002)
|
54
|
+
}
|
55
|
+
}
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rolling_timeout
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Will Jessop
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: a timeout with a rolling window that can be moved forward
|
56
|
+
email:
|
57
|
+
- will@willj.net
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- .travis.yml
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- examples/example.rb
|
69
|
+
- lib/rolling_timeout.rb
|
70
|
+
- lib/rolling_timeout/version.rb
|
71
|
+
- rolling_timeout.gemspec
|
72
|
+
- test/minitest_helper.rb
|
73
|
+
- test/test_rolling_timeout.rb
|
74
|
+
homepage: https://github.com/wjessop/rolling_timeout
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.0.5
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: a timeout with a rolling window that can be moved forward
|
98
|
+
test_files:
|
99
|
+
- test/minitest_helper.rb
|
100
|
+
- test/test_rolling_timeout.rb
|