/
cberl_tests.erl
97 lines (86 loc) · 3.19 KB
/
cberl_tests.erl
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
-module(cberl_tests).
-include_lib("eunit/include/eunit.hrl").
-define(POOLNAME, testpool).
cberl_test_() ->
[{foreach, fun setup/0, fun clean_up/1,
[fun test_set_and_get/1,
fun test_replace_add/1,
fun test_get_and_touch/1,
fun test_append_prepend/1,
fun test_remove/1,
fun test_lock/1]}].
%%%===================================================================
%%% Setup / Teardown
%%%===================================================================
setup() ->
cberl:start_link(?POOLNAME, 3),
ok.
clean_up(_) ->
cberl:remove(?POOLNAME, <<"testkey">>),
cberl:remove(?POOLNAME, <<"testkey1">>),
cberl:remove(?POOLNAME, <<"notestkey">>),
cberl:stop(?POOLNAME).
%%%===================================================================
%%% Tests
%%%===================================================================
test_set_and_get(_) ->
Key = <<"testkey">>,
Value = "testval",
ok = cberl:set(?POOLNAME, Key, 0, Value),
Get1 = cberl:get(?POOLNAME, Key),
ok = cberl:set(?POOLNAME, Key, 0, Value, json),
Get2 = cberl:get(?POOLNAME, Key),
ok = cberl:set(?POOLNAME, Key, 0, Value, raw_binary),
Get3 = cberl:get(?POOLNAME, Key),
[?_assertMatch({Key, _, Value}, Get1),
?_assertMatch({Key, _, Value}, Get2),
?_assertMatch({Key, _, Value}, Get3)
].
test_replace_add(_) ->
Key = <<"testkey">>,
Value = "testval",
ok = cberl:set(?POOLNAME, Key, 0, Value),
AddFail = cberl:add(?POOLNAME, Key, 0, Value),
AddPass = cberl:add(?POOLNAME, <<"testkey1">>, 0, Value),
ReplaceFail = cberl:replace(?POOLNAME, <<"notestkey">>, 0, Value),
ok = cberl:replace(?POOLNAME, Key, 0, "testval1"),
Get1 = cberl:get(?POOLNAME, Key),
[?_assertEqual({error, key_eexists}, AddFail),
?_assertEqual(ok, AddPass),
?_assertEqual({error, key_enoent}, ReplaceFail),
?_assertMatch({Key, _, "testval1"}, Get1)
].
test_append_prepend(_) ->
Key = <<"testkey">>,
ok = cberl:set(?POOLNAME, Key, 0, "base", str),
ok = cberl:append(?POOLNAME, 0, Key, "tail"),
Get1 = cberl:get(?POOLNAME, Key),
ok = cberl:prepend(?POOLNAME, 0, Key, "head"),
Get2 = cberl:get(?POOLNAME, Key),
[?_assertMatch({Key, _, "basetail"}, Get1),
?_assertMatch({Key, _, "headbasetail"}, Get2)
].
test_get_and_touch(_) ->
Key = <<"testkey">>,
Value = "testval",
ok = cberl:set(?POOLNAME, Key, 0, Value),
cberl:get_and_touch(?POOLNAME, Key, 1),
timer:sleep(5000),
[?_assertEqual({Key, {error,key_enoent}}, cberl:get(?POOLNAME, Key))].
test_remove(_) ->
Key = <<"testkey">>,
Value = "testval",
ok = cberl:set(?POOLNAME, Key, 0, Value),
ok = cberl:remove(?POOLNAME, Key),
[?_assertEqual({Key, {error,key_enoent}}, cberl:get(?POOLNAME, Key))].
test_lock(_) ->
Key = <<"testkey">>,
Value = "testval",
Value2 = "testval2",
ok = cberl:set(?POOLNAME, Key, 0, Value),
{Key, CAS, _Exp} = cberl:get_and_lock(?POOLNAME, Key, 100000),
fun () ->
[?assertEqual({error,key_eexists}, cberl:set(?POOLNAME, Key, 0, Value2)),
?assertEqual(ok, cberl:unlock(?POOLNAME, Key, CAS)),
?assertEqual(ok, cberl:set(?POOLNAME, Key, 0, Value2))]
end.