Skip to content

Commit

Permalink
Improve tests (#149)
Browse files Browse the repository at this point in the history
* Move telemetry tests to use message based helper from Bandit

* Use async tests
  • Loading branch information
mtrudel authored Jan 5, 2025
1 parent 8e66e81 commit e9b711d
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 263 deletions.
5 changes: 3 additions & 2 deletions lib/thousand_island/listener.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ defmodule ThousandIsland.Listener do
),
{:ok, {ip, port}} <-
server_config.transport_module.sockname(listener_socket) do
span_meta = %{
span_metadata = %{
handler: server_config.handler_module,
local_address: ip,
local_port: port,
transport_module: server_config.transport_module,
transport_options: server_config.transport_options
}

listener_span = ThousandIsland.Telemetry.start_span(:listener, %{}, span_meta)
listener_span = ThousandIsland.Telemetry.start_span(:listener, %{}, span_metadata)

{:ok,
%{listener_socket: listener_socket, local_info: {ip, port}, listener_span: listener_span}}
Expand Down
10 changes: 8 additions & 2 deletions lib/thousand_island/telemetry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ defmodule ThousandIsland.Telemetry do
@spec start_child_span(t(), span_name(), measurements(), metadata()) :: t()
def start_child_span(parent_span, span_name, measurements \\ %{}, metadata \\ %{}) do
metadata =
Map.put(metadata, :parent_telemetry_span_context, parent_span.telemetry_span_context)
metadata
|> Map.put(:parent_telemetry_span_context, parent_span.telemetry_span_context)
|> Map.put(:handler, parent_span.start_metadata.handler)

start_span(span_name, measurements, metadata)
end
Expand Down Expand Up @@ -361,7 +363,11 @@ defmodule ThousandIsland.Telemetry do
@spec untimed_span_event(t(), event_name() | untimed_event_name(), measurements(), metadata()) ::
:ok
def untimed_span_event(span, name, measurements \\ %{}, metadata \\ %{}) do
metadata = Map.put(metadata, :telemetry_span_context, span.telemetry_span_context)
metadata =
metadata
|> Map.put(:telemetry_span_context, span.telemetry_span_context)
|> Map.put(:handler, span.start_metadata.handler)

event([span.span_name, name], measurements, metadata)
end

Expand Down
38 changes: 0 additions & 38 deletions test/support/telemetry_collector.ex

This file was deleted.

34 changes: 34 additions & 0 deletions test/support/telemetry_helpers.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule TelemetryHelpers do
@moduledoc false

@events [
[:thousand_island, :listener, :start],
[:thousand_island, :listener, :stop],
[:thousand_island, :acceptor, :start],
[:thousand_island, :acceptor, :stop],
[:thousand_island, :acceptor, :spawn_error],
[:thousand_island, :acceptor, :econnaborted],
[:thousand_island, :connection, :start],
[:thousand_island, :connection, :stop],
[:thousand_island, :connection, :ready],
[:thousand_island, :connection, :async_recv],
[:thousand_island, :connection, :recv],
[:thousand_island, :connection, :recv_error],
[:thousand_island, :connection, :send],
[:thousand_island, :connection, :send_error],
[:thousand_island, :connection, :sendfile],
[:thousand_island, :connection, :sendfile_error],
[:thousand_island, :connection, :socket_shutdown]
]

def attach_all_events(handler) do
ref = make_ref()
_ = :telemetry.attach_many(ref, @events, &__MODULE__.handle_event/4, {self(), handler})
fn -> :telemetry.detach(ref) end
end

def handle_event(event, measurements, %{handler: handler} = metadata, {pid, handler}),
do: send(pid, {:telemetry, event, measurements, metadata})

def handle_event(_event, _measurements, _metadata, {_pid, _handler}), do: :ok
end
Loading

0 comments on commit e9b711d

Please sign in to comment.