Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/concurrent/delay.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ def value(timeout = nil)
# this function has been optimized for performance and
# should not be modified without running new benchmarks
synchronize do
execute = @computing = true unless @computing
execute = @evaluation_started = true unless @evaluation_started
if execute
begin
set_state(true, @task.call, nil)
rescue => ex
set_state(false, nil, ex)
end
elsif incomplete?
raise IllegalOperationError, 'Recursive call to #value during evaluation of the Delay'
end
end
if @do_nothing_on_deref
Expand Down Expand Up @@ -144,7 +146,7 @@ def wait(timeout = nil)
def reconfigure(&block)
synchronize do
raise ArgumentError.new('no block given') unless block_given?
unless @computing
unless @evaluation_started
@task = block
true
else
Expand All @@ -160,9 +162,9 @@ def ns_initialize(opts, &block)
set_deref_options(opts)
@executor = opts[:executor]

@task = block
@state = :pending
@computing = false
@task = block
@state = :pending
@evaluation_started = false
end

private
Expand All @@ -173,7 +175,7 @@ def execute_task_once # :nodoc:
# should not be modified without running new benchmarks
execute = task = nil
synchronize do
execute = @computing = true unless @computing
execute = @evaluation_started = true unless @evaluation_started
task = @task
end

Expand Down
12 changes: 12 additions & 0 deletions spec/concurrent/delay_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ def dereferenceable_subject(value, opts = {})
delay = Delay.new(&task)
5.times{ delay.value }
end

it 'raises when called recursively' do
delay = Delay.new { delay.value }
expect { delay.value! }.to raise_error(IllegalOperationError)
expect(delay.reason).to be_a_kind_of(IllegalOperationError)
end

it 'can be called twice' do
delay = Delay.new { 10 }
expect(delay.value).to eq 10
expect(delay.value).to eq 10
end
end
end
end