First
[anni] / test / pleroma / job_queue_monitor_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.JobQueueMonitorTest do
6   use ExUnit.Case, async: true
7
8   alias Pleroma.JobQueueMonitor
9
10   @success {:process_event, :success, 1337,
11             %{
12               args: %{"op" => "refresh_subscriptions"},
13               attempt: 1,
14               id: 339,
15               max_attempts: 5,
16               queue: "federator_outgoing",
17               worker: "Pleroma.Workers.SubscriberWorker"
18             }}
19
20   @failure {:process_event, :failure, 22_521_134,
21             %{
22               args: %{"op" => "force_password_reset", "user_id" => "9nJG6n6Nbu7tj9GJX6"},
23               attempt: 1,
24               error: %RuntimeError{message: "oops"},
25               id: 345,
26               kind: :exception,
27               max_attempts: 1,
28               queue: "background",
29               stack: [
30                 {Pleroma.Workers.BackgroundWorker, :perform, 2,
31                  [file: 'lib/pleroma/workers/background_worker.ex', line: 31]},
32                 {Oban.Queue.Executor, :safe_call, 1,
33                  [file: 'lib/oban/queue/executor.ex', line: 42]},
34                 {:timer, :tc, 3, [file: 'timer.erl', line: 197]},
35                 {Oban.Queue.Executor, :call, 2, [file: 'lib/oban/queue/executor.ex', line: 23]},
36                 {Task.Supervised, :invoke_mfa, 2, [file: 'lib/task/supervised.ex', line: 90]},
37                 {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}
38               ],
39               worker: "Pleroma.Workers.BackgroundWorker"
40             }}
41
42   test "stats/0" do
43     assert %{processed_jobs: _, queues: _, workers: _} = JobQueueMonitor.stats()
44   end
45
46   test "handle_cast/2" do
47     state = %{workers: %{}, queues: %{}, processed_jobs: 0}
48
49     assert {:noreply, state} = JobQueueMonitor.handle_cast(@success, state)
50     assert {:noreply, state} = JobQueueMonitor.handle_cast(@failure, state)
51     assert {:noreply, state} = JobQueueMonitor.handle_cast(@success, state)
52     assert {:noreply, state} = JobQueueMonitor.handle_cast(@failure, state)
53
54     assert state == %{
55              processed_jobs: 4,
56              queues: %{
57                "background" => %{failure: 2, processed_jobs: 2, success: 0},
58                "federator_outgoing" => %{failure: 0, processed_jobs: 2, success: 2}
59              },
60              workers: %{
61                "Pleroma.Workers.BackgroundWorker" => %{
62                  "force_password_reset" => %{failure: 2, processed_jobs: 2, success: 0}
63                },
64                "Pleroma.Workers.SubscriberWorker" => %{
65                  "refresh_subscriptions" => %{failure: 0, processed_jobs: 2, success: 2}
66                }
67              }
68            }
69   end
70 end