92 lines
2.3 KiB
Elixir
92 lines
2.3 KiB
Elixir
defmodule Chronoscope.GeminiTest do
|
|
use Chronoscope.Case, async: true
|
|
|
|
alias Chronoscope.DynamicSupervisorMock
|
|
alias Chronoscope.GenServerMock
|
|
alias Chronoscope.Gemini
|
|
alias Chronoscope.RegistryMock
|
|
|
|
import Mox
|
|
|
|
|
|
describe "Chronoscope.Gemini.healthy?()" do
|
|
test "is healthy" do
|
|
assert Gemini.healthy?() == true
|
|
end
|
|
end
|
|
|
|
describe "Chronoscope.Gemini.list()" do
|
|
test "shows empty client list" do
|
|
DynamicSupervisorMock
|
|
|> expect(:which_children, fn _ -> [] end)
|
|
|
|
assert Gemini.list() == []
|
|
end
|
|
|
|
test "lists all children" do
|
|
DynamicSupervisorMock
|
|
|> expect(:which_children, fn _ -> [{1, 2, 3, 4}, {5, 6, 7, 8}] end)
|
|
|
|
GenServerMock
|
|
|> expect(:call, fn 2, :list, 10_000 -> :one end)
|
|
|> expect(:call, fn 6, :list, 10_000 -> :two end)
|
|
|
|
assert Gemini.list() == [:one, :two]
|
|
end
|
|
end
|
|
|
|
describe "Chronoscope.Gemini.remove()" do
|
|
test "does nothing if the client doesn't exist" do
|
|
RegistryMock
|
|
|> expect(:lookup, fn _, _ -> [] end)
|
|
|
|
assert Gemini.remove("localhost", 1965, "/") == {:error, :not_found}
|
|
end
|
|
|
|
test "removes a client" do
|
|
RegistryMock
|
|
|> expect(:lookup, fn _, _ -> [{1, 2}] end)
|
|
|
|
GenServerMock
|
|
|> expect(:call, fn 1, :terminate, 10_000 -> :terminating end)
|
|
|
|
assert Gemini.remove("localhost", 1965, "/") == {:ok, :terminating}
|
|
end
|
|
end
|
|
|
|
describe "Chronoscope.Gemini.connect()" do
|
|
test "creates a new client" do
|
|
RegistryMock
|
|
|> expect(:lookup, fn _, _ -> [] end)
|
|
|
|
DynamicSupervisorMock
|
|
|> expect(
|
|
:start_child,
|
|
fn Chronoscope.Gemini.DynamicSupervisor,
|
|
{Chronoscope.Gemini.Client,
|
|
[
|
|
resource: %{host: "localhost", port: 1965, path: "/"},
|
|
name: {:via, RegistryMock, {Chronoscope.Gemini.Registry, "localhost:1965/"}}
|
|
]} ->
|
|
{:ok, 1}
|
|
end
|
|
)
|
|
|
|
GenServerMock
|
|
|> expect(:call, fn 1, :connect, 10_000 -> :result end)
|
|
|
|
assert Gemini.connect("localhost", 1965, "/") == :result
|
|
end
|
|
|
|
test "reuses an existing client" do
|
|
RegistryMock
|
|
|> expect(:lookup, fn _, _ -> [{1, 2}] end)
|
|
|
|
GenServerMock
|
|
|> expect(:call, fn 1, :connect, 10_000 -> :result end)
|
|
|
|
assert Gemini.connect("localhost", 1965, "/") == :result
|
|
end
|
|
end
|
|
end
|