rubysl-timeout 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69b851871d6cc5edc12c12c5eaf71d61ae588974
4
+ data.tar.gz: e020b5101fec9ef7b1692af8f44cb513fecd16c7
5
+ SHA512:
6
+ metadata.gz: 303a74f5e6adc5233db9debee9464699a252579c6833fe02060615895c871c1771d28a015df50cbe54f8498fd4fa4a5a66e4cb21746910fd9316d13079674892
7
+ data.tar.gz: c54bc54eb6981cd0822b4de838eae00aae68564200beaefba0b25d6c369564f34fadf47b6ba408755893d262d0be66a4b49d86b78eb31a6c3bb5f5617d4a2f81
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ env:
3
+ - RUBYLIB=lib
4
+ script: bundle exec mspec
5
+ rvm:
6
+ - 1.8.7
7
+ - rbx-nightly-18mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubysl-timeout.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2013, Brian Shirai
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ 3. Neither the name of the library nor the names of its contributors may be
13
+ used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
20
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rubysl::Timeout
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubysl-timeout'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubysl-timeout
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "rubysl/timeout/version"
2
+ require "rubysl/timeout/timeout"
@@ -0,0 +1,197 @@
1
+ #--
2
+ # = timeout.rb
3
+ #
4
+ # execution timeout
5
+ #
6
+ # = Copyright
7
+ #
8
+ # Copyright - (C) 2008 Evan Phoenix
9
+ # Copyright:: (C) 2000 Network Applied Communication Laboratory, Inc.
10
+ # Copyright:: (C) 2000 Information-technology Promotion Agency, Japan
11
+ #
12
+ #++
13
+ #
14
+ # = Description
15
+ #
16
+ # A way of performing a potentially long-running operation in a thread, and
17
+ # terminating it's execution if it hasn't finished within fixed amount of
18
+ # time.
19
+ #
20
+ # Previous versions of timeout didn't use a module for namespace. This version
21
+ # provides both Timeout.timeout, and a backwards-compatible #timeout.
22
+ #
23
+ # = Synopsis
24
+ #
25
+ # require 'timeout'
26
+ # status = Timeout::timeout(5) {
27
+ # # Something that should be interrupted if it takes too much time...
28
+ # }
29
+ #
30
+
31
+ require 'thread'
32
+
33
+ module Timeout
34
+
35
+ ##
36
+ # Raised by Timeout#timeout when the block times out.
37
+
38
+ class Error < Interrupt
39
+ end
40
+
41
+ # A mutex to protect @requests
42
+ @mutex = Mutex.new
43
+
44
+ # All the outstanding TimeoutRequests
45
+ @requests = []
46
+
47
+ # Represents +thr+ asking for it to be timeout at in +secs+
48
+ # seconds. At timeout, raise +exc+.
49
+ class TimeoutRequest
50
+ def initialize(secs, thr, exc)
51
+ @left = secs
52
+ @thread = thr
53
+ @exception = exc
54
+ end
55
+
56
+ attr_reader :thread, :left
57
+
58
+ # Called because +time+ seconds have gone by. Returns
59
+ # true if the request has no more time left to run.
60
+ def elapsed(time)
61
+ @left -= time
62
+ @left <= 0
63
+ end
64
+
65
+ # Raise @exception if @thread.
66
+ def cancel
67
+ if @thread and @thread.alive?
68
+ @thread.raise @exception, "execution expired"
69
+ end
70
+
71
+ @left = 0
72
+ end
73
+
74
+ # Abort this request, ie, we don't care about tracking
75
+ # the thread anymore.
76
+ def abort
77
+ @thread = nil
78
+ @left = 0
79
+ end
80
+ end
81
+
82
+ @chan = Rubinius::Channel.new
83
+
84
+ def self.watch_channel
85
+ reqs = []
86
+
87
+ while true
88
+ begin
89
+ while reqs.empty?
90
+ req = @chan.receive
91
+ reqs << req if req
92
+ end
93
+
94
+ min = reqs.min { |a,b| a.left <=> b.left }
95
+ new_req = nil
96
+
97
+ if min.left > 0
98
+ before = Time.now
99
+
100
+ new_req = @chan.receive_timeout(min.left)
101
+
102
+ slept_for = Time.now - before
103
+ else
104
+ slept_for = 0
105
+ end
106
+
107
+ reqs.delete_if do |r|
108
+ if r.elapsed(slept_for)
109
+ r.cancel
110
+ true
111
+ else
112
+ false
113
+ end
114
+ end
115
+ reqs << new_req if new_req
116
+
117
+ rescue Exception => e
118
+ e.render("ERROR IN TIMEOUT THREAD")
119
+ end
120
+ end
121
+ end
122
+
123
+ # Spin up the thread up front to avoid a thread collision problem spinning
124
+ # it up lazily.
125
+ @controller = Thread.new { watch_channel }
126
+
127
+ def self.add_timeout(time, exc)
128
+ r = TimeoutRequest.new(time, Thread.current, exc)
129
+ @chan << r
130
+ return r
131
+ end
132
+
133
+ ##
134
+ # Executes the method's block. If the block execution terminates before +sec+
135
+ # seconds has passed, it returns true. If not, it terminates the execution
136
+ # and raises +exception+ (which defaults to Timeout::Error).
137
+ #
138
+ # Note that this is both a method of module Timeout, so you can 'include
139
+ # Timeout' into your classes so they have a #timeout method, as well as a
140
+ # module method, so you can call it directly as Timeout.timeout().
141
+
142
+ def timeout(sec, exception=Error)
143
+ return yield if sec == nil or sec.zero?
144
+ raise ThreadError, "timeout within critical session" if Thread.respond_to?(:critical) && Thread.critical
145
+
146
+ req = Timeout.add_timeout sec, exception
147
+
148
+ begin
149
+ yield sec
150
+ ensure
151
+ req.abort
152
+ end
153
+ end
154
+
155
+ module_function :timeout
156
+
157
+ end
158
+
159
+ ##
160
+ # Identical to:
161
+ #
162
+ # Timeout::timeout(n, e, &block).
163
+ #
164
+ # Defined for backwards compatibility with earlier versions of timeout.rb, see
165
+ # Timeout#timeout.
166
+
167
+ def timeout(n, e=Timeout::Error, &block) # :nodoc:
168
+ Timeout.timeout(n, e, &block)
169
+ end
170
+
171
+ ##
172
+ # Another name for Timeout::Error, defined for backwards compatibility with
173
+ # earlier versions of timeout.rb.
174
+
175
+ TimeoutError = Timeout::Error # :nodoc:
176
+
177
+ if __FILE__ == $0
178
+ p timeout(5) {
179
+ 45
180
+ }
181
+ p timeout(5, TimeoutError) {
182
+ 45
183
+ }
184
+ p timeout(nil) {
185
+ 54
186
+ }
187
+ p timeout(0) {
188
+ 54
189
+ }
190
+ p timeout(5) {
191
+ loop {
192
+ p 10
193
+ sleep 1
194
+ }
195
+ }
196
+ end
197
+
@@ -0,0 +1,5 @@
1
+ module RubySL
2
+ module Timeout
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
data/lib/timeout.rb ADDED
@@ -0,0 +1 @@
1
+ require "rubysl/timeout"
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ require './lib/rubysl/timeout/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "rubysl-timeout"
6
+ spec.version = RubySL::Timeout::VERSION
7
+ spec.authors = ["Brian Shirai"]
8
+ spec.email = ["brixen@gmail.com"]
9
+ spec.description = %q{Ruby standard library timeout.}
10
+ spec.summary = %q{Ruby standard library timeout.}
11
+ spec.homepage = "https://github.com/rubysl/rubysl-timeout"
12
+ spec.license = "BSD"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "mspec", "~> 1.5"
22
+ end
@@ -0,0 +1,15 @@
1
+ require 'timeout'
2
+
3
+ describe "Timeout::Error" do
4
+ ruby_version_is ""..."1.9" do
5
+ it "is a subclass of Interrupt" do
6
+ Interrupt.should be_ancestor_of(Timeout::Error)
7
+ end
8
+ end
9
+
10
+ ruby_version_is "1.9" do
11
+ it "is a subclass of RuntimeError" do
12
+ RuntimeError.should be_ancestor_of(Timeout::Error)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ require 'timeout'
2
+
3
+ describe "Timeout.timeout" do
4
+ it "raises Timeout::Error when it times out with no specified error type" do
5
+ lambda {
6
+ Timeout.timeout(1) do
7
+ sleep 3
8
+ end
9
+ }.should raise_error(Timeout::Error)
10
+ end
11
+
12
+ it "raises specified error type when it times out" do
13
+ lambda do
14
+ Timeout.timeout(1, StandardError) do
15
+ sleep 3
16
+ end
17
+ end.should raise_error(StandardError)
18
+ end
19
+
20
+ it "does not wait too long" do
21
+ before_time = Time.now
22
+ lambda do
23
+ Timeout.timeout(1, StandardError) do
24
+ sleep 3
25
+ end
26
+ end.should raise_error(StandardError)
27
+
28
+ Time.now.to_f.should be_close(before_time.to_f, 1.2)
29
+ end
30
+
31
+ it "returns back the last value in the block" do
32
+ Timeout.timeout(1) do
33
+ 42
34
+ end.should == 42
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysl-timeout
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Shirai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-27 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: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ description: Ruby standard library timeout.
56
+ email:
57
+ - brixen@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - lib/rubysl/timeout.rb
69
+ - lib/rubysl/timeout/timeout.rb
70
+ - lib/rubysl/timeout/version.rb
71
+ - lib/timeout.rb
72
+ - rubysl-timeout.gemspec
73
+ - spec/error_spec.rb
74
+ - spec/timeout_spec.rb
75
+ homepage: https://github.com/rubysl/rubysl-timeout
76
+ licenses:
77
+ - BSD
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.7
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Ruby standard library timeout.
99
+ test_files:
100
+ - spec/error_spec.rb
101
+ - spec/timeout_spec.rb