concurrent-ruby 0.1.1.pre.3 → 0.1.1.pre.4

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