Skip to content

Commit

Permalink
Merge pull request #18 from nedap/master
Browse files Browse the repository at this point in the history
Future should not marschal when it contains exceptions
  • Loading branch information
josepjaume committed Feb 11, 2014
2 parents 6c086fd + ea4d8d9 commit bf04b49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/futuroscope/future.rb
Expand Up @@ -46,18 +46,15 @@ def run_future
#
# Returns the Future's block execution result.
def __getobj__
resolved = resolved_future_value

raise resolved[:exception] if resolved[:exception]
resolved[:value]
resolved_future_value_or_raise[:value]
end

def __setobj__ obj
@resolved_future = { value: obj }
end

def marshal_dump
resolved_future_value
resolved_future_value_or_raise
end

def marshal_load value
Expand All @@ -70,6 +67,13 @@ def marshal_load value

private

def resolved_future_value_or_raise
resolved = resolved_future_value

raise resolved[:exception] if resolved[:exception]
resolved
end

def resolved_future_value
@resolved_future || @mutex.synchronize do
@resolved_future ||= @queue.pop
Expand Down
8 changes: 8 additions & 0 deletions spec/futuroscope/future_spec.rb
Expand Up @@ -59,6 +59,14 @@ module Futuroscope
expect(Marshal.load(dumped).future_value).to eq(object)
end

it "re-raises captured exception when trying to marshal" do
future = Future.new{ raise Exception }

expect(lambda{
Marshal.dump(future)
}).to raise_error(Exception)
end

it "correctly duplicates a future object" do
object = [1, 2, 3]
future = Future.new { object }
Expand Down

0 comments on commit bf04b49

Please sign in to comment.