module CacheStoreBehavior

Tests the base functionality that should be identical across all cache stores.

Public Instance Methods

test_array_as_cache_key() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 167
def test_array_as_cache_key
  @cache.write([:fu, "foo"], "bar")
  assert_equal "bar", @cache.read("fu/foo")
end
test_cache_hit_instrumentation() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 300
def test_cache_hit_instrumentation
  key = "test_key"
  @events = []
  ActiveSupport::Notifications.subscribe "cache_read.active_support" do |*args|
    @events << ActiveSupport::Notifications::Event.new(*args)
  end
  assert @cache.write(key, "1", raw: true)
  assert @cache.fetch(key) {}
  assert_equal 1, @events.length
  assert_equal "cache_read.active_support", @events[0].name
  assert_equal :fetch, @events[0].payload[:super_operation]
  assert @events[0].payload[:hit]
ensure
  ActiveSupport::Notifications.unsubscribe "cache_read.active_support"
end
test_cache_key() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 149
def test_cache_key
  obj = Object.new
  def obj.cache_key
    :foo
  end
  @cache.write(obj, "bar")
  assert_equal "bar", @cache.read("foo")
end
test_cache_miss_instrumentation() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 316
def test_cache_miss_instrumentation
  @events = []
  ActiveSupport::Notifications.subscribe(/^cache_(.*)\.active_support$/) do |*args|
    @events << ActiveSupport::Notifications::Event.new(*args)
  end
  assert_not @cache.fetch("bad_key") {}
  assert_equal 3, @events.length
  assert_equal "cache_read.active_support", @events[0].name
  assert_equal "cache_generate.active_support", @events[1].name
  assert_equal "cache_write.active_support", @events[2].name
  assert_equal :fetch, @events[0].payload[:super_operation]
  assert_not @events[0].payload[:hit]
ensure
  ActiveSupport::Notifications.unsubscribe "cache_read.active_support"
end
test_crazy_key_characters() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 278
def test_crazy_key_characters
  crazy_key = "#/:*(<+=> )&$%@?;'\"\'`~-"
  assert @cache.write(crazy_key, "1", raw: true)
  assert_equal "1", @cache.read(crazy_key)
  assert_equal "1", @cache.fetch(crazy_key)
  assert @cache.delete(crazy_key)
  assert_equal "2", @cache.fetch(crazy_key, raw: true) { "2" }
  assert_equal 3, @cache.increment(crazy_key)
  assert_equal 2, @cache.decrement(crazy_key)
end
test_delete() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 193
def test_delete
  @cache.write("foo", "bar")
  assert @cache.exist?("foo")
  assert @cache.delete("foo")
  assert !@cache.exist?("foo")
end
test_exist() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 182
def test_exist
  @cache.write("foo", "bar")
  assert_equal true, @cache.exist?("foo")
  assert_equal false, @cache.exist?("bar")
end
test_expires_in() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 206
def test_expires_in
  time = Time.local(2008, 4, 24)

  Time.stub(:now, time) do
    @cache.write("foo", "bar")
    assert_equal "bar", @cache.read("foo")
  end

  Time.stub(:now, time + 30) do
    assert_equal "bar", @cache.read("foo")
  end

  Time.stub(:now, time + 61) do
    assert_nil @cache.read("foo")
  end
end
test_fetch_multi() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 106
def test_fetch_multi
  @cache.write("foo", "bar")
  @cache.write("fud", "biz")

  values = @cache.fetch_multi("foo", "fu", "fud") { |value| value * 2 }

  assert_equal({ "foo" => "bar", "fu" => "fufu", "fud" => "biz" }, values)
  assert_equal("fufu", @cache.read("fu"))
end
test_fetch_multi_without_block() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 128
def test_fetch_multi_without_block
  assert_raises(ArgumentError) do
    @cache.fetch_multi("foo")
  end
end
test_fetch_with_cache_miss() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 23
def test_fetch_with_cache_miss
  assert_called_with(@cache, :write, ["foo", "baz", @cache.options]) do
    assert_equal "baz", @cache.fetch("foo") { "baz" }
  end
