-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/annotated-interceptor-allows-overriden-intercep…
…t-configurations'
- Loading branch information
Showing
3 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
test/annotated/annotated_interceptor_on_after_own_configuration_test.exs
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,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 |
58 changes: 58 additions & 0 deletions
58
test/intercepted_modules/annotated/annotated_intercepted_on_after_own_configuration.ex
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,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 |
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