extreme_timeout 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 06d5ef4ffecdedd7601d8d5b04c0f8cf7d639c86
4
+ data.tar.gz: 8ca3b6824813d5751e608cc8505d1ee6524ffabf
5
+ SHA512:
6
+ metadata.gz: ed37e5da59879351e65efbdacb524b3eacc7333cc8cdcd8550bb54d58ee9385574784118bee3dc9bb1b61349f690b94e7febac67ef622837663e96fb0052373f
7
+ data.tar.gz: 7e426b30a5fbcab7e6afd3248d42f8287f9b6e3efff8f7775a32b63e04bf6e87774c41112af5b6f251caec8717da85993fb295c240e9029b620db87d5b4a2271
@@ -0,0 +1,20 @@
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
+ *.dll
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in extreme_timeout.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Masaya Suzuki
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.
@@ -0,0 +1,34 @@
1
+ # ExtremeTimeout
2
+
3
+ Do it in 40 seconds, or die.
4
+
5
+ require 'extreme_timeout'
6
+ ExtremeTimeout::timeout(40) do
7
+ do_something
8
+ end
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'extreme_timeout'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install extreme_timeout
23
+
24
+ ## Usage
25
+
26
+ TODO: Write usage instructions here
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/extensiontask"
3
+
4
+ Rake::ExtensionTask.new "extreme_timeout"
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile('extreme_timeout')
@@ -0,0 +1,68 @@
1
+ #include <ruby.h>
2
+ #include <pthread.h>
3
+ #include <stdio.h>
4
+ #include <stdlib.h>
5
+ #include <unistd.h>
6
+
7
+ struct wait_args {
8
+ unsigned int timeout_sec;
9
+ int exitcode;
10
+ };
11
+
12
+ void *
13
+ sleep_thread_main(void *_arg)
14
+ {
15
+ struct wait_args *arg = _arg;
16
+ sleep(arg->timeout_sec);
17
+ fprintf(stderr, "Process exits(ExtremeTimeout::timeout)");
18
+ exit(arg->exitcode);
19
+ }
20
+
21
+ VALUE
22
+ timeout(int argc, VALUE *argv, VALUE self)
23
+ {
24
+ int exitcode = 1;
25
+ unsigned int timeout_sec = 0;
26
+ VALUE timeout_sec_value, exitcode_value, block;
27
+ pthread_t thread;
28
+ struct wait_args arg;
29
+ VALUE retval;
30
+
31
+ rb_scan_args(argc, argv, "11&", &timeout_sec_value, &exitcode_value, &block);
32
+
33
+ if (!FIXNUM_P(timeout_sec_value)) {
34
+ rb_raise(rb_eArgError, "the timeout argument should be Fixnum");
35
+ }
36
+ timeout_sec = FIX2UINT(timeout_sec_value);
37
+
38
+ exitcode = 1;
39
+ if (exitcode_value != Qnil) {
40
+ if (!FIXNUM_P(exitcode_value)) {
41
+ rb_raise(rb_eArgError, "the exitcode argument should be Fixnum");
42
+ }
43
+ exitcode = FIX2INT(exitcode_value);
44
+ }
45
+
46
+ if (block == Qnil) {
47
+ rb_raise(rb_eArgError, "expects block");
48
+ }
49
+
50
+ arg.timeout_sec = timeout_sec;
51
+ arg.exitcode = exitcode;
52
+ if (pthread_create(&thread, NULL, sleep_thread_main, &arg) != 0) {
53
+ rb_raise(rb_eRuntimeError, "pthread_create was failed");
54
+ }
55
+
56
+ retval = rb_funcall(block, rb_intern("call"), 0);
57
+
58
+ pthread_cancel(thread);
59
+ pthread_join(thread, NULL);
60
+ return retval;
61
+ }
62
+
63
+ void
64
+ Init_extreme_timeout(void)
65
+ {
66
+ VALUE module = rb_define_module("ExtremeTimeout");
67
+ rb_define_module_function(module, "timeout", timeout, -1);
68
+ }
@@ -0,0 +1,21 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "extreme_timeout"
4
+ spec.version = "0.1.0"
5
+ spec.authors = ["Masaya SUZUKI"]
6
+ spec.email = ["draftcode@gmail.com"]
7
+ spec.description = "Timeout from the outside of the GVL"
8
+ spec.summary = "Timeout from the outside of the GVL"
9
+ spec.homepage = "https://github.com/draftcode/extreme_timeout"
10
+ spec.license = "MIT"
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.extensions = ["ext/extreme_timeout/extconf.rb"]
16
+
17
+ spec.add_development_dependency "bundler", "~> 1.3"
18
+ spec.add_development_dependency "rake"
19
+ spec.add_development_dependency "rake-compiler"
20
+ spec.add_development_dependency "rspec"
21
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+ require 'extreme_timeout'
3
+
4
+ describe ExtremeTimeout do
5
+ describe '.timeout' do
6
+ it 'checks timeout argument' do
7
+ expect { ExtremeTimeout::timeout("10") { } }.to raise_error(ArgumentError)
8
+ expect { ExtremeTimeout::timeout() { } }.to raise_error(ArgumentError)
9
+ end
10
+
11
+ it 'checks exitcode argument' do
12
+ expect { ExtremeTimeout::timeout(10, "2") { } }.to raise_error(ArgumentError)
13
+ end
14
+
15
+ it 'does not execute the block when it raise an ArgumentError' do
16
+ executed = false
17
+ expect { ExtremeTimeout::timeout('10') { executed = true } }.to raise_error(ArgumentError)
18
+ expect(executed).to be_false
19
+ end
20
+
21
+ it 'executes block' do
22
+ executed = false
23
+ ExtremeTimeout::timeout(10) { executed = true }
24
+ expect(executed).to be_true
25
+ end
26
+
27
+ it 'returns the block return value' do
28
+ expect(ExtremeTimeout::timeout(10) { 42 }).to eq(42)
29
+ end
30
+
31
+ it 'raises ArgumentError if no block given' do
32
+ expect { ExtremeTimeout::timeout(3) }.to raise_error(ArgumentError)
33
+ end
34
+
35
+ it 'exits the process when timeout' do
36
+ pending "This test will terminate the whole rspec process"
37
+ ExtremeTimeout::timeout(1) { sleep }
38
+ end
39
+
40
+ it 'exits the process with the exit code specified when timeout' do
41
+ pending "This test will terminate the whole rspec process"
42
+ ExtremeTimeout::timeout(1, 5) { sleep }
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ ext = File.expand_path('../../ext', __FILE__)
8
+ $LOAD_PATH.unshift(ext) unless $LOAD_PATH.include?(ext)
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extreme_timeout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masaya SUZUKI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-15 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: rake-compiler
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: rspec
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: Timeout from the outside of the GVL
70
+ email:
71
+ - draftcode@gmail.com
72
+ executables: []
73
+ extensions:
74
+ - ext/extreme_timeout/extconf.rb
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - ext/extreme_timeout/extconf.rb
84
+ - ext/extreme_timeout/extreme_timeout.c
85
+ - extreme_timeout.gemspec
86
+ - spec/extreme_timeout_spec.rb
87
+ - spec/spec_helper.rb
88
+ homepage: https://github.com/draftcode/extreme_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.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Timeout from the outside of the GVL
112
+ test_files:
113
+ - spec/extreme_timeout_spec.rb
114
+ - spec/spec_helper.rb