asserts.hrl 3.8 KB

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