/
core.clj
238 lines (219 loc) · 7.21 KB
/
core.clj
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
(ns clj-xmemcached.test.core
(:refer-clojure :exclude [get set replace])
(:use [clj-xmemcached.core])
(:use [clojure.test])
(:import (net.rubyeye.xmemcached MemcachedClient MemcachedClientBuilder XMemcachedClient CASOperation XMemcachedClientBuilder)
(net.rubyeye.xmemcached.impl KetamaMemcachedSessionLocator ArrayMemcachedSessionLocator)
(net.rubyeye.xmemcached.utils AddrUtil)
(net.rubyeye.xmemcached.command BinaryCommandFactory KestrelCommandFactory TextCommandFactory)
(java.net InetSocketAddress)))
(defmacro with-private-fns [[ns fns] & tests]
"Refers private fns from ns and runs tests in context."
`(let ~(reduce #(conj %1 %2 `(ns-resolve '~ns '~%2)) [] fns)
~@tests))
;;Test memcached servers,plz configure it by yourself on your machine.
(def test-servers (System/getProperty "memcached.servers" "localhost:11211"))
(with-private-fns [clj-xmemcached.core [make-command-factory make-session-locator]]
(deftest test-make-command-factory
(is (instance? TextCommandFactory (make-command-factory :text)))
(is (instance? BinaryCommandFactory (make-command-factory :binary)))
(is (instance? KestrelCommandFactory (make-command-factory :kestrel))))
(deftest test-make-session-locator
(is (instance? ArrayMemcachedSessionLocator (make-session-locator :standard)))
(is (instance? KetamaMemcachedSessionLocator (make-session-locator :ketama)))
(is (instance? KetamaMemcachedSessionLocator (make-session-locator :consistent)))))
(deftest test-xmemcached1
(let [cli (deref (memcached test-servers
:protocol :kestrel
:name "test"
:hash :ketama
:pool 10))]
(try
(is (= 10 (.. cli getConnector getSessionSet size)))
(is (= "Kestrel" (.. cli getProtocol name)))
(is (= "test" (.getName cli)))
(finally
(.shutdown cli)))))
(deftest test-xmemcached2
(let [cli (deref (memcached test-servers
:protocol :binary
:name "test"
:hash :standard
:timeout 1000
:pool 2))]
(try
(is (= 2 (.. cli getConnector getSessionSet size)))
(is (= "Binary" (.. cli getProtocol name)))
(is (= "test" (.getName cli)))
(finally
(.shutdown cli)))))
(deftest test-store-get-delete
(let [cli (memcached test-servers
:transcoder clj-json-transcoder)]
(with-client cli
(try
(is (add "a" 1))
(is (not (add "a" 2)))
(is (= 1 (get "a")))
(is (replace "a" 2))
(is (= 2 (get "a")))
(is (set "a" 3))
(is (= 3 (get "a")))
(is (delete "a"))
(is (nil? (get "a")))
(finally
(flush-all)
(shutdown))))))
(deftest test-nippy-transcoder
(let [cli (memcached test-servers
:transcoder nippy-transcoder)]
(with-client cli
(try
(is (add "a" 1))
(is (not (add "a" 2)))
(is (= 1 (get "a")))
(is (replace "a" 2))
(is (= 2 (get "a")))
(is (set "a" 3))
(is (= 3 (get "a")))
(is (delete "a"))
(is (nil? (get "a")))
(finally
(flush-all)
(shutdown))))))
(deftest test-set-client!
(let [cli (memcached test-servers)]
(set-client! cli)
(try
(is (add "a" 1))
(is (not (add "a" 2)))
(is (= 1 (get "a")))
(is (replace "a" 2))
(is (= 2 (get "a")))
(is (set "a" 3))
(is (= 3 (get "a")))
(is (delete "a"))
(is (nil? (get "a")))
(finally
(flush-all @cli)
(shutdown @cli)
(set-client! nil)))))
(deftest test-gets-delete
(let [cli (memcached test-servers :protocol :binary)]
(with-client cli
(try
(is (add "a" 1))
(is (:value (gets "a")))
(is (> (:cas (gets "a")) 0))
(is (not (delete "a" 1999)))
(is (delete "a" (:cas (gets "a"))))
(is (nil? (get "a")))
(finally
(flush-all)
(shutdown))))))
(deftest test-try-lock
(let [cli (memcached test-servers)
counter (atom 0)]
(with-client cli
(try
(future (try-lock "lock" 5 (do (Thread/sleep 3000)
(swap! counter inc))
(println "else1")))
(future (try-lock "lock" 5 (do (Thread/sleep 3000)
(swap! counter inc))
(println "else2")))
(future (try-lock "lock" 5 (do (Thread/sleep 3000)
(swap! counter inc))
(println "else3")))
(Thread/sleep 4000)
(is (nil? (get "lock")))
(is (= 1 @counter))
(finally
(flush-all)
(shutdown))))))
(deftest test-through
(let [cli (memcached test-servers)]
(with-client cli
(try
(through "a" 99)
(dorun (pmap #(through "a" %) [0 1 2 3 4]))
(is (= 99 (get "a")))
(finally
(flush-all)
(shutdown))))))
(deftest test-bulk-get
(let [cli (memcached test-servers)]
(with-client cli
(try
(is (set "key1" 1))
(is (add "key2" 2))
(is (set "key3" 3))
(let [rt (get "key1" "key2" "key3")]
(is (= 3 (count rt)))
(is (contains? rt "key1"))
(is (contains? rt "key2"))
(is (contains? rt "key3"))
(is (= 1 (clojure.core/get rt "key1")))
(is (= 2 (clojure.core/get rt "key2")))
(is (= 3 (clojure.core/get rt "key3"))))
(finally
(flush-all)
(shutdown))))))
(deftest test-expire
(let [cli (memcached test-servers)]
(with-client cli
(try
(is (add "a" 1 1))
(is (= 1 (get "a")))
(Thread/sleep 2000)
(is (nil? (get "a")))
(finally
(flush-all)
(shutdown))))))
(deftest test-incr-decr
(let [cli (memcached test-servers)]
(with-client cli
(try
(is (= 0 (incr "a" 1)))
(is (= 2 (incr "a" 2)))
(is (= 1 (decr "a" 1)))
(is (= "1" (get "a")))
(finally
(flush-all)
(shutdown))))))
(deftest test-incr-decr-with-expire
(let [cli (memcached test-servers)]
(with-client cli
(try
(is (= 0 (incr "a" 1 0 1)))
(is (= 2 (incr "a" 2)))
(Thread/sleep 2000)
(is (nil? (get "a")))
(finally
(flush-all)
(shutdown))))))
(deftest test-append-prepend
(let [cli (memcached test-servers)]
(with-client cli
(try
(is (not (append "a" "hello")))
(is (set "a" "dennis"))
(is (append "a" " zhuang"))
(is (= "dennis zhuang" (get "a")))
(is (prepend "a" "hello,"))
(is (= "hello,dennis zhuang" (get "a")))
(finally
(flush-all)
(shutdown))))))
(deftest test-cas
(let [cli (memcached test-servers)]
(with-client cli
(try
(is (set "key" "hello"))
(is (cas "key" #(str % " world")))
(is (= "hello world" (get "key")))
(is (cas "key" #(.hashCode %) 1))
(is (= (.hashCode "hello world") (get "key")))
(finally
(flush-all)
(shutdown))))))