Mercurial > prosody-hg
comparison tests/test_util_cache.lua @ 7016:e0a0af42b09f
util.cache (and tests): Call on_evict after insertion of the new key, so inside on_evict we can be more certain about the current state of the cache (i.e. full, new item added, old item removed)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 22 Dec 2015 20:10:07 +0000 |
| parents | 31fb9eb9edce |
| children | 42e7545d5ae3 |
comparison
equal
deleted
inserted
replaced
| 7012:990b4ddaf582 | 7016:e0a0af42b09f |
|---|---|
| 168 assert_equal(k, keys[i]); | 168 assert_equal(k, keys[i]); |
| 169 assert_equal(v, values[i]); | 169 assert_equal(v, values[i]); |
| 170 end | 170 end |
| 171 assert_equal(i, 4); | 171 assert_equal(i, 4); |
| 172 | 172 |
| 173 | 173 local evicted_key, evicted_value; |
| 174 local c = new(3, function (_key, _value) | |
| 175 evicted_key, evicted_value = _key, _value; | |
| 176 end); | |
| 177 local function set(k, v, should_evict_key, should_evict_value) | |
| 178 evicted_key, evicted_value = nil, nil; | |
| 179 c:set(k, v); | |
| 180 assert_equal(evicted_key, should_evict_key); | |
| 181 assert_equal(evicted_value, should_evict_value); | |
| 182 end | |
| 183 set("a", 1) | |
| 184 set("a", 1) | |
| 185 set("a", 1) | |
| 186 set("a", 1) | |
| 187 set("a", 1) | |
| 188 | |
| 189 set("b", 2) | |
| 190 set("c", 3) | |
| 191 set("b", 2) | |
| 192 set("d", 4, "a", 1) | |
| 193 set("e", 5, "c", 3) | |
| 194 | |
| 195 | |
| 196 local evicted_key, evicted_value; | |
| 197 local c3 = new(1, function (_key, _value, c3) | |
| 198 evicted_key, evicted_value = _key, _value; | |
| 199 if _key == "a" then | |
| 200 -- Put it back in... | |
| 201 -- Check that the newest key/value was set before on_evict was called | |
| 202 assert_equal(c3:get("b"), 2); | |
| 203 -- Sanity check for what we're evicting | |
| 204 assert_equal(_key, "a"); | |
| 205 assert_equal(_value, 1); | |
| 206 -- Re-insert the evicted key (causes this evict function to run again with "b",2) | |
| 207 c3:set(_key, _value) | |
| 208 assert_equal(c3:get(_key), _value) | |
| 209 end | |
| 210 end); | |
| 211 local function set(k, v, should_evict_key, should_evict_value) | |
| 212 evicted_key, evicted_value = nil, nil; | |
| 213 c3:set(k, v); | |
| 214 assert_equal(evicted_key, should_evict_key); | |
| 215 assert_equal(evicted_value, should_evict_value); | |
| 216 end | |
| 217 set("a", 1) | |
| 218 set("a", 1) | |
| 219 set("a", 1) | |
| 220 set("a", 1) | |
| 221 set("a", 1) | |
| 222 | |
| 223 -- The evict handler re-inserts "a"->1, so "b" gets evicted: | |
| 224 set("b", 2, "b", 2) | |
| 225 -- Check the final state is what we expect | |
| 226 assert_equal(c3:get("a"), 1); | |
| 227 assert_equal(c3:get("b"), nil); | |
| 228 assert_equal(c3:count(), 1); | |
| 174 end | 229 end |
