concurrent-ruby 0.0.1 → 0.1.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 +223 -3
- data/lib/concurrent.rb +19 -0
- data/lib/concurrent/agent.rb +130 -0
- data/lib/concurrent/cached_thread_pool.rb +122 -0
- data/lib/concurrent/defer.rb +69 -0
- data/lib/concurrent/event.rb +60 -0
- data/lib/concurrent/event_machine_defer_proxy.rb +23 -0
- data/lib/concurrent/fixed_thread_pool.rb +89 -0
- data/lib/concurrent/future.rb +42 -0
- data/lib/concurrent/global_thread_pool.rb +3 -0
- data/lib/concurrent/goroutine.rb +25 -0
- data/lib/concurrent/obligation.rb +121 -0
- data/lib/concurrent/promise.rb +194 -0
- data/lib/concurrent/thread_pool.rb +61 -0
- data/lib/concurrent/version.rb +1 -1
- data/lib/concurrent_ruby.rb +1 -0
- data/md/agent.md +123 -0
- data/md/defer.md +174 -0
- data/md/event.md +32 -0
- data/md/future.md +83 -0
- data/md/goroutine.md +52 -0
- data/md/obligation.md +32 -0
- data/md/promise.md +225 -0
- data/md/thread_pool.md +197 -0
- data/spec/concurrent/agent_spec.rb +405 -0
- data/spec/concurrent/cached_thread_pool_spec.rb +112 -0
- data/spec/concurrent/defer_spec.rb +199 -0
- data/spec/concurrent/event_machine_defer_proxy_spec.rb +246 -0
- data/spec/concurrent/event_spec.rb +134 -0
- data/spec/concurrent/fixed_thread_pool_spec.rb +84 -0
- data/spec/concurrent/future_spec.rb +115 -0
- data/spec/concurrent/goroutine_spec.rb +52 -0
- data/spec/concurrent/obligation_shared.rb +121 -0
- data/spec/concurrent/promise_spec.rb +310 -0
- data/spec/concurrent/thread_pool_shared.rb +209 -0
- data/spec/spec_helper.rb +2 -0
- metadata +61 -4
@@ -0,0 +1,209 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Concurrent
|
4
|
+
|
5
|
+
share_examples_for 'Thread Pool' do
|
6
|
+
|
7
|
+
context '#running?' do
|
8
|
+
|
9
|
+
it 'returns true when the thread pool is running' do
|
10
|
+
subject.should be_running
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns false when the thread pool is shutting down' do
|
14
|
+
subject.post{ sleep(1) }
|
15
|
+
subject.shutdown
|
16
|
+
subject.should_not be_running
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'returns false when the thread pool is shutdown' do
|
20
|
+
subject.shutdown
|
21
|
+
subject.should_not be_running
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns false when the thread pool is killed' do
|
25
|
+
subject.shutdown
|
26
|
+
subject.should_not be_running
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context '#shutdown?' do
|
31
|
+
|
32
|
+
it 'returns true if #shutdown has been called' do
|
33
|
+
subject.shutdown
|
34
|
+
subject.should be_shutdown
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'returns false when running' do
|
38
|
+
subject.should_not be_shutdown
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context '#killed?' do
|
43
|
+
|
44
|
+
it 'returns true if tasks were killed at shutdown' do
|
45
|
+
subject.post{ sleep(1) }
|
46
|
+
subject.kill
|
47
|
+
subject.should be_killed
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'returns false when running' do
|
51
|
+
subject.should_not be_killed
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context '#shutdown' do
|
56
|
+
|
57
|
+
it 'stops accepting new tasks' do
|
58
|
+
subject.post{ sleep(1) }
|
59
|
+
subject.shutdown
|
60
|
+
@expected = false
|
61
|
+
subject.post{ @expected = true }.should be_false
|
62
|
+
sleep(1)
|
63
|
+
@expected.should be_false
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'allows in-progress tasks to complete' do
|
67
|
+
@expected = false
|
68
|
+
subject.post{ sleep(0.5); @expected = true }
|
69
|
+
subject.shutdown
|
70
|
+
sleep(1)
|
71
|
+
@expected.should be_true
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'allows pending tasks to complete' do
|
75
|
+
@expected = false
|
76
|
+
subject.post{ sleep(0.2) }
|
77
|
+
subject.post{ sleep(0.2); @expected = true }
|
78
|
+
subject.shutdown
|
79
|
+
sleep(1)
|
80
|
+
@expected.should be_true
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'allows threads to exit normally' do
|
84
|
+
pool = FixedThreadPool.new(5)
|
85
|
+
pool.shutdown
|
86
|
+
sleep(1)
|
87
|
+
pool.status.should be_empty
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context '#kill' do
|
92
|
+
|
93
|
+
it 'stops accepting new tasks' do
|
94
|
+
subject.post{ sleep(1) }
|
95
|
+
subject.kill
|
96
|
+
@expected = false
|
97
|
+
subject.post{ @expected = true }.should be_false
|
98
|
+
sleep(1)
|
99
|
+
@expected.should be_false
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'attempts to kill all in-progress tasks' do
|
103
|
+
@expected = false
|
104
|
+
subject.post{ sleep(1); @expected = true }
|
105
|
+
subject.kill
|
106
|
+
sleep(1)
|
107
|
+
@expected.should be_false
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'rejects all pending tasks' do
|
111
|
+
@expected = false
|
112
|
+
subject.post{ sleep(0.5) }
|
113
|
+
subject.post{ sleep(0.5); @expected = true }
|
114
|
+
subject.kill
|
115
|
+
sleep(1)
|
116
|
+
@expected.should be_false
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context '#wait_for_termination' do
|
121
|
+
|
122
|
+
it 'immediately returns true after shutdown has complete' do
|
123
|
+
subject.shutdown
|
124
|
+
subject.wait_for_termination.should be_true
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'blocks indefinitely when timeout it nil' do
|
128
|
+
subject.post{ sleep(1) }
|
129
|
+
subject.shutdown
|
130
|
+
subject.wait_for_termination(nil).should be_true
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'returns true when shutdown sucessfully completes before timeout' do
|
134
|
+
subject.post{ sleep(0.5) }
|
135
|
+
subject.shutdown
|
136
|
+
subject.wait_for_termination(1).should be_true
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'returns false when shutdown fails to complete before timeout' do
|
140
|
+
subject.post{ sleep(1) }
|
141
|
+
subject.shutdown
|
142
|
+
subject.wait_for_termination(0.5).should be_true
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context '#post' do
|
147
|
+
|
148
|
+
it 'raises an exception if no block is given' do
|
149
|
+
lambda {
|
150
|
+
subject.post
|
151
|
+
}.should raise_error(ArgumentError)
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'returns true when the block is added to the queue' do
|
155
|
+
subject.post{ nil }.should be_true
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'calls the block with the given arguments' do
|
159
|
+
@expected = nil
|
160
|
+
subject.post(1, 2, 3)do |a, b, c|
|
161
|
+
@expected = a + b + c
|
162
|
+
end
|
163
|
+
sleep(0.1)
|
164
|
+
@expected.should eq 6
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'rejects the block while shutting down' do
|
168
|
+
pool = FixedThreadPool.new(5)
|
169
|
+
pool.post{ sleep(1) }
|
170
|
+
pool.shutdown
|
171
|
+
@expected = nil
|
172
|
+
pool.post(1, 2, 3)do |a, b, c|
|
173
|
+
@expected = a + b + c
|
174
|
+
end
|
175
|
+
@expected.should be_nil
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'returns false while shutting down' do
|
179
|
+
subject.post{ sleep(1) }
|
180
|
+
subject.shutdown
|
181
|
+
subject.post{ nil }.should be_false
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'rejects the block once shutdown' do
|
185
|
+
pool = FixedThreadPool.new(5)
|
186
|
+
pool.shutdown
|
187
|
+
@expected = nil
|
188
|
+
pool.post(1, 2, 3)do |a, b, c|
|
189
|
+
@expected = a + b + c
|
190
|
+
end
|
191
|
+
@expected.should be_nil
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'returns false once shutdown' do
|
195
|
+
subject.post{ nil }
|
196
|
+
subject.shutdown
|
197
|
+
sleep(0.1)
|
198
|
+
subject.post{ nil }.should be_false
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'aliases #<<' do
|
202
|
+
@expected = false
|
203
|
+
subject << proc { @expected = true }
|
204
|
+
sleep(0.1)
|
205
|
+
@expected.should be_true
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: concurrent-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jerry D'Antonio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: functional-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.7.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.7.0
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -25,7 +39,7 @@ dependencies:
|
|
25
39
|
- !ruby/object:Gem::Version
|
26
40
|
version: '0'
|
27
41
|
description: |2
|
28
|
-
|
42
|
+
A gem for adding Erlang, Clojure, and Go inspired concurrent programming tools to Ruby.
|
29
43
|
email: jerry.dantonio@gmail.com
|
30
44
|
executables: []
|
31
45
|
extensions: []
|
@@ -35,8 +49,40 @@ extra_rdoc_files:
|
|
35
49
|
files:
|
36
50
|
- README.md
|
37
51
|
- LICENSE
|
52
|
+
- lib/concurrent/agent.rb
|
53
|
+
- lib/concurrent/cached_thread_pool.rb
|
54
|
+
- lib/concurrent/defer.rb
|
55
|
+
- lib/concurrent/event.rb
|
56
|
+
- lib/concurrent/event_machine_defer_proxy.rb
|
57
|
+
- lib/concurrent/fixed_thread_pool.rb
|
58
|
+
- lib/concurrent/future.rb
|
59
|
+
- lib/concurrent/global_thread_pool.rb
|
60
|
+
- lib/concurrent/goroutine.rb
|
61
|
+
- lib/concurrent/obligation.rb
|
62
|
+
- lib/concurrent/promise.rb
|
63
|
+
- lib/concurrent/thread_pool.rb
|
38
64
|
- lib/concurrent/version.rb
|
39
65
|
- lib/concurrent.rb
|
66
|
+
- lib/concurrent_ruby.rb
|
67
|
+
- md/agent.md
|
68
|
+
- md/defer.md
|
69
|
+
- md/event.md
|
70
|
+
- md/future.md
|
71
|
+
- md/goroutine.md
|
72
|
+
- md/obligation.md
|
73
|
+
- md/promise.md
|
74
|
+
- md/thread_pool.md
|
75
|
+
- spec/concurrent/agent_spec.rb
|
76
|
+
- spec/concurrent/cached_thread_pool_spec.rb
|
77
|
+
- spec/concurrent/defer_spec.rb
|
78
|
+
- spec/concurrent/event_machine_defer_proxy_spec.rb
|
79
|
+
- spec/concurrent/event_spec.rb
|
80
|
+
- spec/concurrent/fixed_thread_pool_spec.rb
|
81
|
+
- spec/concurrent/future_spec.rb
|
82
|
+
- spec/concurrent/goroutine_spec.rb
|
83
|
+
- spec/concurrent/obligation_shared.rb
|
84
|
+
- spec/concurrent/promise_spec.rb
|
85
|
+
- spec/concurrent/thread_pool_shared.rb
|
40
86
|
- spec/spec_helper.rb
|
41
87
|
homepage: https://github.com/jdantonio/concurrent-ruby/
|
42
88
|
licenses:
|
@@ -61,6 +107,17 @@ rubyforge_project:
|
|
61
107
|
rubygems_version: 2.0.3
|
62
108
|
signing_key:
|
63
109
|
specification_version: 4
|
64
|
-
summary:
|
110
|
+
summary: Erlang, Clojure, and Go inspired concurrent programming tools for Ruby.
|
65
111
|
test_files:
|
112
|
+
- spec/concurrent/agent_spec.rb
|
113
|
+
- spec/concurrent/cached_thread_pool_spec.rb
|
114
|
+
- spec/concurrent/defer_spec.rb
|
115
|
+
- spec/concurrent/event_machine_defer_proxy_spec.rb
|
116
|
+
- spec/concurrent/event_spec.rb
|
117
|
+
- spec/concurrent/fixed_thread_pool_spec.rb
|
118
|
+
- spec/concurrent/future_spec.rb
|
119
|
+
- spec/concurrent/goroutine_spec.rb
|
120
|
+
- spec/concurrent/obligation_shared.rb
|
121
|
+
- spec/concurrent/promise_spec.rb
|
122
|
+
- spec/concurrent/thread_pool_shared.rb
|
66
123
|
- spec/spec_helper.rb
|