concurrent-ruby 0.2.1 → 0.2.2

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.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -21
  3. data/README.md +276 -275
  4. data/lib/concurrent.rb +28 -28
  5. data/lib/concurrent/agent.rb +114 -114
  6. data/lib/concurrent/cached_thread_pool.rb +131 -131
  7. data/lib/concurrent/defer.rb +65 -65
  8. data/lib/concurrent/event.rb +60 -60
  9. data/lib/concurrent/event_machine_defer_proxy.rb +23 -23
  10. data/lib/concurrent/executor.rb +96 -96
  11. data/lib/concurrent/fixed_thread_pool.rb +99 -99
  12. data/lib/concurrent/functions.rb +120 -120
  13. data/lib/concurrent/future.rb +42 -42
  14. data/lib/concurrent/global_thread_pool.rb +24 -16
  15. data/lib/concurrent/goroutine.rb +29 -29
  16. data/lib/concurrent/null_thread_pool.rb +22 -22
  17. data/lib/concurrent/obligation.rb +67 -67
  18. data/lib/concurrent/promise.rb +174 -174
  19. data/lib/concurrent/reactor.rb +166 -166
  20. data/lib/concurrent/reactor/drb_async_demux.rb +83 -83
  21. data/lib/concurrent/reactor/tcp_sync_demux.rb +131 -131
  22. data/lib/concurrent/supervisor.rb +105 -105
  23. data/lib/concurrent/thread_pool.rb +76 -76
  24. data/lib/concurrent/utilities.rb +32 -32
  25. data/lib/concurrent/version.rb +3 -3
  26. data/lib/concurrent_ruby.rb +1 -1
  27. data/md/agent.md +123 -123
  28. data/md/defer.md +174 -174
  29. data/md/event.md +32 -32
  30. data/md/executor.md +187 -187
  31. data/md/future.md +83 -83
  32. data/md/goroutine.md +52 -52
  33. data/md/obligation.md +32 -32
  34. data/md/promise.md +227 -227
  35. data/md/thread_pool.md +224 -224
  36. data/spec/concurrent/agent_spec.rb +390 -386
  37. data/spec/concurrent/cached_thread_pool_spec.rb +125 -125
  38. data/spec/concurrent/defer_spec.rb +199 -195
  39. data/spec/concurrent/event_machine_defer_proxy_spec.rb +256 -256
  40. data/spec/concurrent/event_spec.rb +134 -134
  41. data/spec/concurrent/executor_spec.rb +200 -200
  42. data/spec/concurrent/fixed_thread_pool_spec.rb +83 -83
  43. data/spec/concurrent/functions_spec.rb +217 -217
  44. data/spec/concurrent/future_spec.rb +112 -108
  45. data/spec/concurrent/global_thread_pool_spec.rb +11 -38
  46. data/spec/concurrent/goroutine_spec.rb +67 -67
  47. data/spec/concurrent/null_thread_pool_spec.rb +57 -57
  48. data/spec/concurrent/obligation_shared.rb +132 -132
  49. data/spec/concurrent/promise_spec.rb +316 -312
  50. data/spec/concurrent/reactor/drb_async_demux_spec.rb +196 -196
  51. data/spec/concurrent/reactor/tcp_sync_demux_spec.rb +410 -410
  52. data/spec/concurrent/reactor_spec.rb +364 -364
  53. data/spec/concurrent/supervisor_spec.rb +269 -269
  54. data/spec/concurrent/thread_pool_shared.rb +204 -204
  55. data/spec/concurrent/uses_global_thread_pool_shared.rb +64 -0
  56. data/spec/concurrent/utilities_spec.rb +74 -74
  57. data/spec/spec_helper.rb +32 -32
  58. metadata +17 -19
