Skip to content

Commit

Permalink
Add test and make it pass
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin pauchet committed Jan 20, 2016
1 parent c029a53 commit 2e8a50b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/request_locals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class << self
end

# Create cache using reentrant mutex. That's fetch call to be nested.
class Cache < Concurrent::Collection::MriMapBackend
class RequestCache < Concurrent::Map
def initialize(options = nil)
super(options)
@write_lock = Monitor.new
Expand All @@ -39,7 +39,7 @@ def initialize(options = nil)
def_delegators :store, :[], :[]=, :delete, :empty?

def initialize
@cache = Cache.new
@cache = RequestCache.new
end

# Public: Removes all the request-local variables.
Expand All @@ -53,7 +53,7 @@ def clear!
#
# Returns nothing.
def clear_all!
@cache = Cache.new
@cache = RequestCache.new
end

# Public: Checks if a value was stored for the given key.
Expand Down Expand Up @@ -92,6 +92,6 @@ def current_request_id
# Internal: Returns a new empty structure where the request-local variables
# will be stored.
def new_store
Cache.new
RequestCache.new
end
end
5 changes: 5 additions & 0 deletions test/request_locals_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ def test_clear_per_request
assert_nil global_store[:different_id]
end

def test_nested_fetch
RequestLocals.clear!
assert_equal 2, RequestLocals.store.fetch(:bar) { 1 + RequestLocals.fetch(:foo){ 1 } }
assert_equal 2, RequestLocals.store.fetch(:bar) { 1 + RequestLocals.fetch(:foo){ 1 } }
end
private

def global_store
Expand Down

0 comments on commit 2e8a50b

Please sign in to comment.