-
Notifications
You must be signed in to change notification settings - Fork 19
/
konserve_test.cljc
173 lines (163 loc) · 8.17 KB
/
konserve_test.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
(ns hitchhiker.konserve-test
(:refer-clojure :exclude [vec])
(:require [clojure.core.rrb-vector :refer [catvec vec]]
[#?(:clj clojure.test :cljs cljs.test)
#?(:clj :refer :cljs :refer-macros) [deftest testing run-tests is
#?(:cljs async)]]
[clojure.test.check.clojure-test #?(:clj :refer :cljs :refer-macros) [defspec]]
[clojure.test.check.generators :as gen :include-macros true]
[clojure.test.check.properties :as prop :include-macros true]
[konserve.filestore :refer [connect-fs-store delete-store list-files]]
[konserve.memory :refer [new-mem-store]]
[hitchhiker.tree.bootstrap.konserve :as kons]
[konserve.cache :as kc]
[hasch.core :as hasch]
[hitchhiker.tree :as core]
[hitchhiker.tree.utils.async :as ha :include-macros true]
[hitchhiker.tree.messaging :as msg]
#?(:cljs [cljs.core.async :refer [promise-chan] :as async]
:clj [clojure.core.async :refer [promise-chan] :as async])
#?(:cljs [cljs.nodejs :as nodejs])
[clojure.string :as str]))
#?(:cljs
(do
(nodejs/enable-util-print!)
(enable-console-print!)))
(defn iter-helper [tree key]
(ha/go-try
(let [iter-ch (async/chan)
path (ha/<? (core/lookup-path tree key))]
(ha/if-async?
(do
(when path
(msg/forward-iterator iter-ch path key))
(ha/<? (async/into [] iter-ch)))
(if path
(msg/forward-iterator path)
[])))))
(deftest simple-konserve-test
(testing "Insert and lookup"
#?(:cljs
(async done
(ha/go-try
(let [folder "/tmp/async-hitchhiker-tree-test"
_ (delete-store folder)
store (kons/add-hitchhiker-tree-handlers
(kc/ensure-cache (async/<!
(new-mem-store)
#_(connect-fs-store folder)))) ;; always use core.async here!
backend (kons/konserve-backend store)
init-tree (ha/<? (ha/reduce< (fn [t i] (msg/insert t i i 0))
(ha/<? (core/b-tree (core/->Config 1 3 (- 3 1))))
(range 1 11)))
flushed (ha/<? (core/flush-tree init-tree backend))
root-key (kons/get-root-key (:tree flushed))
tree (ha/<? (kons/create-tree-from-root-key store root-key))]
(is (= (ha/<? (msg/lookup tree -10)) nil))
(is (= (ha/<? (msg/lookup tree 100)) nil))
(dotimes [i 10]
(is (= (ha/<? (msg/lookup tree (inc i))) (inc i))))
(is (= (map first (ha/<? (iter-helper tree 4))) (range 4 11)))
(is (= (map first (ha/<? (iter-helper tree 0))) (range 1 11)))
(let [deleted (ha/<? (core/flush-tree (ha/<? (msg/delete tree 3 0)) backend))
root-key (kons/get-root-key (:tree deleted))
tree (ha/<? (kons/create-tree-from-root-key store root-key))]
(is (= (ha/<? (msg/lookup tree 2)) 2))
(is (= (ha/<? (msg/lookup tree 3)) nil))
(is (= (ha/<? (msg/lookup tree 4)) 4)))
(delete-store folder)
(done)))))
#?(:clj
(let [folder "/tmp/async-hitchhiker-tree-test"
_ (delete-store folder)
store (kons/add-hitchhiker-tree-handlers
(kc/ensure-cache (async/<!! (connect-fs-store folder :config {:fsync false}))))
backend (kons/konserve-backend store)
flushed (ha/<?? (core/flush-tree
(time (reduce (fn [t i]
(ha/<?? (msg/insert t i i 0)))
(ha/<?? (core/b-tree (core/->Config 1 3 (- 3 1))))
(range 1 11)))
backend))
root-key (kons/get-root-key (:tree flushed))
tree (ha/<?? (kons/create-tree-from-root-key store root-key))]
(is (= (ha/<?? (msg/lookup tree -10)) nil))
(is (= (ha/<?? (msg/lookup tree 100)) nil))
(dotimes [i 10]
(is (= (ha/<?? (msg/lookup tree (inc i))) (inc i))))
(is (= (map first (msg/lookup-fwd-iter tree 4)) (range 4 11)))
(is (= (map first (msg/lookup-fwd-iter tree 0)) (range 1 11)))
(let [deleted (ha/<?? (core/flush-tree (ha/<?? (msg/delete tree 3 0)) backend))
root-key (kons/get-root-key (:tree deleted))
tree (ha/<?? (kons/create-tree-from-root-key store root-key))]
(is (= (ha/<?? (msg/lookup tree 2)) 2))
(is (= (ha/<?? (msg/lookup tree 3)) nil))
(is (= (ha/<?? (msg/lookup tree 4)) 4)))
(delete-store folder)))))
;; ;; adapted from redis tests
(defn insert
[t k]
(msg/insert t k k 0))
(defn ops-test [ops universe-size]
(ha/go-try
(let [folder (str "/tmp/konserve-mixed-workload" (hasch/uuid))
_ #?(:clj (delete-store folder) :cljs nil)
store (kons/add-hitchhiker-tree-handlers
(kc/ensure-cache
#?(:clj (async/<!! (connect-fs-store folder :config {:fsync false}))
:cljs (async/<! (new-mem-store)))))
_ #?(:clj (assert (empty? (list-files folder))
"Start with no keys")
:cljs nil)
;_ (swap! recorded-ops conj ops)
[b-tree root set]
(ha/<? (ha/reduce< (fn [[t root set] [op x]]
(ha/go-try
(let [x-reduced (when x (mod x universe-size))]
(case op
:flush (let [flushed (ha/<? (core/flush-tree t (kons/konserve-backend store)))
t (:tree flushed)]
[t (ha/<? (:storage-addr t)) set])
:add [(ha/<? (insert t x-reduced)) root (conj set x-reduced)]
:del [(ha/<? (msg/delete t x-reduced 0)) root (disj set x-reduced)]))))
[(ha/<? (core/b-tree (core/->Config 3 3 2))) nil #{}]
ops))]
(let [b-tree-order (seq (map first (ha/<? (iter-helper b-tree -1))))
res (= b-tree-order (seq (sort set)))]
(assert res (str "These are unequal: " (pr-str b-tree-order) " " (pr-str (seq (sort set)))))
res))))
;; TODO recheck when https://dev.clojure.org/jira/browse/TCHECK-128 is fixed
;; and share clj mixed-op-seq test, remove ops.cljc then.
#?(:cljs
(deftest manual-mixed-op-seq
(async done
(ha/go-try
(loop [[ops & r] recorded-ops]
(when ops
(is (ha/<? (ops-test ops 1000)))
(recur r)))
(done)))))
#?(:clj
(defn mixed-op-seq
"This is like the basic mixed-op-seq tests, but it also mixes in flushes to a konserve filestore"
[add-freq del-freq flush-freq universe-size num-ops]
(prop/for-all [ops (gen/vector (gen/frequency
[[add-freq (gen/tuple (gen/return :add)
(gen/no-shrink gen/small-integer))]
[flush-freq (gen/return [:flush])]
[del-freq (gen/tuple (gen/return :del)
(gen/no-shrink gen/small-integer))]])
num-ops)]
(ha/<?? (ops-test ops universe-size)))))
#?(:clj
(defspec test-many-keys-bigger-trees
100
(mixed-op-seq 800 200 10 1000 1000)))
#?(:cljs
(defn ^:export test-all [cb]
(defmethod cljs.test/report [:cljs.test/default :end-run-tests] [m]
(cb (clj->js m))
(if (cljs.test/successful? m)
(println "Success!")
(println "FAIL")))
(run-tests)))