Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 781 lines (719 sloc) 24.601 kb
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
1 %% ``The contents of this file are subject to the Erlang Public License,
2 %% Version 1.1, (the "License"); you may not use this file except in
3 %% compliance with the License. You should have received a copy of the
4 %% Erlang Public License along with this software. If not, it can be
5 %% retrieved via the world wide web at http://www.erlang.org/.
6 %%
7 %% Software distributed under the License is distributed on an "AS IS"
8 %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 %% the License for the specific language governing rights and limitations
10 %% under the License.
11 %%
12 %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13 %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14 %% AB. All Rights Reserved.''
15 %%
16 %% @author Ulf Wiger <ulf.wiger@erlang-solutions.com>
17 %%
18 -module(gproc_tests).
19
20 -ifdef(TEST).
21
22 -include_lib("eunit/include/eunit.hrl").
305c2d4 v0.2.1 - bugs in qlc; test cases added
Ulf Wiger authored
23 -include_lib("stdlib/include/qlc.hrl").
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
24
669e1b2 added performance tuning options
Ulf Wiger authored
25 conf_test_() ->
26 {foreach,
27 fun() ->
28 application:unload(gproc)
29 end,
30 fun(_) ->
31 application:stop(gproc)
32 end,
33 [?_test(t_server_opts()),
34 ?_test(t_ets_opts())]}.
35
36 t_server_opts() ->
37 H = 10000,
38 application:set_env(gproc, server_options, [{min_heap_size, H}]),
39 ?assert(ok == application:start(gproc)),
40 {min_heap_size, H1} = process_info(whereis(gproc), min_heap_size),
41 ?assert(is_integer(H1) andalso H1 > H).
42
43 t_ets_opts() ->
44 %% Cannot inspect the write_concurrency attribute on an ets table in
45 %% any easy way, so trace on the ets:new/2 call and check the arguments.
46 application:set_env(gproc, ets_options, [{write_concurrency, false}]),
47 erlang:trace_pattern({ets,new, 2}, [{[gproc,'_'],[],[]}], [global]),
48 erlang:trace(new, true, [call]),
49 ?assert(ok == application:start(gproc)),
50 erlang:trace(new, false, [call]),
51 receive
52 {trace,_,call,{ets,new,[gproc,Opts]}} ->
53 ?assertMatch({write_concurrency, false},
54 lists:keyfind(write_concurrency,1,Opts))
55 after 3000 ->
56 error(timeout)
57 end.
58
59
60
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
61 reg_test_() ->
62 {setup,
63 fun() ->
2a5aed4 set_env can now also update mnesia
Ulf Wiger authored
64 application:start(gproc),
65 application:start(mnesia)
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
66 end,
67 fun(_) ->
2a5aed4 set_env can now also update mnesia
Ulf Wiger authored
68 application:stop(gproc),
69 application:stop(mnesia)
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
70 end,
71 [
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
72 {spawn, ?_test(?debugVal(t_simple_reg()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
73 , ?_test(t_is_clean())
9661221 @uwiger added gproc:reg_or_locate/[1,2]
authored
74 , {spawn, ?_test(?debugVal(t_simple_reg_or_locate()))}
75 , ?_test(t_is_clean())
76 , {spawn, ?_test(?debugVal(t_reg_or_locate2()))}
77 , ?_test(t_is_clean())
e8aaa4f @uwiger gproc:reg_or_locate/3, spawns a regged process
authored
78 , {spawn, ?_test(?debugVal(t_reg_or_locate3()))}
79 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
80 , {spawn, ?_test(?debugVal(t_simple_counter()))}
8c1e685 shared counters; reset_counter
Ulf Wiger authored
81 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
82 , {spawn, ?_test(?debugVal(t_simple_aggr_counter()))}
8c1e685 shared counters; reset_counter
Ulf Wiger authored
83 , ?_test(t_is_clean())
1740e41 @uwiger Fix dist update_counter() bug, add gproc_ps module, add update_counte…
authored
84 , {spawn, ?_test(?debugVal(t_update_counters()))}
85 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
86 , {spawn, ?_test(?debugVal(t_simple_prop()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
87 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
88 , {spawn, ?_test(?debugVal(t_await()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
89 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
90 , {spawn, ?_test(?debugVal(t_await_self()))}
27be35e gproc:nb_wait/1 now works even if waiting on self.
Ulf Wiger authored
91 , ?_test(t_is_clean())
b5c9cd2 @uwiger resolve race in gproc:await/2
authored
92 , {spawn, ?_test(?debugVal(t_await_crash()))}
93 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
94 , {spawn, ?_test(?debugVal(t_simple_mreg()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
95 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
96 , {spawn, ?_test(?debugVal(t_gproc_crash()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
97 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
98 , {spawn, ?_test(?debugVal(t_cancel_wait_and_register()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
99 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
100 , {spawn, ?_test(?debugVal(t_give_away_to_pid()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
101 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
102 , {spawn, ?_test(?debugVal(t_give_away_to_self()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
103 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
104 , {spawn, ?_test(?debugVal(t_give_away_badarg()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
105 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
106 , {spawn, ?_test(?debugVal(t_give_away_to_unknown()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
107 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
108 , {spawn, ?_test(?debugVal(t_give_away_and_back()))}
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
109 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
110 , {spawn, ?_test(?debugVal(t_select()))}
59365c4 Fixed select/qlc scope (default: all); fixed dist eunit test; new rebar
Ulf Wiger authored
111 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
112 , {spawn, ?_test(?debugVal(t_select_count()))}
5485a1a merged select_count from uwiger; added test
Ulf Wiger authored
113 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
114 , {spawn, ?_test(?debugVal(t_qlc()))}
305c2d4 v0.2.1 - bugs in qlc; test cases added
Ulf Wiger authored
115 , ?_test(t_is_clean())
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
116 , {spawn, ?_test(?debugVal(t_qlc_dead()))}
117 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
118 , {spawn, ?_test(?debugVal(t_get_env()))}
9c505d4 eunit tests for get_env/4 et al
Ulf Wiger authored
119 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
120 , {spawn, ?_test(?debugVal(t_get_set_env()))}
9c505d4 eunit tests for get_env/4 et al
Ulf Wiger authored
121 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
122 , {spawn, ?_test(?debugVal(t_set_env()))}
9c505d4 eunit tests for get_env/4 et al
Ulf Wiger authored
123 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
124 , {spawn, ?_test(?debugVal(t_get_env_inherit()))}
af14ad2 doc additions, inherit by name, init_arg
Ulf Wiger authored
125 , ?_test(t_is_clean())
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
126 , {spawn, ?_test(?debugVal(t_monitor()))}
127 , ?_test(t_is_clean())
128 , {spawn, ?_test(?debugVal(t_monitor_give_away()))}
129 , ?_test(t_is_clean())
130 , {spawn, ?_test(?debugVal(t_subscribe()))}
af14ad2 doc additions, inherit by name, init_arg
Ulf Wiger authored
131 , ?_test(t_is_clean())
dd0c3e6 @uwiger Merge branch 'master' of github.com:uwiger/gproc
authored
132 , {spawn, ?_test(?debugVal(t_gproc_info()))}
b4aa843 @uwiger gproc:info(P,current_function) return value
authored
133 , ?_test(t_is_clean())
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
134 ]}.
135
136 t_simple_reg() ->
137 ?assert(gproc:reg({n,l,name}) =:= true),
138 ?assert(gproc:where({n,l,name}) =:= self()),
139 ?assert(gproc:unreg({n,l,name}) =:= true),
140 ?assert(gproc:where({n,l,name}) =:= undefined).
141
9661221 @uwiger added gproc:reg_or_locate/[1,2]
authored
142 t_simple_reg_or_locate() ->
143 P = self(),
144 ?assertMatch({P, undefined}, gproc:reg_or_locate({n,l,name})),
145 ?assertMatch(P, gproc:where({n,l,name})),
146 ?assertMatch({P, my_val}, gproc:reg_or_locate({n,l,name2}, my_val)),
147 ?assertMatch(my_val, gproc:get_value({n,l,name2})).
148
149 t_reg_or_locate2() ->
150 P = self(),
151 {P1,R1} = spawn_monitor(fun() ->
152 Ref = erlang:monitor(process, P),
153 gproc:reg({n,l,foo}, the_value),
154 P ! {self(), ok},
155 receive
156 {'DOWN',Ref,_,_,_} -> ok
157 end
158 end),
159 receive {P1, ok} -> ok end,
160 ?assertMatch({P1, the_value}, gproc:reg_or_locate({n,l,foo})),
161 exit(P1, kill),
162 receive
163 {'DOWN',R1,_,_,_} ->
164 ok
165 end.
166
e8aaa4f @uwiger gproc:reg_or_locate/3, spawns a regged process
authored
167 t_reg_or_locate3() ->
168 P = self(),
169 {P1, Value} = gproc:reg_or_locate(
170 {n,l,foo}, the_value,
171 fun() ->
172 P ! {self(), ok},
173 receive
174 {'DOWN',Ref,_,_,_} -> ok
175 end
176 end),
177 ?assert(P =/= P1),
178 ?assert(Value =:= the_value),
179 Ref = erlang:monitor(process, P1),
180 receive
181 {P1, ok} -> ok;
182 {'DOWN', Ref, _, _, Reason} ->
183 ?assert(process_died_unexpectedly)
184 end,
185 ?assertMatch({P1, the_value}, gproc:reg_or_locate({n,l,foo})),
186 exit(P1, kill),
187 receive
188 {'DOWN',R1,_,_,_} ->
189 ok
190 end.
9661221 @uwiger added gproc:reg_or_locate/[1,2]
authored
191
8c1e685 shared counters; reset_counter
Ulf Wiger authored
192 t_simple_counter() ->
193 ?assert(gproc:reg({c,l,c1}, 3) =:= true),
194 ?assert(gproc:get_value({c,l,c1}) =:= 3),
195 ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
196 ?assert(gproc:reset_counter({c,l,c1}) =:= {7, 3}).
197
198 t_simple_aggr_counter() ->
199 ?assert(gproc:reg({c,l,c1}, 3) =:= true),
200 ?assert(gproc:reg({a,l,c1}) =:= true),
201 ?assert(gproc:get_value({a,l,c1}) =:= 3),
202 P = self(),
203 P1 = spawn_link(fun() ->
204 gproc:reg({c,l,c1}, 5),
205 P ! {self(), ok},
206 receive
9c88453 'git' tag in .app.src; addressed warnings
Ulf Wiger authored
207 {P, goodbye} -> ok
8c1e685 shared counters; reset_counter
Ulf Wiger authored
208 end
209 end),
210 receive {P1, ok} -> ok end,
211 ?assert(gproc:get_value({a,l,c1}) =:= 8),
212 ?assert(gproc:update_counter({c,l,c1}, 4) =:= 7),
213 ?assert(gproc:get_value({a,l,c1}) =:= 12),
214 P1 ! {self(), goodbye},
215 R = erlang:monitor(process, P1),
216 receive {'DOWN', R, _, _, _} ->
217 gproc:audit_process(P1)
218 end,
219 ?assert(gproc:get_value({a,l,c1}) =:= 7).
220
1740e41 @uwiger Fix dist update_counter() bug, add gproc_ps module, add update_counte…
authored
221 t_update_counters() ->
222 ?assert(gproc:reg({c,l,c1}, 3) =:= true),
223 ?assert(gproc:reg({a,l,c1}) =:= true),
224 ?assert(gproc:get_value({a,l,c1}) =:= 3),
225 P = self(),
226 P1 = spawn_link(fun() ->
227 gproc:reg({c,l,c1}, 5),
228 P ! {self(), ok},
229 receive
230 {P, goodbye} -> ok
231 end
232 end),
233 receive {P1, ok} -> ok end,
234 ?assert(gproc:get_value({a,l,c1}) =:= 8),
f9dcbb4 @uwiger Change return value of update_counters/1; add gproc_ps:notify_single_…
authored
235 Me = self(),
236 ?assertEqual([{{c,l,c1},Me,7},
237 {{c,l,c1},P1,8}], gproc:update_counters(l, [{{c,l,c1}, Me, 4},
238 {{c,l,c1}, P1, 3}])),
1740e41 @uwiger Fix dist update_counter() bug, add gproc_ps module, add update_counte…
authored
239 ?assert(gproc:get_value({a,l,c1}) =:= 15),
240 P1 ! {self(), goodbye},
241 R = erlang:monitor(process, P1),
242 receive {'DOWN', R, _, _, _} ->
243 gproc:audit_process(P1)
244 end,
245 ?assert(gproc:get_value({a,l,c1}) =:= 7).
246
8c1e685 shared counters; reset_counter
Ulf Wiger authored
247
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
248 t_simple_prop() ->
249 ?assert(gproc:reg({p,l,prop}) =:= true),
250 ?assert(t_other_proc(fun() ->
251 ?assert(gproc:reg({p,l,prop}) =:= true)
252 end) =:= ok),
253 ?assert(gproc:unreg({p,l,prop}) =:= true).
254
255 t_other_proc(F) ->
256 {_Pid,Ref} = spawn_monitor(fun() -> exit(F()) end),
257 receive
258 {'DOWN',Ref,_,_,R} ->
259 R
260 after 10000 ->
261 erlang:error(timeout)
262 end.
263
264 t_await() ->
265 Me = self(),
266 {_Pid,Ref} = spawn_monitor(
669e1b2 added performance tuning options
Ulf Wiger authored
267 fun() ->
268 exit(?assert(
269 gproc:await({n,l,t_await}) =:= {Me,val}))
270 end),
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
271 ?assert(gproc:reg({n,l,t_await},val) =:= true),
272 receive
273 {'DOWN', Ref, _, _, R} ->
274 ?assertEqual(R, ok)
275 after 10000 ->
276 erlang:error(timeout)
277 end.
278
27be35e gproc:nb_wait/1 now works even if waiting on self.
Ulf Wiger authored
279 t_await_self() ->
280 Me = self(),
281 Ref = gproc:nb_wait({n, l, t_await_self}),
282 ?assert(gproc:reg({n, l, t_await_self}, some_value) =:= true),
283 ?assertEqual(true, receive
284 {gproc, Ref, R, Wh} ->
285 {registered, {{n, l, t_await_self},
286 Me, some_value}} = {R, Wh},
287 true
288 after 10000 ->
289 timeout
290 end).
291
b5c9cd2 @uwiger resolve race in gproc:await/2
authored
292 t_await_crash() ->
293 Name = {n,l,{dummy,?LINE}},
294 {Pid,_} = spawn_regger(Name),
295 ?assertEqual({Pid,undefined}, gproc:await(Name, 1000)),
296 exit(Pid, kill),
297 {NewPid,MRef} = spawn_regger(Name),
298 ?assertEqual(false, is_process_alive(Pid)),
299 ?assertEqual({NewPid,undefined}, gproc:await(Name, 1000)),
300 exit(NewPid, kill),
301 receive {'DOWN', MRef, _, _, _} -> ok end.
302
303 spawn_regger(Name) ->
304 spawn_monitor(fun() ->
305 gproc:reg(Name),
306 timer:sleep(60000)
307 end).
308
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
309 t_is_clean() ->
310 sys:get_status(gproc), % in order to synch
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
311 sys:get_status(gproc_monitor),
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
312 T = ets:tab2list(gproc),
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
313 Tm = ets:tab2list(gproc_monitor),
314 ?assertMatch([], Tm),
315 ?assertMatch([], T -- [{{whereis(gproc_monitor), l}}]).
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
316
317 t_simple_mreg() ->
6d2ef4f merged from ghaskins + eunit test for local mreg
Ulf Wiger authored
318 P = self(),
319 ?assertEqual(true, gproc:mreg(n, l, [{foo, foo_val},
320 {bar, bar_val}])),
321 ?assertEqual(P, gproc:where({n,l,foo})),
75f0298 Added gproc:munreg/3
Ulf Wiger authored
322 ?assertEqual(P, gproc:where({n,l,bar})),
323 ?assertEqual(true, gproc:munreg(n, l, [foo, bar])).
324
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
325
326 t_gproc_crash() ->
327 P = spawn_helper(),
328 ?assert(gproc:where({n,l,P}) =:= P),
329 exit(whereis(gproc), kill),
330 give_gproc_some_time(100),
331 ?assert(whereis(gproc) =/= undefined),
332 %%
333 %% Check that the registration is still there using an ets:lookup(),
334 %% Once we've killed the process, gproc will always return undefined
335 %% if the process is not alive, regardless of whether the registration
336 %% is still there. So, here, the lookup should find something...
337 %%
338 ?assert(ets:lookup(gproc,{{n,l,P},n}) =/= []),
339 ?assert(gproc:where({n,l,P}) =:= P),
340 exit(P, kill),
341 %% ...and here, it shouldn't.
342 %% (sleep for a while first to let gproc handle the EXIT
343 give_gproc_some_time(10),
344 ?assert(ets:lookup(gproc,{{n,l,P},n}) =:= []).
345
346 t_cancel_wait_and_register() ->
347 Alias = {n, l, foo},
348 Me = self(),
349 P = spawn(fun() ->
350 {'EXIT',_} = (catch gproc:await(Alias, 100)),
351 ?assert(element(1,sys:get_status(gproc)) == status),
352 Me ! {self(), go_ahead},
353 timer:sleep(infinity)
354 end),
355 receive
356 {P, go_ahead} ->
357 ?assertEqual(gproc:reg(Alias, undefined), true),
358 exit(P, kill),
359 timer:sleep(500),
360 ?assert(element(1,sys:get_status(gproc)) == status)
361 end.
362
363
364 t_give_away_to_pid() ->
365 From = {n, l, foo},
366 Me = self(),
367 P = spawn_link(fun t_loop/0),
368 ?assertEqual(true, gproc:reg(From, undefined)),
369 ?assertEqual(Me, gproc:where(From)),
370 ?assertEqual(P, gproc:give_away(From, P)),
371 ?assertEqual(P, gproc:where(From)),
372 ?assertEqual(ok, t_call(P, die)).
373
374 t_give_away_to_self() ->
375 From = {n, l, foo},
376 Me = self(),
377 ?assertEqual(true, gproc:reg(From, undefined)),
378 ?assertEqual(Me, gproc:where(From)),
379 ?assertEqual(Me, gproc:give_away(From, Me)),
380 ?assertEqual(Me, gproc:where(From)),
381 ?assertEqual(true, gproc:unreg(From)).
382
383 t_give_away_badarg() ->
384 From = {n, l, foo},
385 Me = self(),
386 ?assertEqual(undefined, gproc:where(From)),
387 ?assertError(badarg, gproc:give_away(From, Me)).
388
389 t_give_away_to_unknown() ->
390 From = {n, l, foo},
391 Unknown = {n, l, unknown},
392 Me = self(),
393 ?assertEqual(true, gproc:reg(From, undefined)),
394 ?assertEqual(Me, gproc:where(From)),
395 ?assertEqual(undefined, gproc:where(Unknown)),
396 ?assertEqual(undefined, gproc:give_away(From, Unknown)),
397 ?assertEqual(undefined, gproc:where(From)).
398
399 t_give_away_and_back() ->
400 From = {n, l, foo},
401 Me = self(),
402 P = spawn_link(fun t_loop/0),
403 ?assertEqual(true, gproc:reg(From, undefined)),
404 ?assertEqual(Me, gproc:where(From)),
405 ?assertEqual(P, gproc:give_away(From, P)),
406 ?assertEqual(P, gproc:where(From)),
407 ?assertEqual(ok, t_call(P, {give_away, From})),
408 ?assertEqual(Me, gproc:where(From)),
409 ?assertEqual(ok, t_call(P, die)).
410
59365c4 Fixed select/qlc scope (default: all); fixed dist eunit test; new rebar
Ulf Wiger authored
411 t_select() ->
412 ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
413 ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
414 ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
415 ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
416 ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
417 ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
418 %% local names
419 ?assertEqual(
420 [{{n,l,{n,1}},self(),x},
421 {{n,l,{n,2}},self(),y}], gproc:select(
422 {local,names},
423 [{{{n,l,'_'},'_','_'},[],['$_']}])),
424 %% mactch local names on value
425 ?assertEqual(
426 [{{n,l,{n,1}},self(),x}], gproc:select(
427 {local,names},
428 [{{{n,l,'_'},'_',x},[],['$_']}])),
429 %% match all on value
430 ?assertEqual(
431 [{{n,l,{n,1}},self(),x},
432 {{p,l,{p,1}},self(),x}], gproc:select(
433 {all,all},
434 [{{{'_',l,'_'},'_',x},[],['$_']}])),
435 %% match all on pid
436 ?assertEqual(
437 [{{a,l,{c,1}},self(),1},
438 {{c,l,{c,1}},self(),1},
439 {{n,l,{n,1}},self(),x},
440 {{n,l,{n,2}},self(),y},
441 {{p,l,{p,1}},self(),x},
442 {{p,l,{p,2}},self(),y}
443 ], gproc:select(
444 {all,all},
445 [{{'_',self(),'_'},[],['$_']}])).
446
5485a1a merged select_count from uwiger; added test
Ulf Wiger authored
447 t_select_count() ->
448 ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
449 ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
450 ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
451 ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
452 ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
453 ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
454 %% local names
455 ?assertEqual(2, gproc:select_count(
456 {local,names}, [{{{n,l,'_'},'_','_'},[],[true]}])),
457 %% mactch local names on value
458 ?assertEqual(1, gproc:select_count(
459 {local,names}, [{{{n,l,'_'},'_',x},[],[true]}])),
460 %% match all on value
461 ?assertEqual(2, gproc:select_count(
462 {all,all}, [{{{'_',l,'_'},'_',x},[],[true]}])),
463 %% match all on pid
464 ?assertEqual(6, gproc:select_count(
465 {all,all}, [{{'_',self(),'_'},[],[true]}])).
466
305c2d4 v0.2.1 - bugs in qlc; test cases added
Ulf Wiger authored
467 t_qlc() ->
468 ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
469 ?assertEqual(true, gproc:reg({n, l, {n,2}}, y)),
470 ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
471 ?assertEqual(true, gproc:reg({p, l, {p,2}}, y)),
472 ?assertEqual(true, gproc:reg({c, l, {c,1}}, 1)),
473 ?assertEqual(true, gproc:reg({a, l, {c,1}}, undefined)),
474 %% local names
475 Exp1 = [{{n,l,{n,1}},self(),x},
476 {{n,l,{n,2}},self(),y}],
477 ?assertEqual(Exp1,
478 qlc:e(qlc:q([N || N <- gproc:table(names)]))),
479 ?assertEqual(Exp1,
480 qlc:e(qlc:q([N || {{n,l,_},_,_} = N <- gproc:table(names)]))),
481
482 %% mactch local names on value
483 Exp2 = [{{n,l,{n,1}},self(),x}],
484 ?assertEqual(Exp2,
485 qlc:e(qlc:q([N || {{n,l,_},_,x} = N <- gproc:table(names)]))),
486
487 %% match all on value
488 Exp3 = [{{n,l,{n,1}},self(),x},
489 {{p,l,{p,1}},self(),x}],
490 ?assertEqual(Exp3,
491 qlc:e(qlc:q([N || {_,_,x} = N <- gproc:table(all)]))),
492
df11635 qlc with exact matching on pid fixed
Ulf Wiger authored
493 %% match all
305c2d4 v0.2.1 - bugs in qlc; test cases added
Ulf Wiger authored
494 Exp4 = [{{a,l,{c,1}},self(),1},
495 {{c,l,{c,1}},self(),1},
496 {{n,l,{n,1}},self(),x},
497 {{n,l,{n,2}},self(),y},
498 {{p,l,{p,1}},self(),x},
499 {{p,l,{p,2}},self(),y}
500 ],
501 ?assertEqual(Exp4,
df11635 qlc with exact matching on pid fixed
Ulf Wiger authored
502 qlc:e(qlc:q([X || X <- gproc:table(all)]))),
503 %% match on pid
504 ?assertEqual(Exp4,
505 qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
506 gproc:table(all), P =:= self()]))),
507 ?assertEqual(Exp4,
508 qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
509 gproc:table(all), P == self()]))).
59365c4 Fixed select/qlc scope (default: all); fixed dist eunit test; new rebar
Ulf Wiger authored
510
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
511 t_qlc_dead() ->
512 P0 = self(),
513 ?assertEqual(true, gproc:reg({n, l, {n,1}}, x)),
514 ?assertEqual(true, gproc:reg({p, l, {p,1}}, x)),
515 P1 = spawn(fun() ->
516 Ref = erlang:monitor(process, P0),
517 gproc:reg({n, l, {n,2}}, y),
518 gproc:reg({p, l, {p,2}}, y),
519 P0 ! {self(), ok},
520 receive
521 {P, goodbye} -> ok;
522 {'DOWN', Ref, _, _, _} ->
523 ok
524 end
525 end),
526 receive {P1, ok} -> ok end,
527 %% now, suspend gproc so it doesn't do cleanup
528 try sys:suspend(gproc),
529 exit(P1, kill),
530 %% local names
531 Exp1 = [{{n,l,{n,1}},self(),x}],
532 ?assertEqual(Exp1,
34a366b @uwiger added 'check_pids' option to gproc:table/2
authored
533 qlc:e(qlc:q([N || N <-
534 gproc:table(names, [check_pids])]))),
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
535 ?assertEqual(Exp1,
536 qlc:e(qlc:q([N || {{n,l,_},_,_} = N <-
34a366b @uwiger added 'check_pids' option to gproc:table/2
authored
537 gproc:table(names, [check_pids])]))),
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
538 %% match local names on value
539 Exp2 = [{{n,l,{n,1}},self(),x}],
540 ?assertEqual(Exp2,
541 qlc:e(qlc:q([N || {{n,l,_},_,x} = N <-
34a366b @uwiger added 'check_pids' option to gproc:table/2
authored
542 gproc:table(names, [check_pids])]))),
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
543 ?assertEqual([],
544 qlc:e(qlc:q([N || {{n,l,_},_,y} = N <-
34a366b @uwiger added 'check_pids' option to gproc:table/2
authored
545 gproc:table(names, [check_pids])]))),
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
546 %% match all on value
547 Exp3 = [{{n,l,{n,1}},self(),x},
548 {{p,l,{p,1}},self(),x}],
549 ?assertEqual(Exp3,
34a366b @uwiger added 'check_pids' option to gproc:table/2
authored
550 qlc:e(qlc:q([N || {_,_,x} = N <-
551 gproc:table(all, [check_pids])]))),
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
552 ?assertEqual([],
34a366b @uwiger added 'check_pids' option to gproc:table/2
authored
553 qlc:e(qlc:q([N || {_,_,y} = N <-
554 gproc:table(all, [check_pids])]))),
555 Exp3b = [{{n,l,{n,2}},P1,y},
556 {{p,l,{p,2}},P1,y}],
557 ?assertEqual(Exp3b,
558 qlc:e(qlc:q([N || {_,_,y} = N <-
559 gproc:table(all)]))),
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
560
561 %% match all
562 Exp4 = [{{n,l,{n,1}},self(),x},
563 {{p,l,{p,1}},self(),x}],
564 ?assertEqual(Exp4,
34a366b @uwiger added 'check_pids' option to gproc:table/2
authored
565 qlc:e(qlc:q([X || X <-
566 gproc:table(all, [check_pids])]))),
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
567 %% match on pid
568 ?assertEqual(Exp4,
569 qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
34a366b @uwiger added 'check_pids' option to gproc:table/2
authored
570 gproc:table(all, [check_pids]),
571 P =:= self()]))),
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
572 ?assertEqual([],
573 qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
34a366b @uwiger added 'check_pids' option to gproc:table/2
authored
574 gproc:table(all, [check_pids]),
575 P =:= P1]))),
576 Exp4b = [{{n,l,{n,2}},P1,y},
577 {{p,l,{p,2}},P1,y}],
578 ?assertEqual(Exp4b,
579 qlc:e(qlc:q([{K,P,V} || {K,P,V} <-
580 gproc:table(all),
581 P =:= P1])))
beddb19 @uwiger make qlc prune dead entries; fn clause in await (g)
authored
582 after
583 sys:resume(gproc)
584 end.
585
586
9c505d4 eunit tests for get_env/4 et al
Ulf Wiger authored
587 t_get_env() ->
588 ?assertEqual(ok, application:set_env(gproc, ssss, "s1")),
589 ?assertEqual(true, os:putenv("SSSS", "s2")),
590 ?assertEqual(true, os:putenv("TTTT", "s3")),
591 ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
592 ?assertEqual(undefined, gproc:get_env(l, gproc, ssss, [])),
af14ad2 doc additions, inherit by name, init_arg
Ulf Wiger authored
593 %%
9c505d4 eunit tests for get_env/4 et al
Ulf Wiger authored
594 ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env])),
595 ?assertEqual("s2", gproc:get_env(l, gproc, ssss, [os_env])),
596 ?assertEqual("s1", gproc:get_env(l, gproc, ssss, [app_env, os_env])),
597 ?assertEqual("s3", gproc:get_env(l, gproc, ssss, [{os_env,"TTTT"}])),
af14ad2 doc additions, inherit by name, init_arg
Ulf Wiger authored
598 ?assertEqual("s4", gproc:get_env(l, gproc, ssss, [{default,"s4"}])),
599 %%
600 ?assertEqual({atomic,ok}, mnesia:create_table(t, [{ram_copies, [node()]}])),
601 ?assertEqual(ok, mnesia:dirty_write({t, foo, bar})),
602 ?assertEqual(bar, gproc:get_env(l, gproc, some_env, [{mnesia,transaction,
603 {t, foo}, 3}])),
604 ?assertEqual("erl", gproc:get_env(l, gproc, progname, [init_arg])).
9c505d4 eunit tests for get_env/4 et al
Ulf Wiger authored
605
606 t_get_set_env() ->
607 ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
608 ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
609 ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
610 ?assertEqual(a, gproc:get_env(l, gproc, aaaa, [error])).
611
612 t_set_env() ->
613 ?assertEqual(ok, application:set_env(gproc, aaaa, a)),
614 ?assertEqual(a, gproc:get_set_env(l, gproc, aaaa, [app_env])),
615 ?assertEqual(ok, application:set_env(gproc, aaaa, undefined)),
616 ?assertEqual(b, gproc:set_env(l, gproc, aaaa, b, [app_env])),
617 ?assertEqual({ok,b}, application:get_env(gproc, aaaa)),
618 %%
619 ?assertEqual(true, os:putenv("SSSS", "s0")),
620 ?assertEqual("s0", gproc:get_env(l, gproc, ssss, [os_env])),
621 ?assertEqual("s1", gproc:set_env(l, gproc, ssss, "s1", [os_env])),
622 ?assertEqual("s1", os:getenv("SSSS")),
623 ?assertEqual(true, os:putenv("SSSS", "s0")),
624 ?assertEqual([{self(),"s1"}],
2a5aed4 set_env can now also update mnesia
Ulf Wiger authored
625 gproc:lookup_values({p,l,{gproc_env,gproc,ssss}})),
626 %%
627 ?assertEqual({atomic,ok}, mnesia:create_table(t_set_env,
628 [{ram_copies,[node()]}])),
629 ?assertEqual(ok, mnesia:dirty_write({t_set_env, a, 1})),
630 ?assertEqual(2, gproc:set_env(l, gproc, a, 2, [{mnesia,async_dirty,
631 {t_set_env,a},3}])),
632 ?assertEqual([{t_set_env,a,2}], mnesia:dirty_read({t_set_env,a})),
633 %% non-existing mnesia obj
634 ?assertEqual(3, gproc:set_env(l, gproc, b, 3, [{mnesia,async_dirty,
635 {t_set_env,b},3}])),
636 ?assertEqual([{t_set_env,b,3}], mnesia:dirty_read({t_set_env,b})).
9c505d4 eunit tests for get_env/4 et al
Ulf Wiger authored
637
af14ad2 doc additions, inherit by name, init_arg
Ulf Wiger authored
638 t_get_env_inherit() ->
639 P = spawn_link(fun() ->
640 ?assertEqual(bar, gproc:set_env(l,gproc,foo,bar,[])),
641 gproc:reg({n,l,get_env_p}),
642 t_loop()
643 end),
644 ?assertEqual({P,undefined}, gproc:await({n,l,get_env_p},1000)),
645 ?assertEqual(bar, gproc:get_env(l, gproc, foo, [{inherit, P}])),
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
646 ?assertEqual(bar, gproc:get_env(l, gproc, foo,
647 [{inherit, {n,l,get_env_p}}])),
af14ad2 doc additions, inherit by name, init_arg
Ulf Wiger authored
648 ?assertEqual(ok, t_call(P, die)).
649
b4aa843 @uwiger gproc:info(P,current_function) return value
authored
650 %% What we test here is that we return the same current_function as the
651 %% process_info() BIF. As we parse the backtrace dump, we check with some
652 %% weirdly named functions.
653 t_gproc_info() ->
654 {A,B} = '-t1-'(),
655 ?assertEqual(A,B),
656 {C,D} = '\'t2'(),
657 ?assertEqual(C,D),
658 {E,F} = '\'t3\''(),
659 ?assertEqual(E,F),
660 {G,H} = t4(),
661 ?assertEqual(G,H).
662
663 '-t1-'() ->
664 {_, I0} = process_info(self(), current_function),
665 {_, I} = gproc:info(self(), current_function),
666 {I0, I}.
667
668 '\'t2'() ->
669 {_, I0} = process_info(self(), current_function),
670 {_, I} = gproc:info(self(), current_function),
671 {I0, I}.
672
673 '\'t3\''() ->
674 {_, I0} = process_info(self(), current_function),
675 {_, I} = gproc:info(self(), current_function),
676 {I0, I}.
677
678
679 t4() ->
680 {_, I0} = process_info(self(), current_function),
681 {_, I} = gproc:info(self(), current_function),
682 {I0, I}.
683
a4d7f6f @uwiger subscription, monitor local,dist + test cases
authored
684 t_monitor() ->
685 Me = self(),
686 P = spawn_link(fun() ->
687 gproc:reg({n,l,a}),
688 Me ! continue,
689 t_loop()
690 end),
691 receive continue ->
692 ok
693 end,
694 Ref = gproc:monitor({n,l,a}),
695 ?assertEqual(ok, t_call(P, die)),
696 receive
697 M ->
698 ?assertEqual({gproc,unreg,Ref,{n,l,a}}, M)
699 end.
700
701 t_monitor_give_away() ->
702 Me = self(),
703 P = spawn_link(fun() ->
704 gproc:reg({n,l,a}),
705 Me ! continue,
706 t_loop()
707 end),
708 receive continue ->
709 ok
710 end,
711 Ref = gproc:monitor({n,l,a}),
712 ?assertEqual(ok, t_call(P, {give_away, {n,l,a}})),
713 receive
714 M ->
715 ?assertEqual({gproc,{migrated,Me},Ref,{n,l,a}}, M)
716 end,
717 ?assertEqual(ok, t_call(P, die)).
718
719 t_subscribe() ->
720 Key = {n,l,a},
721 ?assertEqual(ok, gproc_monitor:subscribe(Key)),
722 ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
723 P = spawn_link(fun() ->
724 gproc:reg({n,l,a}),
725 t_loop()
726 end),
727 ?assertEqual({gproc_monitor, Key, P}, get_msg()),
728 ?assertEqual(ok, t_call(P, {give_away, Key})),
729 ?assertEqual({gproc_monitor, Key, {migrated,self()}}, get_msg()),
730 gproc:give_away(Key, P),
731 ?assertEqual({gproc_monitor, Key, {migrated,P}}, get_msg()),
732 ?assertEqual(ok, t_call(P, die)),
733 ?assertEqual({gproc_monitor, Key, undefined}, get_msg()),
734 ?assertEqual(ok, gproc_monitor:unsubscribe(Key)).
735
736 get_msg() ->
737 receive M ->
738 M
739 after 1000 ->
740 timeout
741 end.
b4aa843 @uwiger gproc:info(P,current_function) return value
authored
742
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
743 t_loop() ->
744 receive
745 {From, {give_away, Key}} ->
746 ?assertEqual(From, gproc:give_away(Key, From)),
747 From ! {self(), ok},
748 t_loop();
749 {From, die} ->
750 From ! {self(), ok}
751 end.
752
753 t_call(P, Msg) ->
754 P ! {self(), Msg},
755 receive
756 {P, Reply} ->
757 Reply
758 end.
759
760 spawn_helper() ->
761 Parent = self(),
762 P = spawn(fun() ->
763 ?assert(gproc:reg({n,l,self()}) =:= true),
764 Ref = erlang:monitor(process, Parent),
765 Parent ! {ok,self()},
669e1b2 added performance tuning options
Ulf Wiger authored
766 receive
2393bf4 Added gproc:give_away/2 and introduced eunit tests for gproc_dist
Ulf Wiger authored
767 {'DOWN', Ref, _, _, _} ->
768 ok
769 end
770 end),
771 receive
772 {ok,P} ->
773 P
774 end.
775
776 give_gproc_some_time(T) ->
777 timer:sleep(T),
778 sys:get_status(gproc).
779
780 -endif.
Something went wrong with that request. Please try again.