asserts.hrl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2023-2024 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(), ?drainMailbox(0)).
  30. -define(drainMailbox(TIMEOUT),
  31. (fun F__Flush_() ->
  32. receive
  33. X__Msg_ -> [X__Msg_ | F__Flush_()]
  34. after TIMEOUT -> []
  35. end
  36. end)()
  37. ).
  38. -define(assertReceive(PATTERN),
  39. ?assertReceive(PATTERN, 1000)
  40. ).
  41. -define(assertReceive(PATTERN, TIMEOUT),
  42. ?assertReceive(PATTERN, TIMEOUT, #{})
  43. ).
  44. -define(assertReceive(PATTERN, TIMEOUT, EXTRA),
  45. (fun() ->
  46. receive
  47. X__V = PATTERN -> X__V
  48. after TIMEOUT ->
  49. erlang:error(
  50. {assertReceive, [
  51. {module, ?MODULE},
  52. {line, ?LINE},
  53. {expression, (??PATTERN)},
  54. {mailbox, ?drainMailbox()},
  55. {extra_info, EXTRA}
  56. ]}
  57. )
  58. end
  59. end)()
  60. ).
  61. -define(assertNotReceive(PATTERN),
  62. ?assertNotReceive(PATTERN, 300)
  63. ).
  64. -define(assertNotReceive(PATTERN, TIMEOUT),
  65. (fun() ->
  66. receive
  67. X__V = PATTERN ->
  68. erlang:error(
  69. {assertNotReceive, [
  70. {module, ?MODULE},
  71. {line, ?LINE},
  72. {expression, (??PATTERN)},
  73. {message, X__V}
  74. ]}
  75. )
  76. after TIMEOUT ->
  77. ok
  78. end
  79. end)()
  80. ).
  81. -define(assertExceptionOneOf(CT1, CT2, EXPR),
  82. (fun() ->
  83. X__Attrs = [
  84. {module, ?MODULE},
  85. {line, ?LINE},
  86. {expression, (??EXPR)},
  87. {pattern, "[ " ++ (??CT1) ++ ", " ++ (??CT2) ++ " ]"}
  88. ],
  89. X__Exc =
  90. try (EXPR) of
  91. X__V -> erlang:error({assertException, [{unexpected_success, X__V} | X__Attrs]})
  92. catch
  93. X__C:X__T:X__S -> {X__C, X__T, X__S}
  94. end,
  95. case {element(1, X__Exc), element(2, X__Exc)} of
  96. CT1 -> ok;
  97. CT2 -> ok;
  98. _ -> erlang:error({assertException, [{unexpected_exception, X__Exc} | X__Attrs]})
  99. end
  100. end)()
  101. ).
  102. -define(retrying(CONFIG, NUM_RETRIES, TEST_BODY_FN), begin
  103. __TEST_CASE = ?FUNCTION_NAME,
  104. (fun
  105. __GO(__CONFIG, __N) when __N >= NUM_RETRIES ->
  106. TEST_BODY_FN(__CONFIG);
  107. __GO(__CONFIG, __N) ->
  108. try
  109. TEST_BODY_FN(__CONFIG)
  110. catch
  111. __KIND:__REASON:__STACKTRACE ->
  112. ct:pal("test errored; will retry\n ~p", [
  113. #{kind => __KIND, reason => __REASON, stacktrace => __STACKTRACE}
  114. ]),
  115. end_per_testcase(__TEST_CASE, __CONFIG),
  116. garbage_collect(),
  117. timer:sleep(1000),
  118. __CONFIG1 = init_per_testcase(__TEST_CASE, __CONFIG),
  119. __GO(__CONFIG1, __N + 1)
  120. end
  121. end)(
  122. CONFIG, 0
  123. )
  124. end).