threads 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11b9c960e3ade614ef2537ed594aca491bf650b0e2efbc0c4723eb6ed06441d7
4
- data.tar.gz: 794bbdead21ac26baffb375566fac252495767a312729c94ce15628078f2d835
3
+ metadata.gz: 1a09538d61c265ef8326785b70cce63cd39f8a5334df87fab2d07e856eaec5cf
4
+ data.tar.gz: a85c00b54c98b7bc430febd831f3fc55b313658385e2c5473021c77f079dbd76
5
5
  SHA512:
6
- metadata.gz: 7ad26d667349ecee26e8671473375d679c4a34edd4ab5b2a015f4d8913415a87560e7bfb2fbd64c0ad913ad34f2721840d8d507380f2611fa9b7e5a9982dc494
7
- data.tar.gz: 74cdf428dde2ec03ca340924414763c4f6cb5204f53e3cda05086c11ae24f8d7dd1ed938ead94100047550a82e84fae8dd29eba00c81768b38e67a4b35f19708
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
@@ -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
- puts Backtrace.new(e)
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
@@ -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
@@ -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.2.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.2.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-25 00:00:00.000000000 Z
11
+ date: 2018-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace