-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,7 @@ | ||
defmodule Example do | ||
@moduledoc """ | ||
Documentation for Example. | ||
""" | ||
defmodule EX.Application do | ||
use Application | ||
|
||
@doc """ | ||
Hello world. | ||
## Examples | ||
iex> Example.hello | ||
:world | ||
""" | ||
def hello do | ||
:world | ||
def start(_type, _args) do | ||
EX.Worker.start_link(name: EX.Worker) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule EX.Worker do | ||
use GenServer | ||
|
||
def start_link(args) do | ||
GenServer.start_link(__MODULE__, :ok, args) | ||
end | ||
|
||
@impl GenServer | ||
def init(:ok) do | ||
state = Shortener.new() | ||
{:ok, state} | ||
end | ||
|
||
def get(pid, hash) do | ||
GenServer.call(pid, {:get, hash}) | ||
end | ||
|
||
def put(pid, hash, url) do | ||
GenServer.cast(pid, {:put, hash, url}) | ||
end | ||
|
||
@impl GenServer | ||
def handle_call({:get, hash}, _timeout, state) do | ||
{:reply, Shortener.get_url(state, hash), state} | ||
end | ||
|
||
@impl GenServer | ||
def handle_cast({:put, hash, url}, state) do | ||
new_state = Shortener.create_short_url(state, hash, url) | ||
{:noreply, new_state} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule EX.WorkerTest do | ||
use ExUnit.Case, async: true | ||
|
||
setup do | ||
pid = start_supervised!(EX.Worker) | ||
%{pid: pid} | ||
end | ||
|
||
test "get and put work", %{pid: pid} do | ||
assert EX.Worker.get(pid, "x") === :undefined | ||
|
||
EX.Worker.put(pid, "x", "google.com") | ||
|
||
assert EX.Worker.get(pid, "x") === "google.com" | ||
end | ||
end |