threads 0.1.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a727b29dfd70ee9fd089218c0342e43955118073e027b937f255c42dc623dcd
4
- data.tar.gz: af92d34fc180a47af611fe5dadfbf0241a576e910b6deaa4f3e430cb009e28de
3
+ metadata.gz: 11b9c960e3ade614ef2537ed594aca491bf650b0e2efbc0c4723eb6ed06441d7
4
+ data.tar.gz: 794bbdead21ac26baffb375566fac252495767a312729c94ce15628078f2d835
5
5
  SHA512:
6
- metadata.gz: 15035cb016d91eef122223906088e73ed9117c404d40af293ece442b443a4724275717a210aadad830050afc9f7d33caed17b4a387f67c705466f66e398cd142
7
- data.tar.gz: 85f9284c7b484db5870a3d39af33aafe71a308671b2180cc602d03ba3c5580a6a1acc0647bca1604c27c5def30f3cd4021d28fa8accc622dad9f117fea99ee6c
6
+ metadata.gz: 7ad26d667349ecee26e8671473375d679c4a34edd4ab5b2a015f4d8913415a87560e7bfb2fbd64c0ad913ad34f2721840d8d507380f2611fa9b7e5a9982dc494
7
+ data.tar.gz: 74cdf428dde2ec03ca340924414763c4f6cb5204f53e3cda05086c11ae24f8d7dd1ed938ead94100047550a82e84fae8dd29eba00c81768b38e67a4b35f19708
data/README.md CHANGED
@@ -3,9 +3,12 @@
3
3
 
4
4
  [![Build Status](https://travis-ci.org/yegor256/threads.svg)](https://travis-ci.org/yegor256/threads)
5
5
  [![Gem Version](https://badge.fury.io/rb/threads.svg)](http://badge.fury.io/rb/threads)
6
- [![Maintainability](https://api.codeclimate.com/v1/badges/0296baf81e86b90fba70/maintainability)](https://codeclimate.com/github/yegor256/threads/maintainability)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/24fc3acdf781d98b8749/maintainability)](https://codeclimate.com/github/yegor256/threads/maintainability)
7
7
 
8
- Ruby test threads.
8
+ When you need to test your code for thread safety, what do you do?
9
+ That's right, you just don't test it.
10
+ That's [wrong](https://www.yegor256.com/2018/03/27/how-to-test-thread-safety.html)!
11
+ This simple gem helps you test your code with just two additional lines of code.
9
12
 
10
13
  First, install it:
11
14
 
@@ -13,12 +16,23 @@ First, install it:
13
16
  $ gem install threads
14
17
  ```
15
18
 
16
- Then, use it like this, to print a threads:
19
+ Then, use it like this, to test your code from multiple concurrently running threads:
17
20
 
18
21
  ```ruby
19
22
  require 'threads'
20
- Threads.new.assert do |i|
21
- puts "Hello from threads no.#{i}"
23
+ Threads.new(5).assert do |i|
24
+ puts "Hello from the thread no.#{i}"
25
+ end
26
+ ```
27
+
28
+ You can put whatever you want into the block. The code will be executed from five threads, concurrently.
29
+ You can also make sure the code block runs only a specific number of times
30
+ specifying the argument in the `assert` method (it can't be smaller than the amount of threads):
31
+
32
+ ```ruby
33
+ require 'threads'
34
+ Threads.new(5).assert(20) do |i, r|
35
+ puts "Hello from the thread no.#{i}, repetition no.#{r}"
22
36
  end
23
37
  ```
24
38
 
@@ -33,9 +33,12 @@ class Threads
33
33
  @total = total
34
34
  end
35
35
 
36
- def assert(reps = 0)
36
+ def assert(reps = @total)
37
+ if reps < @total
38
+ raise "Repetition counter #{reps} can't be smaller than #{@total}"
39
+ end
37
40
  done = Concurrent::AtomicFixnum.new
38
- cycles = Concurrent::AtomicFixnum.new
41
+ rep = Concurrent::AtomicFixnum.new
39
42
  pool = Concurrent::FixedThreadPool.new(@total)
40
43
  latch = Concurrent::CountDownLatch.new(1)
41
44
  @total.times do |t|
@@ -43,14 +46,14 @@ class Threads
43
46
  Thread.current.name = "assert-thread-#{t}"
44
47
  latch.wait(10)
45
48
  loop do
49
+ r = rep.increment
50
+ break if r > reps
46
51
  begin
47
- yield t
52
+ yield(t, r - 1)
48
53
  rescue StandardError => e
49
54
  puts Backtrace.new(e)
50
55
  raise e
51
56
  end
52
- cycles.increment
53
- break if cycles.value > reps
54
57
  end
55
58
  done.increment
56
59
  end
@@ -33,10 +33,17 @@ require_relative '../lib/threads'
33
33
  class ThreadsTest < Minitest::Test
34
34
  def test_multiple_threads
35
35
  done = Concurrent::AtomicFixnum.new
36
- total = 10
37
- Threads.new(total).assert do
36
+ Threads.new(10).assert do
38
37
  done.increment
39
38
  end
40
- assert_equal(total, done.value)
39
+ assert_equal(10, done.value)
40
+ end
41
+
42
+ def test_multiple_threads_with_cap
43
+ done = Concurrent::AtomicFixnum.new
44
+ Threads.new(3).assert(20) do
45
+ done.increment
46
+ end
47
+ assert_equal(20, done.value)
41
48
  end
42
49
  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.1.0'
34
+ s.version = '0.2.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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: threads
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko