jobs_test.exs (2520B)
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 5 defmodule Pleroma.JobsTest do 6 use ExUnit.Case, async: true 7 8 alias Jobs.WorkerMock 9 alias Pleroma.Jobs 10 11 setup do 12 state = %{ 13 queues: Enum.into([Jobs.create_queue(:testing)], %{}), 14 refs: %{} 15 } 16 17 [state: state] 18 end 19 20 test "creates queue" do 21 queue = Jobs.create_queue(:foobar) 22 23 assert {:foobar, set} = queue 24 assert :set == elem(set, 0) |> elem(0) 25 end 26 27 test "enqueues an element according to priority" do 28 queue = [%{item: 1, priority: 2}] 29 30 new_queue = Jobs.enqueue_sorted(queue, 2, 1) 31 assert new_queue == [%{item: 2, priority: 1}, %{item: 1, priority: 2}] 32 33 new_queue = Jobs.enqueue_sorted(queue, 2, 3) 34 assert new_queue == [%{item: 1, priority: 2}, %{item: 2, priority: 3}] 35 end 36 37 test "pop first item" do 38 queue = [%{item: 2, priority: 1}, %{item: 1, priority: 2}] 39 40 assert {2, [%{item: 1, priority: 2}]} = Jobs.queue_pop(queue) 41 end 42 43 test "enqueue a job", %{state: state} do 44 assert {:noreply, new_state} = 45 Jobs.handle_cast({:enqueue, :testing, WorkerMock, [:test_job, :foo, :bar], 3}, state) 46 47 assert %{queues: %{testing: {running_jobs, []}}, refs: _} = new_state 48 assert :sets.size(running_jobs) == 1 49 assert [ref] = :sets.to_list(running_jobs) 50 assert %{refs: %{^ref => :testing}} = new_state 51 end 52 53 test "max jobs setting", %{state: state} do 54 max_jobs = Pleroma.Config.get([Jobs, :testing, :max_jobs]) 55 56 {:noreply, state} = 57 Enum.reduce(1..(max_jobs + 1), {:noreply, state}, fn _, {:noreply, state} -> 58 Jobs.handle_cast({:enqueue, :testing, WorkerMock, [:test_job, :foo, :bar], 3}, state) 59 end) 60 61 assert %{ 62 queues: %{ 63 testing: 64 {running_jobs, [%{item: {WorkerMock, [:test_job, :foo, :bar]}, priority: 3}]} 65 } 66 } = state 67 68 assert :sets.size(running_jobs) == max_jobs 69 end 70 71 test "remove job after it finished", %{state: state} do 72 {:noreply, new_state} = 73 Jobs.handle_cast({:enqueue, :testing, WorkerMock, [:test_job, :foo, :bar], 3}, state) 74 75 %{queues: %{testing: {running_jobs, []}}} = new_state 76 [ref] = :sets.to_list(running_jobs) 77 78 assert {:noreply, %{queues: %{testing: {running_jobs, []}}, refs: %{}}} = 79 Jobs.handle_info({:DOWN, ref, :process, nil, nil}, new_state) 80 81 assert :sets.size(running_jobs) == 0 82 end 83 end