concurrent-ruby 0.1.0 → 0.1.1.pre.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -21
- data/README.md +279 -224
- data/lib/concurrent.rb +27 -20
- data/lib/concurrent/agent.rb +106 -130
- data/lib/concurrent/cached_thread_pool.rb +130 -122
- data/lib/concurrent/defer.rb +67 -69
- data/lib/concurrent/drb_async_demux.rb +72 -0
- data/lib/concurrent/event.rb +60 -60
- data/lib/concurrent/event_machine_defer_proxy.rb +23 -23
- data/lib/concurrent/executor.rb +87 -0
- data/lib/concurrent/fixed_thread_pool.rb +89 -89
- data/lib/concurrent/functions.rb +120 -0
- data/lib/concurrent/future.rb +52 -42
- data/lib/concurrent/global_thread_pool.rb +3 -3
- data/lib/concurrent/goroutine.rb +29 -25
- data/lib/concurrent/obligation.rb +67 -121
- data/lib/concurrent/promise.rb +172 -194
- data/lib/concurrent/reactor.rb +162 -0
- data/lib/concurrent/smart_mutex.rb +66 -0
- data/lib/concurrent/tcp_sync_demux.rb +96 -0
- data/lib/concurrent/thread_pool.rb +65 -61
- data/lib/concurrent/utilities.rb +34 -0
- data/lib/concurrent/version.rb +3 -3
- data/lib/concurrent_ruby.rb +1 -1
- data/md/agent.md +123 -123
- data/md/defer.md +174 -174
- data/md/event.md +32 -32
- data/md/executor.md +176 -0
- data/md/future.md +83 -83
- data/md/goroutine.md +52 -52
- data/md/obligation.md +32 -32
- data/md/promise.md +225 -225
- data/md/thread_pool.md +197 -197
- data/spec/concurrent/agent_spec.rb +376 -405
- data/spec/concurrent/cached_thread_pool_spec.rb +112 -112
- data/spec/concurrent/defer_spec.rb +209 -199
- data/spec/concurrent/event_machine_defer_proxy_spec.rb +250 -246
- data/spec/concurrent/event_spec.rb +134 -134
- data/spec/concurrent/executor_spec.rb +146 -0
- data/spec/concurrent/fixed_thread_pool_spec.rb +84 -84
- data/spec/concurrent/functions_spec.rb +57 -0
- data/spec/concurrent/future_spec.rb +125 -115
- data/spec/concurrent/goroutine_spec.rb +67 -52
- data/spec/concurrent/obligation_shared.rb +121 -121
- data/spec/concurrent/promise_spec.rb +299 -310
- data/spec/concurrent/smart_mutex_spec.rb +234 -0
- data/spec/concurrent/thread_pool_shared.rb +209 -209
- data/spec/concurrent/utilities_spec.rb +74 -0
- data/spec/spec_helper.rb +21 -19
- metadata +38 -14
- checksums.yaml +0 -7
@@ -1,246 +1,250 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
require 'concurrent/agent'
|
4
|
-
require 'concurrent/future'
|
5
|
-
require 'concurrent/promise'
|
6
|
-
|
7
|
-
module Concurrent
|
8
|
-
|
9
|
-
describe EventMachineDeferProxy do
|
10
|
-
|
11
|
-
subject { EventMachineDeferProxy.new }
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
@expected
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
@expected
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
@expected
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
@expected
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
subject.
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
p.
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'concurrent/agent'
|
4
|
+
require 'concurrent/future'
|
5
|
+
require 'concurrent/promise'
|
6
|
+
|
7
|
+
module Concurrent
|
8
|
+
|
9
|
+
describe EventMachineDeferProxy do
|
10
|
+
|
11
|
+
subject { EventMachineDeferProxy.new }
|
12
|
+
|
13
|
+
after(:all) do
|
14
|
+
$GLOBAL_THREAD_POOL = FixedThreadPool.new(1)
|
15
|
+
end
|
16
|
+
|
17
|
+
context '#post' do
|
18
|
+
|
19
|
+
it 'proxies a call without arguments' do
|
20
|
+
@expected = false
|
21
|
+
EventMachine.run do
|
22
|
+
subject.post{ @expected = true }
|
23
|
+
sleep(0.1)
|
24
|
+
EventMachine.stop
|
25
|
+
end
|
26
|
+
@expected.should eq true
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'proxies a call with arguments' do
|
30
|
+
@expected = []
|
31
|
+
EventMachine.run do
|
32
|
+
subject.post(1,2,3){|*args| @expected = args }
|
33
|
+
sleep(0.1)
|
34
|
+
EventMachine.stop
|
35
|
+
end
|
36
|
+
@expected.should eq [1,2,3]
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'aliases #<<' do
|
40
|
+
@expected = false
|
41
|
+
EventMachine.run do
|
42
|
+
subject << proc{ @expected = true }
|
43
|
+
sleep(0.1)
|
44
|
+
EventMachine.stop
|
45
|
+
end
|
46
|
+
@expected.should eq true
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'operation' do
|
51
|
+
|
52
|
+
context 'goroutine' do
|
53
|
+
|
54
|
+
it 'passes all arguments to the block' do
|
55
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
56
|
+
|
57
|
+
EventMachine.run do
|
58
|
+
|
59
|
+
@expected = nil
|
60
|
+
go(1, 2, 3){|a, b, c| @expected = [c, b, a] }
|
61
|
+
sleep(0.1)
|
62
|
+
@expected.should eq [3, 2, 1]
|
63
|
+
|
64
|
+
EventMachine.stop
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context Agent do
|
70
|
+
|
71
|
+
subject { Agent.new(0) }
|
72
|
+
|
73
|
+
it 'supports fulfillment' do
|
74
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
75
|
+
|
76
|
+
EventMachine.run do
|
77
|
+
|
78
|
+
@expected = []
|
79
|
+
subject.post{ @expected << 1 }
|
80
|
+
subject.post{ @expected << 2 }
|
81
|
+
subject.post{ @expected << 3 }
|
82
|
+
sleep(0.1)
|
83
|
+
@expected.should eq [1,2,3]
|
84
|
+
|
85
|
+
EventMachine.stop
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'supports validation' do
|
90
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
91
|
+
|
92
|
+
EventMachine.run do
|
93
|
+
|
94
|
+
@expected = nil
|
95
|
+
subject.validate{ @expected = 10; true }
|
96
|
+
subject.post{ nil }
|
97
|
+
sleep(0.1)
|
98
|
+
@expected.should eq 10
|
99
|
+
|
100
|
+
EventMachine.stop
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'supports rejection' do
|
105
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
106
|
+
|
107
|
+
EventMachine.run do
|
108
|
+
|
109
|
+
@expected = nil
|
110
|
+
subject.
|
111
|
+
on_error(StandardError){|ex| @expected = 1 }.
|
112
|
+
on_error(StandardError){|ex| @expected = 2 }.
|
113
|
+
on_error(StandardError){|ex| @expected = 3 }
|
114
|
+
subject.post{ raise StandardError }
|
115
|
+
sleep(0.1)
|
116
|
+
@expected.should eq 1
|
117
|
+
|
118
|
+
EventMachine.stop
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context Future do
|
124
|
+
|
125
|
+
it 'supports fulfillment' do
|
126
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
127
|
+
|
128
|
+
EventMachine.run do
|
129
|
+
|
130
|
+
@a = @b = @c = nil
|
131
|
+
f = Future.new(1, 2, 3) do |a, b, c|
|
132
|
+
@a, @b, @c = a, b, c
|
133
|
+
end
|
134
|
+
sleep(0.1)
|
135
|
+
[@a, @b, @c].should eq [1, 2, 3]
|
136
|
+
|
137
|
+
sleep(0.1)
|
138
|
+
EventMachine.stop
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context Promise do
|
144
|
+
|
145
|
+
context 'fulfillment' do
|
146
|
+
|
147
|
+
it 'passes all arguments to the first promise in the chain' do
|
148
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
149
|
+
|
150
|
+
EventMachine.run do
|
151
|
+
|
152
|
+
@a = @b = @c = nil
|
153
|
+
p = Promise.new(1, 2, 3) do |a, b, c|
|
154
|
+
@a, @b, @c = a, b, c
|
155
|
+
end
|
156
|
+
sleep(0.1)
|
157
|
+
[@a, @b, @c].should eq [1, 2, 3]
|
158
|
+
|
159
|
+
sleep(0.1)
|
160
|
+
EventMachine.stop
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'passes the result of each block to all its children' do
|
165
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
166
|
+
|
167
|
+
EventMachine.run do
|
168
|
+
@expected = nil
|
169
|
+
Promise.new(10){|a| a * 2 }.then{|result| @expected = result}
|
170
|
+
sleep(0.1)
|
171
|
+
@expected.should eq 20
|
172
|
+
|
173
|
+
sleep(0.1)
|
174
|
+
EventMachine.stop
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'sets the promise value to the result if its block' do
|
179
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
180
|
+
|
181
|
+
EventMachine.run do
|
182
|
+
|
183
|
+
p = Promise.new(10){|a| a * 2 }.then{|result| result * 2}
|
184
|
+
sleep(0.1)
|
185
|
+
p.value.should eq 40
|
186
|
+
|
187
|
+
sleep(0.1)
|
188
|
+
EventMachine.stop
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
context 'rejection' do
|
194
|
+
|
195
|
+
it 'sets the promise reason and error on exception' do
|
196
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
197
|
+
|
198
|
+
EventMachine.run do
|
199
|
+
|
200
|
+
p = Promise.new{ raise StandardError.new('Boom!') }
|
201
|
+
sleep(0.1)
|
202
|
+
p.reason.should be_a(Exception)
|
203
|
+
p.reason.should.to_s =~ /Boom!/
|
204
|
+
p.should be_rejected
|
205
|
+
|
206
|
+
sleep(0.1)
|
207
|
+
EventMachine.stop
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'calls the first exception block with a matching class' do
|
212
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
213
|
+
|
214
|
+
EventMachine.run do
|
215
|
+
|
216
|
+
@expected = nil
|
217
|
+
Promise.new{ raise StandardError }.
|
218
|
+
on_error(StandardError){|ex| @expected = 1 }.
|
219
|
+
on_error(StandardError){|ex| @expected = 2 }.
|
220
|
+
on_error(StandardError){|ex| @expected = 3 }
|
221
|
+
sleep(0.1)
|
222
|
+
@expected.should eq 1
|
223
|
+
|
224
|
+
sleep(0.1)
|
225
|
+
EventMachine.stop
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
it 'passes the exception object to the matched block' do
|
230
|
+
$GLOBAL_THREAD_POOL = EventMachineDeferProxy.new
|
231
|
+
|
232
|
+
EventMachine.run do
|
233
|
+
|
234
|
+
@expected = nil
|
235
|
+
Promise.new{ raise StandardError }.
|
236
|
+
on_error(ArgumentError){|ex| @expected = ex }.
|
237
|
+
on_error(LoadError){|ex| @expected = ex }.
|
238
|
+
on_error(Exception){|ex| @expected = ex }
|
239
|
+
sleep(0.1)
|
240
|
+
@expected.should be_a(StandardError)
|
241
|
+
|
242
|
+
sleep(0.1)
|
243
|
+
EventMachine.stop
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|