Skip to content

Commit

Permalink
Remove hold from account
Browse files Browse the repository at this point in the history
  • Loading branch information
grahamrhay committed Apr 21, 2018
1 parent 928bc2e commit b493db6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/bank_statem.erl
Expand Up @@ -2,9 +2,9 @@

-behaviour(gen_statem).

-export([start/0, stop/0, get_balance/0, close/0, reopen/0, deposit/1, withdraw/1, place_hold/0]).
-export([start/0, stop/0, get_balance/0, close/0, reopen/0, deposit/1, withdraw/1, place_hold/0, remove_hold/0]).
-export([init/1, callback_mode/0]).
-export([open/3, closed/3]).
-export([open/3, held/3, closed/3]).

name() -> bank_statem.

Expand All @@ -29,6 +29,9 @@ withdraw(Amount) ->
place_hold() ->
gen_statem:call(name(), place_hold).

remove_hold() ->
gen_statem:call(name(), remove_hold).

stop() ->
gen_statem:stop(name()).

Expand All @@ -54,5 +57,8 @@ open({call, From}, {withdraw, Amount}, #{balance:=Balance} = Data) when is_numbe
NewBalance = Balance - Amount,
{keep_state, Data#{balance:=NewBalance}, [{reply, From, withdrawal_made}]}.

held({call, From}, remove_hold, Data) ->
{next_state, open, Data, [{reply, From, hold_removed}]}.

closed({call, From}, reopen, Data) ->
{next_state, open, Data, [{reply, From, open}]}.
5 changes: 5 additions & 0 deletions test/bank_statem_tests.erl
Expand Up @@ -38,3 +38,8 @@ place_hold_test() ->
From = self(),
Data = #{},
{next_state, held, Data, [{reply, From, hold_placed}]} = bank_statem:open({call, From}, place_hold, Data).

remove_hold_test() ->
From = self(),
Data = #{},
{next_state, open, Data, [{reply, From, hold_removed}]} = bank_statem:held({call, From}, remove_hold, Data).

0 comments on commit b493db6

Please sign in to comment.