Skip to content

Commit 1d054eb

Browse files
committed
Make Agent work with ImmediateExecutor
1 parent d0e010a commit 1d054eb

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

lib/concurrent/agent.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ def try_rescue(ex) # :nodoc:
174174
# @!visibility private
175175
def work(&handler) # :nodoc:
176176
begin
177-
should_notify = false
178177
validator, value = mutex.synchronize { [@validator, @value] }
179178

180179
begin
@@ -187,18 +186,15 @@ def work(&handler) # :nodoc:
187186
exception = ex
188187
end
189188

190-
mutex.synchronize do
191-
if !exception && valid
192-
@value = result
193-
should_notify = true
194-
end
189+
mutex.lock
190+
should_notify = if !exception && valid
191+
@value = result
192+
true
193+
end
194+
stashed = @stash.shift || (@being_executed = false)
195+
mutex.unlock
195196

196-
if (stashed = @stash.shift)
197-
@executor.post { work(&stashed) }
198-
else
199-
@being_executed = false
200-
end
201-
end
197+
@executor.post { work(&stashed) } if stashed
202198

203199
if should_notify
204200
time = Time.now

spec/concurrent/agent_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ def trigger_observable(observable)
160160
subject.post
161161
sleep(0.1)
162162
end
163+
164+
it 'works with ImmediateExecutor' do
165+
agent = Agent.new(0, executor: ImmediateExecutor.new)
166+
agent.post { |old| old + 1 }
167+
agent.post { |old| old + 1 }
168+
agent.value.should eq 2
169+
end
163170
end
164171

165172
context 'fulfillment' do

0 commit comments

Comments
 (0)