1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2010-2017. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21%%
22%% Tests of the server implemented by diameter_reg.erl.
23%%
24
25-module(diameter_reg_SUITE).
26
27-export([suite/0,
28         all/0,
29         groups/0,
30         init_per_suite/1,
31         end_per_suite/1]).
32
33%% testcases
34-export([add/1,
35         add_new/1,
36         remove/1,
37         down/1,
38         terms/1,
39         pids/1]).
40
41-define(reg,  diameter_reg).
42-define(util, diameter_util).
43
44%% ===========================================================================
45
46suite() ->
47    [{timetrap, {seconds, 60}}].
48
49all() ->
50    [{group, all},
51     {group, all, [parallel]}].
52
53groups() ->
54    [{all, [], tc()}].
55
56tc() ->
57    [add,
58     add_new,
59     remove,
60     down,
61     terms,
62     pids].
63
64init_per_suite(Config) ->
65    ok = diameter:start(),
66    Config.
67
68end_per_suite(_Config) ->
69    ok = diameter:stop().
70
71%% ===========================================================================
72
73add(_) ->
74    Ref = make_ref(),
75    true = ?reg:add(Ref),
76    true = ?reg:add(Ref),
77    [{Ref, Pid}] = ?reg:match(Ref),
78    Pid = self().
79
80add_new(_) ->
81    Ref = make_ref(),
82    true = ?reg:add_new(Ref),
83    false = ?reg:add_new(Ref).
84
85remove(_) ->
86    Ref = make_ref(),
87    true = ?reg:add_new(Ref),
88    true = ?reg:add_new({Ref}),
89    true = ?reg:remove({Ref}),
90    [{Ref, Pid}] = ?reg:match(Ref),
91    Pid = self().
92
93down(_) ->
94    Ref = make_ref(),
95    {_, MRef} = spawn_monitor(fun() -> ?reg:add_new(Ref), timer:sleep(1000) end),
96    receive {'DOWN', MRef, process, _, _} -> ok end,
97    timer:sleep(1000),
98    [] = ?reg:match(Ref).
99
100terms(_) ->
101    Ref = make_ref(),
102    true = ?reg:add_new(Ref),
103    [[Pid]] = [L || {T,L} <- ?reg:terms(), T == Ref],
104    Pid = self().
105
106pids(_) ->
107    Ref = make_ref(),
108    true = ?reg:add_new(Ref),
109    %% Don't match [[Ref]] since this will only necessarily be the
110    %% case when the test is run in its own process.
111    [_|_] = [L || {P,L} <- ?reg:pids(), P == self()].
112