@@ -1,83 +1,83 @@
1
- require 'spec_helper'
2
- require_relative 'thread_pool_shared'
3
-
4
- module Concurrent
5
-
6
- describe FixedThreadPool do
7
-
8
- subject { FixedThreadPool.new(5) }
9
-
10
- it_should_behave_like 'Thread Pool'
11
-
12
- context '#initialize' do
13
-
14
- it 'raises an exception when the pool size is less than one' do
15
- lambda {
16
- FixedThreadPool.new(0)
17
- }.should raise_error(ArgumentError)
18
- end
19
-
20
- it 'raises an exception when the pool size is greater than 1024' do
21
- lambda {
22
- FixedThreadPool.new(1025)
23
- }.should raise_error(ArgumentError)
24
- end
25
-
26
- it 'creates a thread pool of the given size' do
27
- thread = Thread.new{ nil }
28
- # add one for the garbage collector
29
- Thread.should_receive(:new).exactly(5+1).times.and_return(thread)
30
- pool = FixedThreadPool.new(5)
31
- pool.size.should eq 5
32
- end
33
-
34
- it 'aliases Concurrent#new_fixed_thread_pool' do
35
- pool = Concurrent.new_fixed_thread_pool(5)
36
- pool.should be_a(FixedThreadPool)
37
- pool.size.should eq 5
38
- end
39
- end
40
-
41
- context '#kill' do
42
-
43
- it 'kills all threads' do
44
- Thread.should_receive(:kill).at_least(5).times
45
- pool = FixedThreadPool.new(5)
46
- pool.kill
47
- sleep(0.1)
48
- end
49
- end
50
-
51
- context '#size' do
52
-
53
- let(:pool_size) { 3 }
54
- subject { FixedThreadPool.new(pool_size) }
55
-
56
- it 'returns the size of the subject when running' do
57
- subject.size.should eq pool_size
58
- end
59
-
60
- it 'returns zero while shutting down' do
61
- subject.post{ sleep(1) }
62
- subject.shutdown
63
- subject.size.should eq 0
64
- end
65
-
66
- it 'returns zero once shut down' do
67
- subject.shutdown
68
- subject.size.should eq 0
69
- end
70
- end
71
-
72
- context 'exception handling' do
73
-
74
- it 'restarts threads that experience exception' do
75
- pool = FixedThreadPool.new(5)
76
- 3.times{ pool << proc{ raise StandardError } }
77
- sleep(5)
78
- pool.size.should eq 5
79
- pool.status.should_not include(nil)
80
- end
81
- end
82
- end
83
- end
1
+ require 'spec_helper'
2
+ require_relative 'thread_pool_shared'
3
+
4
+ module Concurrent
5
+
6
+ describe FixedThreadPool do
7
+
8
+ subject { FixedThreadPool.new(5) }
9
+
10
+ it_should_behave_like 'Thread Pool'
11
+
12
+ context '#initialize' do
13
+
14
+ it 'raises an exception when the pool size is less than one' do
15
+ lambda {
16
+ FixedThreadPool.new(0)
17
+ }.should raise_error(ArgumentError)
18
+ end
19
+
20
+ it 'raises an exception when the pool size is greater than 1024' do
21
+ lambda {
22
+ FixedThreadPool.new(1025)
23
+ }.should raise_error(ArgumentError)
24
+ end
25
+
26
+ it 'creates a thread pool of the given size' do
27
+ thread = Thread.new{ nil }
28
+ # add one for the garbage collector
29
+ Thread.should_receive(:new).exactly(5+1).times.and_return(thread)
30
+ pool = FixedThreadPool.new(5)
31
+ pool.size.should eq 5
32
+ end
33
+
34
+ it 'aliases Concurrent#new_fixed_thread_pool' do
35
+ pool = Concurrent.new_fixed_thread_pool(5)
36
+ pool.should be_a(FixedThreadPool)
37
+ pool.size.should eq 5
38
+ end
39
+ end
40
+
41
+ context '#kill' do
42
+
43
+ it 'kills all threads' do
44
+ Thread.should_receive(:kill).at_least(5).times
45
+ pool = FixedThreadPool.new(5)
46
+ pool.kill
47
+ sleep(0.1)
48
+ end
49
+ end
50
+
51
+ context '#size' do
52
+
53
+ let(:pool_size) { 3 }
54
+ subject { FixedThreadPool.new(pool_size) }
55
+
56
+ it 'returns the size of the subject when running' do
57
+ subject.size.should eq pool_size
58
+ end
59
+
60
+ it 'returns zero while shutting down' do
61
+ subject.post{ sleep(1) }
62
+ subject.shutdown
63
+ subject.size.should eq 0
64
+ end
65
+
66
+ it 'returns zero once shut down' do
67
+ subject.shutdown
68
+ subject.size.should eq 0
69
+ end
70
+ end
71
+
72
+ context 'exception handling' do
73
+
74
+ it 'restarts threads that experience exception' do
75
+ pool = FixedThreadPool.new(5)
76
+ 3.times{ pool << proc{ raise StandardError } }
77
+ sleep(5)
78
+ pool.size.should eq 5
79
+ pool.status.should_not include(nil)
80
+ end
81
+ end
82
+ end
83
+ end
@@ -1,217 +1,217 @@
1
- require 'spec_helper'
2
-
3
- module Concurrent
4
-
5
- describe 'functions' do
6
-
7
- context '#post' do
8
-
9
- it 'calls #post when supported by the object' do
10
- object = Class.new{
11
- def post() nil; end
12
- }.new
13
- object.should_receive(:post).with(no_args())
14
- post(object){ nil }
15
- end
16
-
17
- it 'raises an exception when not supported by the object' do
18
- object = Class.new{ }.new
19
- lambda {
20
- post(object){ nil }
21
- }.should raise_error(ArgumentError)
22
- end
23
- end
24
-
25
- context '#deref' do
26
-
27
- it 'returns the value of the #deref function' do
28
- object = Class.new{
29
- def deref() nil; end
30
- }.new
31
- object.should_receive(:deref).with(nil)
32
- deref(object, nil){ nil }
33
- end
34
-
35
- it 'returns the value of the #value function' do
36
- object = Class.new{
37
- def value() nil; end
38
- }.new
39
- object.should_receive(:value).with(nil)
40
- deref(object, nil){ nil }
41
- end
42
-
43
- it 'raises an exception when not supported by the object' do
44
- object = Class.new{ }.new
45
- lambda {
46
- deref(object, nil){ nil }
47
- }.should raise_error(ArgumentError)
48
- end
49
- end
50
-
51
- context '#pending?' do
52
-
53
- it 'returns the value of the #pending? function' do
54
- object = Class.new{
55
- def pending?() nil; end
56
- }.new
57
- object.should_receive(:pending?).with(no_args())
58
- pending?(object){ nil }
59
- end
60
-
61
- it 'raises an exception when not supported by the object' do
62
- object = Class.new{ }.new
63
- lambda {
64
- pending?(object){ nil }
65
- }.should raise_error(ArgumentError)
66
- end
67
- end
68
-
69
- context '#fulfilled?' do
70
-
71
- it 'returns the value of the #fulfilled? function' do
72
- object = Class.new{
73
- def fulfilled?() nil; end
74
- }.new
75
- object.should_receive(:fulfilled?).with(no_args())
76
- fulfilled?(object){ nil }
77
- end
78
-
79
- it 'returns the value of the #realized? function' do
80
- object = Class.new{
81
- def realized?() nil; end
82
- }.new
83
- object.should_receive(:realized?).with(no_args())
84
- fulfilled?(object){ nil }
85
- end
86
-
87
- it 'raises an exception when not supported by the object' do
88
- object = Class.new{ }.new
89
- lambda {
90
- fulfilled?(object){ nil }
91
- }.should raise_error(ArgumentError)
92
- end
93
- end
94
-
95
- context '#realized?' do
96
-
97
- it 'returns the value of the #realized? function' do
98
- object = Class.new{
99
- def realized?() nil; end
100
- }.new
101
- object.should_receive(:realized?).with(no_args())
102
- realized?(object){ nil }
103
- end
104
-
105
- it 'returns the value of the #fulfilled? function' do
106
- object = Class.new{
107
- def fulfilled?() nil; end
108
- }.new
109
- object.should_receive(:fulfilled?).with(no_args())
110
- realized?(object){ nil }
111
- end
112
-
113
- it 'raises an exception when not supported by the object' do
114
- object = Class.new{ }.new
115
- lambda {
116
- realized?(object){ nil }
117
- }.should raise_error(ArgumentError)
118
- end
119
- end
120
-
121
- context '#rejected?' do
122
-
123
- it 'returns the value of the #rejected? function' do
124
- object = Class.new{
125
- def rejected?() nil; end
126
- }.new
127
- object.should_receive(:rejected?).with(no_args())
128
- rejected?(object){ nil }
129
- end
130
-
131
- it 'raises an exception when not supported by the object' do
132
- object = Class.new{ }.new
133
- lambda {
134
- rejected?(object){ nil }
135
- }.should raise_error(ArgumentError)
136
- end
137
- end
138
- end
139
-
140
- describe Agent do
141
-
142
- before(:each) do
143
- Agent.thread_pool = FixedThreadPool.new(1)
144
- end
145
-
146
- it 'aliases #<< for Agent#post' do
147
- subject = Agent.new(0)
148
- subject << proc{ 100 }
149
- sleep(0.1)
150
- subject.value.should eq 100
151
- end
152
-
153
- it 'aliases Kernel#agent for Agent.new' do
154
- agent(10).should be_a(Agent)
155
- end
156
-
157
- it 'aliases Kernel#deref for #deref' do
158
- deref(Agent.new(10)).should eq 10
159
- deref(Agent.new(10), 10).should eq 10
160
- end
161
-
162
- it 'aliases Kernel:post for Agent#post' do
163
- subject = Agent.new(0)
164
- post(subject){ 100 }
165
- sleep(0.1)
166
- subject.value.should eq 100
167
- end
168
- end
169
-
170
- describe Defer do
171
-
172
- before(:each) do
173
- Defer.thread_pool = FixedThreadPool.new(1)
174
- end
175
-
176
- it 'aliases Kernel#defer' do
177
- defer{ nil }.should be_a(Defer)
178
- end
179
- end
180
-
181
- describe Executor do
182
-
183
- it 'aliases Kernel#executor' do
184
- ex = executor('executor'){ nil }
185
- ex.should be_a(Executor::ExecutionContext)
186
- ex.kill
187
- end
188
- end
189
-
190
- describe Future do
191
-
192
- before(:each) do
193
- Future.thread_pool = FixedThreadPool.new(1)
194
- end
195
-
196
- it 'aliases Kernel#future for Future.new' do
197
- future().should be_a(Future)
198
- future(){ nil }.should be_a(Future)
199
- future(1, 2, 3).should be_a(Future)
200
- future(1, 2, 3){ nil }.should be_a(Future)
201
- end
202
- end
203
-
204
- describe Promise do
205
-
206
- before(:each) do
207
- Promise.thread_pool = FixedThreadPool.new(1)
208
- end
209
-
210
- it 'aliases Kernel#promise for Promise.new' do
211
- promise().should be_a(Promise)
212
- promise(){ nil }.should be_a(Promise)
213
- promise(1, 2, 3).should be_a(Promise)
214
- promise(1, 2, 3){ nil }.should be_a(Promise)
215
- end
216
- end
217
- end
1
+ require 'spec_helper'
2
+
3
+ module Concurrent
4
+
5
+ describe 'functions' do
6
+
7
+ context '#post' do
8
+
9
+ it 'calls #post when supported by the object' do
10
+ object = Class.new{
11
+ def post() nil; end
12
+ }.new
13
+ object.should_receive(:post).with(no_args())
14
+ post(object){ nil }
15
+ end
16
+
17
+ it 'raises an exception when not supported by the object' do
18
+ object = Class.new{ }.new
19
+ lambda {
20
+ post(object){ nil }
21
+ }.should raise_error(ArgumentError)
22
+ end
23
+ end
24
+
25
+ context '#deref' do
26
+
27
+ it 'returns the value of the #deref function' do
28
+ object = Class.new{
29
+ def deref() nil; end
30
+ }.new
31
+ object.should_receive(:deref).with(nil)
32
+ deref(object, nil){ nil }
33
+ end
34
+
35
+ it 'returns the value of the #value function' do
36
+ object = Class.new{
37
+ def value() nil; end
38
+ }.new
39
+ object.should_receive(:value).with(nil)
40
+ deref(object, nil){ nil }
41
+ end
42
+
43
+ it 'raises an exception when not supported by the object' do
44
+ object = Class.new{ }.new
45
+ lambda {
46
+ deref(object, nil){ nil }
47
+ }.should raise_error(ArgumentError)
48
+ end
49
+ end
50
+
51
+ context '#pending?' do
52
+
53
+ it 'returns the value of the #pending? function' do
54
+ object = Class.new{
55
+ def pending?() nil; end
56
+ }.new
57
+ object.should_receive(:pending?).with(no_args())
58
+ pending?(object){ nil }
59
+ end
60
+
61
+ it 'raises an exception when not supported by the object' do
62
+ object = Class.new{ }.new
63
+ lambda {
64
+ pending?(object){ nil }
65
+ }.should raise_error(ArgumentError)
66
+ end
67
+ end
68
+
69
+ context '#fulfilled?' do
70
+
71
+ it 'returns the value of the #fulfilled? function' do
72
+ object = Class.new{
73
+ def fulfilled?() nil; end
74
+ }.new
75
+ object.should_receive(:fulfilled?).with(no_args())
76
+ fulfilled?(object){ nil }
77
+ end
78
+
79
+ it 'returns the value of the #realized? function' do
80
+ object = Class.new{
81
+ def realized?() nil; end
82
+ }.new
83
+ object.should_receive(:realized?).with(no_args())
84
+ fulfilled?(object){ nil }
85
+ end
86
+
87
+ it 'raises an exception when not supported by the object' do
88
+ object = Class.new{ }.new
89
+ lambda {
90
+ fulfilled?(object){ nil }
91
+ }.should raise_error(ArgumentError)
92
+ end
93
+ end
94
+
95
+ context '#realized?' do
96
+
97
+ it 'returns the value of the #realized? function' do
98
+ object = Class.new{
99
+ def realized?() nil; end
100
+ }.new
101
+ object.should_receive(:realized?).with(no_args())
102
+ realized?(object){ nil }
103
+ end
104
+
105
+ it 'returns the value of the #fulfilled? function' do
106
+ object = Class.new{
107
+ def fulfilled?() nil; end
108
+ }.new
109
+ object.should_receive(:fulfilled?).with(no_args())
110
+ realized?(object){ nil }
111
+ end
112
+
113
+ it 'raises an exception when not supported by the object' do
114
+ object = Class.new{ }.new
115
+ lambda {
116
+ realized?(object){ nil }
117
+ }.should raise_error(ArgumentError)
118
+ end
119
+ end
120
+
121
+ context '#rejected?' do
122
+
123
+ it 'returns the value of the #rejected? function' do
124
+ object = Class.new{
125
+ def rejected?() nil; end
126
+ }.new
127
+ object.should_receive(:rejected?).with(no_args())
128
+ rejected?(object){ nil }
129
+ end
130
+
131
+ it 'raises an exception when not supported by the object' do
132
+ object = Class.new{ }.new
133
+ lambda {
134
+ rejected?(object){ nil }
135
+ }.should raise_error(ArgumentError)
136
+ end
137
+ end
138
+ end
139
+
140
+ describe Agent do
141
+
142
+ before(:each) do
143
+ Agent.thread_pool = FixedThreadPool.new(1)
144
+ end
145
+
146
+ it 'aliases #<< for Agent#post' do
147
+ subject = Agent.new(0)
148
+ subject << proc{ 100 }
149
+ sleep(0.1)
150
+ subject.value.should eq 100
151
+ end
152
+
153
+ it 'aliases Kernel#agent for Agent.new' do
154
+ agent(10).should be_a(Agent)
155
+ end
156
+
157
+ it 'aliases Kernel#deref for #deref' do
158
+ deref(Agent.new(10)).should eq 10
159
+ deref(Agent.new(10), 10).should eq 10
160
+ end
161
+
162
+ it 'aliases Kernel:post for Agent#post' do
163
+ subject = Agent.new(0)
164
+ post(subject){ 100 }
165
+ sleep(0.1)
166
+ subject.value.should eq 100
167
+ end
168
+ end
169
+
170
+ describe Defer do
171
+
172
+ before(:each) do
173
+ Defer.thread_pool = FixedThreadPool.new(1)
174
+ end
175
+
176
+ it 'aliases Kernel#defer' do
177
+ defer{ nil }.should be_a(Defer)
178
+ end
179
+ end
180
+
181
+ describe Executor do
182
+
183
+ it 'aliases Kernel#executor' do
184
+ ex = executor('executor'){ nil }
185
+ ex.should be_a(Executor::ExecutionContext)
186
+ ex.kill
187
+ end
188
+ end
189
+
190
+ describe Future do
191
+
192
+ before(:each) do
193
+ Future.thread_pool = FixedThreadPool.new(1)
194
+ end
195
+
196
+ it 'aliases Kernel#future for Future.new' do
197
+ future().should be_a(Future)
198
+ future(){ nil }.should be_a(Future)
199
+ future(1, 2, 3).should be_a(Future)
200
+ future(1, 2, 3){ nil }.should be_a(Future)
201
+ end
202
+ end
203
+
204
+ describe Promise do
205
+
206
+ before(:each) do
207
+ Promise.thread_pool = FixedThreadPool.new(1)
208
+ end
209
+
210
+ it 'aliases Kernel#promise for Promise.new' do
211
+ promise().should be_a(Promise)
212
+ promise(){ nil }.should be_a(Promise)
213
+ promise(1, 2, 3).should be_a(Promise)
214
+ promise(1, 2, 3){ nil }.should be_a(Promise)
215
+ end
216
+ end
217
+ end