Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ClimateControl.modify re-entrant #24

Merged
merged 1 commit into from
Mar 31, 2017
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
17 changes: 16 additions & 1 deletion lib/climate_control/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@ class Environment

def initialize
@semaphore = Mutex.new
@owner = nil
end

def_delegators :env, :[]=, :to_hash, :[], :delete
def_delegator :@semaphore, :synchronize

def synchronize
if @owner == Thread.current
return yield if block_given?
end

@semaphore.synchronize do
begin
@owner = Thread.current
yield if block_given?
ensure
@owner = nil
end
end
end

private

Expand Down
13 changes: 13 additions & 0 deletions spec/acceptance/climate_control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@
expect(ENV["BAZ"]).to be_nil
end

it "is re-entrant" do
ret = with_modified_env(FOO: "foo") do
with_modified_env(BAR: "bar") do
"bar"
end
end

expect(ret).to eq("bar")

expect(ENV["FOO"]).to be_nil
expect(ENV["BAR"]).to be_nil
end

it "raises when the value cannot be assigned properly" do
Thing = Class.new
message = generate_type_error_for_object(Thing.new)
Expand Down