|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
| 3 | +class LoggerClass |
| 4 | + def self.log(_); end |
| 5 | +end |
| 6 | + |
3 | 7 | describe ::ActiveRemote::Cached::Cache do |
4 | 8 | describe "API" do |
5 | 9 | it "validates #delete present" do |
|
32 | 36 | _(error.message).must_match(/respond_to.*write/i) |
33 | 37 | end |
34 | 38 | end |
| 39 | + |
| 40 | + describe "#delete" do |
| 41 | + it "returns nil when cache write attempt raises an error" do |
| 42 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => true, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 43 | + cache = HashCache.new |
| 44 | + cache.expects(:delete).raises(::RuntimeError, "kaBOOM") |
| 45 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 46 | + _(ar_cache.delete("some_key")).must_be_nil |
| 47 | + end |
| 48 | + |
| 49 | + it "calls error proc" do |
| 50 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => true, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 51 | + cache = HashCache.new |
| 52 | + cache.expects(:delete).raises(::RuntimeError, "kaBOOM") |
| 53 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 54 | + ::ActiveRemote::Cached.default_options[:cache_error_proc].expects(:call) |
| 55 | + ar_cache.delete("some_key") |
| 56 | + end |
| 57 | + |
| 58 | + describe "when fallback_on_cache_error is false" do |
| 59 | + it "raises error" do |
| 60 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => false, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 61 | + cache = HashCache.new |
| 62 | + cache.expects(:delete).raises(::RuntimeError, "kaBOOM") |
| 63 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 64 | + _ { ar_cache.delete("some_key") }.must_raise(::RuntimeError, "kaBOOM") |
| 65 | + end |
| 66 | + |
| 67 | + it "doesn't invoke error proc" do |
| 68 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => false, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 69 | + cache = HashCache.new |
| 70 | + cache.expects(:delete).raises(::RuntimeError, "kaBOOM") |
| 71 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 72 | + ::ActiveRemote::Cached.default_options[:cache_error_proc].expects(:log).never |
| 73 | + _ { ar_cache.delete("some_key") }.must_raise(::RuntimeError, "kaBOOM") |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + describe "#exist?" do |
| 79 | + it "returns false when cache exist attempt check raises an error" do |
| 80 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => true, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 81 | + cache = HashCache.new |
| 82 | + cache.expects(:exist?).raises(::RuntimeError, "kaBOOM") |
| 83 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 84 | + _(ar_cache.exist?("some_key")).must_equal(false) |
| 85 | + end |
| 86 | + |
| 87 | + it "calls error proc" do |
| 88 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => true, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 89 | + cache = HashCache.new |
| 90 | + cache.expects(:exist?).raises(::RuntimeError, "kaBOOM") |
| 91 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 92 | + ::ActiveRemote::Cached.default_options[:cache_error_proc].expects(:call) |
| 93 | + ar_cache.exist?("some_key") |
| 94 | + end |
| 95 | + |
| 96 | + describe "when fallback_on_cache_error is false" do |
| 97 | + it "raises error" do |
| 98 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => false, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 99 | + cache = HashCache.new |
| 100 | + cache.expects(:exist?).raises(::RuntimeError, "kaBOOM") |
| 101 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 102 | + _ { ar_cache.exist?("some_key") }.must_raise(::RuntimeError, "kaBOOM") |
| 103 | + end |
| 104 | + |
| 105 | + it "doesn't invoke error proc" do |
| 106 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => false, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 107 | + cache = HashCache.new |
| 108 | + cache.expects(:delete).raises(::RuntimeError, "kaBOOM") |
| 109 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 110 | + ::ActiveRemote::Cached.default_options[:cache_error_proc].expects(:log).never |
| 111 | + _ { ar_cache.delete("some_key") }.must_raise(::RuntimeError, "kaBOOM") |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + describe "#fetch" do |
| 117 | + it "returns cached value when read finds something" do |
| 118 | + cache = HashCache.new |
| 119 | + cache.write("some_key", "tada!") |
| 120 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 121 | + _(ar_cache.fetch("some_key")).must_equal("tada!") |
| 122 | + end |
| 123 | + |
| 124 | + it "invokes block when read returns nothing" do |
| 125 | + cache = HashCache.new |
| 126 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 127 | + _(ar_cache.fetch("some_key") { "tada!" }).must_equal("tada!") |
| 128 | + end |
| 129 | + |
| 130 | + it "removes cache key when returned value is invalid" do |
| 131 | + cache = HashCache.new |
| 132 | + cache.write("some_key", []) |
| 133 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 134 | + _(cache.exist?("some_key")).must_equal(true) |
| 135 | + ar_cache.fetch("some_key") |
| 136 | + _(cache.exist?("some_key")).must_equal(false) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + describe "#read" do |
| 141 | + it "returns nil when cache read attempt raises an error" do |
| 142 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => true, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 143 | + cache = HashCache.new |
| 144 | + cache.expects(:read).raises(::RuntimeError, "kaBOOM") |
| 145 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 146 | + _(ar_cache.read("some_key")).must_be_nil |
| 147 | + end |
| 148 | + |
| 149 | + it "calls error proc" do |
| 150 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => true, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 151 | + cache = HashCache.new |
| 152 | + cache.expects(:read).raises(::RuntimeError, "kaBOOM") |
| 153 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 154 | + ::ActiveRemote::Cached.default_options[:cache_error_proc].expects(:call) |
| 155 | + ar_cache.read("some_key") |
| 156 | + end |
| 157 | + |
| 158 | + describe "when fallback_on_cache_error is false" do |
| 159 | + it "raises error" do |
| 160 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => false, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 161 | + cache = HashCache.new |
| 162 | + cache.expects(:read).raises(::RuntimeError, "kaBOOM") |
| 163 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 164 | + _ { ar_cache.read("some_key") }.must_raise(::RuntimeError, "kaBOOM") |
| 165 | + end |
| 166 | + |
| 167 | + it "doesn't invoke error proc" do |
| 168 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => false, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 169 | + cache = HashCache.new |
| 170 | + cache.expects(:read).raises(::RuntimeError, "kaBOOM") |
| 171 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 172 | + ::ActiveRemote::Cached.default_options[:cache_error_proc].expects(:log).never |
| 173 | + _ { ar_cache.read("some_key") }.must_raise(::RuntimeError, "kaBOOM") |
| 174 | + end |
| 175 | + end |
| 176 | + end |
| 177 | + |
| 178 | + describe "#write" do |
| 179 | + it "returns nil when cache write attempt raises an error" do |
| 180 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => true, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 181 | + cache = HashCache.new |
| 182 | + cache.expects(:write).raises(::RuntimeError, "kaBOOM") |
| 183 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 184 | + _(ar_cache.write("some_key", "some_value")).must_be_nil |
| 185 | + end |
| 186 | + |
| 187 | + it "calls error proc" do |
| 188 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => true, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 189 | + cache = HashCache.new |
| 190 | + cache.expects(:write).raises(::RuntimeError, "kaBOOM") |
| 191 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 192 | + ::ActiveRemote::Cached.default_options[:cache_error_proc].expects(:call) |
| 193 | + ar_cache.write("some_key", "some_value") |
| 194 | + end |
| 195 | + |
| 196 | + describe "when fallback_on_cache_error is false" do |
| 197 | + it "raises error" do |
| 198 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => false, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 199 | + cache = HashCache.new |
| 200 | + cache.expects(:write).raises(::RuntimeError, "kaBOOM") |
| 201 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 202 | + _ { ar_cache.write("some_key", "some_value") }.must_raise(::RuntimeError, "kaBOOM") |
| 203 | + end |
| 204 | + |
| 205 | + it "doesn't invoke error proc" do |
| 206 | + ::ActiveRemote::Cached.default_options(:fallback_on_cache_error => false, :cache_error_proc => lambda { |e| LoggerClass.log(e) } ) |
| 207 | + cache = HashCache.new |
| 208 | + cache.expects(:write).raises(::RuntimeError, "kaBOOM") |
| 209 | + ar_cache = ::ActiveRemote::Cached::Cache.new(cache) |
| 210 | + ::ActiveRemote::Cached.default_options[:cache_error_proc].expects(:log).never |
| 211 | + _ { ar_cache.write("some_key", "some_value") }.must_raise(::RuntimeError, "kaBOOM") |
| 212 | + end |
| 213 | + end |
| 214 | + end |
35 | 215 | end |
0 commit comments