From 6985b3f2de7691d7fb2f25e0d6dc1ae4dbf4f510 Mon Sep 17 00:00:00 2001 From: sanchda <838104+sanchda@users.noreply.github.com> Date: Fri, 12 Dec 2025 14:43:32 +0000 Subject: [PATCH 1/2] Initial --- test/test_helper.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_helper.exs b/test/test_helper.exs index 77da001..811e427 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -6,7 +6,8 @@ defmodule Statix.TestCase do use ExUnit.CaseTemplate using options do - port = Keyword.get(options, :port, 8125) + # Use random high port to avoid collisions with typical infra (statsd/dogstatsd) + port = Keyword.get(options, :port, 8125 + :rand.uniform(10000)) quote do setup_all do From 856a07219f8297fb7c454d7860cbdb8f92216209 Mon Sep 17 00:00:00 2001 From: sanchda <838104+sanchda@users.noreply.github.com> Date: Wed, 17 Dec 2025 20:45:44 +0000 Subject: [PATCH 2/2] YOU FOOL --- test/statix/config_test.exs | 11 +++++++---- test/statix/overriding_test.exs | 3 --- test/statix/pool_test.exs | 3 ++- test/statix_test.exs | 13 +++++-------- test/support/test_server.exs | 14 ++++++++++++-- test/test_helper.exs | 8 ++++++++ 6 files changed, 34 insertions(+), 18 deletions(-) diff --git a/test/statix/config_test.exs b/test/statix/config_test.exs index 0aabbf2..cb97256 100644 --- a/test/statix/config_test.exs +++ b/test/statix/config_test.exs @@ -1,10 +1,11 @@ defmodule Statix.ConfigTest do - use Statix.TestCase, async: false + use Statix.TestCase, async: false, auto_connect: false use Statix, runtime_config: true test "connect/1" do - connect(tags: ["tag:test"], prefix: "foo") + port = Statix.TestServer.get_port(__MODULE__.Server) + connect(port: port, tags: ["tag:test"], prefix: "foo") increment("sample", 2) assert_receive {:test_server, _, "foo.sample:2|c|#tag:test"} @@ -13,7 +14,8 @@ defmodule Statix.ConfigTest do test "global tags when present" do Application.put_env(:statix, :tags, ["tag:test"]) - connect() + port = Statix.TestServer.get_port(__MODULE__.Server) + connect(port: port) increment("sample", 3) assert_receive {:test_server, _, "sample:3|c|#tag:test"} @@ -27,7 +29,8 @@ defmodule Statix.ConfigTest do test "global connection-specific tags" do Application.put_env(:statix, __MODULE__, tags: ["tag:test"]) - connect() + port = Statix.TestServer.get_port(__MODULE__.Server) + connect(port: port) set("sample", 4) assert_receive {:test_server, _, "sample:4|s|#tag:test"} diff --git a/test/statix/overriding_test.exs b/test/statix/overriding_test.exs index 4ae9d5e..73e050b 100644 --- a/test/statix/overriding_test.exs +++ b/test/statix/overriding_test.exs @@ -35,9 +35,6 @@ defmodule Statix.OverridingTest do super([key, "-overridden"], value, options) end - setup do - connect() - end test "increment/3" do increment("sample", 3, tags: ["foo"]) diff --git a/test/statix/pool_test.exs b/test/statix/pool_test.exs index b945ab3..c1a5f21 100644 --- a/test/statix/pool_test.exs +++ b/test/statix/pool_test.exs @@ -6,7 +6,8 @@ defmodule Statix.PoolingTest do @pool_size 3 setup do - connect(pool_size: @pool_size) + port = Statix.TestServer.get_port(__MODULE__.Server) + connect(port: port, pool_size: @pool_size) end test "starts :pool_size number of ports and randomly picks one" do diff --git a/test/statix_test.exs b/test/statix_test.exs index 077f0d9..108dcb3 100644 --- a/test/statix_test.exs +++ b/test/statix_test.exs @@ -16,9 +16,6 @@ defmodule StatixTest do end) end - setup do - connect() - end test "increment/1,2,3" do __MODULE__.increment("sample") @@ -160,22 +157,22 @@ defmodule StatixTest do assert capture_log(fn -> assert {:error, :port_closed} == increment("sample") - end) =~ "counter metric \"sample\" lost value 1 error=:port_closed\n\e[0m" + end) =~ "counter metric \"sample\" lost value 1 error=:port_closed" assert capture_log(fn -> assert {:error, :port_closed} == decrement("sample") - end) =~ "counter metric \"sample\" lost value -1 error=:port_closed\n\e[0m" + end) =~ "counter metric \"sample\" lost value -1 error=:port_closed" assert capture_log(fn -> assert {:error, :port_closed} == gauge("sample", 2) - end) =~ "gauge metric \"sample\" lost value 2 error=:port_closed\n\e[0m" + end) =~ "gauge metric \"sample\" lost value 2 error=:port_closed" assert capture_log(fn -> assert {:error, :port_closed} == histogram("sample", 3) - end) =~ "histogram metric \"sample\" lost value 3 error=:port_closed\n\e[0m" + end) =~ "histogram metric \"sample\" lost value 3 error=:port_closed" assert capture_log(fn -> assert {:error, :port_closed} == timing("sample", 2.5) - end) =~ "timing metric \"sample\" lost value 2.5 error=:port_closed\n\e[0m" + end) =~ "timing metric \"sample\" lost value 2.5 error=:port_closed" end end diff --git a/test/support/test_server.exs b/test/support/test_server.exs index a51df16..213b63f 100644 --- a/test/support/test_server.exs +++ b/test/support/test_server.exs @@ -2,16 +2,26 @@ defmodule Statix.TestServer do use GenServer def start_link(port, test_module) do - GenServer.start_link(__MODULE__, port, name: test_module) + GenServer.start_link(__MODULE__, {port, test_module}, name: test_module) + end + + def get_port(test_module) do + GenServer.call(test_module, :get_port) end @impl true - def init(port) do + def init({port, _test_module}) do {:ok, socket} = :gen_udp.open(port, [:binary, active: true]) Process.flag(:trap_exit, true) {:ok, %{socket: socket, test: nil}} end + @impl true + def handle_call(:get_port, _from, %{socket: socket} = state) do + {:ok, port} = :inet.port(socket) + {:reply, port, state} + end + @impl true def handle_call({:set_current_test, current_test}, _from, %{test: test} = state) do if is_nil(test) or is_nil(current_test) do diff --git a/test/test_helper.exs b/test/test_helper.exs index 811e427..55e120c 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -8,6 +8,7 @@ defmodule Statix.TestCase do using options do # Use random high port to avoid collisions with typical infra (statsd/dogstatsd) port = Keyword.get(options, :port, 8125 + :rand.uniform(10000)) + auto_connect = Keyword.get(options, :auto_connect, true) quote do setup_all do @@ -17,6 +18,13 @@ defmodule Statix.TestCase do setup do Statix.TestServer.setup(__MODULE__.Server) + + if unquote(auto_connect) do + port = Statix.TestServer.get_port(__MODULE__.Server) + connect(port: port) + end + + :ok end end end