Skip to content

Commit

Permalink
Merge pull request #41 from groundedSAGE/update-cljs-support2
Browse files Browse the repository at this point in the history
Updates clojurescript support for memory store
  • Loading branch information
whilo authored Sep 23, 2020
2 parents da2209c + bac1064 commit f904359
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 125 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pom.xml.asc
/.nrepl-port
/.repl*
out
.shadow-cljs
.cpcache
.calva
node_modules
public

.idea
*.iml
*.iml
4 changes: 4 additions & 0 deletions bin/kaocha
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

clojure -A:test -m kaocha.runner "$@"

18 changes: 18 additions & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.764"}
io.replikativ/incognito {:mvn/version "0.2.5"}
io.replikativ/superv.async {:mvn/version "0.2.9-SNAPSHOT"}
fress {:mvn/version "0.3.1"}
org.clojure/data.fressian {:mvn/version "0.2.1"} ;; for filestore
io.replikativ/hasch {:mvn/version "0.3.7"}
org.clojars.mmb90/cljs-cache {:mvn/version "0.1.4"}
org.lz4/lz4-java {:mvn/version "1.6.0"}
com.taoensso/timbre {:mvn/version "4.10.0"}}
:aliases {:cljs {:extra-deps {thheller/shadow-cljs {:mvn/version "2.10.21"}
binaryage/devtools {:mvn/version "1.0.2"}}
:extra-paths ["test"]}
:dev {:extra-deps {criterium {:mvn/version "0.4.4"}}}
:test {:extra-deps {lambdaisland/kaocha {:mvn/version "1.0.641"}
org.clojure/test.check {:mvn/version "0.9.0"}}
:extra-paths ["test"]}}}
19 changes: 19 additions & 0 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{:deps {:aliases [:cljs]}

:builds
{:app
{:target :browser
:output-dir "public/js"
:asset-path "/js"
:modules {:main {:entries [konserve.core]}}}

:browser-test
{:target :browser-test
:test-dir "resources/public/js/test"
:devtools {:http-port 8021
:http-root "resources/public/js/test"}}

:node-test
{:target :node-test
:output-to "out/node-tests.js"
:autorun true}}}
7 changes: 2 additions & 5 deletions src/konserve/cache.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@
#?(:clj [clojure.core.cache :as cache]
:cljs [cljs.cache :as cache])
[konserve.core :refer [meta-update]]
#?(:clj [konserve.core :refer [go-locked]])
#?(:clj [clojure.core.async :refer [chan poll! put! <! go]]
:cljs [cljs.core.async :refer [chan poll! put! <!]]))
#?(:cljs (:require-macros [cljs.core.async.macros :refer [go]]
[konserve.core :refer [go-locked]])))
[konserve.core :refer [go-locked]]
[clojure.core.async :refer [chan poll! put! <! go]]))

(defn ensure-cache
"Adds a cache to the store. If none is provided it takes a LRU cache with 32
Expand Down
79 changes: 0 additions & 79 deletions src/konserve/compliance_test.cljc

This file was deleted.

45 changes: 11 additions & 34 deletions src/konserve/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,9 @@
-keys]]
[hasch.core :refer [uuid]]
[taoensso.timbre :as timbre :refer [trace]]
#?(:clj [clojure.core.async :refer [chan poll! put! <! go]]
:cljs [cljs.core.async :refer [chan poll! put! <!]]))
#?(:cljs (:require-macros [cljs.core.async.macros :refer [go]]
[konserve.core :refer [go-locked]])))

(defn- cljs-env?
"Take the &env from a macro, and tell whether we are expanding into cljs."
[env]
(boolean (:ns env)))

#?(:clj
(defmacro if-cljs
"Return then if we are generating cljs code and else for Clojure code.
https://groups.google.com/d/msg/clojurescript/iBY5HaQda4A/w1lAQi9_AwsJ"
[then else]
(if (cljs-env? &env) then else)))
[clojure.core.async :refer [chan poll! put! <! go]])
#?(:cljs (:require-macros [konserve.core :refer [go-locked]])))


;; TODO we keep one chan for each key in memory
;; as async ops seem to interfere with the atom state changes
Expand All @@ -34,24 +21,14 @@
(clojure.core/assoc old key c))))
key))))

