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
|
||||
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})
|
||||
{:noreply, state}
|
||||
end
|
||||
@@ -61,7 +63,7 @@ defmodule Arithmetic.Server do
|
||||
GenServer.cast(worker, {:square, x, from})
|
||||
|
||||
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}}
|
||||
end
|
||||
|
||||
|
||||
@@ -18,4 +18,3 @@ end
|
||||
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
|
||||
be found at <https://hexdocs.pm/card>.
|
||||
|
||||
@@ -3,7 +3,7 @@ defmodule Card.Worker do
|
||||
|
||||
### Client API ###
|
||||
def start() do
|
||||
GenServer.start(__MODULE__, fresh(), name: __MODULE__)
|
||||
GenServer.start(__MODULE__, nil, name: __MODULE__)
|
||||
end
|
||||
|
||||
def new() do
|
||||
@@ -31,8 +31,8 @@ defmodule Card.Worker do
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(arg) do
|
||||
{:ok, arg}
|
||||
def init(_arg) do
|
||||
{:ok, fresh()}
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
||||
@@ -1,8 +1,29 @@
|
||||
defmodule CardTest do
|
||||
use ExUnit.Case
|
||||
doctest Card
|
||||
|
||||
test "greets the world" do
|
||||
assert Card.hello() == :world
|
||||
setup do
|
||||
{: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
|
||||
Reference in new issue
Block a user