diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-09-29 08:43:17 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-09-29 08:43:17 +0000 |
commit | 438db964ad1531d3c378c3e4acc25d0007cbc1cc (patch) | |
tree | 428239d889b10d66471a5c93d5fdf943e20b2032 | |
parent | 67d1e0cd3d366cd74caa24bc622442d04ef20e57 (diff) | |
download | rails-438db964ad1531d3c378c3e4acc25d0007cbc1cc.tar.gz rails-438db964ad1531d3c378c3e4acc25d0007cbc1cc.tar.bz2 rails-438db964ad1531d3c378c3e4acc25d0007cbc1cc.zip |
Testing should also use a default REQUEST_METHOD
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2408 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | actionpack/lib/action_controller/caching.rb | 97 | ||||
-rw-r--r-- | actionpack/lib/action_controller/test_process.rb | 1 | ||||
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 5 | ||||
-rw-r--r-- | actionpack/test/controller/caching_filestore.rb | 62 |
4 files changed, 94 insertions, 71 deletions
diff --git a/actionpack/lib/action_controller/caching.rb b/actionpack/lib/action_controller/caching.rb index 88529d8008..d31343dd71 100644 --- a/actionpack/lib/action_controller/caching.rb +++ b/actionpack/lib/action_controller/caching.rb @@ -57,8 +57,7 @@ module ActionController #:nodoc: # By default, the cache extension is .html, which makes it easy for the cached files to be picked up by the web server. If you want # something else, like .php or .shtml, just set Base.page_cache_extension. module Pages - def self.append_features(base) #:nodoc: - super + def self.included(base) #:nodoc: base.extend(ClassMethods) base.class_eval do @@page_cache_directory = defined?(RAILS_ROOT) ? "#{RAILS_ROOT}/public" : "" @@ -270,7 +269,7 @@ module ActionController #:nodoc: end # Called by CacheHelper#cache - def cache_erb_fragment(block, name = {}, options = {}) + def cache_erb_fragment(block, name = {}, options = nil) unless perform_caching then block.call; return end buffer = eval("_erbout", block.binding) @@ -284,33 +283,30 @@ module ActionController #:nodoc: end end - def write_fragment(name, content, options = {}) + def write_fragment(name, content, options = nil) return unless perform_caching key = fragment_cache_key(name) self.class.benchmark "Cached fragment: #{key}" do fragment_cache_store.write(key, content, options) end - content end - def read_fragment(name, options = {}) + def read_fragment(name, options = nil) return unless perform_caching - key, cache = fragment_cache_key(name), nil + key = fragment_cache_key(name) self.class.benchmark "Fragment read: #{key}" do - cache = fragment_cache_store.read(key, options) + fragment_cache_store.read(key, options) end - - cache || false end # Name can take one of three forms: # * String: This would normally take the form of a path like "pages/45/notes" # * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 } # * Regexp: Will destroy all the matched fragments, example: %r{pages/\d*/notes} - def expire_fragment(name, options = {}) + def expire_fragment(name, options = nil) return unless perform_caching key = fragment_cache_key(name) @@ -327,29 +323,58 @@ module ActionController #:nodoc: end # Deprecated -- just call expire_fragment with a regular expression - def expire_matched_fragments(matcher = /.*/, options = {}) #:nodoc: + def expire_matched_fragments(matcher = /.*/, options = nil) #:nodoc: expire_fragment(matcher, options) end - class MemoryStore #:nodoc: - def initialize - @data, @mutex = { }, Mutex.new + + class UnthreadedMemoryStore #:nodoc: + def initialize #:nodoc: + @data = {} end - def read(name, options = {}) #:nodoc: - @mutex.synchronize { @data[name] } rescue nil + def read(name, options=nil) #:nodoc: + @data[name] end - def write(name, value, options = {}) #:nodoc: - @mutex.synchronize { @data[name] = value } + def write(name, value, options=nil) #:nodoc: + @data[name] = value end - def delete(name, options = {}) #:nodoc: - @mutex.synchronize { @data.delete(name) } + def delete(name, options=nil) #:nodoc: + @data.delete(name) end - def delete_matched(matcher, options) #:nodoc: - @mutex.synchronize { @data.delete_if { |k,v| k =~ matcher } } + def delete_matched(matcher, options=nil) #:nodoc: + @data.delete_if { |k,v| k =~ matcher } + end + end + + module ThreadSafety + def read(name, options=nil) #:nodoc: + @mutex.synchronize { super } + end + + def write(name, value, options=nil) #:nodoc: + @mutex.synchronize { super } + end + + def delete(name, options=nil) #:nodoc: + @mutex.synchronize { super } + end + + def delete_matched(matcher, options=nil) #:nodoc: + @mutex.synchronize { super } + end + end + + class MemoryStore < UnthreadedMemoryStore #:nodoc: + def initialize #:nodoc: + super + if ActionController::Base.allow_concurrency + @mutex = Mutex.new + MemoryStore.send(:include, ThreadSafety) + end end end @@ -357,8 +382,9 @@ module ActionController #:nodoc: attr_reader :address def initialize(address = 'druby://localhost:9192') + super() @address = address - @data, @mutex = DRbObject.new(nil, address), Mutex.new + @data = DRbObject.new(nil, address) end end @@ -366,26 +392,27 @@ module ActionController #:nodoc: attr_reader :address def initialize(address = 'localhost') + super() @address = address - @data, @mutex = MemCache.new(address), Mutex.new + @data = MemCache.new(address) end end - class FileStore #:nodoc: + class UnthreadedFileStore #:nodoc: attr_reader :cache_path def initialize(cache_path) @cache_path = cache_path end - def write(name, value, options = {}) #:nodoc: + def write(name, value, options = nil) #:nodoc: ensure_cache_path(File.dirname(real_file_path(name))) File.open(real_file_path(name), "w+") { |f| f.write(value) } rescue => e - Base.logger.error "Couldn't create cache directory: #{name} (#{e.message})" unless Base.logger.nil? + Base.logger.error "Couldn't create cache directory: #{name} (#{e.message})" if Base.logger end - def read(name, options = {}) #:nodoc: + def read(name, options = nil) #:nodoc: IO.read(real_file_path(name)) rescue nil end @@ -427,7 +454,17 @@ module ActionController #:nodoc: end end end - end + end + + class FileStore < UnthreadedFileStore #:nodoc: + def initialize(cache_path) + super(cache_path) + if ActionController::Base.allow_concurrency + @mutex = Mutex.new + FileStore.send(:include, ThreadSafety) + end + end + end end # Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change. diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index 59604e7c34..783d21ff53 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -116,6 +116,7 @@ module ActionController #:nodoc: @request_uri = "/" self.remote_addr = "0.0.0.0" @env["SERVER_PORT"] = 80 + @env['REQUEST_METHOD'] = "GET" end end diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 732e87078f..16f9450df1 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -8,7 +8,7 @@ module ActionView # <% for post in @posts %> # Title: <%= truncate(post.title, 20) %> # <% end %> - module TextHelper + module TextHelper # The regular puts and print are outlawed in eRuby. It's recommended to use the <%= "hello" %> form instead of print "hello". # If you absolutely must use a method-based output, you can use concat. It's use like this <% concat "hello", binding %>. Notice that # it doesn't have an equal sign in front. Using <%= concat "hello" %> would result in a double hello. @@ -265,6 +265,9 @@ module ActionView end end + class << self + include ActionView::Helpers::TextHelper + end private # The cycle helpers need to store the cycles in a place that is diff --git a/actionpack/test/controller/caching_filestore.rb b/actionpack/test/controller/caching_filestore.rb index e2478e9546..389ebe02fa 100644 --- a/actionpack/test/controller/caching_filestore.rb +++ b/actionpack/test/controller/caching_filestore.rb @@ -1,16 +1,15 @@ require 'fileutils' require File.dirname(__FILE__) + '/../abstract_unit' -#generate the greatest logging class that ever lived class TestLogDevice < Logger::LogDevice attr :last_message, true def initialize - @last_message=String.new + @last_message=String.new end def write(message) - @last_message << message + @last_message << message end def clear @@ -23,14 +22,13 @@ TestLog = TestLogDevice.new RAILS_DEFAULT_LOGGER = Logger.new(TestLog) ActionController::Base.logger = RAILS_DEFAULT_LOGGER -#generate a random key to ensure the cache is always in a different location -RANDOM_KEY = rand(99999999).to_s -FILE_STORE_PATH = File.dirname(__FILE__) + '/../temp/' + RANDOM_KEY -ActionController::Base.perform_caching = true -ActionController::Base.fragment_cache_store = ActionController::Caching::Fragments::FileStore.new(FILE_STORE_PATH) - -#setup the routing information...not sure if this does anything -ActionController::Routing::Routes.connect "test", :controller => 'test', :action => 'render_to_cache' +def use_store + #generate a random key to ensure the cache is always in a different location + RANDOM_KEY = rand(99999999).to_s + FILE_STORE_PATH = File.dirname(__FILE__) + '/../temp/' + RANDOM_KEY + ActionController::Base.perform_caching = true + ActionController::Base.fragment_cache_store = :file_store, FILE_STORE_PATH +end class TestController < ActionController::Base caches_action :render_to_cache, :index @@ -45,48 +43,32 @@ class FileStoreTest < Test::Unit::TestCase def setup @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new + @controller = TestController.new @request.host = "hostname.com" end - - #To prime the cache with hostname.com/test - def test_render_to_cache_prime_a - @request.path_parameters = {:controller => "test"} - assert_fragment_cached do process_request end - end - - #To prime the cache with hostname.com/test/render_to_cache - def test_render_to_cache_prime_b - @request.path_parameters = {:action => "render_to_cache", :controller => "test"} - assert_fragment_cached do process_request end + + def teardown + FileUtils.rm_rf(FILE_STORE_PATH) end - #To hit the cache with hostname.com/test - def test_render_to_cache_zhit_a - @request.path_parameters = {:controller => "test"} - assert_fragment_hit do process_request end + def test_render_cached + assert_fragment_cached { get :render_to_cache } + assert_fragment_hit { get :render_to_cache } end - #To hit the cache with hostname.com/test/render_to_cache - def test_render_to_cache_zhit_b - @request.path_parameters = {:action => "render_to_cache", :controller => "test"} - assert_fragment_hit do process_request end - end private - def process_request - TestController.process(@request, @response) - end - - def assert_fragment_cached(&proc) - proc.call + def assert_fragment_cached + yield assert(TestLog.last_message.include?("Cached fragment:"), "--ERROR-- FileStore write failed ----") assert(!TestLog.last_message.include?("Couldn't create cache directory:"), "--ERROR-- FileStore create directory failed ----") TestLog.clear end - def assert_fragment_hit(&proc) - proc.call - assert(TestLog.last_message.include?( "Fragment hit:"), "--ERROR-- Fragment not found in FileStore ----") + def assert_fragment_hit + yield + assert(TestLog.last_message.include?("Fragment read:"), "--ERROR-- Fragment not found in FileStore ----") + assert(!TestLog.last_message.include?("Cached fragment:"), "--ERROR-- Did cache ----") TestLog.clear end end
\ No newline at end of file |