better_thread 0.0.1

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
+ SHA256:
3
+ metadata.gz: b2a2965d6125acc8ee2e25486617aeab5b8f5a7ec59b87772763642de8cd33ef
4
+ data.tar.gz: 46cca4ca6a108556d0298ec40e8b209de1ab55a4ebaeb3867bafb339bcf4c34b
5
+ SHA512:
6
+ metadata.gz: 377061a4041ba071cecf30ff8ec51ed8360ceade4d80267f1116f46db77f333f2ed950867542cdff7aa6b119cd6364cd79159ec503a6628bb2937c2acae51872
7
+ data.tar.gz: 8c852c21b92418b640514b5176a0818671b0c7efb6c8bc9b79aa2d48d1784eb9f8d24ef78236a9fedf8ff0ede034019050f4a44ecb0f2e60074c1e1ee6c8f9d9
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ better_thread
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.1.1
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in better-thread.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "standard", "~> 1.3"
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ better_thread (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ parallel (1.21.0)
12
+ parser (3.1.1.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.1.1)
15
+ rake (13.0.6)
16
+ regexp_parser (2.2.1)
17
+ rexml (3.2.5)
18
+ rspec (3.11.0)
19
+ rspec-core (~> 3.11.0)
20
+ rspec-expectations (~> 3.11.0)
21
+ rspec-mocks (~> 3.11.0)
22
+ rspec-core (3.11.0)
23
+ rspec-support (~> 3.11.0)
24
+ rspec-expectations (3.11.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.11.0)
27
+ rspec-mocks (3.11.0)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-support (3.11.0)
31
+ rubocop (1.26.0)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.1.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.16.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.16.0)
41
+ parser (>= 3.1.1.0)
42
+ rubocop-performance (1.13.3)
43
+ rubocop (>= 1.7.0, < 2.0)
44
+ rubocop-ast (>= 0.4.0)
45
+ ruby-progressbar (1.11.0)
46
+ standard (1.8.0)
47
+ rubocop (= 1.26.0)
48
+ rubocop-performance (= 1.13.3)
49
+ unicode-display_width (2.1.0)
50
+
51
+ PLATFORMS
52
+ arm64-darwin-21
53
+
54
+ DEPENDENCIES
55
+ better_thread!
56
+ rake (~> 13.0)
57
+ rspec (~> 3.0)
58
+ standard (~> 1.3)
59
+
60
+ BUNDLED WITH
61
+ 2.3.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Matti Paksula
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # BetterThread
2
+
3
+ ```ruby
4
+ bt = Better::Thread.new timeout: 3 do
5
+ sleep 5
6
+ end
7
+
8
+ bt.join # joins after 3 seconds
9
+
10
+ bt.timeout? # true
11
+
12
+ ```
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'better_thread'
19
+ ```
20
+ ## Contributing
21
+
22
+ Bug reports and pull requests are welcome on GitHub at https://github.com/matti/better-thread
23
+
24
+ ## License
25
+
26
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "better_thread"
5
+ spec.version = "0.0.1"
6
+ spec.authors = ["Matti Paksula"]
7
+ spec.email = ["matti.paksula@iki.fi"]
8
+
9
+ spec.summary = "BetterThread"
10
+ spec.description = ""
11
+ spec.homepage = "https://github.com/matti/better_thread"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = ">= 2.6.0"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
22
+ end
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ # Uncomment to register a new dependency of your gem
29
+ # spec.add_dependency "example-gem", "~> 1.0"
30
+
31
+ # For more information and examples about making a new gem, check out our
32
+ # guide at: https://bundler.io/guides/creating_gem.html
33
+ end
data/e2e/test.rb ADDED
@@ -0,0 +1,27 @@
1
+ require_relative "../lib/better_thread"
2
+
3
+ bt_normal = BetterThread.new do
4
+ puts "normal"
5
+ end
6
+
7
+ bt_normal.join
8
+
9
+ bt_timeout = BetterThread.new timeout: 0.3 do
10
+ loop do
11
+ puts "timeout"
12
+ sleep 0.1
13
+ end
14
+ end
15
+
16
+ bt_timeout.join
17
+ raise "timeout?" unless bt_timeout.timeout?
18
+
19
+ bt_raise = BetterThread.new timeout: 0.5 do
20
+ raise "wat"
21
+ rescue
22
+ puts "rescued"
23
+ end
24
+
25
+ bt_raise.join
26
+
27
+ puts "OK"
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+
5
+ class BetterThread
6
+ extend Forwardable
7
+
8
+ def_delegators :@thread, :join
9
+
10
+ def initialize(opts = {}, &block)
11
+ @timeout = false
12
+
13
+ __better_thread_killer opts[:timeout] if opts[:timeout]
14
+ @thread = ::Thread.send :new, &block
15
+ end
16
+
17
+ def timeout?
18
+ @timeout
19
+ end
20
+
21
+ private
22
+
23
+ def __better_thread_killer(timeout)
24
+ @killer = Thread.new do
25
+ started_at = Time.now
26
+ loop do
27
+ raise StopIteration unless @thread&.alive?
28
+
29
+ if Time.now - started_at > timeout
30
+ @timeout = true
31
+ break
32
+ end
33
+
34
+ sleep 0.1
35
+ end
36
+
37
+ @thread&.kill if @timeout
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: better_thread
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matti Paksula
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-03-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email:
15
+ - matti.paksula@iki.fi
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".ruby-gemset"
22
+ - ".ruby-version"
23
+ - ".standard.yml"
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - better_thread.gemspec
30
+ - e2e/test.rb
31
+ - lib/better_thread.rb
32
+ homepage: https://github.com/matti/better_thread
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ homepage_uri: https://github.com/matti/better_thread
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 2.6.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.3.7
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: BetterThread
56
+ test_files: []