syn_register_processes_SUITE.erl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. -module(syn_register_processes_SUITE).
  2. %% callbacks
  3. -export([all/0]).
  4. -export([init_per_suite/1, end_per_suite/1]).
  5. -export([groups/0, init_per_group/2, end_per_group/2]).
  6. %% tests
  7. -export([
  8. single_node_when_mnesia_is_ram_find_by_key/1,
  9. single_node_when_mnesia_is_ram_find_by_pid/1
  10. ]).
  11. %% include
  12. -include_lib("common_test/include/ct.hrl").
  13. %% ===================================================================
  14. %% Callbacks
  15. %% ===================================================================
  16. %% -------------------------------------------------------------------
  17. %% Function: all() -> GroupsAndTestCases | {skip,Reason}
  18. %% GroupsAndTestCases = [{group,GroupName} | TestCase]
  19. %% GroupName = atom()
  20. %% TestCase = atom()
  21. %% Reason = term()
  22. %% -------------------------------------------------------------------
  23. all() ->
  24. [
  25. {group, single_node_process_registration}
  26. ].
  27. %% -------------------------------------------------------------------
  28. %% Function: groups() -> [Group]
  29. %% Group = {GroupName,Properties,GroupsAndTestCases}
  30. %% GroupName = atom()
  31. %% Properties = [parallel | sequence | Shuffle | {RepeatType,N}]
  32. %% GroupsAndTestCases = [Group | {group,GroupName} | TestCase]
  33. %% TestCase = atom()
  34. %% Shuffle = shuffle | {shuffle,{integer(),integer(),integer()}}
  35. %% RepeatType = repeat | repeat_until_all_ok | repeat_until_all_fail |
  36. %% repeat_until_any_ok | repeat_until_any_fail
  37. %% N = integer() | forever
  38. %% -------------------------------------------------------------------
  39. groups() ->
  40. [
  41. {single_node_process_registration, [shuffle], [
  42. single_node_when_mnesia_is_ram_find_by_key,
  43. single_node_when_mnesia_is_ram_find_by_pid
  44. ]}
  45. ].
  46. %% -------------------------------------------------------------------
  47. %% Function: init_per_suite(Config0) ->
  48. %% Config1 | {skip,Reason} |
  49. %% {skip_and_save,Reason,Config1}
  50. %% Config0 = Config1 = [tuple()]
  51. %% Reason = term()
  52. %% -------------------------------------------------------------------
  53. init_per_suite(Config) ->
  54. %% config
  55. [
  56. {slave_node_short_name, syn_slave}
  57. | Config
  58. ].
  59. %% -------------------------------------------------------------------
  60. %% Function: end_per_suite(Config0) -> void() | {save_config,Config1}
  61. %% Config0 = Config1 = [tuple()]
  62. %% -------------------------------------------------------------------
  63. end_per_suite(_Config) -> ok.
  64. %% -------------------------------------------------------------------
  65. %% Function: init_per_group(GroupName, Config0) ->
  66. %% Config1 | {skip,Reason} |
  67. %% {skip_and_save,Reason,Config1}
  68. %% GroupName = atom()
  69. %% Config0 = Config1 = [tuple()]
  70. %% Reason = term()
  71. %% -------------------------------------------------------------------
  72. init_per_group(_GroupName, Config) -> Config.
  73. %% -------------------------------------------------------------------
  74. %% Function: end_per_group(GroupName, Config0) ->
  75. %% void() | {save_config,Config1}
  76. %% GroupName = atom()
  77. %% Config0 = Config1 = [tuple()]
  78. %% -------------------------------------------------------------------
  79. end_per_group(_GroupName, _Config) ->
  80. clean_after_test().
  81. %% ===================================================================
  82. %% Tests
  83. %% ===================================================================
  84. single_node_when_mnesia_is_ram_find_by_key(_Config) ->
  85. %% set schema location
  86. application:set_env(mnesia, schema_location, ram),
  87. %% start
  88. ok = syn:start(),
  89. %% start process
  90. Pid = start_process(),
  91. %% retrieve
  92. undefined = syn:find_by_key(<<"my proc">>),
  93. %% register
  94. syn:register(<<"my proc">>, Pid),
  95. %% retrieve
  96. Pid = syn:find_by_key(<<"my proc">>),
  97. %% kill process
  98. kill_process(Pid),
  99. timer:sleep(100),
  100. %% retrieve
  101. undefined = syn:find_by_key(<<"my proc">>).
  102. single_node_when_mnesia_is_ram_find_by_pid(_Config) ->
  103. %% set schema location
  104. application:set_env(mnesia, schema_location, ram),
  105. %% start
  106. ok = syn:start(),
  107. %% start process
  108. Pid = start_process(),
  109. %% register
  110. syn:register(<<"my proc">>, Pid),
  111. %% retrieve
  112. <<"my proc">> = syn:find_by_pid(Pid),
  113. %% kill process
  114. kill_process(Pid),
  115. timer:sleep(100),
  116. %% retrieve
  117. undefined = syn:find_by_pid(Pid).
  118. %% ===================================================================
  119. %% Internal
  120. %% ===================================================================
  121. clean_after_test() ->
  122. %% stop mnesia
  123. mnesia:stop(),
  124. %% delete schema
  125. mnesia:delete_schema([node()]),
  126. %% stop syn
  127. syn:stop().
  128. start_process() ->
  129. Pid = spawn(?MODULE, process_main, []),
  130. Pid.
  131. kill_process(Pid) ->
  132. exit(Pid, kill).
  133. process_main() ->
  134. receive
  135. shutdown -> ok
  136. end.