PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 14ms app.codeStats 0ms

/test/principe_test.erl

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