prop_emqx_schema.erl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. -module(prop_emqx_schema).
  17. -include_lib("proper/include/proper.hrl").
  18. -include_lib("eunit/include/eunit.hrl").
  19. -define(MAX_INT_TIMEOUT_MS, 4294967295).
  20. %%--------------------------------------------------------------------
  21. %% Helper fns
  22. %%--------------------------------------------------------------------
  23. parse(Value, Type) ->
  24. typerefl:from_string(Type, Value).
  25. timeout_within_bounds(RawDuration) ->
  26. case emqx_schema:to_duration_ms(RawDuration) of
  27. {ok, I} when I =< ?MAX_INT_TIMEOUT_MS ->
  28. true;
  29. _ ->
  30. false
  31. end.
  32. parses_the_same(Value, Type1, Type2) ->
  33. parse(Value, Type1) =:= parse(Value, Type2).
  34. %%--------------------------------------------------------------------
  35. %% Properties
  36. %%--------------------------------------------------------------------
  37. prop_timeout_duration_refines_duration() ->
  38. ?FORALL(
  39. RawDuration,
  40. emqx_proper_types:raw_duration(),
  41. ?IMPLIES(
  42. timeout_within_bounds(RawDuration),
  43. parses_the_same(RawDuration, emqx_schema:duration(), emqx_schema:timeout_duration())
  44. )
  45. ).
  46. prop_timeout_duration_ms_refines_duration_ms() ->
  47. ?FORALL(
  48. RawDuration,
  49. emqx_proper_types:raw_duration(),
  50. ?IMPLIES(
  51. timeout_within_bounds(RawDuration),
  52. parses_the_same(
  53. RawDuration, emqx_schema:duration_ms(), emqx_schema:timeout_duration_ms()
  54. )
  55. )
  56. ).
  57. prop_timeout_duration_s_refines_duration_s() ->
  58. ?FORALL(
  59. RawDuration,
  60. emqx_proper_types:raw_duration(),
  61. ?IMPLIES(
  62. timeout_within_bounds(RawDuration),
  63. parses_the_same(RawDuration, emqx_schema:duration_s(), emqx_schema:timeout_duration_s())
  64. )
  65. ).
  66. prop_timeout_duration_is_valid_for_receive_after() ->
  67. ?FORALL(
  68. RawDuration,
  69. emqx_proper_types:large_raw_duration(),
  70. ?IMPLIES(
  71. not timeout_within_bounds(RawDuration),
  72. begin
  73. %% we have to use the the non-strict version, because it's invalid
  74. {ok, Timeout} = parse(RawDuration, emqx_schema:duration()),
  75. Ref = make_ref(),
  76. timer:send_after(20, {Ref, ok}),
  77. ?assertError(
  78. timeout_value,
  79. receive
  80. {Ref, ok} -> error(should_be_invalid)
  81. after Timeout -> error(should_be_invalid)
  82. end
  83. ),
  84. true
  85. end
  86. )
  87. ).