emqx_machine_tests.erl 2.1 KB

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