emqx_request_responser_SUITE.erl 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. %% Copyright (c) 2013-2024 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. Apps = emqx_cth_suite:start([emqx], #{work_dir => emqx_cth_suite:work_dir(Config)}),
  22. [{apps, Apps} | Config].
  23. end_per_suite(Config) ->
  24. emqx_cth_suite:stop(?config(apps, Config)).
  25. all() ->
  26. emqx_common_test_helpers:all(?MODULE).
  27. t_request_response_qos0(_Config) ->
  28. request_response_per_qos(?QOS_0).
  29. t_request_response_qos1(_Config) ->
  30. request_response_per_qos(?QOS_1).
  31. t_request_response_qos2(_Config) ->
  32. request_response_per_qos(?QOS_2).
  33. request_response_per_qos(QoS) ->
  34. ReqTopic = <<"request_topic">>,
  35. RspTopic = <<"response_topic">>,
  36. {ok, Requester} = emqx_request_sender:start_link(
  37. RspTopic,
  38. QoS,
  39. [
  40. {proto_ver, v5},
  41. {clientid, <<"requester">>},
  42. {properties, #{'Request-Response-Information' => 1}}
  43. ]
  44. ),
  45. %% This is a square service
  46. Square = fun(_CorrData, ReqBin) ->
  47. I = b2i(ReqBin),
  48. i2b(I * I)
  49. end,
  50. {ok, Responder} = emqx_request_handler:start_link(
  51. ReqTopic,
  52. QoS,
  53. Square,
  54. [
  55. {proto_ver, v5},
  56. {clientid, <<"responder">>}
  57. ]
  58. ),
  59. ok = emqx_request_sender:send(Requester, ReqTopic, RspTopic, <<"corr-1">>, <<"2">>, QoS),
  60. receive
  61. {response, <<"corr-1">>, <<"4">>} ->
  62. ok;
  63. Other ->
  64. erlang:error({unexpected, Other})
  65. after 100 ->
  66. erlang:error(timeout)
  67. end,
  68. ok = emqx_request_sender:stop(Requester),
  69. ok = emqx_request_handler:stop(Responder).
  70. b2i(B) -> binary_to_integer(B).
  71. i2b(I) -> integer_to_binary(I).