aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/cache/behaviors/failure_safety_behavior.rb
blob: 43b67d81db2b6e531a44cd667a3e1ae549d1c22a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module FailureSafetyBehavior
  def test_fetch_read_failure_returns_nil
    @cache.write("foo", "bar")

    emulating_unavailability do |cache|
      assert_nil cache.fetch("foo")
    end
  end

  def test_fetch_read_failure_does_not_attempt_to_write
  end

  def test_read_failure_returns_nil
    @cache.write("foo", "bar")

    emulating_unavailability do |cache|
      assert_nil cache.read("foo")
    end
  end

  def test_read_multi_failure_returns_empty_hash
    @cache.write_multi("foo" => "bar", "baz" => "quux")

    emulating_unavailability do |cache|
      assert_equal Hash.new, cache.read_multi("foo", "baz")
    end
  end

  def test_write_failure_returns_false
    emulating_unavailability do |cache|
      assert_equal false, cache.write("foo", "bar")
    end
  end

  def test_write_multi_failure_not_raises
    emulating_unavailability do |cache|
      assert_nothing_raised do
        cache.write_multi("foo" => "bar", "baz" => "quux")
      end
    end
  end

  def test_fetch_multi_failure_returns_fallback_results
    @cache.write_multi("foo" => "bar", "baz" => "quux")

    emulating_unavailability do |cache|
      fetched = cache.fetch_multi("foo", "baz") { |k| "unavailable" }
      assert_equal Hash["foo" => "unavailable", "baz" => "unavailable"], fetched
    end
  end

  def test_delete_failure_returns_false
    @cache.write("foo", "bar")

    emulating_unavailability do |cache|
      assert_equal false, cache.delete("foo")
    end
  end

  def test_exist_failure_returns_false
    @cache.write("foo", "bar")

    emulating_unavailability do |cache|
      assert_not cache.exist?("foo")
    end
  end

  def test_increment_failure_returns_nil
    @cache.write("foo", 1, raw: true)

    emulating_unavailability do |cache|
      assert_nil cache.increment("foo")
    end
  end

  def test_decrement_failure_returns_nil
    @cache.write("foo", 1, raw: true)

    emulating_unavailability do |cache|
      assert_nil cache.decrement("foo")
    end
  end

  def test_clear_failure_returns_nil
    emulating_unavailability do |cache|
      assert_nil cache.clear
    end
  end
end