2023-04-22 16:58:26 +12:00
|
|
|
defmodule AshObanTest do
|
2024-02-21 07:02:43 +13:00
|
|
|
use ExUnit.Case, async: false
|
2023-04-22 16:58:26 +12:00
|
|
|
doctest AshOban
|
|
|
|
|
2024-03-30 12:52:22 +13:00
|
|
|
alias AshOban.Test.Domain
|
2023-09-01 03:25:05 +12:00
|
|
|
alias AshOban.Test.Triggered
|
2023-04-22 16:58:26 +12:00
|
|
|
|
2024-02-21 07:02:43 +13:00
|
|
|
setup_all do
|
|
|
|
AshOban.Test.Repo.start_link()
|
2024-03-30 12:52:22 +13:00
|
|
|
Oban.start_link(AshOban.config([Domain], Application.get_env(:ash_oban, :oban)))
|
2024-02-21 07:02:43 +13:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
2024-02-21 07:14:02 +13:00
|
|
|
setup do
|
|
|
|
Enum.each(
|
|
|
|
[:triggered_process, :triggered_process_2, :triggered_say_hello],
|
|
|
|
&Oban.drain_queue(queue: &1)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-02-21 07:02:43 +13:00
|
|
|
test "nothing happens if no records exist" do
|
|
|
|
assert %{success: 1} = AshOban.Test.schedule_and_run_triggers(Triggered)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "if a record exists, it is processed" do
|
|
|
|
Triggered
|
|
|
|
|> Ash.Changeset.for_create(:create, %{})
|
2024-03-30 12:52:22 +13:00
|
|
|
|> Ash.create!()
|
2024-02-21 07:02:43 +13:00
|
|
|
|
|
|
|
assert %{success: 2} =
|
|
|
|
AshOban.Test.schedule_and_run_triggers(Triggered,
|
|
|
|
actor: %AshOban.Test.ActorPersister.FakeActor{id: 1}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "if an actor is not set, it is nil when executing the job" do
|
|
|
|
Triggered
|
|
|
|
|> Ash.Changeset.for_create(:create)
|
2024-03-30 12:52:22 +13:00
|
|
|
|> Ash.create!()
|
2024-02-21 07:02:43 +13:00
|
|
|
|
|
|
|
assert %{success: 1, failure: 1} =
|
|
|
|
AshOban.Test.schedule_and_run_triggers(Triggered)
|
|
|
|
end
|
|
|
|
|
2024-02-15 05:28:52 +13:00
|
|
|
test "dsl introspection" do
|
2023-08-25 08:15:04 +12:00
|
|
|
assert [
|
|
|
|
%AshOban.Trigger{action: :process},
|
|
|
|
%AshOban.Trigger{action: :process, scheduler: nil}
|
|
|
|
] = AshOban.Info.oban_triggers(Triggered)
|
2023-04-22 16:58:26 +12:00
|
|
|
end
|
2024-02-15 05:28:52 +13:00
|
|
|
|
|
|
|
test "cron configuration" do
|
|
|
|
config =
|
2024-03-30 12:52:22 +13:00
|
|
|
AshOban.config([Domain],
|
2024-02-15 05:28:52 +13:00
|
|
|
plugins: [
|
|
|
|
{Oban.Plugins.Cron, []}
|
|
|
|
],
|
|
|
|
queues: [
|
|
|
|
triggered_process: 10,
|
|
|
|
triggered_process_2: 10,
|
|
|
|
triggered_say_hello: 10
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert [
|
|
|
|
plugins: [
|
|
|
|
{Oban.Plugins.Cron,
|
|
|
|
[
|
|
|
|
crontab: [
|
|
|
|
{"0 0 1 1 *", AshOban.Test.Triggered.AshOban.ActionWorker.SayHello, []},
|
|
|
|
{"* * * * *", AshOban.Test.Triggered.AshOban.Scheduler.Process, []}
|
|
|
|
]
|
|
|
|
]}
|
|
|
|
],
|
|
|
|
queues: [
|
|
|
|
triggered_process: 10,
|
|
|
|
triggered_process_2: 10,
|
|
|
|
triggered_say_hello: 10
|
|
|
|
]
|
|
|
|
] = config
|
|
|
|
end
|
2024-04-03 05:27:06 +13:00
|
|
|
|
|
|
|
test "oban pro configuration" do
|
|
|
|
config =
|
2024-04-03 07:11:07 +13:00
|
|
|
AshOban.config([Domain],
|
2024-04-03 05:27:06 +13:00
|
|
|
engine: Oban.Pro.Engines.Smart,
|
|
|
|
plugins: [
|
2024-04-03 07:11:07 +13:00
|
|
|
{Oban.Pro.Plugins.DynamicCron,
|
|
|
|
[
|
|
|
|
timezone: "Europe/Rome",
|
|
|
|
sync_mode: :automatic,
|
|
|
|
crontab: []
|
|
|
|
]},
|
2024-04-03 05:27:06 +13:00
|
|
|
{Oban.Pro.Plugins.DynamicQueues,
|
2024-04-03 07:11:07 +13:00
|
|
|
queues: [
|
|
|
|
triggered_process: 10,
|
|
|
|
triggered_process_2: 10,
|
|
|
|
triggered_say_hello: 10
|
|
|
|
]}
|
2024-04-03 05:27:06 +13:00
|
|
|
],
|
|
|
|
queues: false
|
2024-04-03 07:11:07 +13:00
|
|
|
)
|
2024-04-03 05:27:06 +13:00
|
|
|
|
|
|
|
assert [
|
2024-04-03 07:11:07 +13:00
|
|
|
engine: Oban.Pro.Engines.Smart,
|
|
|
|
plugins: [
|
|
|
|
{Oban.Pro.Plugins.DynamicCron,
|
|
|
|
[
|
2024-04-03 05:27:06 +13:00
|
|
|
timezone: "Europe/Rome",
|
|
|
|
sync_mode: :automatic,
|
|
|
|
crontab: [
|
2024-04-03 07:11:07 +13:00
|
|
|
{"0 0 1 1 *", AshOban.Test.Triggered.AshOban.ActionWorker.SayHello,
|
|
|
|
[paused: false]},
|
|
|
|
{"* * * * *", AshOban.Test.Triggered.AshOban.Scheduler.Process,
|
|
|
|
[paused: false]}
|
2024-04-03 05:27:06 +13:00
|
|
|
]
|
|
|
|
]},
|
2024-04-03 07:11:07 +13:00
|
|
|
{Oban.Pro.Plugins.DynamicQueues,
|
2024-04-03 05:27:06 +13:00
|
|
|
queues: [
|
|
|
|
triggered_process: 10,
|
|
|
|
triggered_process_2: 10,
|
|
|
|
triggered_say_hello: 10
|
|
|
|
]}
|
2024-04-03 07:11:07 +13:00
|
|
|
],
|
|
|
|
queues: false
|
|
|
|
] = config
|
|
|
|
end
|
2023-04-22 16:58:26 +12:00
|
|
|
end
|