asserts.hrl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(assertNotReceive(PATTERN),
  57. ?assertNotReceive(PATTERN, 300)
  58. ).
  59. -define(assertNotReceive(PATTERN, TIMEOUT),
  60. (fun() ->
  61. receive
  62. X__V = PATTERN ->
  63. erlang:error(
  64. {assertNotReceive, [
  65. {module, ?MODULE},
  66. {line, ?LINE},
  67. {expression, (??PATTERN)},
  68. {message, X__V}
  69. ]}
  70. )
  71. after TIMEOUT ->
  72. ok
  73. end
  74. end)()
  75. ).
  76. -define(retrying(CONFIG, NUM_RETRIES, TEST_BODY_FN), begin
  77. __TEST_CASE = ?FUNCTION_NAME,
  78. (fun
  79. __GO(__CONFIG, __N) when __N >= NUM_RETRIES ->
  80. TEST_BODY_FN(__CONFIG);
  81. __GO(__CONFIG, __N) ->
  82. try
  83. TEST_BODY_FN(__CONFIG)
  84. catch
  85. __KIND:__REASON:__STACKTRACE ->
  86. ct:pal("test errored; will retry\n ~p", [
  87. #{kind => __KIND, reason => __REASON, stacktrace => __STACKTRACE}
  88. ]),
  89. end_per_testcase(__TEST_CASE, __CONFIG),
  90. garbage_collect(),
  91. timer:sleep(1000),
  92. __CONFIG1 = init_per_testcase(__TEST_CASE, __CONFIG),
  93. __GO(__CONFIG1, __N + 1)
  94. end
  95. end)(
  96. CONFIG, 0
  97. )
  98. end).