aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/cache/behaviors/local_cache_behavior.rb
blob: 730f0306b86e38c772d9ca258e356e73c4102389 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen_string_literal: true
module LocalCacheBehavior
  def test_local_writes_are_persistent_on_the_remote_cache
    retval = @cache.with_local_cache do
      @cache.write("foo", "bar")
    end
    assert retval
    assert_equal "bar", @cache.read("foo")
  end

  def test_clear_also_clears_local_cache
    @cache.with_local_cache do
      @cache.write("foo", "bar")
      @cache.clear
      assert_nil @cache.read("foo")
    end

    assert_nil @cache.read("foo")
  end

  def test_cleanup_clears_local_cache_but_not_remote_cache
    skip unless @cache.class.instance_methods(false).include?(:cleanup)

    @cache.with_local_cache do
      @cache.write("foo", "bar")
      assert_equal "bar", @cache.read("foo")

      @cache.send(:bypass_local_cache) { @cache.write("foo", "baz") }
      assert_equal "bar", @cache.read("foo")

      @cache.cleanup
      assert_equal "baz", @cache.read("foo")
    end
  end

  def test_local_cache_of_write
    @cache.with_local_cache do
      @cache.write("foo", "bar")
      @peek.delete("foo")
      assert_equal "bar", @cache.read("foo")
    end
  end

  def test_local_cache_of_read
    @cache.write("foo", "bar")
    @cache.with_local_cache do
      assert_equal "bar", @cache.read("foo")
    end
  end

  def test_local_cache_of_read_nil
    @cache.with_local_cache do
      assert_nil @cache.read("foo")
      @cache.send(:bypass_local_cache) { @cache.write "foo", "bar" }
      assert_nil @cache.read("foo")
    end
  end

  def test_local_cache_fetch
    @cache.with_local_cache do
      @cache.send(:local_cache).write "foo", "bar"
      assert_equal "bar", @cache.send(:local_cache).fetch("foo")
    end
  end

  def test_local_cache_of_write_nil
    @cache.with_local_cache do
      assert @cache.write("foo", nil)
      assert_nil @cache.read("foo")
      @peek.write("foo", "bar")
      assert_nil @cache.read("foo")
    end
  end

  def test_local_cache_of_write_with_unless_exist
    @cache.with_local_cache do
      @cache.write("foo", "bar")
      @cache.write("foo", "baz", unless_exist: true)
      assert_equal @peek.read("foo"), @cache.read("foo")
    end
  end

  def test_local_cache_of_delete
    @cache.with_local_cache do
      @cache.write("foo", "bar")
      @cache.delete("foo")
      assert_nil @cache.read("foo")
    end
  end

  def test_local_cache_of_exist
    @cache.with_local_cache do
      @cache.write("foo", "bar")
      @peek.delete("foo")
      assert @cache.exist?("foo")
    end
  end

  def test_local_cache_of_increment
    @cache.with_local_cache do
      @cache.write("foo", 1, raw: true)
      @peek.write("foo", 2, raw: true)
      @cache.increment("foo")
      assert_equal 3, @cache.read("foo")
    end
  end

  def test_local_cache_of_decrement
    @cache.with_local_cache do
      @cache.write("foo", 1, raw: true)
      @peek.write("foo", 3, raw: true)
      @cache.decrement("foo")
      assert_equal 2, @cache.read("foo")
    end
  end

  def test_middleware
    app = lambda { |env|
      result = @cache.write("foo", "bar")
      assert_equal "bar", @cache.read("foo") # make sure 'foo' was written
      assert result
      [200, {}, []]
    }
    app = @cache.middleware.new(app)
    app.call({})
  end
end