-
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
1 parent
a20a7c0
commit c1049e0
Showing
4 changed files
with
285 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
defmodule Deployex.TracerTest do | ||
use ExUnit.Case, async: true | ||
|
||
import Mox | ||
|
||
alias Deployex.Tracer, as: DeployexT | ||
alias Deployex.TracerFixtures | ||
|
||
setup :verify_on_exit! | ||
|
||
test "get_modules/1" do | ||
assert list = DeployexT.get_modules(Node.self()) | ||
assert Enum.member?(list, :kernel) | ||
end | ||
|
||
test "get_module_functions_info/2" do | ||
assert %{ | ||
functions: %{"config_change/3" => %{arity: 3, name: :config_change}}, | ||
module: :kernel, | ||
node: :nonode@nohost | ||
} = DeployexT.get_module_functions_info(Node.self(), :kernel) | ||
|
||
assert %{ | ||
functions: _, | ||
module: :erlang, | ||
node: :nonode@nohost | ||
} = DeployexT.get_module_functions_info(Node.self(), :erlang) | ||
end | ||
|
||
test "get_default_functions_matchspecs/0" do | ||
assert %{ | ||
return_trace: %{pattern: [{:_, [], [{:return_trace}]}]}, | ||
exception_trace: %{pattern: [{:_, [], [{:exception_trace}]}]}, | ||
caller: %{pattern: [{:_, [], [{:message, {:caller}}]}]}, | ||
process_dump: %{pattern: [{:_, [], [{:message, {:process_dump}}]}]} | ||
} = DeployexT.get_default_functions_matchspecs() | ||
end | ||
|
||
describe "start_trace/2" do | ||
test "Only one session is allowed for Tracer" do | ||
functions = [ | ||
%{ | ||
arity: :_, | ||
function: :_, | ||
match_spec: [], | ||
module: TracerFixtures, | ||
node: Node.self() | ||
} | ||
] | ||
|
||
assert {:ok, %{session_id: session_id}} = | ||
DeployexT.start_trace(functions, %{max_messages: 1}) | ||
|
||
assert {:error, :already_started} = | ||
DeployexT.start_trace(functions, %{max_messages: 1}) | ||
|
||
assert :ok == DeployexT.stop_trace(session_id) | ||
|
||
:timer.sleep(50) | ||
end | ||
|
||
test "Success requesting only module" do | ||
node = Node.self() | ||
|
||
functions = [ | ||
%{ | ||
arity: :_, | ||
function: :_, | ||
match_spec: [], | ||
module: TracerFixtures, | ||
node: node | ||
} | ||
] | ||
|
||
assert {:ok, %{session_id: session_id} = state} = | ||
DeployexT.start_trace(functions, %{max_messages: 1}) | ||
|
||
assert state == DeployexT.state() | ||
|
||
TracerFixtures.testing_fun([true, true, true]) | ||
|
||
assert_receive {:new_trace_message, ^session_id, ^node, _index, :call, msg}, 1_000 | ||
|
||
assert msg =~ "TracerFixtures" | ||
|
||
assert :ok == DeployexT.stop_trace(session_id) | ||
|
||
:timer.sleep(50) | ||
end | ||
|
||
test "Success requesting module/function" do | ||
node = Node.self() | ||
|
||
functions = [ | ||
%{ | ||
arity: 2, | ||
function: :testing_adding_fun, | ||
match_spec: [], | ||
module: TracerFixtures, | ||
node: node | ||
} | ||
] | ||
|
||
assert {:ok, %{session_id: session_id}} = | ||
DeployexT.start_trace(functions, %{max_messages: 1}) | ||
|
||
TracerFixtures.testing_adding_fun(50, 50) | ||
|
||
assert_receive {:new_trace_message, ^session_id, ^node, _index, :call, msg}, 1_000 | ||
|
||
assert msg =~ "TracerFixtures" | ||
assert msg =~ "testing_adding_fun" | ||
|
||
assert :ok == DeployexT.stop_trace(session_id) | ||
|
||
:timer.sleep(50) | ||
end | ||
|
||
test "Success requesting module/function with match_spec [return_trace]" do | ||
node = Node.self() | ||
|
||
functions = [ | ||
%{ | ||
arity: 2, | ||
function: :testing_adding_fun, | ||
match_spec: ["return_trace"], | ||
module: TracerFixtures, | ||
node: node | ||
} | ||
] | ||
|
||
assert {:ok, %{session_id: session_id}} = | ||
DeployexT.start_trace(functions, %{max_messages: 2}) | ||
|
||
TracerFixtures.testing_adding_fun(253, 200) | ||
|
||
assert_receive {:new_trace_message, ^session_id, ^node, _index, :return_from, msg}, 1_000 | ||
|
||
assert msg =~ "TracerFixtures" | ||
assert msg =~ "testing_adding_fun" | ||
assert msg =~ "453" | ||
|
||
assert :ok == DeployexT.stop_trace(session_id) | ||
|
||
:timer.sleep(50) | ||
end | ||
|
||
test "Success requesting module/function with match_spec [caller]" do | ||
node = Node.self() | ||
|
||
functions = [ | ||
%{ | ||
arity: 2, | ||
function: :testing_adding_fun, | ||
match_spec: ["caller"], | ||
module: TracerFixtures, | ||
node: node | ||
} | ||
] | ||
|
||
assert {:ok, %{session_id: session_id}} = | ||
DeployexT.start_trace(functions, %{max_messages: 2}) | ||
|
||
TracerFixtures.testing_adding_fun(255, 200) | ||
|
||
assert_receive {:new_trace_message, ^session_id, ^node, _index, :call, msg}, 1_000 | ||
|
||
assert msg =~ "TracerFixtures" | ||
assert msg =~ "testing_adding_fun" | ||
assert msg =~ "caller: {Deployex.TracerTest" | ||
|
||
assert :ok == DeployexT.stop_trace(session_id) | ||
|
||
:timer.sleep(50) | ||
end | ||
|
||
@tag :capture_log | ||
test "Success requesting module/function with match_spec [exception_trace]" do | ||
node = Node.self() | ||
|
||
functions = [ | ||
%{ | ||
arity: 1, | ||
function: :testing_exception_fun, | ||
match_spec: ["exception_trace"], | ||
module: TracerFixtures, | ||
node: node | ||
} | ||
] | ||
|
||
assert {:ok, %{session_id: session_id}} = | ||
DeployexT.start_trace(functions, %{max_messages: 2}) | ||
|
||
spawn(fn -> | ||
TracerFixtures.testing_exception_fun(0) | ||
end) | ||
|
||
assert_receive {:new_trace_message, ^session_id, ^node, _index, :exception_from, msg}, 1_000 | ||
|
||
assert msg =~ "TracerFixtures" | ||
assert msg =~ "testing_exception_fun" | ||
assert msg =~ "exception_value: {:error, :badarith}" | ||
|
||
assert :ok == DeployexT.stop_trace(session_id) | ||
|
||
:timer.sleep(50) | ||
end | ||
end | ||
|
||
describe "stop_trace/2" do | ||
test "Ignore stop_trace with invalid session_id" do | ||
functions = [ | ||
%{ | ||
arity: :_, | ||
function: :_, | ||
match_spec: [], | ||
module: TracerFixtures, | ||
node: Node.self() | ||
} | ||
] | ||
|
||
assert {:ok, %{session_id: session_id}} = | ||
DeployexT.start_trace(functions, %{max_messages: 1}) | ||
|
||
assert %DeployexT{status: :running} = DeployexT.state() | ||
|
||
assert :ok == DeployexT.stop_trace("123456789") | ||
|
||
assert :ok == DeployexT.stop_trace(session_id) | ||
|
||
assert %DeployexT{status: :idle} = DeployexT.state() | ||
:timer.sleep(50) | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule Deployex.TracerFixtures do | ||
@moduledoc """ | ||
This module will handle the tracer fixture | ||
""" | ||
|
||
def testing_fun(_arg1) do | ||
:ok | ||
end | ||
|
||
def testing_adding_fun(arg1, arg2) do | ||
arg1 + arg2 | ||
end | ||
|
||
def testing_caller_fun(arg1, arg2) do | ||
testing_adding_fun(arg1, arg2) | ||
end | ||
|
||
def testing_exception_fun(arg) do | ||
1 / arg | ||
end | ||
end |