end
test_fetch_with_cache_miss_passes_key_to_block() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 29
def test_fetch_with_cache_miss_passes_key_to_block
  cache_miss = false
  assert_equal 3, @cache.fetch("foo") { |key| cache_miss = true; key.length }
  assert cache_miss

  cache_miss = false
  assert_equal 3, @cache.fetch("foo") { |key| cache_miss = true; key.length }
  assert !cache_miss
end
test_fetch_with_cached_nil() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 48
def test_fetch_with_cached_nil
  @cache.write("foo", nil)
  assert_not_called(@cache, :write) do
    assert_nil @cache.fetch("foo") { "baz" }
  end
end
test_fetch_with_forced_cache_miss() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 39
def test_fetch_with_forced_cache_miss
  @cache.write("foo", "bar")
  assert_not_called(@cache, :read) do
    assert_called_with(@cache, :write, ["foo", "bar", @cache.options.merge(force: true)]) do
      @cache.fetch("foo", force: true) { "bar" }
    end
  end
end
test_fetch_with_forced_cache_miss_with_block() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 55
def test_fetch_with_forced_cache_miss_with_block
  @cache.write("foo", "bar")
  assert_equal "foo_bar", @cache.fetch("foo", force: true) { "foo_bar" }
end
test_fetch_with_forced_cache_miss_without_block() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 60
def test_fetch_with_forced_cache_miss_without_block
  @cache.write("foo", "bar")
  assert_raises(ArgumentError) do
    @cache.fetch("foo", force: true)
  end

  assert_equal "bar", @cache.read("foo")
end
test_fetch_without_cache_miss() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 16
def test_fetch_without_cache_miss
  @cache.write("foo", "bar")
  assert_not_called(@cache, :write) do
    assert_equal "bar", @cache.fetch("foo") { "baz" }
  end
end
test_hash_as_cache_key() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 172
def test_hash_as_cache_key
  @cache.write({ foo: 1, fu: 2 }, "bar")
  assert_equal "bar", @cache.read("foo=1/fu=2")
end
test_keys_are_case_sensitive() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 177
def test_keys_are_case_sensitive
  @cache.write("foo", "bar")
  assert_nil @cache.read("FOO")
end
test_multi_with_objects() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 116
def test_multi_with_objects
  cache_struct = Struct.new(:cache_key, :title)
  foo = cache_struct.new("foo", "FOO!")
  bar = cache_struct.new("bar")

  @cache.write("bar", "BAM!")

  values = @cache.fetch_multi(foo, bar) { |object| object.title }

  assert_equal({ foo => "FOO!", bar => "BAM!" }, values)
end
test_nil_exist() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 188
def test_nil_exist
  @cache.write("foo", nil)
  assert @cache.exist?("foo")
end
test_original_store_objects_should_not_be_immutable() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 200
def test_original_store_objects_should_not_be_immutable
  bar = "bar".dup
  @cache.write("foo", bar)
  assert_nothing_raised { bar.gsub!(/.*/, "baz") }
end
test_param_as_cache_key() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 158
def test_param_as_cache_key
  obj = Object.new
  def obj.to_param
    "foo"
  end
  @cache.write(obj, "bar")
  assert_equal "bar", @cache.read("foo")
end
test_race_condition_protection() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 266
def test_race_condition_protection
  time = Time.now
  @cache.write("foo", "bar", expires_in: 60)
  Time.stub(:now, time + 61) do
    result = @cache.fetch("foo", race_condition_ttl: 10) do
      assert_equal "bar", @cache.read("foo")
      "baz"
    end
    assert_equal "baz", result
  end
end
test_race_condition_protection_is_limited() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 236
def test_race_condition_protection_is_limited
  time = Time.now
  @cache.write("foo", "bar", expires_in: 60)
  Time.stub(:now, time + 71) do
    result = @cache.fetch("foo", race_condition_ttl: 10) do
      assert_nil @cache.read("foo")
      "baz"
    end
    assert_equal "baz", result
  end
