|
| 1 | +(ns pool.cache-test |
| 2 | + (:require [clojure.test :refer :all] |
| 3 | + [pool.cache :as c])) |
| 4 | + |
| 5 | +; Initialize Cache |
| 6 | +(def cache |
| 7 | + (c/get-cache |
| 8 | + (fn [k] (let [ts (System/currentTimeMillis)] |
| 9 | + (println (str "Inititalized: " k " at: " ts)) ts)) |
| 10 | + :destroy (fn [k v] (println "Destroyed: " k) true))) |
| 11 | + |
| 12 | +; Initialize Cache with idle timeout |
| 13 | +(def icache |
| 14 | + (c/get-cache |
| 15 | + (fn [k] (let [ts (System/currentTimeMillis)] |
| 16 | + (println (str "Inititalized: " k " at: " ts)) ts)) |
| 17 | + :destroy (fn [k v] (println "Destroyed: " k) true) :idle-timeout 2000)) |
| 18 | + |
| 19 | +(deftest test-cache |
| 20 | + ; Should be possible to add new elements |
| 21 | + (let [ts (c/get cache "first")] |
| 22 | + ; Basic check for value |
| 23 | + (is (>= (System/currentTimeMillis) ts)) |
| 24 | + ; create drift |
| 25 | + (Thread/sleep 500) |
| 26 | + ; Same element key should not be re-initialized |
| 27 | + (is (= ts (c/get cache "first"))) |
| 28 | + ; Added element should be present |
| 29 | + (is (= ts (c/exists? cache "first"))) |
| 30 | + ; Should be possible to drop existing element |
| 31 | + (is (= {} (c/purge cache "first"))) |
| 32 | + ; Object should no longer exist |
| 33 | + (is (nil? (c/exists? cache "first"))) |
| 34 | + ; Purge should be a no-op |
| 35 | + (is (nil? (c/purge cache "first"))))) |
| 36 | + |
| 37 | +(deftest idle-timeout |
| 38 | + ; Should be possible to add new elements |
| 39 | + (let [lts (c/get icache "leech") |
| 40 | + bts (c/get icache "bird")] |
| 41 | + ; Basic check for value |
| 42 | + (is (>= (System/currentTimeMillis) lts)) |
| 43 | + (is (>= (System/currentTimeMillis) bts)) |
| 44 | + ; create drift |
| 45 | + (Thread/sleep 500) |
| 46 | + ; Added element should be present and not re-initialized |
| 47 | + (is (= lts (c/exists? icache "leech"))) |
| 48 | + (is (= lts (c/get icache "leech"))) |
| 49 | + (is (= bts (c/exists? icache "bird"))) |
| 50 | + (is (= bts (c/get icache "bird"))) |
| 51 | + (Thread/sleep 1000) |
| 52 | + ; Same element key should not be re-initialized |
| 53 | + ; but access timestamp should be updated |
| 54 | + (is (= lts (c/get icache "leech"))) |
| 55 | + (Thread/sleep 1000) |
| 56 | + ; Accessed element should be present |
| 57 | + (is (= lts (c/exists? icache "leech"))) |
| 58 | + ; Accessed element should not be re-initialized |
| 59 | + (is (= lts (c/exists? icache "leech"))) |
| 60 | + ; Bird should be re-initialized |
| 61 | + (is (< bts (c/get icache "bird"))) |
| 62 | + ; Should be possible to drop existing element |
| 63 | + (is (c/purge icache "leech")) |
| 64 | + (is (c/purge icache "bird")) |
| 65 | + ; Object should no longer exist |
| 66 | + (is (nil? (c/exists? icache "leech"))) |
| 67 | + (is (nil? (c/exists? icache "bird"))) |
| 68 | + ; Purge should be a no-op |
| 69 | + (is (nil? (c/purge cache "leech"))) |
| 70 | + (is (nil? (c/purge cache "bird"))))) |
0 commit comments