threads 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +19 -5
- data/lib/threads.rb +8 -5
- data/test/test_threads.rb +10 -3
- data/threads.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11b9c960e3ade614ef2537ed594aca491bf650b0e2efbc0c4723eb6ed06441d7
|
4
|
+
data.tar.gz: 794bbdead21ac26baffb375566fac252495767a312729c94ce15628078f2d835
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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/
|
6
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/24fc3acdf781d98b8749/maintainability)](https://codeclimate.com/github/yegor256/threads/maintainability)
|
7
7
|
|
8
|
-
|
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
|
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
|
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
|
|
data/lib/threads.rb
CHANGED
@@ -33,9 +33,12 @@ class Threads
|
|
33
33
|
@total = total
|
34
34
|
end
|
35
35
|
|
36
|
-
def assert(reps =
|
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
|
-
|
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
|
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
|
data/test/test_threads.rb
CHANGED
@@ -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
|
-
|
37
|
-
Threads.new(total).assert do
|
36
|
+
Threads.new(10).assert do
|
38
37
|
done.increment
|
39
38
|
end
|
40
|
-
assert_equal(
|
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
|
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.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'
|