concurrent-ruby 0.6.0.pre.1 → 0.6.0.pre.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -0
  3. data/lib/concurrent.rb +9 -29
  4. data/lib/concurrent/{actor.rb → actor/actor.rb} +3 -3
  5. data/lib/concurrent/actor/actor_context.rb +77 -0
  6. data/lib/concurrent/actor/actor_ref.rb +67 -0
  7. data/lib/concurrent/{postable.rb → actor/postable.rb} +1 -1
  8. data/lib/concurrent/actor/simple_actor_ref.rb +94 -0
  9. data/lib/concurrent/actors.rb +5 -0
  10. data/lib/concurrent/agent.rb +81 -47
  11. data/lib/concurrent/async.rb +35 -35
  12. data/lib/concurrent/atomic/atomic_boolean.rb +157 -0
  13. data/lib/concurrent/atomic/atomic_fixnum.rb +170 -0
  14. data/lib/concurrent/{condition.rb → atomic/condition.rb} +0 -0
  15. data/lib/concurrent/{copy_on_notify_observer_set.rb → atomic/copy_on_notify_observer_set.rb} +48 -13
  16. data/lib/concurrent/{copy_on_write_observer_set.rb → atomic/copy_on_write_observer_set.rb} +41 -20
  17. data/lib/concurrent/atomic/count_down_latch.rb +116 -0
  18. data/lib/concurrent/atomic/cyclic_barrier.rb +106 -0
  19. data/lib/concurrent/atomic/event.rb +103 -0
  20. data/lib/concurrent/{thread_local_var.rb → atomic/thread_local_var.rb} +0 -0
  21. data/lib/concurrent/atomics.rb +9 -0
  22. data/lib/concurrent/channel/buffered_channel.rb +6 -4
  23. data/lib/concurrent/channel/channel.rb +30 -2
  24. data/lib/concurrent/channel/unbuffered_channel.rb +2 -2
  25. data/lib/concurrent/channel/waitable_list.rb +3 -1
  26. data/lib/concurrent/channels.rb +5 -0
  27. data/lib/concurrent/{channel → collection}/blocking_ring_buffer.rb +16 -5
  28. data/lib/concurrent/collection/priority_queue.rb +305 -0
  29. data/lib/concurrent/{channel → collection}/ring_buffer.rb +6 -1
  30. data/lib/concurrent/collections.rb +3 -0
  31. data/lib/concurrent/configuration.rb +68 -19
  32. data/lib/concurrent/dataflow.rb +9 -9
  33. data/lib/concurrent/delay.rb +21 -13
  34. data/lib/concurrent/dereferenceable.rb +40 -33
  35. data/lib/concurrent/exchanger.rb +3 -0
  36. data/lib/concurrent/{cached_thread_pool.rb → executor/cached_thread_pool.rb} +8 -9
  37. data/lib/concurrent/executor/executor.rb +222 -0
  38. data/lib/concurrent/{fixed_thread_pool.rb → executor/fixed_thread_pool.rb} +6 -7
  39. data/lib/concurrent/{immediate_executor.rb → executor/immediate_executor.rb} +5 -5
  40. data/lib/concurrent/executor/java_cached_thread_pool.rb +31 -0
  41. data/lib/concurrent/{java_fixed_thread_pool.rb → executor/java_fixed_thread_pool.rb} +7 -11
  42. data/lib/concurrent/executor/java_single_thread_executor.rb +21 -0
  43. data/lib/concurrent/{java_thread_pool_executor.rb → executor/java_thread_pool_executor.rb} +66 -77
  44. data/lib/concurrent/executor/one_by_one.rb +65 -0
  45. data/lib/concurrent/{per_thread_executor.rb → executor/per_thread_executor.rb} +4 -4
  46. data/lib/concurrent/executor/ruby_cached_thread_pool.rb +29 -0
  47. data/lib/concurrent/{ruby_fixed_thread_pool.rb → executor/ruby_fixed_thread_pool.rb} +5 -4
  48. data/lib/concurrent/executor/ruby_single_thread_executor.rb +72 -0
  49. data/lib/concurrent/executor/ruby_thread_pool_executor.rb +282 -0
  50. data/lib/concurrent/{ruby_thread_pool_worker.rb → executor/ruby_thread_pool_worker.rb} +6 -6
  51. data/lib/concurrent/{safe_task_executor.rb → executor/safe_task_executor.rb} +20 -13
  52. data/lib/concurrent/executor/single_thread_executor.rb +35 -0
  53. data/lib/concurrent/executor/thread_pool_executor.rb +68 -0
  54. data/lib/concurrent/executor/timer_set.rb +138 -0
  55. data/lib/concurrent/executors.rb +9 -0
  56. data/lib/concurrent/future.rb +39 -40
  57. data/lib/concurrent/ivar.rb +22 -15
  58. data/lib/concurrent/mvar.rb +2 -1
  59. data/lib/concurrent/obligation.rb +9 -3
  60. data/lib/concurrent/observable.rb +33 -0
  61. data/lib/concurrent/options_parser.rb +46 -0
  62. data/lib/concurrent/promise.rb +23 -24
  63. data/lib/concurrent/scheduled_task.rb +21 -45
  64. data/lib/concurrent/timer_task.rb +204 -126
  65. data/lib/concurrent/tvar.rb +1 -1
  66. data/lib/concurrent/utilities.rb +3 -36
  67. data/lib/concurrent/{processor_count.rb → utility/processor_count.rb} +1 -1
  68. data/lib/concurrent/utility/timeout.rb +36 -0
  69. data/lib/concurrent/utility/timer.rb +21 -0
  70. data/lib/concurrent/version.rb +1 -1
  71. data/lib/concurrent_ruby_ext.bundle +0 -0
  72. data/spec/concurrent/{actor_context_spec.rb → actor/actor_context_spec.rb} +0 -8
  73. data/spec/concurrent/{actor_ref_shared.rb → actor/actor_ref_shared.rb} +9 -59
  74. data/spec/concurrent/{actor_spec.rb → actor/actor_spec.rb} +43 -41
  75. data/spec/concurrent/{postable_shared.rb → actor/postable_shared.rb} +0 -0
  76. data/spec/concurrent/actor/simple_actor_ref_spec.rb +135 -0
  77. data/spec/concurrent/agent_spec.rb +160 -71
  78. data/spec/concurrent/atomic/atomic_boolean_spec.rb +172 -0
  79. data/spec/concurrent/atomic/atomic_fixnum_spec.rb +186 -0
  80. data/spec/concurrent/{condition_spec.rb → atomic/condition_spec.rb} +2 -2
  81. data/spec/concurrent/{copy_on_notify_observer_set_spec.rb → atomic/copy_on_notify_observer_set_spec.rb} +0 -0
  82. data/spec/concurrent/{copy_on_write_observer_set_spec.rb → atomic/copy_on_write_observer_set_spec.rb} +0 -0
  83. data/spec/concurrent/atomic/count_down_latch_spec.rb +151 -0
  84. data/spec/concurrent/atomic/cyclic_barrier_spec.rb +248 -0
  85. data/spec/concurrent/{event_spec.rb → atomic/event_spec.rb} +18 -3
  86. data/spec/concurrent/{observer_set_shared.rb → atomic/observer_set_shared.rb} +15 -6
  87. data/spec/concurrent/{thread_local_var_spec.rb → atomic/thread_local_var_spec.rb} +0 -0
  88. data/spec/concurrent/channel/buffered_channel_spec.rb +1 -1
  89. data/spec/concurrent/channel/channel_spec.rb +6 -4
  90. data/spec/concurrent/channel/probe_spec.rb +37 -9
  91. data/spec/concurrent/channel/unbuffered_channel_spec.rb +2 -2
  92. data/spec/concurrent/{channel → collection}/blocking_ring_buffer_spec.rb +0 -0
  93. data/spec/concurrent/collection/priority_queue_spec.rb +317 -0
  94. data/spec/concurrent/{channel → collection}/ring_buffer_spec.rb +0 -0
  95. data/spec/concurrent/configuration_spec.rb +4 -70
  96. data/spec/concurrent/dereferenceable_shared.rb +5 -4
  97. data/spec/concurrent/exchanger_spec.rb +10 -5
  98. data/spec/concurrent/{cached_thread_pool_shared.rb → executor/cached_thread_pool_shared.rb} +15 -37
  99. data/spec/concurrent/{fixed_thread_pool_shared.rb → executor/fixed_thread_pool_shared.rb} +0 -0
  100. data/spec/concurrent/{global_thread_pool_shared.rb → executor/global_thread_pool_shared.rb} +10 -8
  101. data/spec/concurrent/{immediate_executor_spec.rb → executor/immediate_executor_spec.rb} +0 -0
  102. data/spec/concurrent/{java_cached_thread_pool_spec.rb → executor/java_cached_thread_pool_spec.rb} +1 -21
  103. data/spec/concurrent/{java_fixed_thread_pool_spec.rb → executor/java_fixed_thread_pool_spec.rb} +0 -0
  104. data/spec/concurrent/executor/java_single_thread_executor_spec.rb +21 -0
  105. data/spec/concurrent/{java_thread_pool_executor_spec.rb → executor/java_thread_pool_executor_spec.rb} +0 -0
  106. data/spec/concurrent/{per_thread_executor_spec.rb → executor/per_thread_executor_spec.rb} +0 -4
  107. data/spec/concurrent/{ruby_cached_thread_pool_spec.rb → executor/ruby_cached_thread_pool_spec.rb} +1 -1
  108. data/spec/concurrent/{ruby_fixed_thread_pool_spec.rb → executor/ruby_fixed_thread_pool_spec.rb} +0 -0
  109. data/spec/concurrent/executor/ruby_single_thread_executor_spec.rb +18 -0
  110. data/spec/concurrent/{ruby_thread_pool_executor_spec.rb → executor/ruby_thread_pool_executor_spec.rb} +12 -24
  111. data/spec/concurrent/executor/safe_task_executor_spec.rb +103 -0
  112. data/spec/concurrent/{thread_pool_class_cast_spec.rb → executor/thread_pool_class_cast_spec.rb} +12 -0
  113. data/spec/concurrent/{thread_pool_executor_shared.rb → executor/thread_pool_executor_shared.rb} +0 -0
  114. data/spec/concurrent/{thread_pool_shared.rb → executor/thread_pool_shared.rb} +84 -119
  115. data/spec/concurrent/executor/timer_set_spec.rb +183 -0
  116. data/spec/concurrent/future_spec.rb +12 -0
  117. data/spec/concurrent/ivar_spec.rb +11 -1
  118. data/spec/concurrent/observable_shared.rb +173 -0
  119. data/spec/concurrent/observable_spec.rb +51 -0
  120. data/spec/concurrent/options_parser_spec.rb +71 -0
  121. data/spec/concurrent/runnable_shared.rb +6 -0
  122. data/spec/concurrent/scheduled_task_spec.rb +60 -40
  123. data/spec/concurrent/timer_task_spec.rb +130 -144
  124. data/spec/concurrent/{processor_count_spec.rb → utility/processor_count_spec.rb} +0 -0
  125. data/spec/concurrent/{utilities_spec.rb → utility/timeout_spec.rb} +0 -0
  126. data/spec/concurrent/utility/timer_spec.rb +52 -0
  127. metadata +147 -108
  128. data/lib/concurrent/actor_context.rb +0 -31
  129. data/lib/concurrent/actor_ref.rb +0 -39
  130. data/lib/concurrent/atomic.rb +0 -121
  131. data/lib/concurrent/channel/probe.rb +0 -19
  132. data/lib/concurrent/count_down_latch.rb +0 -60
  133. data/lib/concurrent/event.rb +0 -80
  134. data/lib/concurrent/java_cached_thread_pool.rb +0 -45
  135. data/lib/concurrent/ruby_cached_thread_pool.rb +0 -37
  136. data/lib/concurrent/ruby_thread_pool_executor.rb +0 -268
  137. data/lib/concurrent/simple_actor_ref.rb +0 -124
  138. data/lib/concurrent/thread_pool_executor.rb +0 -30
  139. data/spec/concurrent/atomic_spec.rb +0 -201
  140. data/spec/concurrent/count_down_latch_spec.rb +0 -125
  141. data/spec/concurrent/safe_task_executor_spec.rb +0 -58
  142. data/spec/concurrent/simple_actor_ref_spec.rb +0 -219
