Skip to content

Commit

Permalink
Improve testing approach and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkowalski committed Oct 27, 2023
1 parent f58b589 commit 989e0d8
Showing 1 changed file with 57 additions and 21 deletions.
78 changes: 57 additions & 21 deletions test/telemetry_metrics_appsignal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ defmodule TelemetryMetricsAppsignalTest do
alias Telemetry.Metrics.Sum
alias Telemetry.Metrics.Summary

@moduletag capture_log: true

setup :verify_on_exit!

test "registering a name with the genserver" do
Expand Down Expand Up @@ -83,7 +81,10 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:web, :request], %{}, %{})
assert capture_log(fn ->
:telemetry.execute([:web, :request], %{}, %{})
end) == ""

assert_receive {^ref, :called}

# Measurements should be ignored for counter metric
Expand All @@ -94,7 +95,10 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:web, :request], %{count: 5}, %{})
assert capture_log(fn ->
:telemetry.execute([:web, :request], %{count: 5}, %{})
end) == ""

assert_receive {^ref, :called}

ref = make_ref()
Expand All @@ -105,7 +109,10 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:worker, :events], %{consumed: 11}, %{})
assert capture_log(fn ->
:telemetry.execute([:worker, :events], %{consumed: 11}, %{})
end) == ""

assert_receive {^ref, :called}
end

Expand All @@ -123,7 +130,10 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:worker, :queue], %{length: 42}, %{})
assert capture_log(fn ->
:telemetry.execute([:worker, :queue], %{length: 42}, %{})
end) == ""

assert_receive {^ref, :called}
end

Expand All @@ -141,7 +151,10 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:db, :query], %{duration: 99}, %{})
assert capture_log(fn ->
:telemetry.execute([:db, :query], %{duration: 99}, %{})
end) == ""

assert_receive {^ref, :called}
end

Expand All @@ -165,7 +178,10 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:db, :query], %{duration: 99}, %{})
assert capture_log(fn ->
:telemetry.execute([:db, :query], %{duration: 99}, %{})
end) == ""

assert_receive {^ref, :called}
end

Expand All @@ -189,7 +205,10 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:db, :query], %{duration: 99}, %{multiplier: 2})
assert capture_log(fn ->
:telemetry.execute([:db, :query], %{duration: 99}, %{multiplier: 2})
end) == ""

assert_receive {^ref, :called}
end

Expand All @@ -207,7 +226,10 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:db, :query], %{duration: native_time}, %{})
assert capture_log(fn ->
:telemetry.execute([:db, :query], %{duration: native_time}, %{})
end) == ""

assert_receive {^ref, :called}
end

Expand All @@ -231,8 +253,11 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:worker, :queue], %{length: 42}, %{number: 2})
:telemetry.execute([:worker, :queue], %{length: 60}, %{number: 5})
assert capture_log(fn ->
:telemetry.execute([:worker, :queue], %{length: 42}, %{number: 2})
:telemetry.execute([:worker, :queue], %{length: 60}, %{number: 5})
end) == ""

assert_receive {^ref, :called}
end

Expand All @@ -256,8 +281,11 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:worker, :queue], %{length: 42}, %{number: 2})
:telemetry.execute([:worker, :queue], %{length: 60}, %{number: 5})
assert capture_log(fn ->
:telemetry.execute([:worker, :queue], %{length: 42}, %{number: 2})
:telemetry.execute([:worker, :queue], %{length: 60}, %{number: 5})
end) == ""

assert_receive {^ref, :called}
end

Expand All @@ -274,10 +302,12 @@ defmodule TelemetryMetricsAppsignalTest do
:ok
end)

:telemetry.execute([:worker, :queue], %{length: 42}, %{
queue: "mailer",
host: "localhost"
})
assert capture_log(fn ->
:telemetry.execute([:worker, :queue], %{length: 42}, %{
queue: "mailer",
host: "localhost"
})
end) == ""

tag_permutations = [
%{queue: "mailer", host: "localhost"},
Expand All @@ -296,21 +326,27 @@ defmodule TelemetryMetricsAppsignalTest do
parent = self()
ref = make_ref()

expect(AppsignalMock, :set_gauge, 1, fn
expect(AppsignalMock, :set_gauge, 2, fn
"worker.queue.length", 42, tags ->
send(parent, {ref, tags})
:ok
end)

:telemetry.execute([:worker, :queue], %{length: 42}, %{})
assert capture_log(fn ->
:telemetry.execute([:worker, :queue], %{length: 42}, %{})
end) == ""

assert_receive({^ref, %{value: "value"}})
assert_receive({^ref, %{value: "any"}})
end

test "handling unsupported metrics" do
metric = distribution("web.request.duration", buckets: [100, 200, 400])
start_reporter(metrics: [metric])
:telemetry.execute([:web, :request], %{duration: 99}, %{})

assert capture_log(fn ->
:telemetry.execute([:web, :request], %{duration: 99}, %{})
end) =~ "Ignoring unsupported metric"
end

test "handling missing measurement" do
Expand Down

0 comments on commit 989e0d8

Please sign in to comment.