2017-07-09 20:06:36 +08:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 21:39:13 +08:00
|
|
|
|
2018-09-30 08:50:43 +08:00
|
|
|
require_relative "../abstract_unit"
|
2017-06-10 16:07:40 +08:00
|
|
|
require "active_support/cache"
|
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
module Cache
|
|
|
|
module Strategy
|
|
|
|
module LocalCache
|
|
|
|
class MiddlewareTest < ActiveSupport::TestCase
|
|
|
|
def test_local_cache_cleared_on_close
|
|
|
|
key = "super awesome key"
|
|
|
|
assert_nil LocalCacheRegistry.cache_for key
|
|
|
|
middleware = Middleware.new("<3", key).new(->(env) {
|
|
|
|
assert LocalCacheRegistry.cache_for(key), "should have a cache"
|
|
|
|
[200, {}, []]
|
|
|
|
})
|
|
|
|
_, _, body = middleware.call({})
|
|
|
|
assert LocalCacheRegistry.cache_for(key), "should still have a cache"
|
2018-09-26 01:18:20 +08:00
|
|
|
body.each { }
|
2017-06-10 16:07:40 +08:00
|
|
|
assert LocalCacheRegistry.cache_for(key), "should still have a cache"
|
|
|
|
body.close
|
|
|
|
assert_nil LocalCacheRegistry.cache_for(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_local_cache_cleared_and_response_should_be_present_on_invalid_parameters_error
|
|
|
|
key = "super awesome key"
|
|
|
|
assert_nil LocalCacheRegistry.cache_for key
|
|
|
|
middleware = Middleware.new("<3", key).new(->(env) {
|
|
|
|
assert LocalCacheRegistry.cache_for(key), "should have a cache"
|
|
|
|
raise Rack::Utils::InvalidParameterError
|
|
|
|
})
|
|
|
|
response = middleware.call({})
|
|
|
|
assert response, "response should exist"
|
|
|
|
assert_nil LocalCacheRegistry.cache_for(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_local_cache_cleared_on_exception
|
|
|
|
key = "super awesome key"
|
|
|
|
assert_nil LocalCacheRegistry.cache_for key
|
|
|
|
middleware = Middleware.new("<3", key).new(->(env) {
|
|
|
|
assert LocalCacheRegistry.cache_for(key), "should have a cache"
|
|
|
|
raise
|
|
|
|
})
|
|
|
|
assert_raises(RuntimeError) { middleware.call({}) }
|
|
|
|
assert_nil LocalCacheRegistry.cache_for(key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_local_cache_cleared_on_throw
|
|
|
|
key = "super awesome key"
|
|
|
|
assert_nil LocalCacheRegistry.cache_for key
|
|
|
|
middleware = Middleware.new("<3", key).new(->(env) {
|
|
|
|
assert LocalCacheRegistry.cache_for(key), "should have a cache"
|
|
|
|
throw :warden
|
|
|
|
})
|
|
|
|
assert_throws(:warden) { middleware.call({}) }
|
|
|
|
assert_nil LocalCacheRegistry.cache_for(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|