Browse Source

Fix code formatting

master
Piotr Orzechowski 7 months ago
parent
commit
a9c1a60690

+ 2
- 0
apps/kv/lib/kv/registry.ex View File

@@ -62,6 +62,7 @@ defmodule KV.Registry do
case lookup(names, name) do
{:ok, _} ->
{:reply, :error, state}

:error ->
{:ok, bucket} = DynamicSupervisor.start_child(KV.BucketSupervisor, KV.Bucket)
ref = Process.monitor(bucket)
@@ -80,6 +81,7 @@ defmodule KV.Registry do
{:ok, bucket} ->
_ = DynamicSupervisor.terminate_child(KV.BucketSupervisor, bucket)
{:reply, :ok, state}

:error ->
{:reply, :error, state}
end

+ 7
- 6
apps/kv/lib/kv/router.ex View File

@@ -8,22 +8,23 @@ defmodule KV.Router do
first = :binary.first(bucket)

# Try to find and entry in the table() or raise
entry = Enum.find(table(), fn {enum, _node} ->
first in enum
end) || no_entry_error(bucket)
entry =
Enum.find(table(), fn {enum, _node} ->
first in enum
end) || no_entry_error(bucket)

# If the entry node is the current node
if elem(entry, 1) == node() do
apply(mod, fun, args)
else
{KV.RouterTasks, elem(entry, 1)}
|> Task.Supervisor.async(KV.Router, :route, [bucket, mod, fun, args])
|> Task.await()
|> Task.Supervisor.async(KV.Router, :route, [bucket, mod, fun, args])
|> Task.await()
end
end

defp no_entry_error(bucket) do
raise "could not find entry for #{inspect bucket} in table #{inspect table()}"
raise "could not find entry for #{inspect(bucket)} in table #{inspect(table())}"
end

@doc """

+ 1
- 1
apps/kv/test/kv/registry_test.exs View File

@@ -58,6 +58,6 @@ defmodule KV.RegistryTest do
Agent.stop(bucket, :shutdown)

# Now trying to call the dead process causes a :noproc exit
catch_exit KV.Bucket.put(bucket, "mleko", 3)
catch_exit(KV.Bucket.put(bucket, "mleko", 3))
end
end

+ 1
- 1
apps/kv/test/test_helper.exs View File

@@ -1,3 +1,3 @@
exclude = if Node.alive?, do: [], else: [distributed: true]
exclude = if Node.alive?(), do: [], else: [distributed: true]

ExUnit.start(exclude: exclude)

+ 3
- 1
apps/kv_server/lib/kv_server.ex View File

@@ -25,7 +25,9 @@ defmodule KVServer do
# 2. `packet: :line` - receives data line by line
# 3. `active: false` - blocks on `:gen_tcp.recv/2` until data is available
# 4. `reuseaddr: true` - allows us to reuse the address if the listener crashes
{:ok, socket} = :gen_tcp.listen(port, [:binary, packet: :line, active: false, reuseaddr: true])
{:ok, socket} =
:gen_tcp.listen(port, [:binary, packet: :line, active: false, reuseaddr: true])

Logger.info("Accepting connections on port #{port}")
loop_acceptor(socket)
end

+ 7
- 7
apps/kv_server/test/kv_server/command_test.exs View File

@@ -12,13 +12,13 @@ defmodule KVServer.CommandTest do
end

test "parses commands" do
assert KVServer.Command.parse("CREATE zakupy") == {:ok, {:create, "zakupy"}}
assert KVServer.Command.parse("DELETE zakupy") == {:ok, {:delete, "zakupy"}}
assert KVServer.Command.parse("PUT zakupy mleko 1") == {:ok, {:put, "zakupy", "mleko", "1"}}
assert KVServer.Command.parse("GET zakupy mleko") == {:ok, {:get, "zakupy", "mleko"}}
assert KVServer.Command.parse("DELETE zakupy mleko") == {:ok, {:delete, "zakupy", "mleko"}}
assert KVServer.Command.parse("UNKNOWN command") == {:error, :unknown_command}
assert KVServer.Command.parse("CREATE zakupy mleko") == {:error, :unknown_command}
assert KVServer.Command.parse("CREATE zakupy") == {:ok, {:create, "zakupy"}}
assert KVServer.Command.parse("DELETE zakupy") == {:ok, {:delete, "zakupy"}}
assert KVServer.Command.parse("PUT zakupy mleko 1") == {:ok, {:put, "zakupy", "mleko", "1"}}
assert KVServer.Command.parse("GET zakupy mleko") == {:ok, {:get, "zakupy", "mleko"}}
assert KVServer.Command.parse("DELETE zakupy mleko") == {:ok, {:delete, "zakupy", "mleko"}}
assert KVServer.Command.parse("UNKNOWN command") == {:error, :unknown_command}
assert KVServer.Command.parse("CREATE zakupy mleko") == {:error, :unknown_command}
end

test "creates and deletes buckets", %{registry: registry} do

Loading…
Cancel
Save