From 66da762929bda303d3525a2bf0ba7ef6bcb9e8b0 Mon Sep 17 00:00:00 2001 From: Josh Glover Date: Thu, 15 Feb 2024 09:00:28 +0100 Subject: [PATCH 1/7] Fix flaky caching tests --- src/quickblog/internal.clj | 6 +----- test/quickblog/api_test.clj | 20 +++++++++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/quickblog/internal.clj b/src/quickblog/internal.clj index 4c8022b..cb6ac3b 100644 --- a/src/quickblog/internal.clj +++ b/src/quickblog/internal.clj @@ -31,12 +31,8 @@ (zipmap (map keyword ks) ks)) -;; Cons-ing *file* directly in `rendering-modified?` doesn't work for some reason -(def ^:private this-file (fs/file *file*)) - (defn rendering-modified? [target-file rendering-system-files] - (let [rendering-system-files (cons this-file rendering-system-files)] - (seq (fs/modified-since target-file rendering-system-files)))) + (seq (fs/modified-since target-file rendering-system-files))) (defn copy-modified [src target] (when (seq (fs/modified-since target src)) diff --git a/test/quickblog/api_test.clj b/test/quickblog/api_test.clj index 344bc9e..5162210 100644 --- a/test/quickblog/api_test.clj +++ b/test/quickblog/api_test.clj @@ -298,8 +298,7 @@ :out-dir out-dir}) (is (not (str/includes? (slurp (fs/file out-dir "test.html")) lib/live-reload-script)))))) -;; disabled, flaky in CI, cc @jmglov -#_(deftest caching +(deftest caching (testing "assets" (with-dirs [assets-dir posts-dir @@ -313,6 +312,7 @@ :out-dir out-dir})] (write-test-post posts-dir) (write-test-file assets-dir "asset.txt" "something") + (Thread/sleep 500) (render) (let [asset-file (fs/file out-dir "assets" "asset.txt") mtime (fs/last-modified-time asset-file)] @@ -332,9 +332,15 @@ (let [render #(api/render {:posts-dir posts-dir :templates-dir templates-dir :cache-dir cache-dir - :out-dir out-dir})] + :out-dir out-dir}) + cache-dir (fs/file cache-dir "prod")] (write-test-post posts-dir) (render) + ;; We need to render again, since the first render will have written + ;; default templates for pages, post links, archive, and index after the + ;; post, which means the post will be considered modified relative to + ;; the templates dir + (render) (let [->mtimes (fn [dir filenames] (->> filenames (map #(let [filename (fs/file dir %)] @@ -350,15 +356,16 @@ (->mtimes (fs/file out-dir "tags") ["clojure.html"]))] ;; Shouldn't rewrite anything when post unmodified + (Thread/sleep 500) (render) (doseq [[filename mtime] (merge content-cached clojure-metadata-cached)] (is (= (map str [filename mtime]) (map str [filename (fs/last-modified-time filename)])))) ;; Should rewrite all but metadata-cached files when post modified (write-test-post posts-dir) + (Thread/sleep 500) (render) - ;; disabled, flaky, /cc @jmglov - #_(doseq [[filename mtime] content-cached] + (doseq [[filename mtime] content-cached] (is (not= (map str [filename mtime]) (map str [filename (fs/last-modified-time filename)])))) (doseq [[filename mtime] clojure-metadata-cached] @@ -366,6 +373,7 @@ (map str [filename (fs/last-modified-time filename)])))) ;; Should rewrite everything when metadata modified (write-test-post posts-dir {:title "Changed", :tags #{"not-clojure"}}) + (Thread/sleep 500) (render) (doseq [[filename mtime] (merge content-cached metadata-cached)] (is (not= (map str [filename mtime]) @@ -422,6 +430,7 @@ :tags #{"clojure"}}) (write-test-post posts-dir {:file "random2.md" :tags #{"something"}}) + (Thread/sleep 500) (let [mtimes (->mtimes out-dir ["atom.xml" "planetclojure.xml"]) _ (render) mtimes-after (->mtimes out-dir ["atom.xml" "planetclojure.xml"])] @@ -437,6 +446,7 @@ "clojure2.html" "clojurescript1.html"} (post-ids (fs/file out-dir "planetclojure.xml")))) + (Thread/sleep 500) (let [mtimes (->mtimes out-dir ["atom.xml" "planetclojure.xml"]) _ (render) mtimes-after (->mtimes out-dir ["atom.xml" "planetclojure.xml"])] From 79b853c3bc8b89812b099791b4e06027a7786c07 Mon Sep 17 00:00:00 2001 From: Josh Glover Date: Thu, 15 Feb 2024 09:02:33 +0100 Subject: [PATCH 2/7] Update changelog for caching tests fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a281954..745cb35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Instances of quickblog can be seen here: ## Unreleased +- Fix flaky caching tests ([@jmglov](https://github.com/jmglov)) - Fix argument passing in test runner ([@jmglov](https://github.com/jmglov)) - Add `--date` to api/new. ([@jmglov](https://github.com/jmglov)) - Support Selmer template for new posts in api/new; see [Templates > New From 7ed7d39ce46cb5193f2d5cdc2cf1b7bd5530afd8 Mon Sep 17 00:00:00 2001 From: Josh Glover Date: Thu, 15 Feb 2024 09:13:24 +0100 Subject: [PATCH 3/7] Add some debug prints for CI --- test/quickblog/api_test.clj | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/quickblog/api_test.clj b/test/quickblog/api_test.clj index 5162210..e585721 100644 --- a/test/quickblog/api_test.clj +++ b/test/quickblog/api_test.clj @@ -12,9 +12,9 @@ (use-fixtures :each (fn [test-fn] - (with-out-str - (test-fn) - (fs/delete-tree test-dir)))) + #_(with-out-str) + (test-fn) + (fs/delete-tree test-dir))) (defn- tmp-dir [dir-name] (fs/file test-dir @@ -373,6 +373,7 @@ (map str [filename (fs/last-modified-time filename)])))) ;; Should rewrite everything when metadata modified (write-test-post posts-dir {:title "Changed", :tags #{"not-clojure"}}) + (println "Posts:" (map str (fs/glob posts-dir "**"))) (Thread/sleep 500) (render) (doseq [[filename mtime] (merge content-cached metadata-cached)] From 67ba9c6ae059e071a37f70c00054d4f03a45f3a0 Mon Sep 17 00:00:00 2001 From: Josh Glover Date: Thu, 15 Feb 2024 09:24:43 +0100 Subject: [PATCH 4/7] Remove caching test for modified metadata --- test/quickblog/api_test.clj | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/test/quickblog/api_test.clj b/test/quickblog/api_test.clj index e585721..80d1ab3 100644 --- a/test/quickblog/api_test.clj +++ b/test/quickblog/api_test.clj @@ -361,18 +361,8 @@ (doseq [[filename mtime] (merge content-cached clojure-metadata-cached)] (is (= (map str [filename mtime]) (map str [filename (fs/last-modified-time filename)])))) - ;; Should rewrite all but metadata-cached files when post modified + ;; Should rewrite everything when post modified (write-test-post posts-dir) - (Thread/sleep 500) - (render) - (doseq [[filename mtime] content-cached] - (is (not= (map str [filename mtime]) - (map str [filename (fs/last-modified-time filename)])))) - (doseq [[filename mtime] clojure-metadata-cached] - (is (= (map str [filename mtime]) - (map str [filename (fs/last-modified-time filename)])))) - ;; Should rewrite everything when metadata modified - (write-test-post posts-dir {:title "Changed", :tags #{"not-clojure"}}) (println "Posts:" (map str (fs/glob posts-dir "**"))) (Thread/sleep 500) (render) From 5b35f0a232dc5d2af52047840e4ddb331fe39d0d Mon Sep 17 00:00:00 2001 From: Josh Glover Date: Thu, 15 Feb 2024 09:33:33 +0100 Subject: [PATCH 5/7] Move sleeps before writes in caching tests --- test/quickblog/api_test.clj | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/test/quickblog/api_test.clj b/test/quickblog/api_test.clj index 80d1ab3..9a1368b 100644 --- a/test/quickblog/api_test.clj +++ b/test/quickblog/api_test.clj @@ -310,9 +310,9 @@ :templates-dir templates-dir :cache-dir cache-dir :out-dir out-dir})] + (Thread/sleep 500) (write-test-post posts-dir) (write-test-file assets-dir "asset.txt" "something") - (Thread/sleep 500) (render) (let [asset-file (fs/file out-dir "assets" "asset.txt") mtime (fs/last-modified-time asset-file)] @@ -356,15 +356,23 @@ (->mtimes (fs/file out-dir "tags") ["clojure.html"]))] ;; Shouldn't rewrite anything when post unmodified - (Thread/sleep 500) (render) (doseq [[filename mtime] (merge content-cached clojure-metadata-cached)] (is (= (map str [filename mtime]) (map str [filename (fs/last-modified-time filename)])))) - ;; Should rewrite everything when post modified + ;; Should rewrite all but metadata-cached files when post modified + (Thread/sleep 500) (write-test-post posts-dir) - (println "Posts:" (map str (fs/glob posts-dir "**"))) + (render) + (doseq [[filename mtime] content-cached] + (is (not= (map str [filename mtime]) + (map str [filename (fs/last-modified-time filename)])))) + (doseq [[filename mtime] clojure-metadata-cached] + (is (= (map str [filename mtime]) + (map str [filename (fs/last-modified-time filename)])))) + ;; Should rewrite everything when metadata modified (Thread/sleep 500) + (write-test-post posts-dir {:title "Changed", :tags #{"not-clojure"}}) (render) (doseq [[filename mtime] (merge content-cached metadata-cached)] (is (not= (map str [filename mtime]) @@ -417,11 +425,11 @@ (is (= #{"clojure1.html" "clojurescript1.html"} (post-ids (fs/file out-dir "planetclojure.xml")))) + (Thread/sleep 500) (write-test-post posts-dir {:file "clojure2.md" :tags #{"clojure"}}) (write-test-post posts-dir {:file "random2.md" :tags #{"something"}}) - (Thread/sleep 500) (let [mtimes (->mtimes out-dir ["atom.xml" "planetclojure.xml"]) _ (render) mtimes-after (->mtimes out-dir ["atom.xml" "planetclojure.xml"])] @@ -437,7 +445,6 @@ "clojure2.html" "clojurescript1.html"} (post-ids (fs/file out-dir "planetclojure.xml")))) - (Thread/sleep 500) (let [mtimes (->mtimes out-dir ["atom.xml" "planetclojure.xml"]) _ (render) mtimes-after (->mtimes out-dir ["atom.xml" "planetclojure.xml"])] From 06ccccb7522fae8c717a3c830d4d3f95f4761bff Mon Sep 17 00:00:00 2001 From: Josh Glover Date: Thu, 15 Feb 2024 09:35:39 +0100 Subject: [PATCH 6/7] Remove debug prints from tests --- test/quickblog/api_test.clj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/quickblog/api_test.clj b/test/quickblog/api_test.clj index 9a1368b..cd38817 100644 --- a/test/quickblog/api_test.clj +++ b/test/quickblog/api_test.clj @@ -12,9 +12,9 @@ (use-fixtures :each (fn [test-fn] - #_(with-out-str) - (test-fn) - (fs/delete-tree test-dir))) + (with-out-str + (test-fn) + (fs/delete-tree test-dir)))) (defn- tmp-dir [dir-name] (fs/file test-dir From d42e34a0e677fb534406111a0035a08eafa7dc46 Mon Sep 17 00:00:00 2001 From: Josh Glover Date: Thu, 15 Feb 2024 14:50:13 +0100 Subject: [PATCH 7/7] Sleep 5 ms instead of 500 ms --- test/quickblog/api_test.clj | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/quickblog/api_test.clj b/test/quickblog/api_test.clj index cd38817..b342cc3 100644 --- a/test/quickblog/api_test.clj +++ b/test/quickblog/api_test.clj @@ -310,7 +310,7 @@ :templates-dir templates-dir :cache-dir cache-dir :out-dir out-dir})] - (Thread/sleep 500) + (Thread/sleep 5) (write-test-post posts-dir) (write-test-file assets-dir "asset.txt" "something") (render) @@ -361,7 +361,7 @@ (is (= (map str [filename mtime]) (map str [filename (fs/last-modified-time filename)])))) ;; Should rewrite all but metadata-cached files when post modified - (Thread/sleep 500) + (Thread/sleep 5) (write-test-post posts-dir) (render) (doseq [[filename mtime] content-cached] @@ -371,7 +371,7 @@ (is (= (map str [filename mtime]) (map str [filename (fs/last-modified-time filename)])))) ;; Should rewrite everything when metadata modified - (Thread/sleep 500) + (Thread/sleep 5) (write-test-post posts-dir {:title "Changed", :tags #{"not-clojure"}}) (render) (doseq [[filename mtime] (merge content-cached metadata-cached)] @@ -425,7 +425,7 @@ (is (= #{"clojure1.html" "clojurescript1.html"} (post-ids (fs/file out-dir "planetclojure.xml")))) - (Thread/sleep 500) + (Thread/sleep 5) (write-test-post posts-dir {:file "clojure2.md" :tags #{"clojure"}}) (write-test-post posts-dir {:file "random2.md"