#?(:clj
(defmacro go-locked [store key & code]
(let [res`(if-cljs
(cljs.core.async.macros/go
(let [l# (get-lock ~store ~key)]
(try
(cljs.core.async/<! l#)
~@code
(finally
(cljs.core.async/put! l# :unlocked)))))
(go
(let [l# (get-lock ~store ~key)]
(try
(<! l#)
~@code
(finally
(put! l# :unlocked))))))]
res)))
(defmacro go-locked [store key & code]
`(go
(let [l# (get-lock ~store ~key)]
(try
(<! l#)
~@code
(finally
(put! l# :unlocked))))))

(defn exists?
"Checks whether value is in the store."
Expand Down
3 changes: 1 addition & 2 deletions src/konserve/gc.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns konserve.gc
(:require [konserve.core :as k]
[superv.async :refer [go-try- <?- reduce<?-]])
#?(:cljs (:require-macros [superv.async :refer [go-try- <?-]])))
[superv.async :refer [go-try- <?- reduce<?-]]))

(defn sweep! [store whitelist ts]
(go-try-
Expand Down
3 changes: 1 addition & 2 deletions src/konserve/memory.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
-bget -bassoc
PKeyIterable
-keys]]
[clojure.core.async :as async])
#?(:cljs (:require-macros [cljs.core.async.macros :refer [go]])))
[clojure.core.async :as async :refer [go]]))


(defrecord MemoryStore [state read-handlers write-handlers locks]
Expand Down
4 changes: 2 additions & 2 deletions src/konserve/serializers.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@
(def serializer-class->byte
(construct->class byte->serializer))

#(:clj
#?(:clj
(defn construct->keys [m]
(->> (map (fn [[k v]] [(-> v class .getSimpleName keyword) v]) m)
(into {}))))

#(:cljs
#?(:cljs
(defn construct->keys [m]
(->> (map (fn [[k v]] [(-> v type pr-str) v]) m)
(into {}))))
Expand Down
101 changes: 101 additions & 0 deletions test/konserve/compliance_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
(ns konserve.compliance-test
(:refer-clojure :exclude [get get-in update update-in assoc assoc-in dissoc exists? keys])
(:require [clojure.core.async :as async :refer [#?(:clj <!!) go chan <!]]
[konserve.core :refer [get assoc assoc-in get-in update-in dissoc bassoc bget keys get-meta exists?]]
#?(:cljs [cljs.test :refer [deftest is testing async]])
#?(:clj [clojure.test :refer :all])
[konserve.memory :refer [new-mem-store]]))

(deftype UnknownType [])

#?(:clj (defn exception? [thing]
(instance? Throwable thing)))

#?(:clj
(defn compliance-test [store]
(testing "Test the core API."
(is (= (<!! (get store :foo))
nil))
(<!! (assoc store :foo :bar))
(is (= (<!! (get store :foo))
:bar))
(<!! (assoc-in store [:foo] :bar2))
(is (= :bar2 (<!! (get store :foo))))
(is (= :default
(<!! (get-in store [:fuu] :default))))
(is (= :bar2 (<!! (get store :foo))))
(is (= :default
(<!! (get-in store [:fuu] :default))))
(<!! (update-in store [:foo] name))
(is (= "bar2"
(<!! (get store :foo))))
(<!! (assoc-in store [:baz] {:bar 42}))
(is (= (<!! (get-in store [:baz :bar]))
42))
(<!! (update-in store [:baz :bar] inc))
(is (= (<!! (get-in store [:baz :bar]))
43))
(<!! (update-in store [:baz :bar] + 2 3))
(is (= (<!! (get-in store [:baz :bar]))
48))
(<!! (dissoc store :foo))
(is (= (<!! (get-in store [:foo]))
nil))
(<!! (bassoc store :binbar (byte-array (range 10))))
(<!! (bget store :binbar (fn [{:keys [input-stream]}]
(go
(is (= (map byte (slurp input-stream))
(range 10)))))))
(let [list-keys (<!! (keys store))]
(are [x y] (= x y)
#{{:key :baz
:type :edn}
{:key :binbar
:type :binary}}
(->> list-keys (map #(clojure.core/dissoc % :konserve.core/timestamp)) set)
true
(every?
(fn [{:keys [:konserve.core/timestamp]}]
(= (type (java.util.Date.)) (type timestamp)))
list-keys)))

(let [params (clojure.core/keys store)
corruptor (fn [s k]
(if (= (type (k s)) clojure.lang.Atom)
(clojure.core/assoc-in s [k] (atom {}))
(clojure.core/assoc-in s [k] (UnknownType.))))
corrupt (reduce corruptor store params)]
(is (exception? (<!! (get corrupt :bad))))
(is (exception? (<!! (get-meta corrupt :bad))))
(is (exception? (<!! (assoc corrupt :bad 10))))
(is (exception? (<!! (dissoc corrupt :bad))))
(is (exception? (<!! (assoc-in corrupt [:bad :robot] 10))))
(is (exception? (<!! (update-in corrupt [:bad :robot] inc))))
(is (exception? (<!! (exists? corrupt :bad))))
(is (exception? (<!! (keys corrupt))))
(is (exception? (<!! (bget corrupt :bad (fn [_] nil)))))
(is (exception? (<!! (bassoc corrupt :binbar (byte-array (range 10))))))))))

#?(:cljs (deftest compliance-test-cljs
(testing "this is a test"
(async done
(go
(let [store (<! (new-mem-store))]
(is (= (<! (get store :foo)) nil))
(<! (assoc store :foo :bar))
(is (= :bar (<! (get store :foo))))
(<! (assoc-in store [:foo] :bar2))
(is (= :bar2 (<! (get store :foo))))
(is (= :default
(<! (get-in store [:fuu] :default))))
(<! (update-in store [:foo] name))
(is (= "bar2" (<! (get store :foo))))
(<! (assoc-in store [:baz] {:bar 42}))
(is (= (<! (get-in store [:baz :bar])) 42))
(<! (update-in store [:baz :bar] inc))
(is (= (<! (get-in store [:baz :bar])) 43))
(<! (update-in store [:baz :bar] + 2 3))
(is (= (<! (get-in store [:baz :bar])) 48))
(<! (dissoc store :foo))
(is (= (<! (get-in store [:foo])) nil))
(done)))))))
2 changes: 2 additions & 0 deletions tests.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#kaocha/v1
{:tests [{:id :unit}]}

0 comments on commit f904359

Please sign in to comment.