Skip to content

Commit

Permalink
Merge branch 'feature/annotated-interceptor-allows-overriden-intercep…
Browse files Browse the repository at this point in the history
…t-configurations'
  • Loading branch information
amalbuquerque committed Sep 3, 2019
2 parents 5ab1e33 + bef91ad commit 9e80544
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
defmodule AnnotatedInterceptorOnAfterOwnConfigurationTest do
use ExUnit.Case

@process_name :annotated_after_test_process

describe "module whose own intercept config points to other module" do
test "it intercepts the function after it is called, overriding the global intercept config" do
{:ok, _pid} = spawn_agent()

result = AnnotatedInterceptedOnAfterOwnConfiguration1.to_intercept()

callback_calls = get_agent_messages()

[{:callback_overridden, callback_result, intercepted_mfa}] = callback_calls

assert length(callback_calls) == 1
assert result == callback_result
assert intercepted_mfa == {AnnotatedInterceptedOnAfterOwnConfiguration1, :to_intercept, []}
end
end

describe "module whose own intercept config has the intercept config" do
test "it intercepts the function after it is called" do
{:ok, _pid} = spawn_agent()

result = AnnotatedInterceptedOnAfterOwnConfiguration2.to_intercept()

callback_calls = get_agent_messages()

[{:callback_overridden, callback_result, intercepted_mfa}] = callback_calls

assert length(callback_calls) == 1
assert result == callback_result
assert intercepted_mfa == {AnnotatedInterceptedOnAfterOwnConfiguration2, :to_intercept, []}
end
end

describe "module whose own intercept config has the streamlined intercept config" do
test "it intercepts the function after it is called" do
{:ok, _pid} = spawn_agent()

result = AnnotatedInterceptedOnAfterOwnConfiguration3.to_intercept()

callback_calls = get_agent_messages()

[{:callback_overridden, callback_result, intercepted_mfa}] = callback_calls

assert length(callback_calls) == 1
assert result == callback_result
assert intercepted_mfa == {AnnotatedInterceptedOnAfterOwnConfiguration3, :to_intercept, []}
end
end

describe "module whose own intercept config has a mixed streamlined intercept config" do
test "it intercepts the function after it is called" do
{:ok, _pid} = spawn_agent()

result = AnnotatedInterceptedOnAfterOwnConfiguration4.to_intercept()

callback_calls = get_agent_messages()

[{:callback_overridden, callback_result, intercepted_mfa}] = callback_calls

assert length(callback_calls) == 1
assert result == callback_result
assert intercepted_mfa == {AnnotatedInterceptedOnAfterOwnConfiguration4, :to_intercept, []}
end
end

defp spawn_agent() do
@process_name
|> Process.whereis()
|> kill_agent()

{:ok, pid} = Agent.start_link(fn -> [] end)
true = Process.register(pid, @process_name)

{:ok, pid}
end

defp kill_agent(nil), do: false
defp kill_agent(pid) do
case Process.alive?(pid) do
true -> Process.exit(pid, :kill)
_ -> false
end
end

defp get_agent_messages(), do: Agent.get(@process_name, &(&1))
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule AnnotatedAfter.OwnCallback do
def right_after({_module, _function, _args} = mfa, result) do
Agent.update(:annotated_after_test_process,
fn messages ->
[{:callback_overridden, result, mfa} | messages]
end)
end
end

defmodule AnnotatedMyOwn.InterceptConfig do
def get_intercept_config do
%{
{AnnotatedInterceptedOnAfterOwnConfiguration1, :to_intercept, 0} => [
after: {AnnotatedAfter.OwnCallback, :right_after, 2}
]
}
end
end

defmodule AnnotatedInterceptedOnAfterOwnConfiguration1 do
use Interceptor.Annotated, config: AnnotatedMyOwn.InterceptConfig

@intercept true
def to_intercept(), do: Interceptor.Utils.timestamp()
end

defmodule AnnotatedInterceptedOnAfterOwnConfiguration2 do
use Interceptor.Annotated, config: %{
{AnnotatedInterceptedOnAfterOwnConfiguration2, :to_intercept, 0} => [
after: {AnnotatedAfter.OwnCallback, :right_after, 2}
]
}

@intercept true
def to_intercept(), do: Interceptor.Utils.timestamp()
end

defmodule AnnotatedInterceptedOnAfterOwnConfiguration3 do
use Interceptor.Annotated, config: %{
"AnnotatedInterceptedOnAfterOwnConfiguration3.to_intercept/0" => [
after: "AnnotatedAfter.OwnCallback.right_after/2"
]
}

@intercept true
def to_intercept(), do: Interceptor.Utils.timestamp()
end

defmodule AnnotatedInterceptedOnAfterOwnConfiguration4 do
use Interceptor.Annotated, config: %{
"AnnotatedInterceptedOnAfterOwnConfiguration4.to_intercept/0" => [
after: {AnnotatedAfter.OwnCallback, :right_after, 2}
]
}

@intercept true
def to_intercept(), do: Interceptor.Utils.timestamp()
end
3 changes: 2 additions & 1 deletion test/intercepted_modules/intercept_config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ defmodule InterceptConfig do
# edge cases
{AnnotatedInterceptedEdgeCases1, :to_intercept, 3} => [on_success: {AnnotatedEdgeCases.Callbacks, :success_cb, 3}, on_error: {AnnotatedEdgeCases.Callbacks, :error_cb, 3}],

# note: currently, Interceptor.Annotated doesn't allow intercepted modules overriding the intercept configuration
# these configs will be overridden by the module own configuration
{AnnotatedInterceptedOnAfterOwnConfiguration1, :to_intercept, 0} => [after: {After.Callback, :right_after, 2}],
}

def get_intercept_config(), do: @config
Expand Down

0 comments on commit 9e80544

Please sign in to comment.