-
Notifications
You must be signed in to change notification settings - Fork 0
/
genserver_testing.ex
60 lines (47 loc) · 1.16 KB
/
genserver_testing.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
defmodule GenserverTesting do
@moduledoc """
Documentation for GenserverTesting.
"""
@doc """
Hello world.
## Examples
iex> GenserverTesting.hello
:world
"""
def hello do
:world
end
defmodule PasswordLock do
use GenServer
def start(password) do
GenServer.start_link(__MODULE__, password, [])
end
def unlock(server_pid, password) do
GenServer.call(server_pid, {:unlock, password})
end
def reset(server_pid, {current_password, new_password}) do
if current_password(server_pid) == current_password do
GenServer.cast(server_pid, {:reset_password, new_password})
{:ok, :reset}
else
{:error, :incorrect_password}
end
end
def handle_call({:unlock, password}, _, state) do
if state == password do
{:reply, true, state}
else
{:reply, false, state}
end
end
def handle_call(:current, _, state) do
{:reply, state, state}
end
def handle_cast({:reset_password, new_password}, state) do
{:noreply, new_password}
end
defp current_password(pid) do
GenServer.call(pid, :current)
end
end
end