gproc_test_lib.erl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% --------------------------------------------------
  3. %% This file is provided to you under the Apache License,
  4. %% Version 2.0 (the "License"); you may not use this file
  5. %% except in compliance with the License. You may obtain
  6. %% 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,
  11. %% software distributed under the License is distributed on an
  12. %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. %% KIND, either express or implied. See the License for the
  14. %% specific language governing permissions and limitations
  15. %% under the License.
  16. %% --------------------------------------------------
  17. -module(gproc_test_lib).
  18. -export([t_spawn/1, t_spawn/2,
  19. t_spawn_reg/2, t_spawn_reg/3, t_spawn_reg/4,
  20. t_spawn_reg_shared/3,
  21. t_spawn_mreg/2,
  22. t_call/2,
  23. t_loop/0, t_loop/1,
  24. t_pool_contains_atleast/2,
  25. got_msg/1, got_msg/2,
  26. no_msg/2]).
  27. -include_lib("eunit/include/eunit.hrl").
  28. t_spawn(Node) ->
  29. t_spawn(Node, false).
  30. t_spawn(Node, Selective) when is_boolean(Selective) ->
  31. Me = self(),
  32. P = spawn(Node, fun() ->
  33. Me ! {self(), ok},
  34. t_loop(Selective)
  35. end),
  36. receive
  37. {P, ok} -> P
  38. after 1000 ->
  39. erlang:error({timeout, t_spawn, [Node, Selective]})
  40. end.
  41. t_spawn_reg(Node, Name) ->
  42. t_spawn_reg(Node, Name, default_value(Name)).
  43. t_spawn_reg(Node, Name, Value) ->
  44. Me = self(),
  45. P = spawn(Node, fun() ->
  46. ?assertMatch(true, gproc:reg(Name, Value)),
  47. Me ! {self(), ok},
  48. t_loop()
  49. end),
  50. receive
  51. {P, ok} -> P
  52. after 1000 ->
  53. erlang:error({timeout, t_spawn_reg, [Node, Name, Value]})
  54. end.
  55. t_spawn_reg(Node, Name, Value, Attrs) ->
  56. Me = self(),
  57. P = spawn(Node, fun() ->
  58. ?assertMatch(true, gproc:reg(Name, Value, Attrs)),
  59. Me ! {self(), ok},
  60. t_loop()
  61. end),
  62. receive
  63. {P, ok} -> P
  64. after 1000 ->
  65. erlang:error({timeout, t_spawn_reg, [Node, Name, Value]})
  66. end.
  67. t_spawn_mreg(Node, KVL) ->
  68. Me = self(),
  69. P = spawn(Node, fun() ->
  70. ?assertMatch(true, gproc:mreg(n, g, KVL)),
  71. Me ! {self(), ok},
  72. t_loop()
  73. end),
  74. receive
  75. {P, ok} -> P
  76. end.
  77. t_spawn_reg_shared(Node, Name, Value) ->
  78. Me = self(),
  79. P = spawn(Node, fun() ->
  80. ?assertMatch(true, gproc:reg_shared(Name, Value)),
  81. Me ! {self(), ok},
  82. t_loop()
  83. end),
  84. receive
  85. {P, ok} -> P
  86. after 1000 ->
  87. erlang:error({timeout, t_spawn_reg_shared, [Node,Name,Value]})
  88. end.
  89. default_value({c,_,_}) -> 0;
  90. default_value(_) -> undefined.
  91. t_call(P, Req) ->
  92. Ref = erlang:monitor(process, P),
  93. P ! {self(), Ref, Req},
  94. receive
  95. {P, Ref, Res} ->
  96. erlang:demonitor(Ref, [flush]),
  97. Res;
  98. {'DOWN', Ref, _, _, Error} ->
  99. erlang:error({'DOWN', P, Error})
  100. after 1000 ->
  101. erlang:error({timeout,t_call,[P,Req]})
  102. end.
  103. t_loop() ->
  104. t_loop(false).
  105. t_loop(Selective) when is_boolean(Selective) ->
  106. receive
  107. {From, Ref, die} ->
  108. From ! {self(), Ref, ok};
  109. {From, Ref, {selective, Bool}} when is_boolean(Bool) ->
  110. From ! {self(), Ref, ok},
  111. t_loop(Bool);
  112. {From, Ref, {apply, M, F, A}} ->
  113. From ! {self(), Ref, apply(M, F, A)},
  114. t_loop(Selective);
  115. {From, Ref, {apply_fun, F}} ->
  116. From ! {self(), Ref, F()},
  117. t_loop(Selective);
  118. Other when not Selective ->
  119. ?debugFmt("got unknown msg: ~p~n", [Other]),
  120. exit({unknown_msg, Other})
  121. end.
  122. got_msg(Pb) ->
  123. t_call(Pb,
  124. {apply_fun,
  125. fun() ->
  126. receive M -> M
  127. after 1000 ->
  128. erlang:error({timeout, got_msg, [Pb]})
  129. end
  130. end}).
  131. got_msg(Pb, Tag) ->
  132. t_call(Pb,
  133. {apply_fun,
  134. fun() ->
  135. receive
  136. M when element(1, M) == Tag ->
  137. M
  138. after 1000 ->
  139. erlang:error({timeout, got_msg, [Pb, Tag]})
  140. end
  141. end}).
  142. no_msg(Pb, Timeout) ->
  143. t_call(Pb,
  144. {apply_fun,
  145. fun() ->
  146. receive
  147. M ->
  148. erlang:error({unexpected_msg, M})
  149. after Timeout ->
  150. ok
  151. end
  152. end}).
  153. t_pool_contains_atleast(Pool,N)->
  154. Existing = lists:foldl(fun({_X,_Y},Acc)->
  155. Acc+1;
  156. (_,Acc) ->
  157. Acc
  158. end, 0, gproc_pool:worker_pool(Pool) ),
  159. Existing >= N.