patch lab 03 from feedback
This commit is contained in:
4 files changed
+30
-8
No files matched your search
@@ -13,6 +13,8 @@ defmodule Arithmetic.Worker do
|
|||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_cast({:square, x, from}, state) do
|
def handle_cast({:square, x, from}, state) do
|
||||||
|
# The server dispatches work asynchronously, so workers handle casts and
|
||||||
|
# reply directly to the client that made the original GenServer.call/2.
|
||||||
GenServer.reply(from, {self(), x * x})
|
GenServer.reply(from, {self(), x * x})
|
||||||
{:noreply, state}
|
{:noreply, state}
|
||||||
end
|
end
|
||||||
@@ -61,7 +63,7 @@ defmodule Arithmetic.Server do
|
|||||||
GenServer.cast(worker, {:square, x, from})
|
GenServer.cast(worker, {:square, x, from})
|
||||||
|
|
||||||
next = rem(index + 1, length(workers))
|
next = rem(index + 1, length(workers))
|
||||||
# dont reply here, worker will reply
|
# Do not reply here; the selected worker replies to `from` after the cast.
|
||||||
{:noreply, {workers, next}}
|
{:noreply, {workers, next}}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -18,4 +18,3 @@ end
|
|||||||
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
|
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||||
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
|
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
|
||||||
be found at <https://hexdocs.pm/card>.
|
be found at <https://hexdocs.pm/card>.
|
||||||
|
|
||||||
@@ -3,7 +3,7 @@ defmodule Card.Worker do
|
|||||||
|
|
||||||
### Client API ###
|
### Client API ###
|
||||||
def start() do
|
def start() do
|
||||||
GenServer.start(__MODULE__, fresh(), name: __MODULE__)
|
GenServer.start(__MODULE__, nil, name: __MODULE__)
|
||||||
end
|
end
|
||||||
|
|
||||||
def new() do
|
def new() do
|
||||||
@@ -31,8 +31,8 @@ defmodule Card.Worker do
|
|||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def init(arg) do
|
def init(_arg) do
|
||||||
{:ok, arg}
|
{:ok, fresh()}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|||||||
@@ -1,8 +1,29 @@
|
|||||||
defmodule CardTest do
|
defmodule CardTest do
|
||||||
use ExUnit.Case
|
use ExUnit.Case
|
||||||
doctest Card
|
|
||||||
|
|
||||||
test "greets the world" do
|
setup do
|
||||||
assert Card.hello() == :world
|
{:ok, pid} = Card.Worker.start()
|
||||||
|
on_exit(fn ->
|
||||||
|
if Process.alive?(pid), do: GenServer.stop(pid)
|
||||||
|
end)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
test "the server creates a fresh deck when it starts" do
|
||||||
|
assert Card.Worker.count() == 52
|
||||||
|
end
|
||||||
|
|
||||||
|
test "dealing removes cards from the server's deck" do
|
||||||
|
assert {:ok, cards} = Card.Worker.deal(5)
|
||||||
|
assert length(cards) == 5
|
||||||
|
assert Card.Worker.count() == 47
|
||||||
|
end
|
||||||
|
|
||||||
|
test "new replaces the current deck with a fresh deck" do
|
||||||
|
assert {:ok, _cards} = Card.Worker.deal(5)
|
||||||
|
Card.Worker.new()
|
||||||
|
|
||||||
|
assert Card.Worker.count() == 52
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
Reference in new issue
Block a user