asserts.hrl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. %% This file contains common macros for testing.
  17. %% It must not be used anywhere except in test suites.
  18. -include_lib("snabbkaffe/include/snabbkaffe.hrl").
  19. -define(assertWaitEvent(Code, EventMatch, Timeout),
  20. ?assertMatch(
  21. {_, {ok, EventMatch}},
  22. ?wait_async_action(
  23. Code,
  24. EventMatch,
  25. Timeout
  26. )
  27. )
  28. ).
  29. -define(drainMailbox(),
  30. (fun F__Flush_() ->
  31. receive
  32. X__Msg_ -> [X__Msg_ | F__Flush_()]
  33. after 0 -> []
  34. end
  35. end)()
  36. ).
  37. -define(assertReceive(PATTERN),
  38. ?assertReceive(PATTERN, 1000)
  39. ).
  40. -define(assertReceive(PATTERN, TIMEOUT),
  41. (fun() ->
  42. receive
  43. X__V = PATTERN -> X__V
  44. after TIMEOUT ->
  45. erlang:error(
  46. {assertReceive, [
  47. {module, ?MODULE},
  48. {line, ?LINE},
  49. {expression, (??PATTERN)},
  50. {mailbox, ?drainMailbox()}
  51. ]}
  52. )
  53. end
  54. end)()
  55. ).
  56. -define(retrying(CONFIG, NUM_RETRIES, TEST_BODY_FN), begin
  57. __TEST_CASE = ?FUNCTION_NAME,
  58. (fun
  59. __GO(__CONFIG, __N) when __N >= NUM_RETRIES ->
  60. TEST_BODY_FN(__CONFIG);
  61. __GO(__CONFIG, __N) ->
  62. try
  63. TEST_BODY_FN(__CONFIG)
  64. catch
  65. __KIND:__REASON:__STACKTRACE ->
  66. ct:pal("test errored; will retry\n ~p", [
  67. #{kind => __KIND, reason => __REASON, stacktrace => __STACKTRACE}
  68. ]),
  69. end_per_testcase(__TEST_CASE, __CONFIG),
  70. garbage_collect(),
  71. timer:sleep(1000),
  72. __CONFIG1 = init_per_testcase(__TEST_CASE, __CONFIG),
  73. __GO(__CONFIG1, __N + 1)
  74. end
  75. end)(
  76. CONFIG, 0
  77. )
  78. end).