spec: serialize objects for MemoryStore and NilStore

to catch problems when using a "real" cache in dev/prod

Change-Id: Ic9ed6bc0420e3435d49ac0be756ba66a6f3cb0f2
Reviewed-on: https://gerrit.instructure.com/25217
Reviewed-by: Brian Palmer <brianp@instructure.com>
Tested-by: Jenkins <jenkins@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2013-10-11 12:37:18 -04:00
parent d28ab03248
commit 5302d44410
2 changed files with 49 additions and 0 deletions

View File

@ -37,6 +37,15 @@ class ActiveRecord::Base
def self.add_any_instantiation(ar_obj)
raise(ArgumentError, "need to save first") if ar_obj.new_record?
@@any_instantiation[ [ar_obj.class.base_ar_class, ar_obj.id] ] = ar_obj
# calling any_instantiation is likely to be because you're stubbing it,
# and to later be cached inadvertently from code that *thinks* it
# has a non-stubbed object. So let it dump, but not load (i.e.
# the MemoryStore and NilStore dumps that are just for testing,
# but just discard the result of dump)
def ar_obj.marshal_dump
nil
end
# no marshal_load; will raise an exception on load
ar_obj
end

View File

@ -126,6 +126,46 @@ class ActiveRecord::ConnectionAdapters::MysqlAdapter < ActiveRecord::ConnectionA
end
end
# Be sure to actually test serializing things to non-existent caches,
# but give Mocks a pass, since they won't exist in dev/prod
Mocha::Mock.class_eval do
def marshal_dump
nil
end
def marshal_load(data)
raise "Mocks aren't really serializeable!"
end
def respond_to_with_marshalling?(symbol, include_private = false)
return true if [:marshal_dump, :marshal_load].include?(symbol)
respond_to_without_marshalling?(symbol, include_private)
end
alias_method_chain :respond_to?, :marshalling
end
[ActiveSupport::Cache::MemoryStore, NilStore].each do |store|
store.class_eval do
def write_with_serialization_check(name, value, options = nil)
Marshal.dump(value)
write_without_serialization_check(name, value, options)
end
alias_method_chain :write, :serialization_check
end
end
unless CANVAS_RAILS2
NilStore.class_eval do
def fetch_with_serialization_check(name, options = {}, &block)
result = fetch_without_serialization_check(name, options, &block)
Marshal.dump(result) if result
result
end
alias_method_chain :fetch, :serialization_check
end
end
Spec::Matchers.define :encompass do |expected|
match do |actual|
if expected.is_a?(Array) && actual.is_a?(Array)