aboutsummaryrefslogtreecommitdiffstats
path: root/lib/diameter/test/diameter_pool_SUITE.erl
blob: a59cd66a2ee422e19550f1b1f3f3180620fc252b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2015. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%

%%
%% Test of the pool_size option in connecting nodes with multiple
%% connections.
%%

-module(diameter_pool_SUITE).

-export([suite/0,
         all/0,
         init_per_testcase/2,
         end_per_testcase/2,
         init_per_suite/1,
         end_per_suite/1]).

%% testcases
-export([tcp_connect/1,
         sctp_connect/1,
         any_connect/1]).

%% ===========================================================================

-define(util, diameter_util).

%% Config for diameter:start_service/2.
-define(SERVICE(Host),
        [{'Origin-Host', Host ++ ".ericsson.com"},
         {'Origin-Realm', "ericsson.com"},
         {'Host-IP-Address', [{127,0,0,1}]},
         {'Vendor-Id', 12345},
         {'Product-Name', "OTP/diameter"},
         {'Auth-Application-Id', [0]},  %% common
         {'Acct-Application-Id', [3]},  %% accounting
         {restrict_connections, false},
         {application, [{alias, common},
                        {dictionary, diameter_gen_base_rfc6733},
                        {module, diameter_callback}]},
         {application, [{alias, accounting},
                        {dictionary, diameter_gen_acct_rfc6733},
                        {module, diameter_callback}]}]).

%% ===========================================================================

suite() ->
    [{timetrap, {seconds, 30}}].

all() ->
    [tcp_connect,
     sctp_connect,
     any_connect].

init_per_testcase(_Name, Config) ->
    Config.

end_per_testcase(_Name, _Config) ->
    diameter:stop().

init_per_suite(Config) ->
    [{sctp, ?util:have_sctp()} | Config].

end_per_suite(_Config) ->
    ok.

%% ===========================================================================

tcp_connect(_Config) ->
    connect(tcp, tcp).

sctp_connect(Config) ->
    case lists:member({sctp, true}, Config) of
       true  -> connect(sctp, sctp);
       false -> {skip, no_sctp}
    end.

any_connect(_Config) ->
    connect(any, tcp).

%% connect/2

%% Establish multiple connections between a client and server.
connect(ClientProt, ServerProt) ->
    ok = diameter:start(),
    [] = [{S,T} || S <- ["server", "client"],
                   T <- [diameter:start_service(S, ?SERVICE(S))],
                   T /= ok],
    %% Listen with a single transport with pool_size = 4. Ensure the
    %% expected number of transport processes are started.
    LRef = ?util:listen("server", ServerProt, [{pool_size, 4}]),
    {4,0} = count("server", LRef, accept), %% 4 transports, no connections
    %% Establish 5 connections.
    Ref = ?util:connect("client", ClientProt, LRef, [{pool_size, 5}]),
    {5,5} = count("client", Ref, pool),    %% 5 connections
    %% Ensure the server has started replacement transports within a
    %% reasonable time. Sleepsince there's no guarantee the
    %% replacements have been started before the client has received
    %% 'up' events. (Although it's likely.)
    sleep(),
    {9,5} = count("server", LRef, accept), %% 5 connections + 4 accepting
    %% Ensure ther are still the expected number of accepting transports
    %% after stopping the client service.
    ok = diameter:stop_service("client"),
    sleep(),
    {4,0} = count("server", LRef, accept), %% 4 transports, no connections
    %% Done.
    ok = diameter:stop_service("server").

count(Name, Ref, Key) ->
    [{transport, [[{ref, Ref} | T]]},
     {connections, Cs}]
        = diameter:service_info(Name, [transport, connections]),
    {Key, Ps} = lists:keyfind(Key, 1, T),
    {length(Ps), length(Cs)}.  %% number of processes, connections

sleep() ->
    receive after 1000 -> ok end.