1: %%
    2: %% %CopyrightBegin%
    3: %% 
    4: %% Copyright Ericsson AB 2007-2011. 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: 
   20: -module(erl_drv_thread_SUITE).
   21: -author('rickard.s.green@ericsson.com').
   22: -export([all/0, suite/0,groups/0,init_per_suite/1, end_per_suite/1, 
   23: 	 init_per_group/2,end_per_group/2]).
   24: 
   25: -export([basic/1, rwlock/1, tsd/1]).
   26: 
   27: -include_lib("test_server/include/test_server.hrl").
   28: 
   29: -define(DEFAULT_TIMETRAP_SECS, 240).
   30: 
   31: suite() -> [{ct_hooks,[ts_install_cth]}].
   32: 
   33: all() -> 
   34:     [basic, rwlock, tsd].
   35: 
   36: groups() -> 
   37:     [].
   38: 
   39: init_per_suite(Config) ->
   40:     Config.
   41: 
   42: end_per_suite(_Config) ->
   43:     ok.
   44: 
   45: init_per_group(_GroupName, Config) ->
   46:     Config.
   47: 
   48: end_per_group(_GroupName, Config) ->
   49:     Config.
   50: 
   51: 
   52: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   53: %%                                                                        %%
   54: %% Testcases                                                              %%
   55: %%                                                                        %%
   56: 
   57: basic(suite) -> [];
   58: basic(doc) ->   [];
   59: basic(Cfg) -> ?line drv_case(Cfg, basic).
   60: 
   61: rwlock(suite) -> [];
   62: rwlock(doc) ->   [];
   63: rwlock(Cfg) -> ?line drv_case(Cfg, rwlock).
   64: 
   65: tsd(suite) -> [];
   66: tsd(doc) ->   [];
   67: tsd(Cfg) -> ?line drv_case(Cfg, tsd).
   68: 
   69: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   70: %%                                                                        %%
   71: %% Internal functions                                                     %%
   72: %%                                                                        %%
   73: 
   74: drv_case(Config, CaseName) ->
   75:     drv_case(Config, CaseName, "").
   76: 
   77: drv_case(Config, CaseName, TimeTrap) when is_integer(TimeTrap) ->
   78:     drv_case(Config, CaseName, "", TimeTrap);
   79: drv_case(Config, CaseName, Command) when is_list(Command) ->
   80:     drv_case(Config, CaseName, Command, ?DEFAULT_TIMETRAP_SECS).
   81: 
   82: drv_case(Config, CaseName, TimeTrap, Command) when is_list(Command),
   83: 						   is_integer(TimeTrap) ->
   84:     drv_case(Config, CaseName, Command, TimeTrap);
   85: drv_case(Config, CaseName, Command, TimeTrap) when is_list(Config),
   86: 						   is_atom(CaseName),
   87: 						   is_list(Command),
   88: 						   is_integer(TimeTrap) ->
   89:     case ?t:os_type() of
   90: 	{Family, _} when Family == unix; Family == win32 ->
   91: 	    ?line run_drv_case(Config, CaseName, Command, TimeTrap);
   92: 	SkipOs ->
   93: 	    ?line {skipped,
   94: 		   lists:flatten(["Not run on "
   95: 				  | io_lib:format("~p",[SkipOs])])}
   96:     end.
   97: 
   98: run_drv_case(Config, CaseName, Command, TimeTrap) ->
   99:     ?line Dog = test_server:timetrap(test_server:seconds(TimeTrap)),
  100:     ?line DataDir = ?config(data_dir,Config),
  101:     case erl_ddll:load_driver(DataDir, CaseName) of
  102: 	ok -> ok;
  103: 	{error, Error} ->
  104: 	    io:format("~s\n", [erl_ddll:format_error(Error)]),
  105: 	    ?line ?t:fail()
  106:     end,
  107:     ?line Port = open_port({spawn, atom_to_list(CaseName)}, []),
  108:     ?line true = is_port(Port),
  109:     ?line Port ! {self(), {command, Command}},
  110:     ?line Result = receive_drv_result(Port, CaseName),
  111:     ?line Port ! {self(), close},
  112:     ?line receive 
  113: 	      {Port, closed} ->
  114: 		  ok
  115: 	  end,
  116:     ?line ok = erl_ddll:unload_driver(CaseName),
  117:     ?line test_server:timetrap_cancel(Dog),
  118:     ?line Result.
  119: 
  120: receive_drv_result(Port, CaseName) ->
  121:     ?line receive
  122: 	      {print, Port, CaseName, Str} ->
  123: 		  ?line ?t:format("~s", [Str]),
  124: 		  ?line receive_drv_result(Port, CaseName);
  125: 	      {'EXIT', Port, Error} ->
  126: 		  ?line ?t:fail(Error);
  127: 	      {'EXIT', error, Error} ->
  128: 		  ?line ?t:fail(Error);
  129: 	      {failed, Port, CaseName, Comment} ->
  130: 		  ?line ?t:fail(Comment);
  131: 	      {skipped, Port, CaseName, Comment} ->
  132: 		  ?line {skipped, Comment};
  133: 	      {succeeded, Port, CaseName, ""} ->
  134: 		  ?line succeeded;
  135: 	      {succeeded, Port, CaseName, Comment} ->
  136: 		  ?line {comment, Comment}
  137: 	  end.