1: %%
    2: %% %CopyrightBegin%
    3: %% 
    4: %% Copyright Ericsson AB 1996-2013. All Rights Reserved.
    5: %% 
    6: %% The contents of this file are subject to the Erlang Public License,
    7: %% Version 1.1, (the "License"); you may not use this file except in
    8: %% compliance with the License. You should have received a copy of the
    9: %% Erlang Public License along with this software. If not, it can be
   10: %% retrieved online at http://www.erlang.org/.
   11: %% 
   12: %% Software distributed under the License is distributed on an "AS IS"
   13: %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
   14: %% the License for the specific language governing rights and limitations
   15: %% under the License.
   16: %% 
   17: %% %CopyrightEnd%
   18: %%
   19: -module(sys_SUITE).
   20: -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
   21: 	 init_per_group/2,end_per_group/2,log/1,log_to_file/1,
   22: 	 stats/1,trace/1,suspend/1,install/1]).
   23: -export([handle_call/3,terminate/2,init/1]).
   24: -include_lib("test_server/include/test_server.hrl").
   25: 
   26: -define(server,sys_SUITE_server).
   27: 
   28: 
   29: %% Doesn't look into change_code at all
   30: %% Doesn't address writing your own process that understands
   31: %% system messages at all.
   32: 
   33: 
   34: suite() -> [{ct_hooks,[ts_install_cth]}].
   35: 
   36: all() -> 
   37:     [log, log_to_file, stats, trace, suspend, install].
   38: 
   39: groups() -> 
   40:     [].
   41: 
   42: init_per_suite(Config) ->
   43:     Config.
   44: 
   45: end_per_suite(_Config) ->
   46:     ok.
   47: 
   48: init_per_group(_GroupName, Config) ->
   49:     Config.
   50: 
   51: end_per_group(_GroupName, Config) ->
   52:     Config.
   53: 
   54: 
   55: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   56: 
   57: log(suite) -> [];
   58: log(Config) when is_list(Config) ->
   59:     {ok,_Server} = start(),
   60:     ok = sys:log(?server,true),
   61:     {ok,-44} = public_call(44),
   62:     ok = sys:log(?server,false),
   63:     ok = sys:log(?server,print),
   64:     stop(),
   65:     ok.
   66: 
   67: log_to_file(suite) -> [];
   68: log_to_file(Config) when is_list(Config) ->
   69:     TempName = test_server:temp_name(?config(priv_dir,Config) ++ "sys."),
   70:     {ok,_Server} = start(),
   71:     ok = sys:log_to_file(?server,TempName),
   72:     {ok,-44} = public_call(44),
   73:     ok = sys:log_to_file(?server,false),
   74:     {ok,Fd} = file:open(TempName,[read]),
   75:     Msg1 = io:get_line(Fd,''),
   76:     Msg2 = io:get_line(Fd,''),
   77:     file:close(Fd),
   78:     lists:prefix("*DBG* sys_SUITE_server got call {req,44} from ",Msg1),
   79:     lists:prefix("*DBG* sys_SUITE_server sent {ok,-44} to ",Msg2),
   80:     stop(),
   81:     ok.
   82: 
   83: stats(suite) -> [];
   84: stats(Config) when is_list(Config) ->
   85:     Self = self(),
   86:     {ok,_Server} = start(),
   87:     ok = sys:statistics(?server,true),
   88:     {ok,-44} = public_call(44),
   89:     {ok,Stats} = sys:statistics(?server,get),
   90:     lists:member({messages_in,1},Stats),
   91:     lists:member({messages_out,1},Stats),
   92:     ok = sys:statistics(?server,false),
   93:     {status,_Pid,{module,_Mod},[_PDict,running,Self,_,_]} =
   94: 	sys:get_status(?server),
   95:     {ok,no_statistics} = sys:statistics(?server,get),
   96:     stop(),
   97:     ok.
   98: 
   99: trace(suite) -> [];
  100: trace(Config) when is_list(Config) ->
  101:     {ok,_Server} = start(),
  102:     test_server:sleep(2000),
  103:     test_server:capture_start(),
  104:     sys:trace(?server,true),
  105:     {ok,-44} = public_call(44),
  106:     %% ho, hum, allow for the io to reach us..
  107:     test_server:sleep(1000),
  108:     test_server:capture_stop(),
  109:     [Msg1,Msg2] = test_server:capture_get(),
  110:     lists:prefix("*DBG* sys_SUITE_server got call {req,44} from ",Msg1),
  111:     lists:prefix("*DBG* sys_SUITE_server sent {ok,-44} to ",Msg2),
  112:     stop(),
  113:     ok.
  114: 
  115: suspend(suite) -> [];
  116: suspend(Config) when is_list(Config) ->
  117:     ?line {ok,_Server} = start(),
  118:     ?line sys:suspend(?server,1000),
  119:     ?line {'EXIT',_} = (catch public_call(48)),
  120:     ?line {status,_,_,[_,suspended,_,_,_]} = sys:get_status(?server),
  121:     ?line sys:suspend(?server,1000), %% doing it twice is no error
  122:     ?line {'EXIT',_} = (catch public_call(48)),
  123:     ?line sys:resume(?server),
  124:     ?line {status,_,_,[_,running,_,_,_]} = sys:get_status(?server),
  125:     ?line {ok,-48} = (catch public_call(48)),
  126:     ?line sys:resume(?server), %% doing it twice is no error
  127:     ?line {ok,-48} = (catch public_call(48)),
  128:     ?line stop(),
  129:     ok.
  130: 
  131: install(suite) -> [];
  132: install(Config) when is_list(Config) ->
  133:     ?line {ok,_Server} = start(),
  134:     ?line Master = self(),
  135:     ?line SpyFun = 
  136: 	fun(func_state,Event,ProcState) ->
  137: 		case Event of
  138: 		    {in,{'$gen_call',_From,{req,Arg}}} ->
  139: 			io:format("Trigged\n"),
  140: 			Master ! {spy_got,{request,Arg},ProcState};
  141: 		    Other ->
  142: 			io:format("Trigged other=~p\n",[Other])
  143: 		end
  144: 	end,
  145:     ?line sys:install(?server,{SpyFun,func_state}),
  146:     ?line {ok,-1} = (catch public_call(1)),
  147:     ?line sys:no_debug(?server),
  148:     ?line {ok,-2} = (catch public_call(2)),
  149:     ?line sys:install(?server,{SpyFun,func_state}),
  150:     ?line sys:install(?server,{SpyFun,func_state}),
  151:     ?line {ok,-3} = (catch public_call(3)),
  152:     ?line sys:remove(?server,SpyFun),
  153:     ?line {ok,-4} = (catch public_call(4)),
  154:     ?line Msgs = test_server:messages_get(),
  155:     ?line [{spy_got,{request,1},sys_SUITE_server},
  156: 	   {spy_got,{request,3},sys_SUITE_server}] = Msgs,
  157:     ?line stop(),
  158:     ok.
  159: 
  160: %%%%%%%%%%%%%%%%%%%%
  161: %% Dummy server
  162: 
  163: public_call(Arg) ->
  164:     gen_server:call(?server,{req,Arg},1000).
  165: 
  166: start() ->
  167:     gen_server:start_link({local,?server},?MODULE,[],[]).
  168: 
  169: stop() ->
  170:     gen_server:call(?server,stop,1000).
  171: 
  172: init([]) ->
  173:     {ok,0}.
  174: 
  175: handle_call({req,Arg},_From,State) ->
  176:     NewState = State+1,
  177:     {reply,{ok,-Arg},NewState};
  178: handle_call(stop,_From,State) ->
  179:     {stop,normal,ok,State}.
  180: 
  181: terminate(_Reason, _State) ->
  182:     ok.
  183: 
  184: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%