pleroma_job_queue_test.exs 3.43 KB
Newer Older
minibikini's avatar
Initial  
minibikini committed
1 2 3 4 5 6
# PleromaJobQueue: A lightweight job queue
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule PleromaJobQueueTest do
  use ExUnit.Case
7
  alias PleromaJobQueue.Scheduler
minibikini's avatar
Initial  
minibikini committed
8 9

  defmodule Worker do
minibikini's avatar
minibikini committed
10
    defp pid, do: Application.get_env(:pleroma_job_queue, :test_pid)
minibikini's avatar
Initial  
minibikini committed
11 12 13

    def perform, do: send(pid(), {:test, :no_args})
    def perform(:skip), do: nil
minibikini's avatar
minibikini committed
14 15 16
    def perform(:sync), do: :sync
    def perform(:test_job), do: send(pid(), :test_job)
    def perform(:test_job, a, b), do: send(pid(), {:test_job, {a, b}})
minibikini's avatar
Initial  
minibikini committed
17 18 19 20 21 22 23 24 25
    def perform(:priority, priority), do: send(pid(), {:priority, priority})
  end

  @queue_name :testing

  test "enqueue/4" do
    set_pid()

    assert :ok == PleromaJobQueue.enqueue(@queue_name, Worker)
minibikini's avatar
minibikini committed
26
    assert_receive {:test, :no_args}
minibikini's avatar
Initial  
minibikini committed
27 28

    assert :ok == PleromaJobQueue.enqueue(@queue_name, Worker, [:test_job])
minibikini's avatar
minibikini committed
29
    assert_receive :test_job
minibikini's avatar
Initial  
minibikini committed
30 31

    assert :ok == PleromaJobQueue.enqueue(@queue_name, Worker, [:test_job, :foo, :bar])
minibikini's avatar
minibikini committed
32
    assert_receive {:test_job, {:foo, :bar}}
minibikini's avatar
Initial  
minibikini committed
33 34
  end

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
  test "enqueue_at/5" do
    set_pid()

    future = :os.system_time(:millisecond) + :timer.seconds(4)
    past = :os.system_time(:millisecond) - :timer.seconds(4)
    :ok = PleromaJobQueue.enqueue_at(future, @queue_name, Worker, [:test_job, :future, :bar])
    :ok = PleromaJobQueue.enqueue_at(past, @queue_name, Worker, [:test_job, :past, :bar])
    pid = Process.whereis(Scheduler)
    Process.send(pid, :poll, [])

    refute_receive {:test_job, {:future, :bar}}
    assert_receive {:test_job, {:past, :bar}}
  end

  test "enqueue_in/5" do
    set_pid()

    offset1 = :timer.seconds(4)
    offset2 = :timer.seconds(14)
    :ok = PleromaJobQueue.enqueue_in(offset1, @queue_name, Worker, [:test_job, :foo, :bar1])
    :ok = PleromaJobQueue.enqueue_in(offset2, @queue_name, Worker, [:test_job, :foo, :bar2])
    pid = Process.whereis(Scheduler)
    Process.send(pid, :poll, [])

    result = Scheduler.get_all() |> Enum.map(&elem(&1, 1).args)
    assert [:test_job, :foo, :bar1] in result
    assert [:test_job, :foo, :bar2] in result
  end

minibikini's avatar
Initial  
minibikini committed
64
  test "max_jobs/1" do
minibikini's avatar
minibikini committed
65
    assert Application.get_env(:pleroma_job_queue, @queue_name, 1) ==
minibikini's avatar
Initial  
minibikini committed
66 67 68
             PleromaJobQueue.max_jobs(@queue_name)
  end

minibikini's avatar
minibikini committed
69 70 71 72 73 74
  test "disable" do
    Application.put_env(:pleroma_job_queue, :disabled, true)
    assert :sync == PleromaJobQueue.enqueue(@queue_name, Worker, [:sync])
    Application.put_env(:pleroma_job_queue, :disabled, false)
  end

minibikini's avatar
Initial  
minibikini committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  test "priority" do
    set_pid()

    PleromaJobQueue.enqueue(@queue_name, Worker, [:skip], 11)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 12], 12)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 13], 13)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 20], 20)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 14], 14)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 15], 15)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 16], 16)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 17], 17)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 18], 18)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 19], 19)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 19], 19)
    PleromaJobQueue.enqueue(@queue_name, Worker, [:priority, 1], 1)

minibikini's avatar
minibikini committed
91 92
    assert_receive {:priority, priority}
    assert priority == 1
minibikini's avatar
Initial  
minibikini committed
93 94
  end

minibikini's avatar
minibikini committed
95
  defp set_pid, do: Application.put_env(:pleroma_job_queue, :test_pid, self())
minibikini's avatar
Initial  
minibikini committed
96
end