You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

  1. -module(crash_statem).
  2. %% we're only going to compile this on OTP 19+
  3. -ifdef(test_statem).
  4. -behaviour(gen_statem).
  5. -export([
  6. start/0,
  7. crash/0,
  8. stop/1,
  9. timeout/0,
  10. handle_event/4
  11. ]).
  12. -export([terminate/3,code_change/4,init/1,callback_mode/0]).
  13. start() ->
  14. gen_statem:start({local,?MODULE}, ?MODULE, [], []).
  15. crash() ->
  16. gen_statem:call(?MODULE, boom).
  17. stop(Reason) ->
  18. gen_statem:call(?MODULE, {stop, Reason}).
  19. timeout() ->
  20. gen_statem:call(?MODULE, timeout).
  21. %% Mandatory callback functions
  22. terminate(_Reason, _State, _Data) -> ok.
  23. code_change(_Vsn, State, Data, _Extra) -> {ok,State,Data}.
  24. init([]) ->
  25. %% insert rant here about breaking changes in minor versions...
  26. case erlang:system_info(version) of
  27. "8.0" -> {callback_mode(),state1,undefined};
  28. _ -> {ok, state1, undefined}
  29. end.
  30. callback_mode() -> handle_event_function.
  31. %%% state callback(s)
  32. handle_event(state_timeout, timeout, state1, _) ->
  33. {stop, timeout};
  34. handle_event({call, _From}, timeout, _Arg, _Data) ->
  35. {keep_state_and_data, [{state_timeout, 0, timeout}]};
  36. handle_event({call, _From}, {stop, Reason}, state1, _Data) ->
  37. {stop, Reason}.
  38. -else.
  39. -export([start/0, crash/0]).
  40. start() -> ok.
  41. crash() -> ok.
  42. -endif.