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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1e61241a244fa1f608588c3c156d01f8bad93eff
4
- data.tar.gz: 47ad810211ec3eb3f8843d28af75c2f2c593b1ce
3
+ metadata.gz: 06775ef0519c05357d40793c690e1d920d6986fd
4
+ data.tar.gz: b99341b499d3f242c89a9e34dc1e5709475a309f
5
5
  SHA512:
6
- metadata.gz: e70de9f4469875502be7c335c380962f7db0bc8737007e008bc37de1f296547dba1c5ca47ab3ef161b68e5c84f3ef85c7a15a1067c770a7cce4d34b068e2d045
7
- data.tar.gz: 9b6b631aa5b791a66475898ddbd9ec465f52e254e5caf9d17d41f317dda2df47af64e9b0a72f1b218f941af81c1aa793761dcee832ce6d7199d564fe1e5f5f8f
6
+ metadata.gz: af5195c38784de4e8e8053abd2aa1a9d6aa0769504ddb1eb9b7f985956a9d2a0d1a607ed2582f33612085a93a1213ae3113237170bd52f6f012b75c722b72468
7
+ data.tar.gz: 8ceab0a43ed94525654db0d74f77b7902ada06248b58b011ff16e003ca454a871ff9ec54eccee7a1214e7d574dac60955f0f82ff32e8d5172ef6bea0a426201a
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.2"
4
+ - "1.9.3"
5
+ - "2.0.0"
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ - rbx-19mode
data/README.md CHANGED
@@ -16,7 +16,7 @@ Example
16
16
  ```ruby
17
17
  require 'thread/pool'
18
18
 
19
- pool = Thread::Pool.new(4)
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::Channel.new
44
+ channel = Thread.channel
45
45
  channel.send 'wat'
46
46
  channel.receive # => 'wat'
47
47
 
48
- channel = Thread::Channel.new { |o| o.is_a?(Integer) }
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::Pipe.new \
83
- |-> d { d * 2 } |-> d { d * 4 }
84
-
82
+ p = Thread |-> d { d * 2 } |-> d { d * 4 }
85
83
  p << 2
86
- puts ~p
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
- puts ~future {
123
+ f = Thread.future {
125
124
  sleep 5
126
125
 
127
126
  42
128
- } # => 42
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
- puts ~delay {
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
@@ -96,3 +96,10 @@ private
96
96
  @cond ||= ConditionVariable.new
97
97
  end
98
98
  end
99
+
100
+ class Thread
101
+ # Helper to create a channel.
102
+ def self.channel (*args, &block)
103
+ Thread::Channel.new(*args, &block)
104
+ end
105
+ end
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
@@ -89,3 +89,10 @@ class Thread::Pipe
89
89
  alias deq pop
90
90
  alias ~ pop
91
91
  end
92
+
93
+ class Thread
94
+ # Helper to create a pipe.
95
+ def self.| (func)
96
+ Pipe.new | func
97
+ end
98
+ end
data/lib/thread/pool.rb CHANGED
@@ -360,3 +360,10 @@ private
360
360
  }
361
361
  end
362
362
  end
363
+
364
+ class Thread
365
+ # Helper to create a pool.
366
+ def self.pool (*args, &block)
367
+ Thread::Pool.new(*args, &block)
368
+ end
369
+ end
@@ -68,7 +68,15 @@ private
68
68
  end
69
69
  end
70
70
 
71
+ class Thread
72
+ # Helper method to create a promise.
73
+ def self.promise
74
+ Thread::Promise.new
75
+ end
76
+ end
77
+
71
78
  module Kernel
79
+ # Helper method to create a promise.
72
80
  def promise
73
81
  Thread::Promise.new
74
82
  end
@@ -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
@@ -0,0 +1,13 @@
1
+ #! /usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'thread/delay'
4
+
5
+ describe Thread::Delay do
6
+ it 'delivers a value properly' do
7
+ d = Thread.delay {
8
+ 42
9
+ }
10
+
11
+ d.value.should == 42
12
+ end
13
+ end
@@ -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
@@ -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.5'
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.5
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-28 00:00:00.000000000 Z
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: []