Add tests

This commit is contained in:
Mike Cifelli 2024-07-26 09:29:42 -04:00
parent bb05e5fad8
commit 548ef87bc4
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
6 changed files with 22 additions and 6 deletions

View File

@ -28,4 +28,5 @@ config :chronoscope,
registry: Chronoscope.RegistryMock, registry: Chronoscope.RegistryMock,
gen_server: Chronoscope.GenServerMock, gen_server: Chronoscope.GenServerMock,
nts: Chronoscope.NTSMock, nts: Chronoscope.NTSMock,
gemini: Chronoscope.GeminiMock gemini: Chronoscope.GeminiMock,
endpoint: Chronoscope.EndpointMock

View File

@ -3,7 +3,6 @@ defmodule Chronoscope.NTS.Client do
alias Chronoscope.NTS alias Chronoscope.NTS
alias Chronoscope.NTS.KeyEstablishmentClient alias Chronoscope.NTS.KeyEstablishmentClient
alias ChronoscopeWeb.Endpoint
@interval_in_seconds 30 @interval_in_seconds 30
@timeout_in_milliseconds 10_000 @timeout_in_milliseconds 10_000
@ -11,6 +10,7 @@ defmodule Chronoscope.NTS.Client do
@topic Application.compile_env(:chronoscope, :nts_topic) @topic Application.compile_env(:chronoscope, :nts_topic)
@date_time Application.compile_env(:chronoscope, :date_time, DateTime) @date_time Application.compile_env(:chronoscope, :date_time, DateTime)
@endpoint Application.compile_env(:chronoscope, :endpoint, ChronoscopeWeb.Endpoint)
def start_link(server: server, name: name) do def start_link(server: server, name: name) do
GenServer.start_link(__MODULE__, server, name: name) GenServer.start_link(__MODULE__, server, name: name)
@ -18,7 +18,7 @@ defmodule Chronoscope.NTS.Client do
@impl true @impl true
def init(server) do def init(server) do
Endpoint.broadcast(@topic, "initializing", server) @endpoint.broadcast(@topic, "initializing", server)
{:ok, {:ok,
%{ %{
@ -54,7 +54,7 @@ defmodule Chronoscope.NTS.Client do
@impl true @impl true
def handle_call(:cancel_auto_refresh, _from, %{timer: timer} = state) do def handle_call(:cancel_auto_refresh, _from, %{timer: timer} = state) do
:timer.cancel(timer) :timer.cancel(timer)
Endpoint.broadcast(@topic, "cancel-auto-refresh", state.server) @endpoint.broadcast(@topic, "cancel-auto-refresh", state.server)
{:reply, :ok, Map.delete(state, :timer)} {:reply, :ok, Map.delete(state, :timer)}
end end
@ -82,7 +82,7 @@ defmodule Chronoscope.NTS.Client do
if interval_surpassed?(now, state.last_key_establishment) do if interval_surpassed?(now, state.last_key_establishment) do
state state
|> Map.merge(current_data(state, now)) |> Map.merge(current_data(state, now))
|> tap(&Endpoint.broadcast(@topic, "key-exchange", Map.delete(&1, :timer))) |> tap(&@endpoint.broadcast(@topic, "key-establishment", Map.delete(&1, :timer)))
else else
state state
end end

View File

@ -15,7 +15,7 @@ defmodule ChronoscopeWeb.IndexLive do
end end
@impl true @impl true
def handle_info(%{topic: @topic, event: "key-exchange", payload: client}, socket) do def handle_info(%{topic: @topic, event: "key-establishment", payload: client}, socket) do
if client.server in socket.assigns.servers do if client.server in socket.assigns.servers do
{:noreply, update(socket, :clients, &update_client(&1, client))} {:noreply, update(socket, :clients, &update_client(&1, client))}
else else

View File

@ -3,6 +3,7 @@ defmodule Chronoscope.NTS.ClientTest do
alias Chronoscope.Certificate alias Chronoscope.Certificate
alias Chronoscope.DateTimeMock alias Chronoscope.DateTimeMock
alias Chronoscope.EndpointMock
alias Chronoscope.NTS.Client alias Chronoscope.NTS.Client
alias Chronoscope.SSLMock alias Chronoscope.SSLMock
@ -19,6 +20,9 @@ defmodule Chronoscope.NTS.ClientTest do
describe "Chronoscope.NTS.Client.init()" do describe "Chronoscope.NTS.Client.init()" do
test "initializes successfully" do test "initializes successfully" do
EndpointMock
|> expect(:broadcast, fn "nts-servers", "initializing", %{host: "localhost", port: 3333} -> :ok end)
assert Client.init(%{host: "localhost", port: 3333}) == assert Client.init(%{host: "localhost", port: 3333}) ==
{:ok, {:ok,
%{ %{
@ -48,6 +52,9 @@ defmodule Chronoscope.NTS.ClientTest do
end end
test ":cancel_auto_refresh" do test ":cancel_auto_refresh" do
EndpointMock
|> expect(:broadcast, fn "nts-servers", "cancel-auto-refresh", _ -> :ok end)
assert Client.handle_call(:cancel_auto_refresh, nil, %{server: true, timer: true}) == {:reply, :ok, %{server: true}} assert Client.handle_call(:cancel_auto_refresh, nil, %{server: true, timer: true}) == {:reply, :ok, %{server: true}}
end end
@ -79,6 +86,9 @@ defmodule Chronoscope.NTS.ClientTest do
|> expect(:peercert, fn :socket -> {:ok, peercert} end) |> expect(:peercert, fn :socket -> {:ok, peercert} end)
|> expect(:close, fn :socket -> :ok end) |> expect(:close, fn :socket -> :ok end)
EndpointMock
|> expect(:broadcast, fn "nts-servers", "key-establishment", _ -> :ok end)
assert {:reply, {:ok, %{cert_expiration: ^peercert_expiration}}, assert {:reply, {:ok, %{cert_expiration: ^peercert_expiration}},
%{ %{
server: %{host: "localhost", port: 3333}, server: %{host: "localhost", port: 3333},

View File

@ -22,3 +22,7 @@ defmodule Chronoscope.GenServer.Behaviour do
@callback call(pid(), any(), any()) :: any() @callback call(pid(), any(), any()) :: any()
@callback call(pid(), any()) :: any() @callback call(pid(), any()) :: any()
end end
defmodule ChronoscopeWeb.Endpoint.Behaviour do
@callback broadcast(any(), any(), any()) :: any()
end

View File

@ -5,3 +5,4 @@ Mox.defmock(Chronoscope.DynamicSupervisorMock, for: Chronoscope.DynamicSuperviso
Mox.defmock(Chronoscope.GenServerMock, for: Chronoscope.GenServer.Behaviour) Mox.defmock(Chronoscope.GenServerMock, for: Chronoscope.GenServer.Behaviour)
Mox.defmock(Chronoscope.NTSMock, for: Chronoscope.NTS.Behaviour) Mox.defmock(Chronoscope.NTSMock, for: Chronoscope.NTS.Behaviour)
Mox.defmock(Chronoscope.GeminiMock, for: Chronoscope.Gemini.Behaviour) Mox.defmock(Chronoscope.GeminiMock, for: Chronoscope.Gemini.Behaviour)
Mox.defmock(Chronoscope.EndpointMock, for: ChronoscopeWeb.Endpoint.Behaviour)