simple_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 +22 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +7 -0
- data/lib/simple_timeout.rb +85 -0
- data/lib/simple_timeout/version.rb +3 -0
- data/simple_timeout.gemspec +25 -0
- data/spec/simple_timeout_spec.rb +111 -0
- data/spec/spec_helper.rb +8 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a9c4d635e206f6ea8a80a54447edbb3c233155ad
|
4
|
+
data.tar.gz: 4199f3c59fab78b2e718cb7e1a014cd04a823787
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8f1d6da8fe70ff826797553618a231b7ae6c02d2020b59028941784e9b8ba8039e568bf15a4defd0b15feb4b61051d292efcd386d9c2bdf917ef2f014ecfcb9c
|
7
|
+
data.tar.gz: a37bb78b1b74a8cafff326f04f49a4caaeab2415de108168dc2033985e9627b9e4c09576df69d86bc9bca9647d0956d5dbdb60c40c7d622de485aa1e001aec28
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Andrew Kane
|
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,58 @@
|
|
1
|
+
# Simple Timeout
|
2
|
+
|
3
|
+
A simple implementation of Timeout.
|
4
|
+
|
5
|
+
The standard `Timeout::timeout` will always wait until the block finishes execution, and then, when the thread where the block is been executed can't be killed for some reason, your whole program get blocked. `SimpleTimeout::timeout` lets you choose if you want to wait for the block to finish execution or not. By default it won't wait.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
Basic usage:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
SimpleTimeout::Timeout(5) do
|
13
|
+
# Some code that should not take more than 5 seconds
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
17
|
+
## Options
|
18
|
+
|
19
|
+
`SimpleTimeout::timeout` receives 4 parameters:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
def self.timeout(seconds,timeout_error_class=SimpleTimeout::Error,wait_for_block=false,&block)
|
23
|
+
# the implementation ...
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
* **seconds:** How much seconds to wait before raise `timeout_error_class`.
|
28
|
+
* **timeout_error_class:** The error to raise in case of timeout.
|
29
|
+
* **wait_for_block:** This one is the main reason for me to implement this gem. The standard `Timeout::timeout` will always wait until the block finishes execution, and then, when the thread where the block is been executed can't be killed for some reason, your whole program get blocked. `SimpleTimeout::timeout` lets you choose if you want to wait for the block to finish execution or not. By default it won't wait.
|
30
|
+
* **block:** The block to execute.
|
31
|
+
|
32
|
+
|
33
|
+
## Installation
|
34
|
+
|
35
|
+
Add this line to your application’s Gemfile:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
gem 'simple_timeout'
|
39
|
+
```
|
40
|
+
sadfasdf
|
41
|
+
And then execute:
|
42
|
+
|
43
|
+
```sh
|
44
|
+
bundle
|
45
|
+
```
|
46
|
+
asdasdf
|
47
|
+
## History
|
48
|
+
|
49
|
+
View the [changelog](https://github.com/simon0191/simple_timeout/blob/master/CHANGELOG.md)
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
54
|
+
|
55
|
+
- [Report bugs](https://github.com/simon0191/simple_timeout/issues)
|
56
|
+
- Fix bugs and [submit pull requests](https://github.com/simon0191/simple_timeout/pulls)
|
57
|
+
- Write, clarify, or fix documentation
|
58
|
+
- Suggest or add new features
|
data/Rakefile
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
module SimpleTimeout
|
2
|
+
def self.timeout(seconds,timeout_error_class=SimpleTimeout::Error,wait_for_block=false,&block)
|
3
|
+
if seconds == 0
|
4
|
+
raise timeout_error_class.new, "execution expired"
|
5
|
+
end
|
6
|
+
|
7
|
+
if seconds.nil? || !seconds.is_a?(Numeric) || seconds < 0
|
8
|
+
raise ArgumentError.new, "seconds must be an Integer number greater or equal to 0"
|
9
|
+
end
|
10
|
+
|
11
|
+
if block_given?
|
12
|
+
control = SimpleTimeout::Control.new(seconds,timeout_error_class)
|
13
|
+
|
14
|
+
block_thread = build_block_thread(control,&block)
|
15
|
+
|
16
|
+
timeout_thread = build_timeout_thread(control)
|
17
|
+
|
18
|
+
while control.status == :waiting;end
|
19
|
+
|
20
|
+
timeout_thread.kill if timeout_thread.alive?
|
21
|
+
block_thread.kill if block_thread.alive?
|
22
|
+
|
23
|
+
if wait_for_block
|
24
|
+
block_thread.join
|
25
|
+
end
|
26
|
+
|
27
|
+
handle_control_status control
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Error < ::StandardError; end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
class Control
|
37
|
+
attr_accessor :mutex,:status,:result,:error,:timeout_in_seconds,:timeout_error_class
|
38
|
+
def initialize(timeout_in_seconds,timeout_error_class)
|
39
|
+
self.timeout_in_seconds = timeout_in_seconds
|
40
|
+
self.timeout_error_class = timeout_error_class
|
41
|
+
self.status = :waiting
|
42
|
+
self.mutex = Mutex.new
|
43
|
+
self.result = nil
|
44
|
+
self.error = nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.build_block_thread(control,&block)
|
49
|
+
Thread.new(control) do |control|
|
50
|
+
begin
|
51
|
+
control.result = block.call
|
52
|
+
rescue Exception => e
|
53
|
+
control.error = e
|
54
|
+
end
|
55
|
+
control.mutex.synchronize do
|
56
|
+
if control.status == :waiting
|
57
|
+
control.status = :finished
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.build_timeout_thread(control)
|
64
|
+
Thread.new(control) do |control|
|
65
|
+
sleep(control.timeout_in_seconds)
|
66
|
+
control.mutex.synchronize do
|
67
|
+
if control.status == :waiting
|
68
|
+
control.status = :timeout
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.handle_control_status(control)
|
75
|
+
if control.status == :finished
|
76
|
+
if control.error.nil?
|
77
|
+
return control.result
|
78
|
+
else
|
79
|
+
raise control.error.class.new, control.error.message
|
80
|
+
end
|
81
|
+
else
|
82
|
+
raise control.timeout_error_class.new, "execution expired"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "simple_timeout/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "simple_timeout"
|
8
|
+
spec.version = SimpleTimeout::VERSION
|
9
|
+
spec.authors = ["Simon Soriano"]
|
10
|
+
spec.email = ["simon0191@gmail.com"]
|
11
|
+
spec.summary = "A simple implementation of timeout"
|
12
|
+
spec.description = "A simple implementation of timeout"
|
13
|
+
spec.homepage = "https://github.com/simon0191/simple_timeout"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "byebug"
|
25
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require_relative "spec_helper"
|
2
|
+
|
3
|
+
RSpec.describe SimpleTimeout do
|
4
|
+
|
5
|
+
let(:small_timeout) { 0.1 }
|
6
|
+
let(:medium_timeout) { 0.4 }
|
7
|
+
let(:big_timeout) { 1 }
|
8
|
+
|
9
|
+
describe "#timeout" do
|
10
|
+
context "0 seconds" do
|
11
|
+
it "should raise a SimpleTimeout::Error" do
|
12
|
+
expect {SimpleTimeout.timeout(0)}.to raise_error(SimpleTimeout::Error)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
context "negative seconds" do
|
16
|
+
it "should raise an ArgumentError" do
|
17
|
+
expect {SimpleTimeout.timeout(-1)}.to raise_error(ArgumentError)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
context "nil seconds" do
|
21
|
+
it "should raise an ArgumentError" do
|
22
|
+
expect {SimpleTimeout.timeout(nil)}.to raise_error(ArgumentError)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
context "non Integer seconds" do
|
26
|
+
it "should raise an ArgumentError" do
|
27
|
+
expect {SimpleTimeout.timeout( {} )}.to raise_error(ArgumentError)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
context "block is not given" do
|
31
|
+
it "should return nil" do
|
32
|
+
expect(SimpleTimeout.timeout(small_timeout)).to be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
context "block raise an Error" do
|
36
|
+
it "should raise the Error" do
|
37
|
+
class SomeError < StandardError;end
|
38
|
+
message = "Some message"
|
39
|
+
expect do
|
40
|
+
SimpleTimeout.timeout(big_timeout) { raise SomeError.new(message) }
|
41
|
+
end.to raise_error(SomeError,message)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
context "block execution takes more time than timeout" do
|
45
|
+
it "should kill block thread" do
|
46
|
+
thread_spy = spy('thread')
|
47
|
+
allow(SimpleTimeout).to receive(:build_block_thread).and_return(thread_spy)
|
48
|
+
begin
|
49
|
+
SimpleTimeout.timeout(small_timeout) { }
|
50
|
+
fail
|
51
|
+
rescue SimpleTimeout::Error => e
|
52
|
+
expect(thread_spy).to have_received(:kill)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should raise SimpleTimeout::Error" do
|
57
|
+
expect do
|
58
|
+
SimpleTimeout.timeout(small_timeout) { sleep(big_timeout) }
|
59
|
+
end.to raise_error(SimpleTimeout::Error)
|
60
|
+
end
|
61
|
+
|
62
|
+
context "and is given a custom error class" do
|
63
|
+
it "should raise SimpleTimeout::Error" do
|
64
|
+
class SomeError < StandardError;end
|
65
|
+
expect do
|
66
|
+
SimpleTimeout.timeout(small_timeout,SomeError) { sleep(big_timeout) }
|
67
|
+
end.to raise_error(SomeError)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "and block execution can't be killed" do
|
72
|
+
it "should finish and raise SimpleTimeout::Error" do
|
73
|
+
thread_stub = Thread.new { while true; end }
|
74
|
+
allow(SimpleTimeout).to receive(:build_block_thread).and_return(thread_stub)
|
75
|
+
allow(thread_stub).to receive(:kill).and_return(nil)
|
76
|
+
expect{ SimpleTimeout.timeout(small_timeout) { } }.to raise_error(SimpleTimeout::Error)
|
77
|
+
end
|
78
|
+
|
79
|
+
context "and wait_for_block is true" do
|
80
|
+
it "should wait until the block finishes execution" do
|
81
|
+
thread_spy = spy('thread')
|
82
|
+
allow(SimpleTimeout).to receive(:build_block_thread).and_return(thread_spy)
|
83
|
+
|
84
|
+
expect{
|
85
|
+
SimpleTimeout.timeout(small_timeout,SimpleTimeout::Error,true) { }
|
86
|
+
}.to raise_error(SimpleTimeout::Error)
|
87
|
+
|
88
|
+
expect(thread_spy).to have_received(:join)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
end
|
95
|
+
context "block execution finishes before timeout" do
|
96
|
+
it "should return what the block returned" do
|
97
|
+
expected_return = "Expected return value"
|
98
|
+
expect(
|
99
|
+
SimpleTimeout.timeout(big_timeout,SomeError) { expected_return }
|
100
|
+
).to eq expected_return
|
101
|
+
end
|
102
|
+
it "should kill timeout thread" do
|
103
|
+
thread_spy = spy('thread')
|
104
|
+
allow(SimpleTimeout).to receive(:build_timeout_thread).and_return(thread_spy)
|
105
|
+
SimpleTimeout.timeout(small_timeout) { }
|
106
|
+
expect(thread_spy).to have_received(:kill)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_timeout
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Simon Soriano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-23 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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: rspec
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: A simple implementation of timeout
|
70
|
+
email:
|
71
|
+
- simon0191@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .travis.yml
|
78
|
+
- CHANGELOG.md
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/simple_timeout.rb
|
84
|
+
- lib/simple_timeout/version.rb
|
85
|
+
- simple_timeout.gemspec
|
86
|
+
- spec/simple_timeout_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: https://github.com/simon0191/simple_timeout
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.8
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: A simple implementation of timeout
|
112
|
+
test_files:
|
113
|
+
- spec/simple_timeout_spec.rb
|
114
|
+
- spec/spec_helper.rb
|