<commit_before>%%%-------------------------------------------------------------------
%%% @author tihon
%%% @copyright (C) 2015, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 20. Jul 2015 14:11
%%%-------------------------------------------------------------------
-module(erwa_realms_test).
-author("tihon").

-include_lib("eunit/include/eunit.hrl").
-import(erwa_realms,
[
start/0,
stop/0,
set_autocreate/1,
get_routing/1,
kill/1,
add/1,
add/2,
get_middleware_list/1,
shutdown/1
]).

start_stop_test() ->
  {ok, _} = start(),
  {ok, stopped} = stop().

environment_test() ->
  application:set_env(erwa, router_middleware, [erwa_mw_allow]),
  [erwa_mw_allow] = application:get_env(erwa, router_middleware, [erwa_mw_default]),
  application:unset_env(erwa, router_middleware),
  [erwa_mw_default] = application:get_env(erwa, router_middleware, [erwa_mw_default]).

garbage_test() ->
  {ok, Pid} = start(),
  ignored = gen_server:call(?MODULE, some_garbage),
  ok = gen_server:cast(?MODULE, some_garbage),
  Pid ! some_garbage,
  {ok, stopped} = stop().

add_remove_test() ->
  {ok, _} = erwa_routing_sup:start_link(),
  {ok, _} = start(),
  Name1 = <<"com.doesnotexist.wamp">>,
  Name2 = <<"com.doesnotexist.pamw">>,
  MWL = [erwa_mw_allow],
  ok = set_autocreate(false),
  0 = get_tablesize(),
  {error, not_found} = get_routing(Name1),
  0 = get_tablesize(),
  {error, not_running} = kill(Name1),
  0 = get_tablesize(),
  ok = add(Name1),
  2 = get_tablesize(),
  {error, already_exists} = add(Name1),
  2 = get_tablesize(),
  ok = add(Name2, MWL),
  4 = get_tablesize(),
  {ok, _} = get_routing(Name1),
  {ok, _} = get_middleware_list(Name1),
  {ok, _} = get_routing(Name2),
  {ok, killed} = kill(Name1),
  ok = ensure_tablesize(2, 5000),
  {error, not_found} = get_routing(Name1),
  {ok, shutting_down} = shutdown(Name2),
  ok = ensure_tablesize(0, 5000),
  {error, not_found} = get_routing(Name2),
  ok = set_autocreate(true),
  {ok, _} = get_routing(Name1),
  ok = set_autocreate(false),
  2 = get_tablesize(),
  {ok, killed} = kill(Name1),
  ok = ensure_tablesize(0, 5000),
  timeout = ensure_tablesize(5, 10),
  {ok, stopped} = stop().


get_tablesize() ->
  Pid = whereis(?MODULE),
  Tables = ets:all(),
  [Table] = lists:filter(fun(T) -> ets:info(T, owner) == Pid end, Tables),
  ets:info(Table, size).


ensure_tablesize(_Number, MaxTime) when MaxTime =< 0 ->
  timeout;
ensure_tablesize(Number, MaxTime) ->
  case get_tablesize() of
    Number -> ok;
    _ ->
      receive
      after 10 -> ok
      end,
      NewTime = MaxTime - 10,
      ensure_tablesize(Number, NewTime)
  end.<commit_msg>small run fix in test<commit_after>%%%-------------------------------------------------------------------
%%% @author tihon
%%% @copyright (C) 2015, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 20. Jul 2015 14:11
%%%-------------------------------------------------------------------
-module(erwa_realms_test).
-author("tihon").

-include_lib("eunit/include/eunit.hrl").

start_stop_test() ->
  {ok, _} = erwa_realms:start(),
  {ok, stopped} = erwa_realms:stop().

environment_test() ->
  application:set_env(erwa, router_middleware, [erwa_mw_allow]),
  [erwa_mw_allow] = application:get_env(erwa, router_middleware, [erwa_mw_default]),
  application:unset_env(erwa, router_middleware),
  [erwa_mw_default] = application:get_env(erwa, router_middleware, [erwa_mw_default]).

garbage_test() ->
  {ok, Pid} = erwa_realms:start(),
  ignored = gen_server:call(erwa_realms, some_garbage),
  ok = gen_server:cast(erwa_realms, some_garbage),
  Pid ! some_garbage,
  {ok, stopped} = erwa_realms:stop().

add_remove_test() ->  %TODO functional?
  {ok, _} = erwa_routing_sup:start_link(),
  {ok, _} = erwa_realms:start(),
  Name1 = <<"com.doesnotexist.wamp">>,
  Name2 = <<"com.doesnotexist.pamw">>,
  MWL = [erwa_mw_allow],
  ok = erwa_realms:set_autocreate(false),
  0 = get_tablesize(),
  {error, not_found} = erwa_realms:get_routing(Name1),
  0 = get_tablesize(),
  {error, not_running} = erwa_realms:kill(Name1),
  0 = get_tablesize(),
  ok = erwa_realms:add(Name1),
  2 = get_tablesize(),
  {error, already_exists} = erwa_realms:add(Name1),
  2 = get_tablesize(),
  ok = erwa_realms:add(Name2, MWL),
  4 = get_tablesize(),
  {ok, _} = erwa_realms:get_routing(Name1),
  {ok, _} = erwa_realms:get_middleware_list(Name1),
  {ok, _} = erwa_realms:get_routing(Name2),
  {ok, killed} = erwa_realms:kill(Name1),
  ok = ensure_tablesize(2, 5000),
  {error, not_found} = erwa_realms:get_routing(Name1),
  {ok, shutting_down} = erwa_realms:shutdown(Name2),
  ok = ensure_tablesize(0, 5000),
  {error, not_found} = erwa_realms:get_routing(Name2),
  ok = erwa_realms:set_autocreate(true),
  {ok, _} = erwa_realms:get_routing(Name1),
  ok = erwa_realms:set_autocreate(false),
  2 = get_tablesize(),
  {ok, killed} = erwa_realms:kill(Name1),
  ok = ensure_tablesize(0, 5000),
  timeout = ensure_tablesize(5, 10),
  {ok, stopped} = erwa_realms:stop().

%% @private
get_tablesize() ->
  Pid = whereis(erwa_realms),
  Tables = ets:all(),
  [Table] = lists:filter(fun(T) -> ets:info(T, owner) == Pid end, Tables),
  ets:info(Table, size).

%% @private
ensure_tablesize(_Number, MaxTime) when MaxTime =< 0 ->
  timeout;
ensure_tablesize(Number, MaxTime) ->
  case get_tablesize() of
    Number -> ok;
    _ ->
      timer:sleep(10),
      NewTime = MaxTime - 10,
      ensure_tablesize(Number, NewTime)
  end.
{'fraction_non_alphanumeric': 0.2149082143519377, 'fraction_numerical': 0.02391989616169108, 'mean_word_length': 3.7027027027027026, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6415732', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2106, 'n_words': 359}