Skip to content

Commit

Permalink
test: decent tests (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
grzuy authored Aug 2, 2024
1 parent ce79d7f commit 2b46833
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- elixir: '1.14'
erlang: '24.3.4.17'
- elixir: '1.13'
erlang: '23.3.4.20'
erlang: '24.3.4.17'
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
Expand Down
7 changes: 5 additions & 2 deletions lib/tower_rollbar/rollbar/client.ex
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
defmodule TowerRollbar.Rollbar.Client do
@base_url "https://api.rollbar.com/api/1"
@access_token_header ~c"X-Rollbar-Access-Token"

def post(path, payload) when is_map(payload) do
:httpc.request(
:post,
{
~c"#{@base_url}#{path}",
~c"#{base_url()}#{path}",
[{@access_token_header, access_token()}],
~c"application/json",
Jason.encode!(payload)
Expand Down Expand Up @@ -53,4 +52,8 @@ defmodule TowerRollbar.Rollbar.Client do
defp access_token do
Application.fetch_env!(:tower_rollbar, :access_token)
end

defp base_url do
Application.fetch_env!(:tower_rollbar, :rollbar_base_url)
end
end
10 changes: 8 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ defmodule TowerRollbar.MixProject do
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger, :public_key, :inets]
extra_applications: [:logger, :public_key, :inets],
env: [
rollbar_base_url: "https://api.rollbar.com/api/1"
]
]
end

Expand All @@ -37,7 +40,10 @@ defmodule TowerRollbar.MixProject do
{:plug, "~> 1.16"},

# Only needed for Erlang < 25
{:castore, "~> 1.0", optional: true}
{:castore, "~> 1.0", optional: true},

# Test
{:bypass, "~> 2.1"}
]
end

