-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
133 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / main (1.15, 25.3.2.12)
Check warning on line 68 in test/tower_rollbar_test.exs GitHub Actions / main (1.14, 24.3.4.17)
|
||
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 |