gproc_test_lib.erl 4.1 KB

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