Expand Down
6 changes: 6 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
%{
"bypass": {:hex, :bypass, "2.1.0", "909782781bf8e20ee86a9cabde36b259d44af8b9f38756173e8f5e2e1fabb9b1", [:mix], [{:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "d9b5df8fa5b7a6efa08384e9bbecfe4ce61c77d28a4282f79e02f1ef78d96b80"},
"castore": {:hex, :castore, "1.0.8", "dedcf20ea746694647f883590b82d9e96014057aff1d44d03ec90f36a5c0dc6e", [:mix], [], "hexpm", "0b2b66d2ee742cb1d9cb8c8be3b43c3a70ee8651f37b75a8b982e036752983f1"},
"cowboy": {:hex, :cowboy, "2.12.0", "f276d521a1ff88b2b9b4c54d0e753da6c66dd7be6c9fca3d9418b561828a3731", [:make, :rebar3], [{:cowlib, "2.13.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "8a7abe6d183372ceb21caa2709bec928ab2b72e18a3911aa1771639bef82651e"},
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"},
"cowlib": {:hex, :cowlib, "2.13.0", "db8f7505d8332d98ef50a3ef34b34c1afddec7506e4ee4dd4a3a266285d282ca", [:make, :rebar3], [], "hexpm", "e1e1284dc3fc030a64b1ad0d8382ae7e99da46c3246b815318a4b848873800a4"},
"jason": {:hex, :jason, "1.4.3", "d3f984eeb96fe53b85d20e0b049f03e57d075b5acda3ac8d465c969a2536c17b", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "9a90e868927f7c777689baa16d86f4d0e086d968db5c05d917ccff6d443e58a3"},
"mime": {:hex, :mime, "2.0.5", "dc34c8efd439abe6ae0343edbb8556f4d63f178594894720607772a041b04b02", [:mix], [], "hexpm", "da0d64a365c45bc9935cc5c8a7fc5e49a0e0f9932a761c55d6c52b142780a05c"},
"plug": {:hex, :plug, "1.16.1", "40c74619c12f82736d2214557dedec2e9762029b2438d6d175c5074c933edc9d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2 or ~> 2.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "a13ff6b9006b03d7e33874945b2755253841b238c34071ed85b0e86057f8cddc"},
"plug_cowboy": {:hex, :plug_cowboy, "2.7.1", "87677ffe3b765bc96a89be7960f81703223fe2e21efa42c125fcd0127dd9d6b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "02dbd5f9ab571b864ae39418db7811618506256f6d13b4a45037e5fe78dc5de3"},
"plug_crypto": {:hex, :plug_crypto, "2.1.0", "f44309c2b06d249c27c8d3f65cfe08158ade08418cf540fd4f72d4d6863abb7b", [:mix], [], "hexpm", "131216a4b030b8f8ce0f26038bc4421ae60e4bb95c5cf5395e1421437824c4fa"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},
"telemetry": {:hex, :telemetry, "1.2.1", "68fdfe8d8f05a8428483a97d7aab2f268aaff24b49e0f599faa091f1d4e7f61c", [:rebar3], [], "hexpm", "dad9ce9d8effc621708f99eac538ef1cbe05d6a874dd741de2e689c47feafed5"},
"tower": {:git, "https://github.com/mimiquate/tower.git", "20883244a6ac4639c70dc1a8874928c52cd5ea4c", []},
"uniq": {:hex, :uniq, "0.6.1", "369660ecbc19051be526df3aa85dc393af5f61f45209bce2fa6d7adb051ae03c", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "6426c34d677054b3056947125b22e0daafd10367b85f349e24ac60f44effb916"},
Expand Down
75 changes: 0 additions & 75 deletions test/tower_rollbar/rollbar/item_test.exs

This file was deleted.

113 changes: 113 additions & 0 deletions test/tower_rollbar_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,117 @@
defmodule TowerRollbarTest do
use ExUnit.Case
doctest TowerRollbar

setup do
bypass = Bypass.open()

Application.put_env(:tower, :reporters, [TowerRollbar.Reporter])
Application.put_env(:tower_rollbar, :rollbar_base_url, "http://localhost:#{bypass.port}/")
Application.put_env(:tower_rollbar, :environment, :test)
Application.put_env(:tower_rollbar, :access_token, "fake-token")
Application.put_env(:tower_rollbar, :enabled, true)

Tower.attach()

on_exit(fn ->
Tower.detach()
end)

{:ok, bypass: bypass}
end

@tag capture_log: true
test "reports arithmetic error", %{bypass: bypass} do
# ref message synchronization trick copied from
# https://github.com/PSPDFKit-labs/bypass/issues/112
parent = self()
ref = make_ref()

Bypass.expect_once(bypass, "POST", "/item", fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)

assert(
%{
"data" => %{
"environment" => "test",
"timestamp" => _,
"level" => "error",
"body" => %{
"trace" => %{
"exception" => %{
"class" => "ArithmeticError",
"message" => "bad argument in arithmetic expression"
},
"frames" => frames
}
}
}
} = Jason.decode!(body)
)

assert(
%{
"method" => ~s(anonymous fn/0 in TowerRollbarTest."test reports arithmetic error"/1),
"filename" => "test/tower_rollbar_test.exs",
"lineno" => 68
} = List.last(frames)
)

send(parent, {ref, :sent})

conn
|> Plug.Conn.put_resp_content_type("application/json")
|> Plug.Conn.resp(200, Jason.encode!(%{"ok" => true}))
end)

in_unlinked_process(fn ->
1 / 0

Check warning on line 68 in test/tower_rollbar_test.exs

View workflow job for this annotation

GitHub Actions / main (1.15, 25.3.2.12)

the call to //2 will fail with ArithmeticError

Check warning on line 68 in test/tower_rollbar_test.exs

View workflow job for this annotation

GitHub Actions / main (1.14, 24.3.4.17)

the call to //2 will fail with ArithmeticError

Check warning on line 68 in test/tower_rollbar_test.exs

View workflow job for this annotation

GitHub Actions / main (1.13, 24.3.4.17)

the call to //2 will fail with ArithmeticError
end)

assert_receive({^ref, :sent}, 500)
end

test "reports message", %{bypass: bypass} do
# ref message synchronization trick copied from
# https://github.com/PSPDFKit-labs/bypass/issues/112
parent = self()
ref = make_ref()

Bypass.expect_once(bypass, "POST", "/item", fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)

assert(
%{
"data" => %{
"environment" => "test",
"timestamp" => _,
"level" => "info",
"body" => %{
"message" => %{
"body" => "something interesting happened"
}
}
}
} = Jason.decode!(body)
)

send(parent, {ref, :sent})

conn
|> Plug.Conn.put_resp_content_type("application/json")
|> Plug.Conn.resp(200, Jason.encode!(%{"ok" => true}))
end)

Tower.handle_message(:info, "something interesting happened")

assert_receive({^ref, :sent}, 500)
end

defp in_unlinked_process(fun) when is_function(fun, 0) do
{:ok, pid} = Task.Supervisor.start_link()

pid
|> Task.Supervisor.async_nolink(fun)
|> Task.yield()
end
end

0 comments on commit 2b46833

Please sign in to comment.