threads 0.2.0 → 0.3.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 +4 -4
- data/README.md +9 -1
- data/lib/threads.rb +17 -2
- data/test/test_threads.rb +22 -0
- data/threads.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1a09538d61c265ef8326785b70cce63cd39f8a5334df87fab2d07e856eaec5cf
|
4
|
+
data.tar.gz: a85c00b54c98b7bc430febd831f3fc55b313658385e2c5473021c77f079dbd76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e57cb56f9e2c817b58130575c1c0e551a4dd60402c965957d5166f2c6833e5dadaa972c9878d1611dfb3699c1e9e2c5511fbdff0bb095e30e12a6c488dc886e
|
7
|
+
data.tar.gz: 45b9cdf4a3b730f7da3fd111bb8084535d569c3ecad493bad3072a63bc65b417092bd9ca1f6d6179c33c3390cedb8afb8ea3a54abd1f2e7272e7320e5fd1ade5
|
data/README.md
CHANGED
@@ -30,12 +30,20 @@ You can also make sure the code block runs only a specific number of times
|
|
30
30
|
specifying the argument in the `assert` method (it can't be smaller than the amount of threads):
|
31
31
|
|
32
32
|
```ruby
|
33
|
-
require 'threads'
|
34
33
|
Threads.new(5).assert(20) do |i, r|
|
35
34
|
puts "Hello from the thread no.#{i}, repetition no.#{r}"
|
36
35
|
end
|
37
36
|
```
|
38
37
|
|
38
|
+
You can also provide a logger, which will print exception backtraces.
|
39
|
+
It has to implement either method `error(msg)` or `puts(msg)`:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
Threads.new(5, log: STDOUT).assert do
|
43
|
+
this_code_fails_for_sure
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
39
47
|
That's it.
|
40
48
|
|
41
49
|
# How to contribute
|
data/lib/threads.rb
CHANGED
@@ -23,14 +23,19 @@
|
|
23
23
|
# SOFTWARE.
|
24
24
|
|
25
25
|
require 'concurrent'
|
26
|
+
require 'backtrace'
|
26
27
|
|
27
28
|
# Threads.
|
28
29
|
# Author:: Yegor Bugayenko (yegor256@gmail.com)
|
29
30
|
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
|
30
31
|
# License:: MIT
|
31
32
|
class Threads
|
32
|
-
def initialize(total = Concurrent.processor_count * 8)
|
33
|
+
def initialize(total = Concurrent.processor_count * 8, log: STDOUT)
|
34
|
+
raise "Total can't be nil" if total.nil?
|
35
|
+
raise "Total can't be negative or zero: #{total}" unless total.positive?
|
33
36
|
@total = total
|
37
|
+
raise "Log can't be nil" if log.nil?
|
38
|
+
@log = log
|
34
39
|
end
|
35
40
|
|
36
41
|
def assert(reps = @total)
|
@@ -51,7 +56,7 @@ class Threads
|
|
51
56
|
begin
|
52
57
|
yield(t, r - 1)
|
53
58
|
rescue StandardError => e
|
54
|
-
|
59
|
+
print(Backtrace.new(e))
|
55
60
|
raise e
|
56
61
|
end
|
57
62
|
end
|
@@ -64,4 +69,14 @@ class Threads
|
|
64
69
|
return if done.value == @total
|
65
70
|
raise "Only #{done.value} out of #{@total} threads completed successfully"
|
66
71
|
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def print(msg)
|
76
|
+
if @log.respond_to?(:error)
|
77
|
+
@log.error(msg)
|
78
|
+
elsif @log.respond_to?(:puts)
|
79
|
+
@log.puts(msg)
|
80
|
+
end
|
81
|
+
end
|
67
82
|
end
|
data/test/test_threads.rb
CHANGED
@@ -46,4 +46,26 @@ class ThreadsTest < Minitest::Test
|
|
46
46
|
end
|
47
47
|
assert_equal(20, done.value)
|
48
48
|
end
|
49
|
+
|
50
|
+
def test_multiple_threads_with_errors
|
51
|
+
log = FakeLog.new
|
52
|
+
assert_raises do
|
53
|
+
Threads.new(3, log: log).assert(20) do
|
54
|
+
this_code_is_broken
|
55
|
+
end
|
56
|
+
end
|
57
|
+
assert_equal(3, log.logs.count)
|
58
|
+
end
|
59
|
+
|
60
|
+
class FakeLog
|
61
|
+
attr_reader :logs
|
62
|
+
|
63
|
+
def initialize
|
64
|
+
@logs = []
|
65
|
+
end
|
66
|
+
|
67
|
+
def error(msg)
|
68
|
+
@logs << msg
|
69
|
+
end
|
70
|
+
end
|
49
71
|
end
|
data/threads.gemspec
CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
s.rubygems_version = '2.3.3'
|
32
32
|
s.required_ruby_version = '>=2.3'
|
33
33
|
s.name = 'threads'
|
34
|
-
s.version = '0.
|
34
|
+
s.version = '0.3.0'
|
35
35
|
s.license = 'MIT'
|
36
36
|
s.summary = 'Test threads'
|
37
37
|
s.description = 'Rugy Gem to test a piece of code in concurrent threads'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: threads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: backtrace
|