emqx_request_responser_SUITE.erl 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. %% Copyright (c) 2013-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  2. %%
  3. %% Licensed under the Apache License, Version 2.0 (the "License");
  4. %% you may not use this file except in compliance with the License.
  5. %% You may obtain a copy of the License at
  6. %%
  7. %% http://www.apache.org/licenses/LICENSE-2.0
  8. %%
  9. %% Unless required by applicable law or agreed to in writing, software
  10. %% distributed under the License is distributed on an "AS IS" BASIS,
  11. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. %% See the License for the specific language governing permissions and
  13. %% limitations under the License.
  14. -module(emqx_request_responser_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -include_lib("emqx/include/emqx_mqtt.hrl").
  18. -include_lib("eunit/include/eunit.hrl").
  19. -include_lib("common_test/include/ct.hrl").
  20. init_per_suite(Config) ->
  21. emqx_common_test_helpers:boot_modules(all),
  22. emqx_common_test_helpers:start_apps([]),
  23. Config.
  24. end_per_suite(_Config) ->
  25. emqx_common_test_helpers:stop_apps([]).
  26. all() ->
  27. emqx_common_test_helpers:all(?MODULE).
  28. t_request_response_qos0(_Config) ->
  29. request_response_per_qos(?QOS_0).
  30. t_request_response_qos1(_Config) ->
  31. request_response_per_qos(?QOS_1).
  32. t_request_response_qos2(_Config) ->
  33. request_response_per_qos(?QOS_2).
  34. request_response_per_qos(QoS) ->
  35. ReqTopic = <<"request_topic">>,
  36. RspTopic = <<"response_topic">>,
  37. {ok, Requester} = emqx_request_sender:start_link(
  38. RspTopic,
  39. QoS,
  40. [
  41. {proto_ver, v5},
  42. {clientid, <<"requester">>},
  43. {properties, #{'Request-Response-Information' => 1}}
  44. ]
  45. ),
  46. %% This is a square service
  47. Square = fun(_CorrData, ReqBin) ->
  48. I = b2i(ReqBin),
  49. i2b(I * I)
  50. end,
  51. {ok, Responder} = emqx_request_handler:start_link(
  52. ReqTopic,
  53. QoS,
  54. Square,
  55. [
  56. {proto_ver, v5},
  57. {clientid, <<"responder">>}
  58. ]
  59. ),
  60. ok = emqx_request_sender:send(Requester, ReqTopic, RspTopic, <<"corr-1">>, <<"2">>, QoS),
  61. receive
  62. {response, <<"corr-1">>, <<"4">>} ->
  63. ok;
  64. Other ->
  65. erlang:error({unexpected, Other})
  66. after 100 ->
  67. erlang:error(timeout)
  68. end,
  69. ok = emqx_request_sender:stop(Requester),
  70. ok = emqx_request_handler:stop(Responder).
  71. b2i(B) -> binary_to_integer(B).
  72. i2b(I) -> integer_to_binary(I).