summaryrefslogtreecommitdiff
path: root/test/pleroma/job_queue_monitor_test.exs
blob: 65c1e9f29c03e6129b1e1056946351802e97f22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.JobQueueMonitorTest do
  use ExUnit.Case, async: true

  alias Pleroma.JobQueueMonitor

  @success {:process_event, :success, 1337,
            %{
              args: %{"op" => "refresh_subscriptions"},
              attempt: 1,
              id: 339,
              max_attempts: 5,
              queue: "federator_outgoing",
              worker: "Pleroma.Workers.SubscriberWorker"
            }}

  @failure {:process_event, :failure, 22_521_134,
            %{
              args: %{"op" => "force_password_reset", "user_id" => "9nJG6n6Nbu7tj9GJX6"},
              attempt: 1,
              error: %RuntimeError{message: "oops"},
              id: 345,
              kind: :exception,
              max_attempts: 1,
              queue: "background",
              stack: [
                {Pleroma.Workers.BackgroundWorker, :perform, 2,
                 [file: 'lib/pleroma/workers/background_worker.ex', line: 31]},
                {Oban.Queue.Executor, :safe_call, 1,
                 [file: 'lib/oban/queue/executor.ex', line: 42]},
                {:timer, :tc, 3, [file: 'timer.erl', line: 197]},
                {Oban.Queue.Executor, :call, 2, [file: 'lib/oban/queue/executor.ex', line: 23]},
                {Task.Supervised, :invoke_mfa, 2, [file: 'lib/task/supervised.ex', line: 90]},
                {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}
              ],
              worker: "Pleroma.Workers.BackgroundWorker"
            }}

  test "stats/0" do
    assert %{processed_jobs: _, queues: _, workers: _} = JobQueueMonitor.stats()
  end

  test "handle_cast/2" do
    state = %{workers: %{}, queues: %{}, processed_jobs: 0}

    assert {:noreply, state} = JobQueueMonitor.handle_cast(@success, state)
    assert {:noreply, state} = JobQueueMonitor.handle_cast(@failure, state)
    assert {:noreply, state} = JobQueueMonitor.handle_cast(@success, state)
    assert {:noreply, state} = JobQueueMonitor.handle_cast(@failure, state)

    assert state == %{
             processed_jobs: 4,
             queues: %{
               "background" => %{failure: 2, processed_jobs: 2, success: 0},
               "federator_outgoing" => %{failure: 0, processed_jobs: 2, success: 2}
             },
             workers: %{
               "Pleroma.Workers.BackgroundWorker" => %{
                 "force_password_reset" => %{failure: 2, processed_jobs: 2, success: 0}
               },
               "Pleroma.Workers.SubscriberWorker" => %{
                 "refresh_subscriptions" => %{failure: 0, processed_jobs: 2, success: 2}
               }
             }
           }
  end
end