gproc_test_lib.erl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_spawn_mreg/3,
  23. t_call/2,
  24. t_loop/0, t_loop/1,
  25. t_pool_contains_atleast/2,
  26. got_msg/1, got_msg/2,
  27. no_msg/2]).
  28. -include_lib("eunit/include/eunit.hrl").
  29. t_spawn(Node) ->
  30. t_spawn(Node, false).
  31. t_spawn(Node, Selective) when is_boolean(Selective) ->
  32. Me = self(),
  33. P = spawn(Node, fun() ->
  34. Me ! {self(), ok},
  35. t_loop(Selective)
  36. end),
  37. receive
  38. {P, ok} -> P
  39. after 1000 ->
  40. erlang:error({timeout, t_spawn, [Node, Selective]})
  41. end.
  42. t_spawn_reg(Node, Name) ->
  43. t_spawn_reg(Node, Name, default_value(Name)).
  44. t_spawn_reg(Node, Name, Value) ->
  45. Me = self(),
  46. P = spawn(Node, fun() ->
  47. ?assertMatch(true, gproc:reg(Name, Value)),
  48. Me ! {self(), ok},
  49. t_loop()
  50. end),
  51. receive
  52. {P, ok} ->
  53. P
  54. after 1000 ->
  55. erlang:error({timeout, t_spawn_reg, [Node, Name, Value]})
  56. end.
  57. t_spawn_reg(Node, Name, Value, Attrs) ->
  58. Me = self(),
  59. P = spawn(Node, fun() ->
  60. ?assertMatch(true, gproc:reg(Name, Value, Attrs)),
  61. Me ! {self(), ok},
  62. t_loop()
  63. end),
  64. receive
  65. {P, ok} ->
  66. P
  67. after 1000 ->
  68. erlang:error({timeout, t_spawn_reg, [Node, Name, Value]})
  69. end.
  70. t_spawn_mreg(Node, KVL) ->
  71. t_spawn_mreg(Node, n, KVL).
  72. t_spawn_mreg(Node, T, KVL) ->
  73. Me = self(),
  74. P = spawn(Node, fun() ->
  75. ?assertMatch(true, gproc:mreg(T, g, KVL)),
  76. Me ! {self(), ok},
  77. t_loop()
  78. end),
  79. receive
  80. {P, ok} ->
  81. P
  82. after 1000 ->
  83. error({timeout, t_spawn_mreg, [Node, T, KVL]})
  84. end.
  85. t_spawn_reg_shared(Node, Name, Value) ->
  86. Me = self(),
  87. P = spawn(Node, fun() ->
  88. ?assertMatch(true, gproc:reg_shared(Name, Value)),
  89. Me ! {self(), ok},
  90. t_loop()
  91. end),
  92. receive
  93. {P, ok} -> P
  94. after 1000 ->
  95. erlang:error({timeout, t_spawn_reg_shared, [Node,Name,Value]})
  96. end.
  97. default_value({c,_,_}) -> 0;
  98. default_value(_) -> undefined.
  99. t_call(P, Req) ->
  100. Ref = erlang:monitor(process, P),
  101. P ! {self(), Ref, Req},
  102. receive
  103. {P, Ref, Res} ->
  104. erlang:demonitor(Ref, [flush]),
  105. Res;
  106. {'DOWN', Ref, _, _, Error} ->
  107. erlang:error({'DOWN', P, Error})
  108. after 1000 ->
  109. erlang:error({timeout,t_call,[P,Req]})
  110. end.
  111. t_loop() ->
  112. t_loop(false).
  113. t_loop(Selective) when is_boolean(Selective) ->
  114. receive
  115. {From, Ref, die} ->
  116. From ! {self(), Ref, ok};
  117. {From, Ref, {selective, Bool}} when is_boolean(Bool) ->
  118. From ! {self(), Ref, ok},
  119. t_loop(Bool);
  120. {From, Ref, {apply, M, F, A}} ->
  121. From ! {self(), Ref, apply(M, F, A)},
  122. t_loop(Selective);
  123. {From, Ref, {apply_fun, F}} ->
  124. From ! {self(), Ref, F()},
  125. t_loop(Selective);
  126. Other when not Selective ->
  127. ?debugFmt("got unknown msg: ~p~n", [Other]),
  128. exit({unknown_msg, Other})
  129. end.
  130. got_msg(Pb) ->
  131. t_call(Pb,
  132. {apply_fun,
  133. fun() ->
  134. receive M -> M
  135. after 1000 ->
  136. erlang:error({timeout, got_msg, [Pb]})
  137. end
  138. end}).
  139. got_msg(Pb, Tag) ->
  140. t_call(Pb,
  141. {apply_fun,
  142. fun() ->
  143. receive
  144. M when element(1, M) == Tag ->
  145. M
  146. after 1000 ->
  147. erlang:error({timeout, got_msg, [Pb, Tag]})
  148. end
  149. end}).
  150. no_msg(Pb, Timeout) ->
  151. t_call(Pb,
  152. {apply_fun,
  153. fun() ->
  154. receive
  155. M ->
  156. erlang:error({unexpected_msg, M})
  157. after Timeout ->
  158. ok
  159. end
  160. end}).
  161. t_pool_contains_atleast(Pool,N)->
  162. Existing = lists:foldl(fun({_X,_Y},Acc)->
  163. Acc+1;
  164. (_,Acc) ->
  165. Acc
  166. end, 0, gproc_pool:worker_pool(Pool) ),
  167. Existing >= N.