1: %%==============================================================================
    2: %% Copyright 2013 Erlang Solutions Ltd.
    3: %%
    4: %% Licensed under the Apache License, Version 2.0 (the "License");
    5: %% you may not use this file except in compliance with the License.
    6: %% You may obtain a copy of the License at
    7: %%
    8: %% http://www.apache.org/licenses/LICENSE-2.0
    9: %%
   10: %% Unless required by applicable law or agreed to in writing, software
   11: %% distributed under the License is distributed on an "AS IS" BASIS,
   12: %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   13: %% See the License for the specific language governing permissions and
   14: %% limitations under the License.
   15: %%==============================================================================
   16: 
   17: -module(metrics_session_SUITE).
   18: -compile([export_all, nowarn_export_all]).
   19: 
   20: -include_lib("escalus/include/escalus.hrl").
   21: -include_lib("common_test/include/ct.hrl").
   22: 
   23: -define(RT_WINDOW, 3).  % seconds
   24: 
   25: -import(metrics_helper, [assert_counter/2,
   26:                          assert_counter/3,
   27:                          get_counter_value/1,
   28:                          wait_for_counter/2,
   29:                          wait_for_counter/3]).
   30: %%--------------------------------------------------------------------
   31: %% Suite configuration
   32: %%--------------------------------------------------------------------
   33: 
   34: all() ->
   35:     [{group, session},
   36:      {group, session_global}].
   37: 
   38: groups() ->
   39:     [{session, [sequence], [login_one,
   40:                             login_many,
   41:                             auth_failed]},
   42:      {session_global, [sequence], [session_global,
   43:                                    session_unique]}].
   44: 
   45: suite() ->
   46:     [{require, ejabberd_node} | escalus:suite()].
   47: 
   48: %%--------------------------------------------------------------------
   49: %% Init & teardown
   50: %%--------------------------------------------------------------------
   51: 
   52: init_per_suite(Config) ->
   53:     escalus:init_per_suite(Config).
   54: 
   55: end_per_suite(Config) ->
   56:     escalus:end_per_suite(Config).
   57: 
   58: init_per_group(_GroupName, Config) ->
   59:     escalus:create_users(Config, escalus:get_users([alice, bob])).
   60: 
   61: end_per_group(_GroupName, Config) ->
   62:     escalus:delete_users(Config, escalus:get_users([alice, bob])).
   63: 
   64: init_per_testcase(CaseName, Config) ->
   65:     escalus:init_per_testcase(CaseName, Config).
   66: 
   67: end_per_testcase(CaseName, Config) ->
   68:     escalus:end_per_testcase(CaseName, Config).
   69: 
   70: %%--------------------------------------------------------------------
   71: %% Tests
   72: %%--------------------------------------------------------------------
   73: 
   74: 
   75: login_one(Config) ->
   76:     {value, Logins} = get_counter_value(sessionSuccessfulLogins),
   77:     escalus:story(Config, [{alice, 1}], fun(Alice) ->
   78: 
   79:         assert_counter(1, sessionCount),
   80:         assert_counter(Logins + 1, sessionSuccessfulLogins),
   81: 
   82:         {value, Logouts} = get_counter_value(sessionLogouts),
   83:         escalus_client:stop(Config, Alice),
   84:         wait_for_counter(0, sessionCount),
   85:         wait_for_counter(Logouts + 1, sessionLogouts)
   86: 
   87:     end).
   88: 
   89: login_many(Config) ->
   90:     {value, Logins} = get_counter_value(sessionSuccessfulLogins),
   91:     escalus:story(Config, [{alice, 1}, {bob, 1}], fun(_Alice, _Bob) ->
   92: 
   93:         assert_counter(2, sessionCount),
   94:         assert_counter(Logins + 2, sessionSuccessfulLogins)
   95: 
   96:         end).
   97: 
   98: auth_failed(Config) ->
   99:     {value, AuthFails} = get_counter_value(sessionAuthFails),
  100: 
  101:     [{_, UserSpec} | _] = escalus_config:get_config(escalus_users, Config),
  102:     UserSpecM = proplists:delete(password, UserSpec) ++ [{password, <<"mazabe">>}],
  103: 
  104:     {error, _} = escalus_client:start(Config, UserSpecM, <<"res1">>),
  105:     assert_counter(0, sessionCount),
  106:     assert_counter(AuthFails + 1, sessionAuthFails).
  107: 
  108: %% Global
  109: 
  110: session_global(Config) ->
  111:     escalus:story(Config, [{alice, 1}], fun(_Alice) ->
  112:         metrics_helper:sample(totalSessionCount),
  113:         wait_for_counter(global, 1, totalSessionCount)
  114:         end).
  115: 
  116: session_unique(Config) ->
  117:     escalus:story(Config, [{alice, 2}], fun(_Alice1, _Alice2) ->
  118:         metrics_helper:sample(uniqueSessionCount),
  119:         metrics_helper:sample(totalSessionCount),
  120:         wait_for_counter(global, 1, uniqueSessionCount),
  121:         wait_for_counter(global, 2, totalSessionCount)
  122:         end).