/test/principe_test.erl
Erlang | 239 lines | 210 code | 23 blank | 6 comment | 0 complexity | 567a2e6291e25f98639590efe7572378 MD5 | raw file
1-module(principe_test). 2 3%% Simple test-suite for principe 4%% Would be nice if this could be an eunit test, but I can't seem to figure 5%% out how to make eunit play nice with parameterized modules. 6-export([test/0, test/1]). 7 8test() -> 9 test([]). 10 11test(ConnectParams) -> 12 ok = put_get_test(ConnectParams), 13 ok = put_get_random_test(ConnectParams, 1024), 14 ok = putkeep_test(ConnectParams), 15 ok = putcat_test(ConnectParams), 16 ok = putshl_test(ConnectParams), 17 ok = putnr_test(ConnectParams), 18 ok = out_test(ConnectParams), 19 ok = mget_test(ConnectParams), 20 ok = vsiz_test(ConnectParams), 21 ok = vanish_test(ConnectParams), 22 ok = iter_test(ConnectParams), 23 ok = fwmkeys_test(ConnectParams), 24 ok = addint_test(ConnectParams), 25 ok = sync_test(ConnectParams), 26 ok = rnum_test(ConnectParams), 27 ok = size_test(ConnectParams), 28 ok = stat_test(ConnectParams), 29 ok = optimize_test(ConnectParams), 30 ok = misc_test(ConnectParams), 31 ok. 32 33 34put_get_test(ConnectParams) -> 35 {ok, Socket} = principe:connect(ConnectParams), 36 ok = principe:put(Socket, "put_get1", "testval"), 37 ok = principe:put(Socket, <<"put_get2">>, <<32,145,56,0,14>>), 38 <<"testval">> = principe:get(Socket, <<"put_get1">>), 39 <<32, 145, 56, 0, 14>> = principe:get(Socket, "put_get2"), 40 case proplists:get_value(bigend, principe:stat(Socket)) of 41 "0" -> 42 ok = principe:put(Socket, <<"put_get3">>, 42, little), 43 <<42:32/little>> = principe:get(Socket, <<"put_get3">>); 44 "1" -> 45 ok = principe:put(Socket, <<"put_get3">>, 42, big), 46 <<42:32>> = principe:get(Socket, <<"put_get3">>) 47 end, 48 ok. 49 50put_get_random_test(ConnectParams, ElementCount) -> 51 crypto:start(), 52 {A1,A2,A3} = now(), 53 random:seed(A1, A2, A3), 54 {ok, Socket} = principe:connect(ConnectParams), 55 PutVals = lists:foldl(fun(_Seq, Acc) -> 56 KeySize = random:uniform(1024), 57 Key = crypto:rand_bytes(KeySize), 58 ValSize = random:uniform(65536), 59 Val = crypto:rand_bytes(ValSize), 60 principe:put(Socket, Key, Val), 61 [{Key, Val} | Acc] 62 end, [], lists:seq(1, ElementCount)), 63 lists:foreach(fun({K, V}) -> 64 V = principe:get(Socket, K) 65 end, PutVals), 66 ok. 67 68putkeep_test(ConnectParams) -> 69 {ok, Socket} = principe:connect(ConnectParams), 70 ok = principe:vanish(Socket), 71 ok = principe:put(Socket, <<"test">>, <<"foo">>), 72 <<"foo">> = principe:get(Socket, <<"test">>), 73 {error, _} = principe:putkeep(Socket, <<"test">>, <<"bar">>), 74 <<"foo">> = principe:get(Socket, <<"test">>), % no effect if key already exists before putkeep 75 ok = principe:putkeep(Socket, <<"another">>, <<"baz">>), 76 <<"baz">> = principe:get(Socket, <<"another">>), % puts the key if key does not exist already 77 ok. 78 79putcat_test(ConnectParams) -> 80 {ok, Socket} = principe:connect(ConnectParams), 81 ok = principe:vanish(Socket), 82 ok = principe:put(Socket, <<"putcat1">>, <<"foo">>), 83 % append "bar" to the end 84 ok = principe:putcat(Socket, <<"putcat1">>, <<"bar">>), 85 <<"foobar">> = principe:get(Socket, <<"putcat1">>), 86 ok. 87 88putshl_test(ConnectParams) -> 89 {ok, Socket} = principe:connect(ConnectParams), 90 ok = principe:vanish(Socket), 91 ok = principe:put(Socket, <<"putshl">>, <<"foo">>), 92 % append "bar" to the end and shift to the left to retain the width of "4" 93 ok = principe:putshl(Socket, <<"putshl">>, <<"bar">>, 4), 94 <<"obar">> = principe:get(Socket, <<"putshl">>), 95 ok. 96 97putnr_test(ConnectParams) -> 98 {ok, Socket} = principe:connect(ConnectParams), 99 ok = principe:vanish(Socket), 100 principe:putnr(Socket, <<"putnr1">>, <<"no reply">>), 101 <<"no reply">> = principe:get(Socket, <<"putnr1">>), 102 ok. 103 104out_test(ConnectParams) -> 105 {ok, Socket} = principe:connect(ConnectParams), 106 ok = principe:vanish(Socket), 107 ok = principe:put(Socket, <<"out1">>, <<"to remove">>), 108 <<"to remove">> = principe:get(Socket, <<"out1">>), 109 ok = principe:out(Socket, <<"out1">>), 110 {error, _} = principe:get(Socket, <<"out1">>), 111 ok. 112 113mget_test(ConnectParams) -> 114 {ok, Socket} = principe:connect(ConnectParams), 115 ok = principe:vanish(Socket), 116 ok = principe:put(Socket, <<"mget1">>, <<"alice">>), 117 ok = principe:put(Socket, <<"mget2">>, <<"bob">>), 118 ok = principe:put(Socket, <<"mget3">>, <<"carol">>), 119 ok = principe:put(Socket, <<"mget4">>, <<"trent">>), 120 [{<<"mget1">>, <<"alice">>}, 121 {<<"mget2">>, <<"bob">>}, 122 {<<"mget3">>, <<"carol">>}, 123 {<<"mget4">>, <<"trent">>}] = principe:mget(Socket, [<<"mget1">>, 124 <<"mget2">>, 125 <<"mget3">>, 126 <<"mget4">>]), 127 ok. 128 129vsiz_test(ConnectParams) -> 130 {ok, Socket} = principe:connect(ConnectParams), 131 ok = principe:vanish(Socket), 132 ok = principe:put(Socket, <<"vsiz1">>, <<"vsiz test">>), 133 9 = principe:vsiz(Socket, <<"vsiz1">>), 134 ok. 135 136vanish_test(ConnectParams) -> 137 {ok, Socket} = principe:connect(ConnectParams), 138 ok = principe:vanish(Socket), 139 ok = principe:put(Socket, <<"vanish1">>, <<"going away">>), 140 ok = principe:vanish(Socket), 141 {error, _} = principe:get(Socket, <<"vanish1">>), 142 ok. 143 144iter_test(ConnectParams) -> 145 {ok, Socket} = principe:connect(ConnectParams), 146 ok = principe:vanish(Socket), 147 ok = principe:put(Socket, <<"a">>, <<"first">>), 148 ok = principe:iterinit(Socket), 149 <<"a">> = principe:iternext(Socket), % "a" should be the first key 150 % Now to test a bit of real iteration 151 ok = principe:put(Socket, <<"b">>, <<"second">>), 152 ok = principe:put(Socket, <<"c">>, <<"third">>), 153 ok = principe:iterinit(Socket), 154 One = principe:iternext(Socket), 155 Two = principe:iternext(Socket), 156 Three = principe:iternext(Socket), 157 {error, _} = principe:iternext(Socket), 158 [<<"a">>, <<"b">>, <<"c">>] = lists:sort([One, Two, Three]), 159 ok. 160 161fwmkeys_test(ConnectParams) -> 162 {ok, Socket} = principe:connect(ConnectParams), 163 ok = principe:put(Socket, <<"fwmkeys1">>, <<"1">>), 164 ok = principe:put(Socket, <<"fwmkeys2">>, <<"2">>), 165 ok = principe:put(Socket, <<"fwmkeys3">>, <<"3">>), 166 ok = principe:put(Socket, <<"fwmkeys4">>, <<"4">>), 167 Keys1 = principe:fwmkeys(Socket, <<"fwmkeys">>, 4), 168 4 = length(Keys1), 169 true = lists:member(<<"fwmkeys1">>, Keys1), 170 true = lists:member(<<"fwmkeys2">>, Keys1), 171 true = lists:member(<<"fwmkeys3">>, Keys1), 172 true = lists:member(<<"fwmkeys4">>, Keys1), 173 Keys2 = principe:fwmkeys(Socket, <<"fwmkeys">>, 2), 174 2 = length(Keys2), 175 ok. 176 177addint_test(ConnectParams) -> 178 {ok, Socket} = principe:connect(ConnectParams), 179 case proplists:get_value(bigend, principe:stat(Socket)) of 180 "0" -> 181 principe:put(Socket, <<"addint1">>, 100, little); 182 "1" -> 183 principe:put(Socket, <<"addint1">>, 100) 184 end, 185 120 = principe:addint(Socket, <<"addint1">>, 20), 186 ok. 187 188sync_test(ConnectParams) -> 189 {ok, Socket} = principe:connect(ConnectParams), 190 ok = principe:sync(Socket), 191 ok. 192 193rnum_test(ConnectParams) -> 194 {ok, Socket} = principe:connect(ConnectParams), 195 ok = principe:vanish(Socket), 196 ok = principe:put(Socket, <<"rnum1">>, <<"foo">>), 197 ok = principe:put(Socket, <<"rnum2">>, <<"foo">>), 198 2 = principe:rnum(Socket), 199 ok = principe:vanish(Socket), 200 0 = principe:rnum(Socket), 201 ok. 202 203size_test(ConnectParams) -> 204 {ok, Socket} = principe:connect(ConnectParams), 205 principe:size(Socket), 206 ok. 207 208stat_test(ConnectParams) -> 209 {ok, Socket} = principe:connect(ConnectParams), 210 principe:stat(Socket), 211 ok. 212 213optimize_test(ConnectParams) -> 214 {ok, Socket} = principe:connect(ConnectParams), 215 OldSize = list_to_integer(proplists:get_value(size, principe:stat(Socket))), 216 ok = principe:optimize(Socket, "#bnum=1000000#opts=ld"), 217 NewSize = list_to_integer(proplists:get_value(size, principe:stat(Socket))), 218 true = NewSize > OldSize, 219 ok. 220 221misc_test(ConnectParams) -> 222 {ok, Socket} = principe:connect(ConnectParams), 223 ok = principe:vanish(Socket), 224 [] = principe:misc(Socket, "putlist", 225 ["key1", "value1", 226 "key2", "value2", 227 "key3", "value3", 228 "key4", "value4"]), 229 4 = principe:rnum(Socket), 230 <<"value1">> = principe:get(Socket, "key1"), 231 [] = principe:misc(Socket, "outlist", 232 ["key1", "key2", "key3"]), 233 1 = principe:rnum(Socket), 234 principe:put(Socket, "key5", "value5"), 235 GetlistOut = principe:misc(Socket, "getlist", ["key4", "key5"]), 236 true = lists:all(fun (K) -> lists:member(K, GetlistOut) end, 237 [<<"key4">>, <<"value4">>, 238 <<"key5">>, <<"value5">>]), 239 ok.