2022-08-27 09:57:03 +12:00
|
|
|
defmodule Ash.Test.TracerTest.AsyncLoadTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: false
|
|
|
|
|
|
|
|
import ExUnit.CaptureLog
|
|
|
|
require Ash.Query
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
defmodule Author do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:06:40 +13:00
|
|
|
domain: Domain,
|
2022-08-27 09:57:03 +12:00
|
|
|
data_layer: Ash.DataLayer.Mnesia,
|
|
|
|
authorizers: [
|
|
|
|
Ash.Test.Authorizer
|
|
|
|
]
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :name, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_many :posts, Ash.Test.TracerTest.AsyncLoadTest.Post,
|
|
|
|
destination_attribute: :author_id,
|
|
|
|
public?: true
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Post do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:06:40 +13:00
|
|
|
domain: Domain,
|
2022-08-27 09:57:03 +12:00
|
|
|
data_layer: Ash.DataLayer.Mnesia
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :title, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :author, Author, public?: true
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
many_to_many :categories, Ash.Test.TracerTest.AsyncLoadTest.Category,
|
|
|
|
through: Ash.Test.TracerTest.AsyncLoadTest.PostCategory,
|
|
|
|
destination_attribute_on_join_resource: :category_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
source_attribute_on_join_resource: :post_id,
|
|
|
|
public?: true
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule PostCategory do
|
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Mnesia
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :post, Post, primary_key?: true, allow_nil?: false, public?: true
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
belongs_to :category, Ash.Test.TracerTest.AsyncLoadTest.Category,
|
|
|
|
primary_key?: true,
|
2024-03-28 09:06:40 +13:00
|
|
|
allow_nil?: false,
|
|
|
|
public?: true
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Category do
|
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Mnesia
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :name, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
many_to_many :posts, Post,
|
|
|
|
through: PostCategory,
|
|
|
|
destination_attribute_on_join_resource: :post_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
source_attribute_on_join_resource: :category_id,
|
|
|
|
public?: true
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
capture_log(fn ->
|
2024-03-28 09:06:40 +13:00
|
|
|
Ash.DataLayer.Mnesia.start(Domain, [Author, Post, Category, PostCategory])
|
2022-08-27 09:57:03 +12:00
|
|
|
end)
|
|
|
|
|
|
|
|
pid = self()
|
|
|
|
|
|
|
|
events =
|
|
|
|
[
|
|
|
|
[:ash, :request_step],
|
2024-03-28 09:06:40 +13:00
|
|
|
[:ash, :domain, :read],
|
|
|
|
[:ash, :domain, :create],
|
|
|
|
[:ash, :domain, :destroy],
|
|
|
|
[:ash, :domain, :update],
|
2022-08-27 09:57:03 +12:00
|
|
|
[:ash, :flow]
|
|
|
|
]
|
|
|
|
|> Enum.flat_map(fn list ->
|
|
|
|
[list ++ [:start], list ++ [:stop]]
|
|
|
|
end)
|
|
|
|
|
2022-08-29 00:40:21 +12:00
|
|
|
capture_log(fn ->
|
|
|
|
:telemetry.attach_many(
|
|
|
|
:test_handler,
|
|
|
|
events,
|
|
|
|
fn event_name, event_measurements, event_metadata, handler_config ->
|
|
|
|
send(
|
|
|
|
pid,
|
|
|
|
{:telemetry, {event_name, event_measurements, event_metadata, handler_config}}
|
|
|
|
)
|
|
|
|
end,
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
end)
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
on_exit(fn ->
|
|
|
|
:telemetry.detach(:test_handler)
|
|
|
|
|
|
|
|
capture_log(fn ->
|
|
|
|
:mnesia.stop()
|
|
|
|
:mnesia.delete_schema([node()])
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
start_supervised(
|
|
|
|
{Ash.Test.Authorizer,
|
|
|
|
strict_check: :authorized,
|
|
|
|
check: {:error, Ash.Error.Forbidden.exception([])},
|
|
|
|
strict_check_context: [:query]}
|
|
|
|
)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a simple read calls the tracer with the action" do
|
2024-03-28 09:06:40 +13:00
|
|
|
Ash.read!(Post, tracer: Ash.Tracer.Simple)
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
assert [
|
|
|
|
%Ash.Tracer.Simple.Span{
|
|
|
|
type: :action,
|
2024-03-28 09:06:40 +13:00
|
|
|
name: "domain:post.read",
|
2022-08-27 09:57:03 +12:00
|
|
|
metadata: %{
|
|
|
|
action: :read,
|
|
|
|
resource: Ash.Test.TracerTest.AsyncLoadTest.Post
|
|
|
|
}
|
|
|
|
}
|
|
|
|
] = Ash.Tracer.Simple.gather_spans()
|
|
|
|
end
|
|
|
|
|
|
|
|
test "a read with async loads calls the tracer for each" do
|
|
|
|
Ash.Changeset.for_create(Post, :create, %{title: "title"}, tracer: Ash.Tracer.Simple)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
assert [
|
|
|
|
%Ash.Tracer.Simple.Span{
|
|
|
|
type: :changeset,
|
2022-09-13 03:16:51 +12:00
|
|
|
name: "changeset:post:create"
|
2022-08-27 09:57:03 +12:00
|
|
|
},
|
|
|
|
%Ash.Tracer.Simple.Span{
|
|
|
|
type: :action,
|
2024-03-28 09:06:40 +13:00
|
|
|
name: "domain:post.create",
|
2022-08-27 09:57:03 +12:00
|
|
|
metadata: %{
|
|
|
|
action: :create,
|
2024-03-28 09:06:40 +13:00
|
|
|
domain: Ash.Test.Domain,
|
2022-08-27 09:57:03 +12:00
|
|
|
resource: Ash.Test.TracerTest.AsyncLoadTest.Post
|
|
|
|
}
|
|
|
|
}
|
|
|
|
] = Ash.Tracer.Simple.gather_spans()
|
|
|
|
|
|
|
|
assert_receive {:telemetry,
|
2024-03-28 09:06:40 +13:00
|
|
|
{[:ash, :domain, :create, :start], %{system_time: _},
|
2022-08-27 09:57:03 +12:00
|
|
|
%{resource_short_name: :post}, _}}
|
|
|
|
|
|
|
|
assert_receive {:telemetry,
|
2024-03-28 09:06:40 +13:00
|
|
|
{[:ash, :domain, :create, :stop], %{duration: _},
|
|
|
|
%{resource_short_name: :post}, _}}
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
Post
|
|
|
|
|> Ash.Query.load(:author)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.read!(tracer: Ash.Tracer.Simple)
|
2022-08-27 09:57:03 +12:00
|
|
|
|
|
|
|
assert_receive {:telemetry,
|
2024-03-28 09:06:40 +13:00
|
|
|
{[:ash, :domain, :read, :start], %{system_time: _},
|
2022-08-27 09:57:03 +12:00
|
|
|
%{resource_short_name: :post}, []}}
|
|
|
|
|
|
|
|
assert_receive {:telemetry,
|
2024-03-28 09:06:40 +13:00
|
|
|
{[:ash, :domain, :read, :start], %{system_time: _},
|
2022-08-27 09:57:03 +12:00
|
|
|
%{resource_short_name: :author}, []}}
|
|
|
|
|
|
|
|
assert_receive {:telemetry,
|
2024-03-28 09:06:40 +13:00
|
|
|
{[:ash, :domain, :read, :stop], %{duration: _, system_time: _},
|
2022-08-27 09:57:03 +12:00
|
|
|
%{resource_short_name: :author}, []}}
|
|
|
|
|
|
|
|
assert_receive {:telemetry,
|
2024-03-28 09:06:40 +13:00
|
|
|
{[:ash, :domain, :read, :stop], %{duration: _, system_time: _},
|
2022-08-27 09:57:03 +12:00
|
|
|
%{resource_short_name: :post}, []}}
|
|
|
|
|
|
|
|
refute_receive {:telemetry, _}
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Ash.Tracer.Simple.Span{
|
|
|
|
type: :action,
|
2024-03-28 09:06:40 +13:00
|
|
|
name: "domain:post.read",
|
2022-08-27 09:57:03 +12:00
|
|
|
metadata: %{
|
|
|
|
action: :read,
|
|
|
|
resource: Ash.Test.TracerTest.AsyncLoadTest.Post
|
|
|
|
},
|
|
|
|
spans: spans
|
|
|
|
}
|
|
|
|
] = Ash.Tracer.Simple.gather_spans()
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
assert Enum.any?(spans, &(&1.name == "domain:author.read"))
|
2022-08-27 09:57:03 +12:00
|
|
|
end
|
|
|
|
end
|