From de149d56e872718d671337cba8e7d9d383f9147f Mon Sep 17 00:00:00 2001 From: Ido Barkan Date: Fri, 18 Sep 2020 09:52:56 +0300 Subject: [PATCH] improve tests, small bug fix --- .gitignore | 2 ++ project.clj | 2 +- src/cloffeine/async_loading_cache.clj | 8 ++++---- test/cloffeine/core_test.clj | 19 +++++++++++++------ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index ba4defe..31fca38 100644 --- a/.gitignore +++ b/.gitignore @@ -96,3 +96,5 @@ fabric.properties .clj-kondo/.cache *.png +.clj-kondo +.lsp/sqlite.1.db diff --git a/project.clj b/project.clj index 09ff5c5..12dc484 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject com.appsflyer/cloffeine "0.1.5" +(defproject com.appsflyer/cloffeine "0.1.6" :description "A warpper over https://github.com/ben-manes/caffeine" :url "https://github.com/AppsFlyer/cloffeine" :license {:name "Eclipse Public License" diff --git a/src/cloffeine/async_loading_cache.clj b/src/cloffeine/async_loading_cache.clj index e5a627c..68f2008 100644 --- a/src/cloffeine/async_loading_cache.clj +++ b/src/cloffeine/async_loading_cache.clj @@ -9,11 +9,11 @@ "Create an AsyncLoadingCache. See `cloffeine.common/builder` for settings. A semi-persistent mapping from keys to values. Values are automatically loaded by the cache asynchronously, and are stored in the cache until either evicted or manually invalidated.) Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads." - (^AsyncLoadingCache [^CacheLoader cl] - (make-cache cl {})) - (^AsyncLoadingCache [^CacheLoader cl settings] + (^AsyncLoadingCache [cache-loader] + (make-cache cache-loader {})) + (^AsyncLoadingCache [cache-loader settings] (let [bldr (common/make-builder settings)] - (.buildAsync bldr cl)))) + (.buildAsync bldr cache-loader)))) (defn make-cache-async-loader "Create a CacheLoader" diff --git a/test/cloffeine/core_test.clj b/test/cloffeine/core_test.clj index 894b42d..c09e178 100644 --- a/test/cloffeine/core_test.clj +++ b/test/cloffeine/core_test.clj @@ -72,12 +72,14 @@ (deftest loading-exceptions (let [loads (atom 0) throw? (atom false) + load-nil? (atom false) cl (common/reify-cache-loader (fn [k] - (if @throw? - (throw (ex-info "fail" {})) - (do - (swap! loads inc) - (name k))))) + (cond + @throw? (throw (ex-info "fail" {})) + @load-nil? nil + :else (do + (swap! loads inc) + (name k))))) ticker (FakeTicker.) lcache (loading-cache/make-cache cl {:refreshAfterWrite 10 :timeUnit :s @@ -101,7 +103,12 @@ (is (= "key" (loading-cache/get lcache :key))) (is (= 1 @loads)) (is (= "key" (loading-cache/get lcache :key))) - (is (= 1 @loads))))) + (is (= 1 @loads))) + (testing "loading a missing key but loader returns nil. nil returns, but mapping is not changed" + (reset! throw? false) + (reset! load-nil? true) + (is (nil? (loading-cache/get lcache :not-there))) + (is (= [:key] (keys (.asMap lcache))))))) (deftest get-if-present (let [cache (cache/make-cache)]