Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.5KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.JobsTest do
  5. use ExUnit.Case, async: true
  6. alias Pleroma.Jobs
  7. alias Jobs.WorkerMock
  8. setup do
  9. state = %{
  10. queues: Enum.into([Jobs.create_queue(:testing)], %{}),
  11. refs: %{}
  12. }
  13. [state: state]
  14. end
  15. test "creates queue" do
  16. queue = Jobs.create_queue(:foobar)
  17. assert {:foobar, set} = queue
  18. assert :set == elem(set, 0) |> elem(0)
  19. end
  20. test "enqueues an element according to priority" do
  21. queue = [%{item: 1, priority: 2}]
  22. new_queue = Jobs.enqueue_sorted(queue, 2, 1)
  23. assert new_queue == [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
  24. new_queue = Jobs.enqueue_sorted(queue, 2, 3)
  25. assert new_queue == [%{item: 1, priority: 2}, %{item: 2, priority: 3}]
  26. end
  27. test "pop first item" do
  28. queue = [%{item: 2, priority: 1}, %{item: 1, priority: 2}]
  29. assert {2, [%{item: 1, priority: 2}]} = Jobs.queue_pop(queue)
  30. end
  31. test "enqueue a job", %{state: state} do
  32. assert {:noreply, new_state} =
  33. Jobs.handle_cast({:enqueue, :testing, WorkerMock, [:test_job, :foo, :bar], 3}, state)
  34. assert %{queues: %{testing: {running_jobs, []}}, refs: _} = new_state
  35. assert :sets.size(running_jobs) == 1
  36. assert [ref] = :sets.to_list(running_jobs)
  37. assert %{refs: %{^ref => :testing}} = new_state
  38. end
  39. test "max jobs setting", %{state: state} do
  40. max_jobs = Pleroma.Config.get([Jobs, :testing, :max_jobs])
  41. {:noreply, state} =
  42. Enum.reduce(1..(max_jobs + 1), {:noreply, state}, fn _, {:noreply, state} ->
  43. Jobs.handle_cast({:enqueue, :testing, WorkerMock, [:test_job, :foo, :bar], 3}, state)
  44. end)
  45. assert %{
  46. queues: %{
  47. testing:
  48. {running_jobs, [%{item: {WorkerMock, [:test_job, :foo, :bar]}, priority: 3}]}
  49. }
  50. } = state
  51. assert :sets.size(running_jobs) == max_jobs
  52. end
  53. test "remove job after it finished", %{state: state} do
  54. {:noreply, new_state} =
  55. Jobs.handle_cast({:enqueue, :testing, WorkerMock, [:test_job, :foo, :bar], 3}, state)
  56. %{queues: %{testing: {running_jobs, []}}} = new_state
  57. [ref] = :sets.to_list(running_jobs)
  58. assert {:noreply, %{queues: %{testing: {running_jobs, []}}, refs: %{}}} =
  59. Jobs.handle_info({:DOWN, ref, :process, nil, nil}, new_state)
  60. assert :sets.size(running_jobs) == 0
  61. end
  62. end