timeout-extensions 0.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 +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +84 -0
- data/Rakefile +1 -0
- data/lib/timeout/extensions.rb +45 -0
- data/lib/timeout/extensions/version.rb +3 -0
- data/spec/spec_helper.rb +65 -0
- data/spec/timeout/extensions_spec.rb +35 -0
- data/timeout-extensions.gemspec +25 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4ad2130b411b304b307f17a4db55b4c034ff84ff
|
4
|
+
data.tar.gz: 0566ffbb332db2b4bdf1862474a97f9ec5d6a451
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 61f6d8bb54f5b00b49e14847bce4d219f26faf76fd6854bcfec4e1fe9fab30b747504ebd2411720c680ee940612bca3e1a766d46b8cd1e080d390cb61872c3cd
|
7
|
+
data.tar.gz: 2bf51f1671ebcea3dd664d40141e07ed9a541a3c16a3bf9001a35158bf274efd8a1bc86c15043a8f000396606acf94707ee5ba9725e11da027cf3ce642cb2d03
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Tony Arcieri
|
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,84 @@
|
|
1
|
+
Timeout::Extensions
|
2
|
+
===================
|
3
|
+
|
4
|
+
The Timeout::Extensions Gem augments Ruby's `timeout.rb` API with support for
|
5
|
+
multiple timeout backends which can be mixed and matched within a single app.
|
6
|
+
|
7
|
+
It supports pluggable timeout implementations which can selectively hook
|
8
|
+
into multiple backend schedulers rather than resorting to running code in a
|
9
|
+
separate thread to implement timeouts when there's a better backend available.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'timeout-extensions'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install timeout-extensions
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
The Timeout::Extensions gem enhances the Ruby standard library's
|
28
|
+
`timeout.rb` API:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'timeout/extensions'
|
32
|
+
|
33
|
+
Timeout.new(30) do
|
34
|
+
# Times out after 30 seconds raising Timeout::Error
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
However, where `timeout.rb` provides one implementation, the Timeout Gem
|
39
|
+
provides support for multiple, swappable backends:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'timeout/extensions'
|
43
|
+
|
44
|
+
module MyTimeoutThingy
|
45
|
+
# WARNING: don't use this. It's just a strawman example
|
46
|
+
def self.timeout(secs)
|
47
|
+
current = Thread.current
|
48
|
+
sleeper = Thread.start do
|
49
|
+
begin
|
50
|
+
sleep secs
|
51
|
+
rescue => ex
|
52
|
+
current.raise ex
|
53
|
+
else
|
54
|
+
current.raise Timeout::Error, "execution expired"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
return yield(secs)
|
58
|
+
ensure
|
59
|
+
if sleeper
|
60
|
+
sleeper.kill
|
61
|
+
sleeper.join
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
Timeout.backend(MyTimeoutThingy) do
|
67
|
+
Timeout.new(30) do
|
68
|
+
# Manage timeouts with MyTimeoutThingy instead of timeout.rb
|
69
|
+
# Plug in your own backend just this easily!
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
# Contributing
|
75
|
+
|
76
|
+
* Fork this repository on github
|
77
|
+
* Make your changes and send us a pull request
|
78
|
+
* If we like them we'll merge them
|
79
|
+
* If we've accepted a patch, feel free to ask for commit access
|
80
|
+
|
81
|
+
# License
|
82
|
+
|
83
|
+
Copyright (c) 2014-2015 Tony Arcieri, Tiago Cardoso
|
84
|
+
Distributed under the MIT License. See LICENSE.txt for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'timeout/extensions/version'
|
2
|
+
|
3
|
+
class Thread
|
4
|
+
attr_accessor :timeout_handler
|
5
|
+
attr_accessor :sleep_handler
|
6
|
+
end
|
7
|
+
|
8
|
+
module Timeout::Extensions
|
9
|
+
def self.extended(mod)
|
10
|
+
mod.singleton_class.class_eval do
|
11
|
+
alias_method :timeout_without_handler, :timeout
|
12
|
+
alias_method :timeout, :timeout_with_handler
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def timeout_with_handler(*args, &block)
|
17
|
+
if timeout_handler = Thread.current.timeout_handler
|
18
|
+
timeout_handler.call(*args, &block)
|
19
|
+
else
|
20
|
+
timeout_without_handler(*args, &block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
module_function :timeout_with_handler
|
24
|
+
public :timeout_with_handler
|
25
|
+
end
|
26
|
+
|
27
|
+
module Kernel::Extensions
|
28
|
+
def self.included(mod)
|
29
|
+
mod.class_eval do
|
30
|
+
alias_method :sleep_without_handler, :sleep
|
31
|
+
alias_method :sleep, :sleep_with_handler
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def sleep_with_handler(*args)
|
36
|
+
if sleep_handler = Thread.current.sleep_handler
|
37
|
+
sleep_handler.call(*args)
|
38
|
+
else
|
39
|
+
sleep_without_handler(*args)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
Timeout.extend Timeout::Extensions
|
45
|
+
include Kernel::Extensions
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'logger'
|
4
|
+
require 'timeout'
|
5
|
+
require 'timeout/extensions'
|
6
|
+
require 'celluloid/autostart'
|
7
|
+
require 'celluloid/rspec'
|
8
|
+
|
9
|
+
logfile = File.open(File.expand_path("../../log/test.log", __FILE__), 'a')
|
10
|
+
logfile.sync = true
|
11
|
+
|
12
|
+
logger = Celluloid.logger = Logger.new(logfile)
|
13
|
+
|
14
|
+
Celluloid.shutdown_timeout = 1
|
15
|
+
|
16
|
+
|
17
|
+
class Celluloid::Actor
|
18
|
+
# Important that these two are left out, or else
|
19
|
+
# timeout calls inside the actor scope will never propagate
|
20
|
+
# to the proper extensions
|
21
|
+
# TODO: eventually remove this if celluloid adopts the gem
|
22
|
+
undef_method :timeout if instance_methods(false).include?(:timeout)
|
23
|
+
undef_method :sleep if instance_methods(false).include?(:sleep)
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
RSpec.configure do |config|
|
28
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
29
|
+
config.run_all_when_everything_filtered = true
|
30
|
+
config.filter_run :focus
|
31
|
+
|
32
|
+
config.before do
|
33
|
+
Celluloid.logger = logger
|
34
|
+
if Celluloid.running?
|
35
|
+
Celluloid.shutdown
|
36
|
+
sleep 0.01
|
37
|
+
Celluloid.internal_pool.assert_inactive
|
38
|
+
end
|
39
|
+
|
40
|
+
Celluloid.boot
|
41
|
+
|
42
|
+
FileUtils.rm("/tmp/cell_sock") if File.exist?("/tmp/cell_sock")
|
43
|
+
include Timeout::Extensions
|
44
|
+
end
|
45
|
+
|
46
|
+
config.order = 'random'
|
47
|
+
end
|
48
|
+
|
49
|
+
class ExampleActor
|
50
|
+
include Celluloid
|
51
|
+
execute_block_on_receiver :wrap
|
52
|
+
|
53
|
+
def wrap
|
54
|
+
yield
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def within_actor(&block)
|
59
|
+
actor = ExampleActor.new
|
60
|
+
actor.wrap(&block)
|
61
|
+
ensure
|
62
|
+
actor.terminate if actor and actor.alive?
|
63
|
+
end
|
64
|
+
|
65
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Timeout::Extensions do
|
4
|
+
describe "timeout" do
|
5
|
+
let(:dummy_timeout) { double(:meta_timeout) }
|
6
|
+
let(:exception) { double(:exception) }
|
7
|
+
let(:action) { Proc.new{ |t| } }
|
8
|
+
context "inside and outside of actor" do
|
9
|
+
it "hits the proper timeout handler" do
|
10
|
+
within_actor do
|
11
|
+
Thread.current.timeout_handler = dummy_timeout
|
12
|
+
expect(dummy_timeout).to receive(:call).with(2, exception, &action)
|
13
|
+
timeout(2, exception, &action)
|
14
|
+
end
|
15
|
+
expect(Timeout).to receive(:timeout_without_handler)
|
16
|
+
timeout(2, exception, &action)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "sleep" do
|
22
|
+
let(:dummy_sleep) { double(:meta_sleep) }
|
23
|
+
context "inside and outside of actor" do
|
24
|
+
it "hits the proper sleep handler" do
|
25
|
+
within_actor do
|
26
|
+
Thread.current.sleep_handler = dummy_sleep
|
27
|
+
expect(dummy_sleep).to receive(:call).with(2)
|
28
|
+
sleep(2)
|
29
|
+
end
|
30
|
+
expect(self).to receive(:sleep_without_handler)
|
31
|
+
sleep(2)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
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 'timeout/extensions/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "timeout-extensions"
|
8
|
+
spec.version = Timeout::Extensions::VERSION
|
9
|
+
spec.authors = ["Tony Arcieri", "Tiago Cardoso"]
|
10
|
+
spec.email = ["bascule@gmail.com"]
|
11
|
+
spec.summary = "Extensions to the Ruby standard library's timeout API"
|
12
|
+
spec.description = "A timeout extension for Ruby which plugs into multiple timeout backends"
|
13
|
+
spec.homepage = "https://github.com/celluloid/timeout-extensions"
|
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 "rake", "~> 10.4.2"
|
22
|
+
spec.add_development_dependency "pry", "~> 0.10.1"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.14.0"
|
24
|
+
spec.add_development_dependency "celluloid", ">= 0.16.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: timeout-extensions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tony Arcieri
|
8
|
+
- Tiago Cardoso
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-01-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 10.4.2
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 10.4.2
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: pry
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 0.10.1
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 0.10.1
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 2.14.0
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.14.0
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: celluloid
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.16.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.16.0
|
70
|
+
description: A timeout extension for Ruby which plugs into multiple timeout backends
|
71
|
+
email:
|
72
|
+
- bascule@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/timeout/extensions.rb
|
84
|
+
- lib/timeout/extensions/version.rb
|
85
|
+
- log/.gitkeep
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
- spec/timeout/extensions_spec.rb
|
88
|
+
- timeout-extensions.gemspec
|
89
|
+
homepage: https://github.com/celluloid/timeout-extensions
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.4.5
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Extensions to the Ruby standard library's timeout API
|
113
|
+
test_files:
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
- spec/timeout/extensions_spec.rb
|