end
test_race_condition_protection_is_safe() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 248
def test_race_condition_protection_is_safe
  time = Time.now
  @cache.write("foo", "bar", expires_in: 60)
  Time.stub(:now, time + 61) do
    begin
      @cache.fetch("foo", race_condition_ttl: 10) do
        assert_equal "bar", @cache.read("foo")
        raise ArgumentError.new
      end
    rescue ArgumentError
    end
    assert_equal "bar", @cache.read("foo")
  end
  Time.stub(:now, time + 91) do
    assert_nil @cache.read("foo")
  end
end
test_race_condition_protection_skipped_if_not_defined() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 223
def test_race_condition_protection_skipped_if_not_defined
  @cache.write("foo", "bar")
  time = @cache.send(:read_entry, @cache.send(:normalize_key, "foo", {}), {}).expires_at

  Time.stub(:now, Time.at(time)) do
    result = @cache.fetch("foo") do
      assert_nil @cache.read("foo")
      "baz"
    end
    assert_equal "baz", result
  end
end
test_read_and_write_compressed_large_data() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 139
def test_read_and_write_compressed_large_data
  @cache.write("foo", "bar", compress: true, compress_threshold: 2)
  assert_equal "bar", @cache.read("foo")
end
test_read_and_write_compressed_nil() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 144
def test_read_and_write_compressed_nil
  @cache.write("foo", nil, compress: true)
  assert_nil @cache.read("foo")
end
test_read_and_write_compressed_small_data() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 134
def test_read_and_write_compressed_small_data
  @cache.write("foo", "bar", compress: true)
  assert_equal "bar", @cache.read("foo")
end
test_read_multi() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 89
def test_read_multi
  @cache.write("foo", "bar")
  @cache.write("fu", "baz")
  @cache.write("fud", "biz")
  assert_equal({ "foo" => "bar", "fu" => "baz" }, @cache.read_multi("foo", "fu"))
end
test_read_multi_with_expires() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 96
def test_read_multi_with_expires
  time = Time.now
  @cache.write("foo", "bar", expires_in: 10)
  @cache.write("fu", "baz")
  @cache.write("fud", "biz")
  Time.stub(:now, time + 11) do
    assert_equal({ "fu" => "baz" }, @cache.read_multi("foo", "fu"))
  end
end
test_really_long_keys() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 289
def test_really_long_keys
  key = "".dup
  900.times { key << "x" }
  assert @cache.write(key, "bar")
  assert_equal "bar", @cache.read(key)
  assert_equal "bar", @cache.fetch(key)
  assert_nil @cache.read("#{key}x")
  assert_equal({ key => "bar" }, @cache.read_multi(key))
  assert @cache.delete(key)
end
test_should_overwrite() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 10
def test_should_overwrite
  @cache.write("foo", "bar")
  @cache.write("foo", "baz")
  assert_equal "baz", @cache.read("foo")
end
test_should_read_and_write_false() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 84
def test_should_read_and_write_false
  assert @cache.write("foo", false)
  assert_equal false, @cache.read("foo")
end
test_should_read_and_write_hash() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 69
def test_should_read_and_write_hash
  assert @cache.write("foo", a: "b")
  assert_equal({ a: "b" }, @cache.read("foo"))
end
test_should_read_and_write_integer() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 74
def test_should_read_and_write_integer
  assert @cache.write("foo", 1)
  assert_equal 1, @cache.read("foo")
end
test_should_read_and_write_nil() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 79
def test_should_read_and_write_nil
  assert @cache.write("foo", nil)
  assert_nil @cache.read("foo")
end
test_should_read_and_write_strings() click to toggle source
# File activesupport/test/cache/behaviors/cache_store_behavior.rb, line 5
def test_should_read_and_write_strings
  assert @cache.write("foo", "bar")
  assert_equal "bar", @cache.read("foo")
end