Close TD-14 replace polling with messaging
This commit is contained in:
@@ -76,6 +76,47 @@ defmodule BDS.CliSyncTest do
|
||||
assert is_integer(seen_notification.seen_at)
|
||||
end
|
||||
|
||||
test "watcher skips notification queries when sqlite data_version is unchanged" do
|
||||
test_pid = self()
|
||||
data_version = :erlang.make_ref()
|
||||
:persistent_term.put(data_version, [7, 7])
|
||||
|
||||
data_version_reader = fn ->
|
||||
[next | rest] = :persistent_term.get(data_version)
|
||||
:persistent_term.put(data_version, rest)
|
||||
next
|
||||
end
|
||||
|
||||
notification_fetcher = fn ->
|
||||
send(test_pid, :notifications_fetched)
|
||||
{:ok, []}
|
||||
end
|
||||
|
||||
pruner = fn ->
|
||||
send(test_pid, :notifications_pruned)
|
||||
{:ok, %{processed: 0, unprocessed: 0}}
|
||||
end
|
||||
|
||||
on_exit(fn -> :persistent_term.erase(data_version) end)
|
||||
|
||||
watcher =
|
||||
start_supervised!(
|
||||
{Watcher,
|
||||
poll_interval_ms: 60_000,
|
||||
data_version_reader: data_version_reader,
|
||||
notification_fetcher: notification_fetcher,
|
||||
pruner: pruner}
|
||||
)
|
||||
|
||||
:ok = Watcher.poll_now(watcher)
|
||||
assert_receive :notifications_fetched, 500
|
||||
assert_receive :notifications_pruned, 500
|
||||
|
||||
:ok = Watcher.poll_now(watcher)
|
||||
refute_receive :notifications_fetched, 100
|
||||
refute_receive :notifications_pruned, 100
|
||||
end
|
||||
|
||||
test "processed notifications are pruned after one hour and unprocessed notifications after one day" do
|
||||
now = BDS.Persistence.now_ms()
|
||||
|
||||
|
||||
@@ -914,6 +914,21 @@ defmodule BDS.Desktop.ShellCommandsTest do
|
||||
assert message =~ "Project database is not initialized"
|
||||
end
|
||||
|
||||
test "rebuild sequencing waits on task messages instead of sleep polling" do
|
||||
source = File.read!("lib/bds/desktop/shell_commands.ex")
|
||||
|
||||
func_source =
|
||||
Regex.scan(~r/defp wait_for_group_phase(?:_message)?\(.*?(?=\n defp |\nend)/s, source)
|
||||
|> Enum.map(&List.first/1)
|
||||
|> Enum.join("\n")
|
||||
|
||||
refute String.contains?(func_source, "Process.sleep"),
|
||||
"wait_for_group_phase should not use sleep polling"
|
||||
|
||||
assert String.contains?(func_source, "Phoenix.PubSub.subscribe")
|
||||
assert String.contains?(func_source, "receive")
|
||||
end
|
||||
|
||||
defp wait_for_task(task_id, matcher, timeout \\ 2_000)
|
||||
|
||||
defp wait_for_task(task_id, _matcher, timeout) when timeout <= 0 do
|
||||
|
||||
@@ -266,6 +266,20 @@ defmodule BDS.TasksTest do
|
||||
assert running.id in task_ids
|
||||
end
|
||||
|
||||
test "terminal task states are broadcast on PubSub" do
|
||||
Phoenix.PubSub.subscribe(BDS.PubSub, BDS.Tasks.topic())
|
||||
|
||||
assert {:ok, completed} =
|
||||
BDS.Tasks.submit_task("broadcast completion", fn _report -> {:ok, :done} end,
|
||||
%{group_id: "broadcast-group", group_name: "Maintenance"}
|
||||
)
|
||||
|
||||
assert_receive {:task_terminal, task_event}, 1_000
|
||||
assert task_event.id == completed.id
|
||||
assert task_event.group_id == "broadcast-group"
|
||||
assert task_event.status == :completed
|
||||
end
|
||||
|
||||
defp receive_started do
|
||||
receive do
|
||||
{:started, name, pid} -> {name, pid}
|
||||
|
||||
Reference in New Issue
Block a user