emqx_machine_tests.erl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-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. -module(emqx_machine_tests).
  17. -include_lib("eunit/include/eunit.hrl").
  18. sorted_reboot_apps_test_() ->
  19. Apps1 = [
  20. {1, [2, 3, 4]},
  21. {2, [3, 4]}
  22. ],
  23. Apps2 = [
  24. {1, [2, 3, 4]},
  25. {2, [3, 4]},
  26. {5, [4, 3, 2, 1, 1]}
  27. ],
  28. [
  29. fun() -> check_order(Apps1) end,
  30. fun() -> check_order(Apps2) end
  31. ].
  32. sorted_reboot_apps_cycle_test() ->
  33. Apps = [{1, [2]}, {2, [1, 3]}],
  34. ?assertError(
  35. {circular_application_dependency, [[1, 2, 1], [2, 1, 2]]},
  36. check_order(Apps)
  37. ).
  38. check_order(Apps) ->
  39. AllApps = lists:usort(lists:append([[A | Deps] || {A, Deps} <- Apps])),
  40. [emqx_conf | Sorted] = emqx_machine_boot:sorted_reboot_apps(Apps),
  41. case length(AllApps) =:= length(Sorted) of
  42. true -> ok;
  43. false -> error({AllApps, Sorted})
  44. end,
  45. {_, SortedWithIndex} =
  46. lists:foldr(fun(A, {I, Acc}) -> {I + 1, [{A, I} | Acc]} end, {1, []}, Sorted),
  47. do_check_order(Apps, SortedWithIndex).
  48. do_check_order([], _) ->
  49. ok;
  50. do_check_order([{A, Deps} | Rest], Sorted) ->
  51. case lists:filter(fun(Dep) -> is_sorted_before(Dep, A, Sorted) end, Deps) of
  52. [] -> do_check_order(Rest, Sorted);
  53. Bad -> throw({A, Bad})
  54. end.
  55. is_sorted_before(A, B, Sorted) ->
  56. {A, IndexA} = lists:keyfind(A, 1, Sorted),
  57. {B, IndexB} = lists:keyfind(B, 1, Sorted),
  58. IndexA < IndexB.