2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/topic'
|
|
|
|
require 'models/task'
|
|
|
|
require 'models/category'
|
|
|
|
require 'models/post'
|
2012-01-16 06:36:41 -05:00
|
|
|
require 'rack'
|
2007-09-08 00:31:26 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class QueryCacheTest < ActiveRecord::TestCase
|
2008-01-17 20:55:11 -05:00
|
|
|
fixtures :tasks, :topics, :categories, :posts, :categories_posts
|
2007-09-17 02:15:58 -04:00
|
|
|
|
2014-01-15 08:08:45 -05:00
|
|
|
teardown do
|
2010-03-22 12:53:07 -04:00
|
|
|
Task.connection.clear_query_cache
|
2011-05-02 14:30:49 -04:00
|
|
|
ActiveRecord::Base.connection.disable_query_cache!
|
2010-03-22 12:53:07 -04:00
|
|
|
end
|
|
|
|
|
2011-08-28 17:15:51 -04:00
|
|
|
def test_exceptional_middleware_clears_and_disables_cache_on_error
|
|
|
|
assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache off'
|
|
|
|
|
|
|
|
mw = ActiveRecord::QueryCache.new lambda { |env|
|
|
|
|
Task.find 1
|
|
|
|
Task.find 1
|
|
|
|
assert_equal 1, ActiveRecord::Base.connection.query_cache.length
|
|
|
|
raise "lol borked"
|
|
|
|
}
|
|
|
|
assert_raises(RuntimeError) { mw.call({}) }
|
|
|
|
|
|
|
|
assert_equal 0, ActiveRecord::Base.connection.query_cache.length
|
|
|
|
assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache off'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exceptional_middleware_leaves_enabled_cache_alone
|
|
|
|
ActiveRecord::Base.connection.enable_query_cache!
|
|
|
|
|
|
|
|
mw = ActiveRecord::QueryCache.new lambda { |env|
|
|
|
|
raise "lol borked"
|
|
|
|
}
|
|
|
|
assert_raises(RuntimeError) { mw.call({}) }
|
|
|
|
|
2011-08-28 19:31:04 -04:00
|
|
|
assert ActiveRecord::Base.connection.query_cache_enabled, 'cache on'
|
2011-08-28 17:15:51 -04:00
|
|
|
end
|
|
|
|
|
2012-07-22 11:46:00 -04:00
|
|
|
def test_exceptional_middleware_assigns_original_connection_id_on_error
|
|
|
|
connection_id = ActiveRecord::Base.connection_id
|
|
|
|
|
|
|
|
mw = ActiveRecord::QueryCache.new lambda { |env|
|
|
|
|
ActiveRecord::Base.connection_id = self.object_id
|
|
|
|
raise "lol borked"
|
|
|
|
}
|
|
|
|
assert_raises(RuntimeError) { mw.call({}) }
|
|
|
|
|
2012-10-12 03:56:39 -04:00
|
|
|
assert_equal connection_id, ActiveRecord::Base.connection_id
|
2012-07-22 11:46:00 -04:00
|
|
|
end
|
|
|
|
|
2011-05-02 14:13:49 -04:00
|
|
|
def test_middleware_delegates
|
|
|
|
called = false
|
|
|
|
mw = ActiveRecord::QueryCache.new lambda { |env|
|
|
|
|
called = true
|
2012-01-16 06:36:41 -05:00
|
|
|
[200, {}, nil]
|
2011-05-02 14:13:49 -04:00
|
|
|
}
|
|
|
|
mw.call({})
|
|
|
|
assert called, 'middleware should delegate'
|
|
|
|
end
|
|
|
|
|
2011-05-02 14:17:31 -04:00
|
|
|
def test_middleware_caches
|
2011-05-02 14:13:49 -04:00
|
|
|
mw = ActiveRecord::QueryCache.new lambda { |env|
|
|
|
|
Task.find 1
|
|
|
|
Task.find 1
|
|
|
|
assert_equal 1, ActiveRecord::Base.connection.query_cache.length
|
2012-01-16 06:36:41 -05:00
|
|
|
[200, {}, nil]
|
2011-05-02 14:13:49 -04:00
|
|
|
}
|
|
|
|
mw.call({})
|
|
|
|
end
|
|
|
|
|
2011-05-02 14:17:31 -04:00
|
|
|
def test_cache_enabled_during_call
|
|
|
|
assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache off'
|
|
|
|
|
|
|
|
mw = ActiveRecord::QueryCache.new lambda { |env|
|
|
|
|
assert ActiveRecord::Base.connection.query_cache_enabled, 'cache on'
|
2012-01-16 06:36:41 -05:00
|
|
|
[200, {}, nil]
|
2011-05-02 14:17:31 -04:00
|
|
|
}
|
|
|
|
mw.call({})
|
|
|
|
end
|
|
|
|
|
2011-05-02 14:30:49 -04:00
|
|
|
def test_cache_on_during_body_write
|
|
|
|
streaming = Class.new do
|
|
|
|
def each
|
|
|
|
yield ActiveRecord::Base.connection.query_cache_enabled
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
mw = ActiveRecord::QueryCache.new lambda { |env|
|
|
|
|
[200, {}, streaming.new]
|
|
|
|
}
|
|
|
|
body = mw.call({}).last
|
|
|
|
body.each { |x| assert x, 'cache should be on' }
|
|
|
|
body.close
|
|
|
|
assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache disabled'
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_off_after_close
|
2012-01-16 06:36:41 -05:00
|
|
|
mw = ActiveRecord::QueryCache.new lambda { |env| [200, {}, nil] }
|
2011-05-02 14:30:49 -04:00
|
|
|
body = mw.call({}).last
|
|
|
|
|
|
|
|
assert ActiveRecord::Base.connection.query_cache_enabled, 'cache enabled'
|
|
|
|
body.close
|
|
|
|
assert !ActiveRecord::Base.connection.query_cache_enabled, 'cache disabled'
|
|
|
|
end
|
|
|
|
|
2011-05-04 18:24:26 -04:00
|
|
|
def test_cache_clear_after_close
|
|
|
|
mw = ActiveRecord::QueryCache.new lambda { |env|
|
2012-04-26 13:32:55 -04:00
|
|
|
Post.first
|
2012-01-16 06:36:41 -05:00
|
|
|
[200, {}, nil]
|
2011-05-04 18:24:26 -04:00
|
|
|
}
|
|
|
|
body = mw.call({}).last
|
|
|
|
|
|
|
|
assert !ActiveRecord::Base.connection.query_cache.empty?, 'cache not empty'
|
|
|
|
body.close
|
|
|
|
assert ActiveRecord::Base.connection.query_cache.empty?, 'cache should be empty'
|
|
|
|
end
|
|
|
|
|
2014-03-13 13:05:10 -04:00
|
|
|
def test_cache_passing_a_relation
|
|
|
|
post = Post.first
|
|
|
|
Post.cache do
|
|
|
|
query = post.categories.select(:post_id)
|
|
|
|
assert Post.connection.select_all(query).is_a?(ActiveRecord::Result)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-06 16:16:07 -05:00
|
|
|
def test_find_queries
|
2012-03-01 22:10:06 -05:00
|
|
|
assert_queries(2) { Task.find(1); Task.find(1) }
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_queries_with_cache
|
|
|
|
Task.cache do
|
2007-09-17 02:15:58 -04:00
|
|
|
assert_queries(1) { Task.find(1); Task.find(1) }
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
end
|
2007-09-02 19:52:58 -04:00
|
|
|
|
2010-10-27 17:33:02 -04:00
|
|
|
def test_find_queries_with_cache_multi_record
|
2010-10-27 17:05:40 -04:00
|
|
|
Task.cache do
|
|
|
|
assert_queries(2) { Task.find(1); Task.find(1); Task.find(2) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-28 15:42:34 -04:00
|
|
|
def test_find_queries_with_multi_cache_blocks
|
|
|
|
Task.cache do
|
|
|
|
Task.cache do
|
|
|
|
assert_queries(2) { Task.find(1); Task.find(2) }
|
|
|
|
end
|
|
|
|
assert_queries(0) { Task.find(1); Task.find(1); Task.find(2) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-02 19:52:58 -04:00
|
|
|
def test_count_queries_with_cache
|
|
|
|
Task.cache do
|
2007-09-17 02:15:58 -04:00
|
|
|
assert_queries(1) { Task.count; Task.count }
|
2007-09-02 19:52:58 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-07-24 23:48:30 -04:00
|
|
|
def test_query_cache_dups_results_correctly
|
|
|
|
Task.cache do
|
|
|
|
now = Time.now.utc
|
|
|
|
task = Task.find 1
|
|
|
|
assert_not_equal now, task.starting
|
|
|
|
task.starting = now
|
|
|
|
task.reload
|
|
|
|
assert_not_equal now, task.starting
|
|
|
|
end
|
|
|
|
end
|
2007-02-06 16:16:07 -05:00
|
|
|
|
2007-09-17 02:15:58 -04:00
|
|
|
def test_cache_is_flat
|
2007-02-06 16:16:07 -05:00
|
|
|
Task.cache do
|
2007-07-24 23:48:30 -04:00
|
|
|
Topic.columns # don't count this query
|
2007-09-17 02:15:58 -04:00
|
|
|
assert_queries(1) { Topic.find(1); Topic.find(1); }
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
2007-09-17 02:15:58 -04:00
|
|
|
|
2007-02-20 18:42:04 -05:00
|
|
|
ActiveRecord::Base.cache do
|
2007-09-17 02:15:58 -04:00
|
|
|
assert_queries(1) { Task.find(1); Task.find(1) }
|
2007-02-20 18:42:04 -05:00
|
|
|
end
|
|
|
|
end
|
2007-09-17 02:15:58 -04:00
|
|
|
|
|
|
|
def test_cache_does_not_wrap_string_results_in_arrays
|
|
|
|
Task.cache do
|
2009-03-22 18:19:27 -04:00
|
|
|
# Oracle adapter returns count() as Fixnum or Float
|
|
|
|
if current_adapter?(:OracleAdapter)
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_kind_of Numeric, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks")
|
2015-03-21 16:51:55 -04:00
|
|
|
elsif current_adapter?(:SQLite3Adapter, :Mysql2Adapter, :PostgreSQLAdapter)
|
2010-01-26 16:00:19 -05:00
|
|
|
# Future versions of the sqlite3 adapter will return numeric
|
|
|
|
assert_instance_of Fixnum,
|
|
|
|
Task.connection.select_value("SELECT count(*) AS count_all FROM tasks")
|
2009-03-22 18:19:27 -04:00
|
|
|
else
|
|
|
|
assert_instance_of String, Task.connection.select_value("SELECT count(*) AS count_all FROM tasks")
|
|
|
|
end
|
2007-02-21 16:54:41 -05:00
|
|
|
end
|
|
|
|
end
|
2012-07-06 07:03:49 -04:00
|
|
|
|
|
|
|
def test_cache_is_ignored_for_locked_relations
|
|
|
|
task = Task.find 1
|
|
|
|
|
|
|
|
Task.cache do
|
|
|
|
assert_queries(2) { task.lock!; task.lock! }
|
|
|
|
end
|
|
|
|
end
|
2012-10-30 13:21:52 -04:00
|
|
|
|
|
|
|
def test_cache_is_available_when_connection_is_connected
|
|
|
|
conf = ActiveRecord::Base.configurations
|
|
|
|
|
|
|
|
ActiveRecord::Base.configurations = {}
|
|
|
|
Task.cache do
|
|
|
|
assert_queries(1) { Task.find(1); Task.find(1) }
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.configurations = conf
|
|
|
|
end
|
2014-11-27 14:45:02 -05:00
|
|
|
|
|
|
|
def test_query_cache_doesnt_leak_cached_results_of_rolled_back_queries
|
|
|
|
ActiveRecord::Base.connection.enable_query_cache!
|
|
|
|
post = Post.first
|
|
|
|
|
|
|
|
Post.transaction do
|
|
|
|
post.update_attributes(title: 'rollback')
|
|
|
|
assert_equal 1, Post.where(title: 'rollback').to_a.count
|
|
|
|
raise ActiveRecord::Rollback
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 0, Post.where(title: 'rollback').to_a.count
|
|
|
|
|
|
|
|
ActiveRecord::Base.connection.uncached do
|
|
|
|
assert_equal 0, Post.where(title: 'rollback').to_a.count
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
Post.transaction do
|
|
|
|
post.update_attributes(title: 'rollback')
|
|
|
|
assert_equal 1, Post.where(title: 'rollback').to_a.count
|
|
|
|
raise 'broken'
|
|
|
|
end
|
2015-03-06 06:13:40 -05:00
|
|
|
rescue Exception
|
2014-11-27 14:45:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 0, Post.where(title: 'rollback').to_a.count
|
|
|
|
|
|
|
|
ActiveRecord::Base.connection.uncached do
|
|
|
|
assert_equal 0, Post.where(title: 'rollback').to_a.count
|
|
|
|
end
|
|
|
|
end
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class QueryCacheExpiryTest < ActiveRecord::TestCase
|
2008-07-31 16:59:53 -04:00
|
|
|
fixtures :tasks, :posts, :categories, :categories_posts
|
2007-02-06 16:16:07 -05:00
|
|
|
|
2011-10-27 11:25:42 -04:00
|
|
|
def test_cache_gets_cleared_after_migration
|
|
|
|
# warm the cache
|
|
|
|
Post.find(1)
|
|
|
|
|
|
|
|
# change the column definition
|
2014-01-15 08:08:45 -05:00
|
|
|
Post.connection.change_column :posts, :title, :string, limit: 80
|
2011-10-27 11:25:42 -04:00
|
|
|
assert_nothing_raised { Post.find(1) }
|
|
|
|
|
|
|
|
# restore the old definition
|
|
|
|
Post.connection.change_column :posts, :title, :string
|
|
|
|
end
|
|
|
|
|
2007-02-06 16:16:07 -05:00
|
|
|
def test_find
|
2015-08-15 15:35:42 -04:00
|
|
|
assert_called(Task.connection, :clear_query_cache) do
|
|
|
|
assert !Task.connection.query_cache_enabled
|
|
|
|
Task.cache do
|
|
|
|
assert Task.connection.query_cache_enabled
|
|
|
|
Task.find(1)
|
2007-09-17 02:15:58 -04:00
|
|
|
|
2015-08-15 15:35:42 -04:00
|
|
|
Task.uncached do
|
|
|
|
assert !Task.connection.query_cache_enabled
|
|
|
|
Task.find(1)
|
|
|
|
end
|
2007-09-17 02:15:58 -04:00
|
|
|
|
2015-08-15 15:35:42 -04:00
|
|
|
assert Task.connection.query_cache_enabled
|
2007-09-17 02:15:58 -04:00
|
|
|
end
|
2015-08-15 15:35:42 -04:00
|
|
|
assert !Task.connection.query_cache_enabled
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-17 02:15:58 -04:00
|
|
|
def test_update
|
2015-08-15 15:35:42 -04:00
|
|
|
assert_called(Task.connection, :clear_query_cache, times: 2) do
|
|
|
|
Task.cache do
|
|
|
|
task = Task.find(1)
|
|
|
|
task.starting = Time.now.utc
|
|
|
|
task.save!
|
|
|
|
end
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_destroy
|
2015-08-15 15:35:42 -04:00
|
|
|
assert_called(Task.connection, :clear_query_cache, times: 2) do
|
|
|
|
Task.cache do
|
|
|
|
Task.find(1).destroy
|
|
|
|
end
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-17 02:15:58 -04:00
|
|
|
def test_insert
|
2015-08-15 15:35:42 -04:00
|
|
|
assert_called(ActiveRecord::Base.connection, :clear_query_cache, times: 2) do
|
|
|
|
Task.cache do
|
|
|
|
Task.create!
|
|
|
|
end
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|
|
|
|
end
|
2008-01-17 20:55:11 -05:00
|
|
|
|
|
|
|
def test_cache_is_expired_by_habtm_update
|
2015-08-15 15:35:42 -04:00
|
|
|
assert_called(ActiveRecord::Base.connection, :clear_query_cache, times: 2) do
|
|
|
|
ActiveRecord::Base.cache do
|
|
|
|
c = Category.first
|
|
|
|
p = Post.first
|
|
|
|
p.categories << c
|
|
|
|
end
|
2008-01-17 20:55:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_cache_is_expired_by_habtm_delete
|
2015-08-15 15:35:42 -04:00
|
|
|
assert_called(ActiveRecord::Base.connection, :clear_query_cache, times: 2) do
|
|
|
|
ActiveRecord::Base.cache do
|
|
|
|
p = Post.find(1)
|
|
|
|
assert p.categories.any?
|
|
|
|
p.categories.delete_all
|
|
|
|
end
|
2008-01-17 20:55:11 -05:00
|
|
|
end
|
|
|
|
end
|
2007-02-06 16:16:07 -05:00
|
|
|
end
|