-
Notifications
You must be signed in to change notification settings - Fork 2
/
memcached_test.ml
189 lines (157 loc) · 5.17 KB
/
memcached_test.ml
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
open Core.Std
open Async.Std
module Ints = struct
type t = int
let to_string = string_of_int
let of_string = int_of_string
end
module IntCache = Memcached.Make(Ints)
let int_suite = Test_async.(
create ~before_all:(fun () ->
IntCache.connect "localhost" 11212
)
~before:(fun cache ->
return cache
)
~after:(fun cache ->
IntCache.flush cache >>= fun _ ->
Deferred.unit
)
~after_all:(fun cache ->
IntCache.close cache
)
|> test "set" (fun cache ->
Bool.assert_equal (IntCache.set cache "foo" 1) true
)
|> test "get" (fun cache ->
IntCache.set cache "foo" 1 >>= fun _ ->
Int.assert_equal (IntCache.get cache "foo") 1
)
|> test "get missing" (fun cache ->
assert_error (IntCache.get cache "foo") "Not found"
)
|> test "flush" (fun cache ->
IntCache.set cache "foo" 1 >>= fun _ ->
IntCache.flush cache >>= fun _ ->
assert_error (IntCache.get cache "foo") "Not found"
)
|> test "delete" (fun cache ->
IntCache.set cache "foo" 1 >>= fun _ ->
IntCache.delete cache "foo" >>= fun _ ->
assert_error (IntCache.get cache "foo") "Not found"
)
|> test "incr initial" (fun cache ->
Int64.assert_equal (IntCache.incr cache ~key:"foo" ~initial:0L ~delta:1L) 0L
)
|> test "incr" (fun cache ->
Int64.assert_equal (IntCache.incr cache ~key:"foo" ~initial:0L ~delta:1L) 0L
>>= fun _ ->
Int64.assert_equal (IntCache.incr cache ~key:"foo" ~initial:0L ~delta:1L) 1L
)
|> test "set then incr" (fun cache ->
Bool.assert_equal (IntCache.set cache "foo" 1) true >>= fun _ ->
Int64.assert_equal (IntCache.incr cache ~key:"foo" ~initial:0L ~delta:2L) 3L
)
|> test "decr initial" (fun cache ->
Int64.assert_equal (IntCache.decr cache ~key:"foo" ~initial:2L ~delta:1L) 2L
)
|> test "decr" (fun cache ->
Int64.assert_equal (IntCache.decr cache ~key:"foo" ~initial:2L ~delta:1L) 2L
>>= fun _ ->
Int64.assert_equal (IntCache.decr cache ~key:"foo" ~initial:2L ~delta:1L) 1L
)
|> test "set then decr" (fun cache ->
Bool.assert_equal (IntCache.set cache "foo" 5) true
>>= fun _ ->
Int64.assert_equal (IntCache.decr cache ~key:"foo" ~initial:0L ~delta:2L) 3L
)
|> test "add" (fun cache ->
Bool.assert_equal (IntCache.add cache "foo" 3) true
)
|> test "add existing" (fun cache ->
IntCache.set cache "foo" 3 >>= fun _ ->
Bool.assert_equal (IntCache.add cache "foo" 3) false
)
|> test "replace" (fun cache ->
IntCache.set cache "foo" 3 >>= fun _ ->
Bool.assert_equal (IntCache.replace cache "foo" 3) true
)
|> test "replace missing" (fun cache ->
Bool.assert_equal (IntCache.replace cache "foo" 3) false
)
|> test "prepend missing" (fun cache ->
Bool.assert_equal (IntCache.prepend cache "foo" 1) false
>>= fun _ ->
assert_error (IntCache.get cache "foo") "Not found"
)
|> test "prepend present" (fun cache ->
IntCache.set cache "foo" 1 >>= fun _ ->
IntCache.prepend cache "foo" 2 >>= fun _ ->
Int.assert_equal (IntCache.get cache "foo") 21
)
|> test "append missing" (fun cache ->
Bool.assert_equal (IntCache.append cache "foo" 1) false
>>= fun _ ->
assert_error (IntCache.get cache "foo") "Not found"
)
|> test "append present" (fun cache ->
IntCache.set cache "foo" 1 >>= fun _ ->
IntCache.append cache "foo" 2 >>= fun _ ->
Int.assert_equal (IntCache.get cache "foo") 12
)
)
module Strings = struct
type t = string
let to_string = ident
let of_string = ident
end
module StringCache = Memcached.Make(Strings)
let string_suite = Test_async.(
create ~before_all:(fun () ->
StringCache.connect "localhost" 11212
)
~before:(fun cache ->
return cache
)
~after:(fun cache ->
StringCache.flush cache
>>= fun _ -> Deferred.unit
)
~after_all:(fun cache ->
StringCache.close cache
)
|> test "set" (fun cache ->
Bool.assert_equal (StringCache.set cache "foo" "bar") true
)
|> test "set then get" (fun cache ->
StringCache.set cache "foo" "bar" >>= fun _ ->
String.assert_equal (StringCache.get cache "foo") "bar"
)
|> test "prepend missing" (fun cache ->
Bool.assert_equal (StringCache.prepend cache "foo" "bar") false
>>= fun _ ->
assert_error (StringCache.get cache "foo") "Not found"
)
|> test "prepend present" (fun cache ->
StringCache.set cache "foo" "bar" >>= fun _ ->
StringCache.prepend cache "foo" "baz" >>= fun _ ->
String.assert_equal (StringCache.get cache "foo") "bazbar"
)
|> test "append missing" (fun cache ->
Bool.assert_equal (StringCache.append cache "foo" "bar") false
>>= fun _ ->
assert_error (StringCache.get cache "foo") "Not found"
)
|> test "append present" (fun cache ->
StringCache.set cache "foo" "bar" >>= fun _ ->
StringCache.append cache "foo" "baz" >>= fun _ ->
String.assert_equal (StringCache.get cache "foo") "barbaz"
)
)
let main () =
Test_async.run int_suite >>> fun _ ->
Test_async.run string_suite >>> fun _ ->
Async_unix.Shutdown.shutdown 1
in
main ();
Scheduler.go ()