So I have this common OTP application, starting up several (mostly Phoenix related) children. One of them is a GenServer I created to poll an external TCP socket every second. This is something I don’t want to do in the test environment. Is the following snippet the “common” way to disable this within my application?
defmodule Example.Application do
# See https://hexdocs.pm/elixir/Application.html
# for more information on OTP Applications
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
# Start the Ecto repository
Example.Repo,
# Start the TCP poller
poller_spec(),
# Start the Telemetry supervisor
ExampleWeb.Telemetry,
# Start the PubSub system
{Phoenix.PubSub, name: Example.PubSub},
# Start the Endpoint (http/https)
ExampleWeb.Endpoint
]
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: Example.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
@impl true
def config_change(changed, _new, removed) do
ExampleWeb.Endpoint.config_change(changed, removed)
:ok
end
defp poller_spec() do
if Mix.env() == :test
%{id: Example.Poller, start: {Function, :identity, [:ignore]}}
else
{Example.Poller, host: "example.com"}
end
end
end
I searched a lot on Github to find such a pattern (found this one in Dashbit’s Bytepack repo), but there aren’t that many open-source Phoenix examples to peek at. Maybe it is super obvious thing to implement if you have more GenServer experience