pgsql_tests.erl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. -module(pgsql_tests).
  2. -export([run_tests/0]).
  3. -include_lib("eunit/include/eunit.hrl").
  4. -include_lib("ssl/include/OTP-PKIX.hrl").
  5. -include("pgsql.hrl").
  6. -define(host, "localhost").
  7. -define(port, 5432).
  8. connect_test() ->
  9. connect_only([[]]).
  10. connect_to_db_test() ->
  11. connect_only([[{database, "epgsql_test_db1"}]]).
  12. connect_as_test() ->
  13. connect_only(["epgsql_test", [{database, "epgsql_test_db1"}]]).
  14. connect_with_cleartext_test() ->
  15. connect_only(["epgsql_test_cleartext",
  16. "epgsql_test_cleartext",
  17. [{database, "epgsql_test_db1"}]]).
  18. connect_with_md5_test() ->
  19. connect_only(["epgsql_test_md5",
  20. "epgsql_test_md5",
  21. [{database, "epgsql_test_db1"}]]).
  22. connect_with_invalid_password_test() ->
  23. {error, invalid_authorization_specification} =
  24. pgsql:connect(?host,
  25. "epgsql_test_md5",
  26. "epgsql_test_sha1",
  27. [{port, ?port}, {database, "epgsql_test_db1"}]).
  28. connect_with_ssl_test() ->
  29. lists:foreach(fun application:start/1, [crypto, ssl]),
  30. with_connection(
  31. fun(C) ->
  32. {ok, _Cols, [{true}]} = pgsql:equery(C, "select ssl_is_used()")
  33. end,
  34. "epgsql_test",
  35. [{ssl, true}]).
  36. connect_with_client_cert_test() ->
  37. lists:foreach(fun application:start/1, [crypto, ssl]),
  38. Dir = filename:join(filename:dirname(code:which(pgsql_tests)), "../test_data"),
  39. File = fun(Name) -> filename:join(Dir, Name) end,
  40. {ok, Cert} = ssl_pkix:decode_cert_file(File("epgsql.crt"), [pem, pkix]),
  41. #'TBSCertificate'{serialNumber = Serial} = Cert#'Certificate'.tbsCertificate,
  42. Serial2 = list_to_binary(integer_to_list(Serial)),
  43. with_connection(
  44. fun(C) ->
  45. {ok, _, [{true}]} = pgsql:equery(C, "select ssl_is_used()"),
  46. {ok, _, [{Serial2}]} = pgsql:equery(C, "select ssl_client_serial()")
  47. end,
  48. "epgsql_test_cert",
  49. [{ssl, true}, {keyfile, File("epgsql.key")}, {certfile, File("epgsql.crt")}]).
  50. select_test() ->
  51. with_connection(
  52. fun(C) ->
  53. {ok, Cols, Rows} = pgsql:squery(C, "select * from test_table1"),
  54. [#column{name = <<"id">>, type = int4, size = 4},
  55. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  56. [{<<"1">>, <<"one">>}, {<<"2">>, <<"two">>}] = Rows
  57. end).
  58. insert_test() ->
  59. with_rollback(
  60. fun(C) ->
  61. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')")
  62. end).
  63. update_test() ->
  64. with_rollback(
  65. fun(C) ->
  66. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  67. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  68. {ok, 2} = pgsql:squery(C, "update test_table1 set value = 'foo' where id > 2"),
  69. {ok, _, [{<<"2">>}]} = pgsql:squery(C, "select count(*) from test_table1 where value = 'foo'")
  70. end).
  71. delete_test() ->
  72. with_rollback(
  73. fun(C) ->
  74. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  75. {ok, 1} = pgsql:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  76. {ok, 2} = pgsql:squery(C, "delete from test_table1 where id > 2"),
  77. {ok, _, [{<<"2">>}]} = pgsql:squery(C, "select count(*) from test_table1")
  78. end).
  79. create_and_drop_table_test() ->
  80. with_rollback(
  81. fun(C) ->
  82. {ok, [], []} = pgsql:squery(C, "create table test_table3 (id int4)"),
  83. {ok, [#column{type = int4}], []} = pgsql:squery(C, "select * from test_table3"),
  84. {ok, [], []} = pgsql:squery(C, "drop table test_table3")
  85. end).
  86. cursor_test() ->
  87. with_connection(
  88. fun(C) ->
  89. {ok, [], []} = pgsql:squery(C, "begin"),
  90. {ok, [], []} = pgsql:squery(C, "declare c cursor for select id from test_table1"),
  91. {ok, 2} = pgsql:squery(C, "move forward 2 from c"),
  92. {ok, 1} = pgsql:squery(C, "move backward 1 from c"),
  93. {ok, 1, _Cols, [{<<"2">>}]} = pgsql:squery(C, "fetch next from c"),
  94. {ok, [], []} = pgsql:squery(C, "close c")
  95. end).
  96. multiple_result_test() ->
  97. with_connection(
  98. fun(C) ->
  99. [{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}] = pgsql:squery(C, "select 1; select 2"),
  100. [{ok, _, [{<<"1">>}]}, {error, #error{}}] = pgsql:squery(C, "select 1; select foo;")
  101. end).
  102. extended_select_test() ->
  103. with_connection(
  104. fun(C) ->
  105. {ok, Cols, Rows} = pgsql:equery(C, "select * from test_table1", []),
  106. [#column{name = <<"id">>, type = int4, size = 4},
  107. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  108. [{1, <<"one">>}, {2, <<"two">>}] = Rows
  109. end).
  110. extended_sync_ok_test() ->
  111. with_connection(
  112. fun(C) ->
  113. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1]),
  114. {ok, _Cols, [{<<"two">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [2])
  115. end).
  116. extended_sync_error_test() ->
  117. with_connection(
  118. fun(C) ->
  119. {error, #error{}} = pgsql:equery(C, "select _alue from test_table1 where id = $1", [1]),
  120. {ok, _Cols, [{<<"one">>}]} = pgsql:equery(C, "select value from test_table1 where id = $1", [1])
  121. end).
  122. returning_from_insert_test() ->
  123. with_rollback(
  124. fun(C) ->
  125. {ok, 1, _Cols, [{3}]} = pgsql:equery(C, "insert into test_table1 (id) values (3) returning id")
  126. end).
  127. returning_from_update_test() ->
  128. with_rollback(
  129. fun(C) ->
  130. {ok, 2, _Cols, [{1}, {2}]} = pgsql:equery(C, "update test_table1 set value = 'hi' returning id")
  131. end).
  132. returning_from_delete_test() ->
  133. with_rollback(
  134. fun(C) ->
  135. {ok, 2, _Cols, [{1}, {2}]} = pgsql:equery(C, "delete from test_table1 returning id")
  136. end).
  137. parse_test() ->
  138. with_connection(
  139. fun(C) ->
  140. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  141. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  142. ok = pgsql:close(C, S),
  143. ok = pgsql:sync(C)
  144. end).
  145. parse_column_format_test() ->
  146. with_connection(
  147. fun(C) ->
  148. {ok, S} = pgsql:parse(C, "select 1::int4, false::bool, 2.0::float4"),
  149. [#column{type = int4},
  150. #column{type = bool},
  151. #column{type = float4}] = S#statement.columns,
  152. ok = pgsql:bind(C, S, []),
  153. {ok, [{1, false, 2.0}]} = pgsql:execute(C, S, 0),
  154. ok = pgsql:close(C, S),
  155. ok = pgsql:sync(C)
  156. end).
  157. parse_error_test() ->
  158. with_connection(
  159. fun(C) ->
  160. {error, #error{}} = pgsql:parse(C, "select _ from test_table1"),
  161. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  162. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  163. ok = pgsql:close(C, S),
  164. ok = pgsql:sync(C)
  165. end).
  166. parse_and_close_test() ->
  167. with_connection(
  168. fun(C) ->
  169. Parse = fun() -> pgsql:parse(C, "test", "select * from test_table1", []) end,
  170. {ok, S} = Parse(),
  171. {error, #error{code = <<"42P05">>}} = Parse(),
  172. pgsql:close(C, S),
  173. {ok, S} = Parse(),
  174. ok = pgsql:sync(C)
  175. end).
  176. bind_test() ->
  177. with_connection(
  178. fun(C) ->
  179. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  180. ok = pgsql:bind(C, S, [1]),
  181. ok = pgsql:close(C, S),
  182. ok = pgsql:sync(C)
  183. end).
  184. bind_parameter_format_test() ->
  185. with_connection(
  186. fun(C) ->
  187. {ok, S} = pgsql:parse(C, "select $1, $2, $3", [int2, text, bool]),
  188. [int2, text, bool] = S#statement.types,
  189. ok = pgsql:bind(C, S, [1, "hi", true]),
  190. {ok, [{1, <<"hi">>, true}]} = pgsql:execute(C, S, 0),
  191. ok = pgsql:close(C, S),
  192. ok = pgsql:sync(C)
  193. end).
  194. bind_error_test() ->
  195. with_connection(
  196. fun(C) ->
  197. {ok, S} = pgsql:parse(C, "select $1::char"),
  198. {error, #error{}} = pgsql:bind(C, S, [0]),
  199. ok = pgsql:bind(C, S, [$A]),
  200. ok = pgsql:close(C, S),
  201. ok = pgsql:sync(C)
  202. end).
  203. bind_and_close_test() ->
  204. with_connection(
  205. fun(C) ->
  206. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  207. ok = pgsql:bind(C, S, "one", []),
  208. {error, #error{code = <<"42P03">>}} = pgsql:bind(C, S, "one", []),
  209. ok = pgsql:close(C, portal, "one"),
  210. ok = pgsql:bind(C, S, "one", []),
  211. ok = pgsql:sync(C)
  212. end).
  213. describe_test() ->
  214. with_connection(
  215. fun(C) ->
  216. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  217. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  218. {ok, S} = pgsql:describe(C, S),
  219. ok = pgsql:close(C, S),
  220. ok = pgsql:sync(C)
  221. end).
  222. describe_with_param_test() ->
  223. with_connection(
  224. fun(C) ->
  225. {ok, S} = pgsql:parse(C, "select id from test_table1 where id = $1"),
  226. [int4] = S#statement.types,
  227. [#column{name = <<"id">>}] = S#statement.columns,
  228. {ok, S} = pgsql:describe(C, S),
  229. ok = pgsql:close(C, S),
  230. ok = pgsql:sync(C)
  231. end).
  232. describe_named_test() ->
  233. with_connection(
  234. fun(C) ->
  235. {ok, S} = pgsql:parse(C, "name", "select * from test_table1", []),
  236. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  237. {ok, S} = pgsql:describe(C, S),
  238. ok = pgsql:close(C, S),
  239. ok = pgsql:sync(C)
  240. end).
  241. describe_error_test() ->
  242. with_connection(
  243. fun(C) ->
  244. {error, #error{}} = pgsql:describe(C, statement, ""),
  245. {ok, S} = pgsql:parse(C, "select * from test_table1"),
  246. {ok, S} = pgsql:describe(C, statement, ""),
  247. ok = pgsql:sync(C)
  248. end).
  249. portal_test() ->
  250. with_connection(
  251. fun(C) ->
  252. {ok, S} = pgsql:parse(C, "select value from test_table1"),
  253. ok = pgsql:bind(C, S, []),
  254. {partial, [{<<"one">>}]} = pgsql:execute(C, S, 1),
  255. {partial, [{<<"two">>}]} = pgsql:execute(C, S, 1),
  256. {ok, []} = pgsql:execute(C, S,1),
  257. ok = pgsql:close(C, S),
  258. ok = pgsql:sync(C)
  259. end).
  260. returning_test() ->
  261. with_rollback(
  262. fun(C) ->
  263. {ok, S} = pgsql:parse(C, "update test_table1 set value = $1 returning id"),
  264. ok = pgsql:bind(C, S, ["foo"]),
  265. {ok, 2, [{1}, {2}]} = pgsql:execute(C, S),
  266. ok = pgsql:sync(C)
  267. end).
  268. multiple_statement_test() ->
  269. with_connection(
  270. fun(C) ->
  271. {ok, S1} = pgsql:parse(C, "one", "select value from test_table1 where id = 1", []),
  272. ok = pgsql:bind(C, S1, []),
  273. {partial, [{<<"one">>}]} = pgsql:execute(C, S1, 1),
  274. {ok, S2} = pgsql:parse(C, "two", "select value from test_table1 where id = 2", []),
  275. ok = pgsql:bind(C, S2, []),
  276. {partial, [{<<"two">>}]} = pgsql:execute(C, S2, 1),
  277. {ok, []} = pgsql:execute(C, S1, 1),
  278. {ok, []} = pgsql:execute(C, S2, 1),
  279. ok = pgsql:close(C, S1),
  280. ok = pgsql:close(C, S2),
  281. ok = pgsql:sync(C)
  282. end).
  283. multiple_portal_test() ->
  284. with_connection(
  285. fun(C) ->
  286. {ok, S} = pgsql:parse(C, "select value from test_table1 where id = $1"),
  287. ok = pgsql:bind(C, S, "one", [1]),
  288. ok = pgsql:bind(C, S, "two", [2]),
  289. {ok, [{<<"one">>}]} = pgsql:execute(C, S, "one", 0),
  290. {ok, [{<<"two">>}]} = pgsql:execute(C, S, "two", 0),
  291. ok = pgsql:close(C, S),
  292. ok = pgsql:sync(C)
  293. end).
  294. execute_function_test() ->
  295. with_rollback(
  296. fun(C) ->
  297. {ok, _Cols1, [{3}]} = pgsql:equery(C, "select insert_test1(3, 'three')"),
  298. {ok, _Cols2, [{<<>>}]} = pgsql:equery(C, "select do_nothing()")
  299. end).
  300. parameter_get_test() ->
  301. with_connection(
  302. fun(C) ->
  303. {ok, <<"off">>} = pgsql:get_parameter(C, "is_superuser")
  304. end).
  305. parameter_set_test() ->
  306. with_connection(
  307. fun(C) ->
  308. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'ISO, MDY'"),
  309. {ok, <<"ISO, MDY">>} = pgsql:get_parameter(C, "DateStyle"),
  310. {ok, _Cols, [{<<"2000-01-02">>}]} = pgsql:squery(C, "select '2000-01-02'::date"),
  311. {ok, [], []} = pgsql:squery(C, "set DateStyle = 'German'"),
  312. {ok, <<"German, DMY">>} = pgsql:get_parameter(C, "DateStyle"),
  313. {ok, _Cols, [{<<"02.01.2000">>}]} = pgsql:squery(C, "select '2000-01-02'::date")
  314. end).
  315. numeric_type_test() ->
  316. check_type(int2, "1", 1, [0, 256, -32768, +32767]),
  317. check_type(int4, "1", 1, [0, 512, -2147483648, +2147483647]),
  318. check_type(int8, "1", 1, [0, 1024, -9223372036854775808, +9223372036854775807]),
  319. check_type(float4, "1.0", 1.0, [0.0, 1.23456, -1.23456]),
  320. check_type(float8, "1.0", 1.0, [0.0, 1.23456789012345, -1.23456789012345]).
  321. character_type_test() ->
  322. check_type(bpchar, "'A'", $A, [1, $1, 255], "c_char"),
  323. check_type(text, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  324. check_type(varchar, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]).
  325. date_time_type_test() ->
  326. with_connection(
  327. fun(C) ->
  328. case pgsql:get_parameter(C, "integer_datetimes") of
  329. {ok, <<"on">>} -> MaxTsDate = 294276;
  330. {ok, <<"off">>} -> MaxTsDate = 5874897
  331. end,
  332. check_type(date, "'2008-01-02'", {2008,1,2}, [{-4712,1,1}, {5874897,1,1}]),
  333. check_type(time, "'00:01:02'", {0,1,2.0}, [{0,0,0.0}, {24,0,0.0}]),
  334. check_type(timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60},
  335. [{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]),
  336. check_type(timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}},
  337. [{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}]),
  338. check_type(interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0},
  339. [{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}])
  340. end).
  341. misc_type_test() ->
  342. check_type(bool, "true", true, [true, false]),
  343. check_type(bytea, "E'\001\002'", <<1,2>>, [<<>>, <<0,128,255>>]).
  344. text_format_test() ->
  345. with_connection(
  346. fun(C) ->
  347. Select = fun(Type, V) ->
  348. V2 = list_to_binary(V),
  349. Query = "select $1::" ++ Type,
  350. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V]),
  351. {ok, _Cols, [{V2}]} = pgsql:equery(C, Query, [V2])
  352. end,
  353. Select("inet", "127.0.0.1"),
  354. Select("numeric", "123456")
  355. end).
  356. connect_timeout_test() ->
  357. {error, timeout} = pgsql:connect(?host, [{port, ?port}, {timeout, 0}]).
  358. query_timeout_test() ->
  359. with_connection(
  360. fun(C) ->
  361. {error, timeout} = pgsql:squery(C, "select pg_sleep(1)"),
  362. {error, timeout} = pgsql:equery(C, "select pg_sleep(2)"),
  363. {ok, _Cols, [{1}]} = pgsql:equery(C, "select 1")
  364. end,
  365. [{timeout, 10}]).
  366. execute_timeout_test() ->
  367. with_connection(
  368. fun(C) ->
  369. {ok, S} = pgsql:parse(C, "select pg_sleep($1)"),
  370. ok = pgsql:bind(C, S, [2]),
  371. {error, timeout} = pgsql:execute(C, S, 0),
  372. ok = pgsql:bind(C, S, [0]),
  373. {ok, [{<<>>}]} = pgsql:execute(C, S, 0),
  374. ok = pgsql:close(C, S),
  375. ok = pgsql:sync(C)
  376. end,
  377. [{timeout, 10}]).
  378. connection_closed_test() ->
  379. P = self(),
  380. F = fun() ->
  381. process_flag(trap_exit, true),
  382. {ok, C} = pgsql:connect(?host, [{port, ?port}]),
  383. P ! {connected, C},
  384. receive
  385. Any -> P ! Any
  386. end
  387. end,
  388. spawn_link(F),
  389. receive
  390. {connected, C} ->
  391. timer:sleep(100),
  392. pgsql:close(C),
  393. {'EXIT', C, _} = receive R -> R end
  394. end,
  395. flush().
  396. active_connection_closed_test() ->
  397. P = self(),
  398. F = fun() ->
  399. process_flag(trap_exit, true),
  400. {ok, C} = pgsql:connect(?host, [{port, ?port}]),
  401. P ! {connected, C},
  402. R = pgsql:squery(C, "select pg_sleep(10)"),
  403. P ! R
  404. end,
  405. spawn_link(F),
  406. receive
  407. {connected, C} ->
  408. timer:sleep(100),
  409. pgsql:close(C),
  410. {error, closed} = receive R -> R end
  411. end,
  412. flush().
  413. %% -- run all tests --
  414. run_tests() ->
  415. Files = filelib:wildcard("test_ebin/*tests.beam"),
  416. Mods = [list_to_atom(filename:basename(F, ".beam")) || F <- Files],
  417. eunit:test(Mods, []).
  418. %% -- internal functions --
  419. connect_only(Args) ->
  420. {ok, C} = apply(pgsql, connect, [?host, [{port, ?port} | Args]]),
  421. pgsql:close(C),
  422. flush().
  423. with_connection(F) ->
  424. with_connection(F, "epgsql_test", []).
  425. with_connection(F, Args) ->
  426. with_connection(F, "epgsql_test", Args).
  427. with_connection(F, Username, Args) ->
  428. Args2 = [{port, ?port}, {database, "epgsql_test_db1"} | Args],
  429. {ok, C} = pgsql:connect(?host, Username, Args2),
  430. try
  431. F(C)
  432. after
  433. pgsql:close(C)
  434. end,
  435. flush().
  436. with_rollback(F) ->
  437. with_connection(
  438. fun(C) ->
  439. try
  440. pgsql:squery(C, "begin"),
  441. F(C)
  442. after
  443. pgsql:squery(C, "rollback")
  444. end
  445. end).
  446. check_type(Type, In, Out, Values) ->
  447. Column = "c_" ++ atom_to_list(Type),
  448. check_type(Type, In, Out, Values, Column).
  449. check_type(Type, In, Out, Values, Column) ->
  450. with_connection(
  451. fun(C) ->
  452. Select = io_lib:format("select ~s::~w", [In, Type]),
  453. {ok, [#column{type = Type}], [{Out}]} = pgsql:equery(C, Select),
  454. Sql = io_lib:format("insert into test_table2 (~s) values ($1) returning ~s", [Column, Column]),
  455. {ok, #statement{columns = [#column{type = Type}]} = S} = pgsql:parse(C, Sql),
  456. Insert = fun(V) ->
  457. pgsql:bind(C, S, [V]),
  458. {ok, 1, [{V2}]} = pgsql:execute(C, S),
  459. case compare(Type, V, V2) of
  460. true -> ok;
  461. false -> ?debugFmt("~p =/= ~p~n", [V, V2]), ?assert(false)
  462. end,
  463. ok = pgsql:sync(C)
  464. end,
  465. lists:foreach(Insert, [null | Values])
  466. end).
  467. compare(_Type, null, null) -> true;
  468. compare(float4, V1, V2) -> abs(V2 - V1) < 0.000001;
  469. compare(float8, V1, V2) -> abs(V2 - V1) < 0.000000000000001;
  470. compare(_Type, V1, V2) -> V1 =:= V2.
  471. %% flush mailbox
  472. flush() ->
  473. ?assertEqual([], flush([])).
  474. flush(Acc) ->
  475. receive
  476. {'EXIT', _Pid, normal} -> flush(Acc);
  477. M -> flush([M | Acc])
  478. after
  479. 0 -> lists:reverse(Acc)
  480. end.