Skip to content

Commit

Permalink
Modify the stop_timer call to return the time left for the timer to b…
Browse files Browse the repository at this point in the history
…e stopped.

It can be a useful information in some use cases.
  • Loading branch information
lastres committed Mar 7, 2013
1 parent 409cb0e commit c368680
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/chronos.erl
Expand Up @@ -66,7 +66,7 @@ stop(ServerName) ->
start_timer(ServerName, TimerName, Timeout, Callback) ->
call(ServerName, {start_timer, TimerName, Timeout, Callback}).

-spec stop_timer(server_name(), timer_name()) -> 'ok' | 'not_running' | {'error',term()}.
-spec stop_timer(server_name(), timer_name()) -> {'ok', pos_integer()} | 'not_running' | {'error',term()}.
stop_timer(ServerName, TimerName) ->
call(ServerName, {stop_timer, TimerName}).

Expand Down Expand Up @@ -102,8 +102,8 @@ handle_call({stop_timer, Name}, _From,
#chronos_state{running=R}=State) ->
case lists:keytake(Name, 1, R) of
{value, {_, TRef}, Rnext} ->
erlang:cancel_timer(TRef),
{reply, ok, State#chronos_state{running=Rnext}};
TimeLeft = erlang:cancel_timer(TRef),
{reply, {ok, TimeLeft}, State#chronos_state{running=Rnext}};
false ->
{reply, not_running, State}
end.
Expand Down
2 changes: 2 additions & 0 deletions test/chronos_eqc.erl
Expand Up @@ -79,6 +79,8 @@ postcondition(_S,
true;
postcondition(_S, {call, timer_expiry, stop_timer, [_Server, _Timer]}, ok) ->
true;
postcondition(_S, {call, timer_expiry, stop_timer, [_Server, _Timer]}, {ok, _TimeLeft}) ->
true;
postcondition(_S, {call, timer_expiry, stop_timer, [Server, Timer]}, {error,{not_running,Timer}}) ->
valid_stop_timer_status({Server, Timer});
postcondition(S, {call, ?MODULE, advance_time, [_Duration]}, _) ->
Expand Down

0 comments on commit c368680

Please sign in to comment.