pidq_test.erl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. -module(pidq_test).
  2. -include_lib("eunit/include/eunit.hrl").
  3. -compile([export_all]).
  4. % The `user' processes represent users of the pidq library. A user
  5. % process will take a pid, report details on the pid it has, release
  6. % and take a new pid, stop cleanly, and crash.
  7. start_user() ->
  8. TC = pidq:take_pid(),
  9. spawn(fun() -> user_loop(TC) end).
  10. user_id(Pid) ->
  11. Pid ! {get_tc_id, self()},
  12. receive
  13. {Type, Id} ->
  14. {Type, Id}
  15. end.
  16. user_new_tc(Pid) ->
  17. Pid ! new_tc.
  18. user_stop(Pid) ->
  19. Pid ! stop.
  20. user_crash(Pid) ->
  21. Pid ! crash.
  22. user_loop(MyTC) ->
  23. receive
  24. {get_tc_id, From} ->
  25. From ! get_tc_id(MyTC),
  26. user_loop(MyTC);
  27. new_tc ->
  28. pidq:return_pid(MyTC, ok),
  29. MyNewTC = pidq:take_pid(),
  30. user_loop(MyNewTC);
  31. stop ->
  32. pidq:return_pid(MyTC, ok),
  33. stopped;
  34. crash ->
  35. erlang:error({user_loop, kaboom})
  36. end.
  37. % The `tc' processes represent the pids tracked by pidq for testing.
  38. % They have a type and an ID and can report their type and ID and
  39. % stop.
  40. tc_loop({Type, Id}) ->
  41. receive
  42. {get_id, From} ->
  43. From ! {ok, Type, Id},
  44. tc_loop({Type, Id});
  45. stop -> stopped;
  46. crash ->
  47. erlang:error({tc_loop, kaboom})
  48. end.
  49. get_tc_id(Pid) ->
  50. Pid ! {get_id, self()},
  51. receive
  52. {ok, Type, Id} ->
  53. {Type, Id}
  54. after 200 ->
  55. timeout
  56. end.
  57. stop_tc(Pid) ->
  58. Pid ! stop.
  59. tc_starter(Type) ->
  60. Ref = make_ref(),
  61. spawn(fun() -> tc_loop({Type, Ref}) end).
  62. tc_sanity_test() ->
  63. Pid1 = tc_starter("1"),
  64. {"1", Id1} = get_tc_id(Pid1),
  65. Pid2 = tc_starter("1"),
  66. {"1", Id2} = get_tc_id(Pid2),
  67. ?assertNot(Id1 == Id2),
  68. stop_tc(Pid1),
  69. stop_tc(Pid2).
  70. user_sanity_test() ->
  71. Pid1 = tc_starter("1"),
  72. User = spawn(fun() -> user_loop(Pid1) end),
  73. ?assertMatch({"1", _Ref}, user_id(User)),
  74. user_crash(User),
  75. stop_tc(Pid1).
  76. pidq_integration_test_() ->
  77. {foreach,
  78. % setup
  79. fun() ->
  80. Pools = [[{name, "p1"},
  81. {max_pids, 20},
  82. {min_free, 3},
  83. {init_size, 10},
  84. {pid_starter_args, ["type-0"]}]],
  85. Config = [{pid_starter, {?MODULE, tc_starter}},
  86. {pid_stopper, {?MODULE, stop_tc}},
  87. {pools, Pools}],
  88. pidq:start(Config),
  89. Users = [ start_user() || _X <- lists:seq(1, 10) ],
  90. Users
  91. end,
  92. % cleanup
  93. fun(Users) ->
  94. [ user_stop(U) || U <- Users ],
  95. pidq:stop()
  96. end,
  97. %
  98. [
  99. fun(Users) ->
  100. fun() ->
  101. % each user has a different tc ID
  102. TcIds = lists:sort([ user_id(UPid) || UPid <- Users ]),
  103. ?assertEqual(lists:usort(TcIds), TcIds)
  104. end
  105. end
  106. ]
  107. }.
  108. % fun(Users) ->
  109. % ]}
  110. % {"users still unique after a renew cycle",
  111. % fun() ->
  112. % Users = [ start_user() || _X <- lists:seq(1, 10) ],
  113. % % return and take new tc pids, expect unique
  114. % [ user_new_tc(UPid) || UPid <- Users ],
  115. % TcIds = lists:sort([ user_id(UPid) || UPid <- Users ]),
  116. % % each user has a different tc ID
  117. % ?assertEqual(lists:usort(TcIds), TcIds)
  118. % ]}.
  119. % % return and take new tc pids, still unique
  120. % [ user_new_tc(UPid) || UPid <- Users ],
  121. % TcIds2 = lists:sort([ user_id(UPid) || UPid <- Users ]),
  122. % ?assertEqual(lists:usort(TcIds2), TcIds2),
  123. % % if the users all crash...
  124. % [ user_crash(UPid) || UPid <- Users ],
  125. % Users2 = [ start_user() || _X <- lists:seq(1, 10) ],
  126. % TcIds3 = lists:sort([ user_id(UPid) || UPid <- Users ]),
  127. % ?assertEqual(lists:usort(TcIds3), TcIds3)