Software / code / prosody
Comparison
tests/test_util_cache.lua @ 7290:70ab13e81cf5
util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 17 Mar 2016 19:08:42 +0000 |
| parent | 7289:42e7545d5ae3 |
| child | 7291:688a7f5d3624 |
comparison
equal
deleted
inserted
replaced
| 7289:42e7545d5ae3 | 7290:70ab13e81cf5 |
|---|---|
| 220 | 220 |
| 221 local evicted_key, evicted_value; | 221 local evicted_key, evicted_value; |
| 222 local c3 = new(1, function (_key, _value, c3) | 222 local c3 = new(1, function (_key, _value, c3) |
| 223 evicted_key, evicted_value = _key, _value; | 223 evicted_key, evicted_value = _key, _value; |
| 224 if _key == "a" then | 224 if _key == "a" then |
| 225 -- Put it back in... | |
| 226 -- Check that the newest key/value was set before on_evict was called | |
| 227 assert_equal(c3:get("b"), 2); | |
| 228 -- Sanity check for what we're evicting | 225 -- Sanity check for what we're evicting |
| 229 assert_equal(_key, "a"); | 226 assert_equal(_key, "a"); |
| 230 assert_equal(_value, 1); | 227 assert_equal(_value, 1); |
| 231 -- Re-insert the evicted key (causes this evict function to run again with "b",2) | 228 -- We're going to block eviction of this key/value, so set to nil... |
| 232 c3:set(_key, _value) | 229 evicted_key, evicted_value = nil, nil; |
| 233 assert_equal(c3:get(_key), _value) | 230 -- Returning false to block eviction |
| 231 return false | |
| 234 end | 232 end |
| 235 end); | 233 end); |
| 236 local function set(k, v, should_evict_key, should_evict_value) | 234 local function set(k, v, should_evict_key, should_evict_value) |
| 237 evicted_key, evicted_value = nil, nil; | 235 evicted_key, evicted_value = nil, nil; |
| 238 c3:set(k, v); | 236 local ret = c3:set(k, v); |
| 239 assert_equal(evicted_key, should_evict_key); | 237 assert_equal(evicted_key, should_evict_key); |
| 240 assert_equal(evicted_value, should_evict_value); | 238 assert_equal(evicted_value, should_evict_value); |
| 241 end | 239 return ret; |
| 242 set("a", 1) | 240 end |
| 243 set("a", 1) | 241 set("a", 1) |
| 244 set("a", 1) | 242 set("a", 1) |
| 245 set("a", 1) | 243 set("a", 1) |
| 246 set("a", 1) | 244 set("a", 1) |
| 247 | 245 set("a", 1) |
| 248 -- The evict handler re-inserts "a"->1, so "b" gets evicted: | 246 |
| 249 set("b", 2, "b", 2) | 247 -- Our on_evict prevents "a" from being evicted, causing this to fail... |
| 248 assert_equal(set("b", 2), false, "Failed to prevent eviction, or signal result"); | |
| 249 | |
| 250 expect_kv("a", 1, c3:head()); | |
| 251 expect_kv("a", 1, c3:tail()); | |
| 252 | |
| 250 -- Check the final state is what we expect | 253 -- Check the final state is what we expect |
| 251 assert_equal(c3:get("a"), 1); | 254 assert_equal(c3:get("a"), 1); |
| 252 assert_equal(c3:get("b"), nil); | 255 assert_equal(c3:get("b"), nil); |
| 253 assert_equal(c3:count(), 1); | 256 assert_equal(c3:count(), 1); |
| 257 | |
| 258 | |
| 259 local c4 = new(3, false); | |
| 260 | |
| 261 assert_equal(c4:set("a", 1), true); | |
| 262 assert_equal(c4:set("a", 1), true); | |
| 263 assert_equal(c4:set("a", 1), true); | |
| 264 assert_equal(c4:set("a", 1), true); | |
| 265 assert_equal(c4:set("b", 2), true); | |
| 266 assert_equal(c4:set("c", 3), true); | |
| 267 assert_equal(c4:set("d", 4), false); | |
| 268 assert_equal(c4:set("d", 4), false); | |
| 269 assert_equal(c4:set("d", 4), false); | |
| 270 | |
| 271 expect_kv("c", 3, c4:head()); | |
| 272 expect_kv("a", 1, c4:tail()); | |
| 273 | |
| 254 end | 274 end |