Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix. Mock not working for grandchildren and beyond #746

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/tesla/mock.ex
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,13 @@ defmodule Tesla.Mock do
defp pdict_get do
pid_holder =
Enum.find(Process.get(:"$ancestors", []), self(), fn ancestor ->
!is_nil(Process.get(ancestor, __MODULE__))
is_holder? =
ancestor
|> Process.info()
|> Keyword.get(:dictionary)
|> Keyword.get(__MODULE__)

!is_nil(is_holder?)
end)
|> case do
nil -> raise "Unknown pid_holder in mock"
Expand Down
22 changes: 19 additions & 3 deletions test/tesla/mock_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,31 @@ defmodule Tesla.MockTest do
assert env.body == %{"id" => 42}
end

test "mock a request inside a spawned process" do
task =
test "mock a request inside a child process" do
child_task =
Task.async(fn ->
assert {:ok, %Tesla.Env{} = env} = Client.get("/json")
assert env.status == 200
assert env.body == %{"json" => 123}
end)

Task.await(task)
Task.await(child_task)
end

test "mock a request inside a grandchild process" do
grandchild_task =
Task.async(fn ->
child_task =
Task.async(fn ->
assert {:ok, %Tesla.Env{} = env} = Client.get("/json")
assert env.status == 200
assert env.body == %{"json" => 123}
end)

Task.await(child_task)
end)

Task.await(grandchild_task)
end
end

Expand Down
Loading