Skip to content

Commit

Permalink
add timeout to payment exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rubio committed Mar 4, 2020
1 parent d280e58 commit 6b5e27e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions exercises/gen_state_machine/payment/lib/payment.ex
Expand Up @@ -4,6 +4,8 @@ defmodule Payment do
:state_enter
]

@timeout_each_step 10_000

def start(), do: GenStateMachine.start(__MODULE__, [])
def get_order(pid), do: GenStateMachine.call(pid, :get_order)
def set_creds(pid, name), do: GenStateMachine.cast(pid, {:set_creds, name})
Expand Down Expand Up @@ -53,6 +55,15 @@ defmodule Payment do
IO.puts("Paid!")
:keep_state_and_data
end
def handle_event(:enter, _old, _new, _data) do
actions = [{:state_timeout, @timeout_each_step, :go_away}]
{:keep_state_and_data, actions}
end

def handle_event(:state_timeout, :go_away, _state, _data) do
IO.puts "Time is over!"
{:stop, :normal}
end

def handle_event(_type, _content, _state_name, _state_data) do
:keep_state_and_data
Expand Down

0 comments on commit 6b5e27e

Please sign in to comment.