From fbd29d394636242ce549cb50a18c6a920b5d3ce8 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Thu, 4 Jul 2024 11:09:16 -0400 Subject: [PATCH] Add tests --- test/chronoscope/nts_test.exs | 99 +++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/test/chronoscope/nts_test.exs b/test/chronoscope/nts_test.exs index 8924b64..29ae414 100644 --- a/test/chronoscope/nts_test.exs +++ b/test/chronoscope/nts_test.exs @@ -67,6 +67,105 @@ defmodule Chronoscope.NTSTest do end end + describe "Chronoscope.NTS.start_client()" do + test "starts a client" do + RegistryMock + |> expect(:lookup, fn _, "test:1" -> [] end) + + DynamicSupervisorMock + |> expect( + :start_child, + fn Chronoscope.NTS.DynamicSupervisor, + {Chronoscope.NTS.Client, + [ + server: %{host: "test", port: 1}, + name: {:via, RegistryMock, {Chronoscope.NTS.Registry, "test:1"}} + ]} -> + {:ok, 2} + end + ) + + assert NTS.start_client(%{host: "test", port: 1}) == 2 + end + + test "returns an existing client" do + RegistryMock + |> expect(:lookup, fn _, "test:1" -> [{1, nil}] end) + + assert NTS.start_client(%{host: "test", port: 1}) == 1 + end + end + + describe "Chronoscope.NTS.auto_refresh()" do + test "sets up auto refresh for a new client" do + RegistryMock + |> expect(:lookup, fn _, "test:1" -> [] end) + + DynamicSupervisorMock + |> expect( + :start_child, + fn Chronoscope.NTS.DynamicSupervisor, + {Chronoscope.NTS.Client, + [ + server: %{host: "test", port: 1}, + name: {:via, RegistryMock, {Chronoscope.NTS.Registry, "test:1"}} + ]} -> + {:ok, 2} + end + ) + + GenServerMock + |> expect(:call, fn 2, :auto_refresh, 10_000 -> :ok end) + + assert NTS.auto_refresh(%{host: "test", port: 1}) == :ok + end + + test "sets up auto refresh for an existing client" do + RegistryMock + |> expect(:lookup, fn _, "test:1" -> [{2, nil}] end) + + GenServerMock + |> expect(:call, fn 2, :auto_refresh, 10_000 -> :ok end) + + assert NTS.auto_refresh(%{host: "test", port: 1}) == :ok + end + end + + describe "Chronoscope.NTS.cancel_auto_refresh()" do + test "cancels auto refresh for a new client" do + RegistryMock + |> expect(:lookup, fn _, "test:1" -> [] end) + + DynamicSupervisorMock + |> expect( + :start_child, + fn Chronoscope.NTS.DynamicSupervisor, + {Chronoscope.NTS.Client, + [ + server: %{host: "test", port: 1}, + name: {:via, RegistryMock, {Chronoscope.NTS.Registry, "test:1"}} + ]} -> + {:ok, 2} + end + ) + + GenServerMock + |> expect(:call, fn 2, :cancel_auto_refresh, 10_000 -> :ok end) + + assert NTS.cancel_auto_refresh(%{host: "test", port: 1}) == :ok + end + + test "cancels auto refresh for an existing client" do + RegistryMock + |> expect(:lookup, fn _, "test:1" -> [{2, nil}] end) + + GenServerMock + |> expect(:call, fn 2, :cancel_auto_refresh, 10_000 -> :ok end) + + assert NTS.cancel_auto_refresh(%{host: "test", port: 1}) == :ok + end + end + describe "Chronoscope.NTS.remove()" do test "does nothing if the client doesn't exist" do RegistryMock