thread 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/README.md +14 -11
- data/Rakefile +20 -0
- data/lib/thread/channel.rb +7 -0
- data/lib/thread/delay.rb +9 -0
- data/lib/thread/future.rb +8 -0
- data/lib/thread/pipe.rb +7 -0
- data/lib/thread/pool.rb +7 -0
- data/lib/thread/promise.rb +8 -0
- data/tests/channel_spec.rb +41 -0
- data/tests/delay_spec.rb +13 -0
- data/tests/future_spec.rb +37 -0
- data/tests/pipe_spec.rb +15 -0
- data/tests/promise_spec.rb +37 -0
- data/thread.gemspec +4 -1
- metadata +38 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06775ef0519c05357d40793c690e1d920d6986fd
|
4
|
+
data.tar.gz: b99341b499d3f242c89a9e34dc1e5709475a309f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af5195c38784de4e8e8053abd2aa1a9d6aa0769504ddb1eb9b7f985956a9d2a0d1a607ed2582f33612085a93a1213ae3113237170bd52f6f012b75c722b72468
|
7
|
+
data.tar.gz: 8ceab0a43ed94525654db0d74f77b7902ada06248b58b011ff16e003ca454a871ff9ec54eccee7a1214e7d574dac60955f0f82ff32e8d5172ef6bea0a426201a
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -16,7 +16,7 @@ Example
|
|
16
16
|
```ruby
|
17
17
|
require 'thread/pool'
|
18
18
|
|
19
|
-
pool = Thread
|
19
|
+
pool = Thread.pool(4)
|
20
20
|
|
21
21
|
10.times {
|
22
22
|
pool.process {
|
@@ -41,11 +41,11 @@ Example
|
|
41
41
|
```ruby
|
42
42
|
require 'thread/channel'
|
43
43
|
|
44
|
-
channel = Thread
|
44
|
+
channel = Thread.channel
|
45
45
|
channel.send 'wat'
|
46
46
|
channel.receive # => 'wat'
|
47
47
|
|
48
|
-
channel = Thread
|
48
|
+
channel = Thread.channel { |o| o.is_a?(Integer) }
|
49
49
|
channel.send 'wat' # => ArgumentError: guard mismatch
|
50
50
|
|
51
51
|
Thread.new {
|
@@ -79,11 +79,10 @@ Example
|
|
79
79
|
```ruby
|
80
80
|
require 'thread/pipe'
|
81
81
|
|
82
|
-
p = Thread
|
83
|
-
|-> d { d * 2 } |-> d { d * 4 }
|
84
|
-
|
82
|
+
p = Thread |-> d { d * 2 } |-> d { d * 4 }
|
85
83
|
p << 2
|
86
|
-
|
84
|
+
|
85
|
+
puts ~p # => 16
|
87
86
|
```
|
88
87
|
|
89
88
|
Promise
|
@@ -98,7 +97,7 @@ Example
|
|
98
97
|
```ruby
|
99
98
|
require 'thread/promise'
|
100
99
|
|
101
|
-
p = promise
|
100
|
+
p = Thread.promise
|
102
101
|
|
103
102
|
Thread.new {
|
104
103
|
sleep 5
|
@@ -121,11 +120,13 @@ Example
|
|
121
120
|
```ruby
|
122
121
|
require 'thread/future'
|
123
122
|
|
124
|
-
|
123
|
+
f = Thread.future {
|
125
124
|
sleep 5
|
126
125
|
|
127
126
|
42
|
128
|
-
}
|
127
|
+
}
|
128
|
+
|
129
|
+
puts ~f # => 42
|
129
130
|
```
|
130
131
|
|
131
132
|
Delay
|
@@ -139,7 +140,9 @@ Example
|
|
139
140
|
```ruby
|
140
141
|
require 'thread/delay'
|
141
142
|
|
142
|
-
|
143
|
+
d = Thread.delay {
|
143
144
|
42
|
144
145
|
}
|
146
|
+
|
147
|
+
puts ~d # => 42
|
145
148
|
```
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
task :default => [:install, :test]
|
5
|
+
|
6
|
+
task :install do
|
7
|
+
sh 'gem install --no-force rspec'
|
8
|
+
sh 'gem build *.gemspec'
|
9
|
+
sh 'gem install *.gem'
|
10
|
+
end
|
11
|
+
|
12
|
+
task :test do
|
13
|
+
FileUtils.cd 'tests' do
|
14
|
+
sh 'rspec channel_spec.rb --backtrace --color --format doc'
|
15
|
+
sh 'rspec promise_spec.rb --backtrace --color --format doc'
|
16
|
+
sh 'rspec future_spec.rb --backtrace --color --format doc'
|
17
|
+
sh 'rspec delay_spec.rb --backtrace --color --format doc'
|
18
|
+
sh 'rspec pipe_spec.rb --backtrace --color --format doc'
|
19
|
+
end
|
20
|
+
end
|
data/lib/thread/channel.rb
CHANGED
data/lib/thread/delay.rb
CHANGED
@@ -8,6 +8,8 @@
|
|
8
8
|
# 0. You just DO WHAT THE FUCK YOU WANT TO.
|
9
9
|
#++
|
10
10
|
|
11
|
+
require 'thread'
|
12
|
+
|
11
13
|
# A delay is an object that incapsulates a block which is called upon
|
12
14
|
# value retrieval, and its result cached.
|
13
15
|
class Thread::Delay
|
@@ -77,6 +79,13 @@ class Thread::Delay
|
|
77
79
|
alias ! value!
|
78
80
|
end
|
79
81
|
|
82
|
+
class Thread
|
83
|
+
# Helper to create Thread::Delay
|
84
|
+
def self.delay (&block)
|
85
|
+
Thread::Delay.new(&block)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
80
89
|
module Kernel
|
81
90
|
# Helper to create a Thread::Delay
|
82
91
|
def delay (&block)
|
data/lib/thread/future.rb
CHANGED
@@ -129,7 +129,15 @@ private
|
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
132
|
+
class Thread
|
133
|
+
# Helper to create a future
|
134
|
+
def self.future (&block)
|
135
|
+
Thread::Future.new(&block)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
132
139
|
module Kernel
|
140
|
+
# Helper to create a future.
|
133
141
|
def future (&block)
|
134
142
|
Thread::Future.new(&block)
|
135
143
|
end
|
data/lib/thread/pipe.rb
CHANGED
data/lib/thread/pool.rb
CHANGED
data/lib/thread/promise.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'thread/channel'
|
4
|
+
|
5
|
+
describe Thread::Channel do
|
6
|
+
it 'receives in the proper order' do
|
7
|
+
ch = Thread.channel
|
8
|
+
ch.send 'lol'
|
9
|
+
ch.send 'wut'
|
10
|
+
|
11
|
+
ch.receive.should == 'lol'
|
12
|
+
ch.receive.should == 'wut'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'receives with constraints properly' do
|
16
|
+
ch = Thread.channel
|
17
|
+
ch.send 'lol'
|
18
|
+
ch.send 'wut'
|
19
|
+
|
20
|
+
ch.receive { |v| v == 'wut' }.should == 'wut'
|
21
|
+
ch.receive.should == 'lol'
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'receives nil when using non blocking mode and the channel is empty' do
|
25
|
+
ch = Thread.channel
|
26
|
+
|
27
|
+
ch.receive!.should == nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'guards sending properly' do
|
31
|
+
ch = Thread.channel { |v| v.is_a? Integer }
|
32
|
+
|
33
|
+
expect {
|
34
|
+
ch.send 23
|
35
|
+
}.to_not raise_error
|
36
|
+
|
37
|
+
expect {
|
38
|
+
ch.send 'lol'
|
39
|
+
}.to raise_error
|
40
|
+
end
|
41
|
+
end
|
data/tests/delay_spec.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'thread/future'
|
4
|
+
|
5
|
+
describe Thread::Future do
|
6
|
+
it 'delivers a value properly' do
|
7
|
+
f = Thread.future {
|
8
|
+
sleep 0.2
|
9
|
+
|
10
|
+
42
|
11
|
+
}
|
12
|
+
|
13
|
+
f.value.should == 42
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'properly checks if anything has been delivered' do
|
17
|
+
f = Thread.future {
|
18
|
+
sleep 0.2
|
19
|
+
|
20
|
+
42
|
21
|
+
}
|
22
|
+
|
23
|
+
f.delivered?.should == false
|
24
|
+
sleep 0.3
|
25
|
+
f.delivered?.should == true
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'does not block when a timeout is passed' do
|
29
|
+
f = Thread.future {
|
30
|
+
sleep 0.2
|
31
|
+
|
32
|
+
42
|
33
|
+
}
|
34
|
+
|
35
|
+
f.value(0).should == nil
|
36
|
+
end
|
37
|
+
end
|
data/tests/pipe_spec.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'thread/pipe'
|
4
|
+
|
5
|
+
describe Thread::Pipe do
|
6
|
+
it 'handles passing properly' do
|
7
|
+
p = Thread |-> d { d * 2 } |-> d { d * 4 }
|
8
|
+
|
9
|
+
p << 2
|
10
|
+
p << 4
|
11
|
+
|
12
|
+
p.deq.should == 16
|
13
|
+
p.deq.should == 32
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
require 'rubygems'
|
3
|
+
require 'thread/promise'
|
4
|
+
|
5
|
+
describe Thread::Promise do
|
6
|
+
it 'delivers a value properly' do
|
7
|
+
p = Thread.promise
|
8
|
+
|
9
|
+
Thread.new {
|
10
|
+
sleep 0.2
|
11
|
+
|
12
|
+
p << 42
|
13
|
+
}
|
14
|
+
|
15
|
+
p.value.should == 42
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'properly checks if anything has been delivered' do
|
19
|
+
p = Thread.promise
|
20
|
+
|
21
|
+
Thread.new {
|
22
|
+
sleep 0.2
|
23
|
+
|
24
|
+
p << 42
|
25
|
+
}
|
26
|
+
|
27
|
+
p.delivered?.should == false
|
28
|
+
sleep 0.3
|
29
|
+
p.delivered?.should == true
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'does not block when a timeout is passed' do
|
33
|
+
p = Thread.promise
|
34
|
+
|
35
|
+
p.value(0).should == nil
|
36
|
+
end
|
37
|
+
end
|
data/thread.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new {|s|
|
2
2
|
s.name = 'thread'
|
3
|
-
s.version = '0.0.
|
3
|
+
s.version = '0.0.6'
|
4
4
|
s.author = 'meh.'
|
5
5
|
s.email = 'meh@schizofreni.co'
|
6
6
|
s.homepage = 'http://github.com/meh/ruby-thread'
|
@@ -12,4 +12,7 @@ Gem::Specification.new {|s|
|
|
12
12
|
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
13
13
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
14
|
s.require_paths = ['lib']
|
15
|
+
|
16
|
+
s.add_development_dependency 'rspec'
|
17
|
+
s.add_development_dependency 'rake'
|
15
18
|
}
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thread
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- meh.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-03-
|
12
|
-
dependencies:
|
11
|
+
date: 2013-03-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
description: Includes a thread pool, message passing capabilities, a recursive mutex,
|
14
42
|
promise, future and delay.
|
15
43
|
email: meh@schizofreni.co
|
@@ -17,7 +45,9 @@ executables: []
|
|
17
45
|
extensions: []
|
18
46
|
extra_rdoc_files: []
|
19
47
|
files:
|
48
|
+
- .travis.yml
|
20
49
|
- README.md
|
50
|
+
- Rakefile
|
21
51
|
- lib/thread/channel.rb
|
22
52
|
- lib/thread/delay.rb
|
23
53
|
- lib/thread/future.rb
|
@@ -25,6 +55,11 @@ files:
|
|
25
55
|
- lib/thread/pool.rb
|
26
56
|
- lib/thread/promise.rb
|
27
57
|
- lib/thread/recursive_mutex.rb
|
58
|
+
- tests/channel_spec.rb
|
59
|
+
- tests/delay_spec.rb
|
60
|
+
- tests/future_spec.rb
|
61
|
+
- tests/pipe_spec.rb
|
62
|
+
- tests/promise_spec.rb
|
28
63
|
- thread.gemspec
|
29
64
|
homepage: http://github.com/meh/ruby-thread
|
30
65
|
licenses: []
|