Permalink
Browse files

Testing should also use a default REQUEST_METHOD

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2408 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 67d1e0c commit 438db964ad1531d3c378c3e4acc25d0007cbc1cc @dhh dhh committed Sep 29, 2005
@@ -57,8 +57,7 @@ def self.append_features(base) #: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 @@ def fragment_cache_key(name)
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 @@ def cache_erb_fragment(block, name = {}, options = {})
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,65 +323,96 @@ def expire_fragment(name, options = {})
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
class DRbStore < MemoryStore #: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
class MemCacheStore < MemoryStore #: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 @@ def search_dir(dir, &callback)
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.
@@ -116,6 +116,7 @@ def initialize_default_values
@request_uri = "/"
self.remote_addr = "0.0.0.0"
@env["SERVER_PORT"] = 80
+ @env['REQUEST_METHOD'] = "GET"
end
end
@@ -8,7 +8,7 @@ module Helpers #:nodoc:
# <% 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 @@ def to_s
end
end
+ class << self
+ include ActionView::Helpers::TextHelper
+ end
private
# The cycle helpers need to store the cycles in a place that is
@@ -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 @@ def clear
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

0 comments on commit 438db96

Please sign in to comment.