@@ -1,58 +0,0 @@
1
- require 'spec_helper'
2
- require 'concurrent/safe_task_executor'
3
-
4
- module Concurrent
5
-
6
- describe SafeTaskExecutor do
7
-
8
- describe '#execute' do
9
-
10
- context 'happy execution' do
11
-
12
- let(:task) { Proc.new { 42 } }
13
- let(:executor) { SafeTaskExecutor.new(task) }
14
-
15
- it 'should return success' do
16
- success, value, reason = executor.execute
17
- success.should be_true
18
- end
19
-
20
- it 'should return task value' do
21
- success, value, reason = executor.execute
22
- value.should eq 42
23
- end
24
-
25
- it 'should return a nil reason' do
26
- success, value, reason = executor.execute
27
- reason.should be_nil
28
- end
29
-
30
- end
31
-
32
- context 'failing execution' do
33
-
34
- let(:task) { Proc.new { raise StandardError.new('an error') } }
35
- let(:executor) { SafeTaskExecutor.new(task) }
36
-
37
- it 'should return false success' do
38
- success, value, reason = executor.execute
39
- success.should be_false
40
- end
41
-
42
- it 'should return a nil value' do
43
- success, value, reason = executor.execute
44
- value.should be_nil
45
- end
46
-
47
- it 'should return the reason' do
48
- success, value, reason = executor.execute
49
- reason.should be_a(StandardError)
50
- reason.message.should eq 'an error'
51
- end
52
-
53
- end
54
-
55
- end
56
-
57
- end
58
- end
@@ -1,219 +0,0 @@
1
- require 'spec_helper'
2
- require_relative 'actor_ref_shared'
3
-
4
- module Concurrent
5
-
6
- describe SimpleActorRef do
7
-
8
- after(:each) do
9
- subject.shutdown
10
- sleep(0.1)
11
- end
12
-
13
- subject do
14
- shared_actor_test_class.spawn
15
- end
16
-
17
- it_should_behave_like :actor_ref
18
-
19
- context 'construction' do
20
-
21
- it 'supports :args being nil' do
22
- subject = shared_actor_test_class.spawn
23
- actor = subject.instance_variable_get(:@actor)
24
- actor.argv.should be_empty
25
- end
26
-
27
- it 'passes all :args option to the actor constructor' do
28
- subject = shared_actor_test_class.spawn(args: [1, 2, 3, 4])
29
- actor = subject.instance_variable_get(:@actor)
30
- actor.argv.should eq [1, 2, 3, 4]
31
- end
32
-
33
- it 'passes the options hash to the ActorRef constructor' do
34
- subject # prevent the after(:all) block from breaking this test
35
- opts = {foo: :bar, hello: :world}
36
- described_class.should_receive(:new).once.with(anything, opts)
37
- shared_actor_test_class.spawn(opts)
38
- end
39
- end
40
-
41
- context 'supervision' do
42
-
43
- it 'does not start a new thread on construction' do
44
- Thread.should_not_receive(:new).with(any_args)
45
- subject = shared_actor_test_class.spawn
46
- end
47
-
48
- it 'starts a new thread on the first post' do
49
- thread = Thread.new{ nil }
50
- Thread.should_receive(:new).once.with(no_args).and_return(thread)
51
- subject << :foo
52
- end
53
-
54
- it 'does not start a new thread after the first post' do
55
- subject << :foo
56
- sleep(0.1)
57
- expected = Thread.list.length
58
- 5.times{ subject << :foo }
59
- Thread.list.length.should eq expected
60
- end
61
-
62
- it 'starts a new thread when the prior thread has died' do
63
- subject << :foo
64
- sleep(0.1)
65
-
66
- subject << :terminate
67
- sleep(0.1)
68
-
69
- thread = Thread.new{ nil }
70
- Thread.should_receive(:new).once.with(no_args).and_return(thread)
71
- subject << :foo
72
- end
73
-
74
- it 'does not reset the thread after shutdown' do
75
- thread = Thread.new{ nil }
76
- Thread.should_receive(:new).once.with(no_args).and_return(thread)
77
- subject << :foo
78
- sleep(0.1)
79
-
80
- subject.shutdown
81
- sleep(0.1)
82
-
83
- subject << :foo
84
- end
85
-
86
- it 'calls #on_start when the thread is first started' do
87
- actor = subject.instance_variable_get(:@actor)
88
- actor.should_receive(:on_start).once.with(no_args)
89
- subject << :foo
90
- end
91
-
92
- it 'calls #on_reset when the thread is started after the first time' do
93
- actor = subject.instance_variable_get(:@actor)
94
- actor.should_receive(:on_reset).once.with(no_args)
95
- subject << :terminate
96
- sleep(0.1)
97
- subject << :foo
98
- end
99
- end
100
-
101
- context 'abort_on_exception' do
102
-
103
- after(:each) do
104
- @ref.shutdown if @ref
105
- end
106
-
107
- it 'gets set on the actor thread' do
108
- @ref = shared_actor_test_class.spawn(abort_on_exception: true)
109
- @ref << :foo
110
- sleep(0.1)
111
- @ref.instance_variable_get(:@thread).abort_on_exception.should be_true
112
-
113
- @ref = shared_actor_test_class.spawn(abort_on_exception: false)
114
- @ref << :foo
115
- sleep(0.1)
116
- @ref.instance_variable_get(:@thread).abort_on_exception.should be_false
117
- end
118
-
119
- it 'defaults to true' do
120
- @ref = shared_actor_test_class.spawn
121
- @ref << :foo
122
- sleep(0.1)
123
- @ref.instance_variable_get(:@thread).abort_on_exception.should be_true
124
- end
125
- end
126
-
127
- context 'reset_on_error' do
128
-
129
- after(:each) do
130
- @ref.shutdown if @ref
131
- end
132
-
133
- it 'causes #on_reset to be called on exception when true' do
134
- @ref = shared_actor_test_class.spawn(reset_on_error: true)
135
- actor = @ref.instance_variable_get(:@actor)
136
- actor.should_receive(:on_reset).once.with(no_args)
137
- @ref << :poison
138
- sleep(0.1)
139
- end
140
-
141
- it 'prevents #on_reset form being called on exception when false' do
142
- @ref = shared_actor_test_class.spawn(reset_on_error: false)
143
- actor = @ref.instance_variable_get(:@actor)
144
- actor.should_not_receive(:on_reset).with(any_args)
145
- @ref << :poison
146
- sleep(0.1)
147
- end
148
-
149
- it 'defaults to true' do
150
- @ref = shared_actor_test_class.spawn
151
- actor = @ref.instance_variable_get(:@actor)
152
- actor.should_receive(:on_reset).once.with(no_args)
153
- @ref << :poison
154
- sleep(0.1)
155
- end
156
- end
157
-
158
- context 'rescue_exception' do
159
-
160
- after(:each) do
161
- @ref.shutdown if @ref
162
- end
163
-
164
- it 'rescues Exception in the actor thread when true' do
165
- @ref = shared_actor_test_class.spawn(
166
- abort_on_exception: false,
167
- rescue_exception: true
168
- )
169
-
170
- ivar = @ref.post(:poison)
171
- sleep(0.1)
172
- ivar.reason.should be_a StandardError
173
-
174
- ivar = @ref.post(:bullet)
175
- sleep(0.1)
176
- ivar.reason.should be_a Exception
177
- end
178
-
179
- it 'rescues StandardError in the actor thread when false' do
180
- @ref = shared_actor_test_class.spawn(
181
- abort_on_exception: false,
182
- rescue_exception: false
183
- )
184
-
185
- ivar = @ref.post(:poison)
186
- sleep(0.1)
187
- ivar.reason.should be_a StandardError
188
-
189
- ivar = @ref.post(:bullet)
190
- sleep(0.1)
191
- ivar.reason.should be_nil
192
- end
193
-
194
- it 'defaults to false' do
195
- @ref = shared_actor_test_class.spawn(abort_on_exception: false)
196
-
197
- ivar = @ref.post(:poison)
198
- sleep(0.1)
199
- ivar.reason.should be_a StandardError
200
-
201
- ivar = @ref.post(:bullet)
202
- sleep(0.1)
203
- ivar.reason.should be_nil
204
- end
205
- end
206
-
207
- context '#shutdown' do
208
-
209
- it 'calls #on_shutdown when shutdown' do
210
- actor = subject.instance_variable_get(:@actor)
211
- actor.should_receive(:on_shutdown).once.with(no_args)
212
- subject << :foo
213
- sleep(0.1)
214
-
215
- subject.shutdown
216
- end
217
- end
218
- end
219
- end