|
3 | 3 | module Katello |
4 | 4 | module Applicability |
5 | 5 | class SchedulerTest < ActiveSupport::TestCase |
6 | | - def queue |
7 | | - Katello::ApplicableHostQueue |
| 6 | + let(:scheduler) { Katello::Applicability::Scheduler } |
| 7 | + let(:queue) { @queue } |
| 8 | + |
| 9 | + def setup_queue(queue) |
| 10 | + @queue = queue |
| 11 | + scheduler.stubs(:queue).returns(queue) |
| 12 | + end |
| 13 | + |
| 14 | + class TestQueue |
| 15 | + def pop_hosts |
| 16 | + @ids = @queue.pop |
| 17 | + yield(@ids) |
| 18 | + @ids |
| 19 | + end |
| 20 | + |
| 21 | + def queue_depth |
| 22 | + @queue.flatten.length |
| 23 | + end |
| 24 | + end |
| 25 | + |
| 26 | + class EmptyQueue < TestQueue |
| 27 | + def initialize |
| 28 | + @queue = [] |
| 29 | + super |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + class SingleBatchQueue < TestQueue |
| 34 | + def initialize |
| 35 | + @queue = [[], [1]] |
| 36 | + super |
| 37 | + end |
| 38 | + |
| 39 | + def batch_size |
| 40 | + 1 |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + class LowVolumeQueue < TestQueue |
| 45 | + def initialize |
| 46 | + @queue = [[], [1]] |
| 47 | + super |
| 48 | + end |
| 49 | + |
| 50 | + def batch_size |
| 51 | + 5 |
| 52 | + end |
8 | 53 | end |
9 | 54 |
|
10 | 55 | class DrainLoopTest < SchedulerTest |
11 | 56 | test "does nothing when queue is empty" do |
12 | | - queue.expects(:pop_hosts).returns([]) |
| 57 | + setup_queue EmptyQueue.new |
| 58 | + |
13 | 59 | ForemanTasks.expects(:async_task).never |
| 60 | + scheduler.expects(:sleep).never |
14 | 61 |
|
15 | | - Katello::Applicability::Scheduler.drain_loop |
| 62 | + scheduler.drain_loop |
16 | 63 | end |
17 | 64 |
|
18 | 65 | test "spawns BulkGenerate" do |
19 | | - queue.stubs(:pop_hosts).returns([1]).then.returns([]) |
| 66 | + setup_queue SingleBatchQueue.new |
| 67 | + |
20 | 68 | ForemanTasks.expects(:async_task).with(Actions::Katello::Applicability::Hosts::BulkGenerate, host_ids: [1]) |
21 | | - Katello::Applicability::Scheduler.expects(:sleep) |
| 69 | + scheduler.expects(:sleep).never |
22 | 70 |
|
23 | | - Katello::Applicability::Scheduler.drain_loop |
| 71 | + scheduler.drain_loop |
24 | 72 | end |
25 | 73 |
|
26 | | - test "doesn't sleep when queue is full" do |
27 | | - queue.stubs(:pop_hosts).returns([1, 2, 3]).then.returns([]) |
28 | | - queue.stubs(:batch_size).returns(3) |
29 | | - ForemanTasks.expects(:async_task) |
30 | | - Katello::Applicability::Scheduler.expects(:sleep).never |
| 74 | + test "sleeps when queue is filling" do |
| 75 | + setup_queue LowVolumeQueue.new |
| 76 | + |
| 77 | + ForemanTasks.expects(:async_task).with(Actions::Katello::Applicability::Hosts::BulkGenerate, host_ids: [1]) |
| 78 | + scheduler.expects(:sleep) |
31 | 79 |
|
32 | | - Katello::Applicability::Scheduler.drain_loop |
| 80 | + scheduler.drain_loop |
33 | 81 | end |
34 | 82 | end |
35 | 83 |
|
36 | 84 | class TriggerDrainTest < SchedulerTest |
37 | 85 | test "spawns BulkGenerate" do |
38 | | - queue.expects(:batch_size).returns(2) |
39 | | - queue.expects(:queue_depth).returns(1) |
40 | | - Katello::Applicability::Scheduler.expects(:bulk_generate_tasks).returns([]) |
41 | | - queue.expects(:pop_hosts).returns([:fake]) |
42 | | - ForemanTasks.expects(:async_task).with(Actions::Katello::Applicability::Hosts::BulkGenerate, host_ids: [:fake]) |
| 86 | + setup_queue LowVolumeQueue.new |
| 87 | + scheduler.expects(:bulk_generate_tasks).returns([]) |
| 88 | + ForemanTasks.expects(:async_task).with(Actions::Katello::Applicability::Hosts::BulkGenerate, host_ids: [1]) |
43 | 89 |
|
44 | 90 | Katello::Applicability::Scheduler.trigger_drain |
45 | 91 | end |
46 | 92 |
|
47 | 93 | test "spawns scheduler task" do |
48 | | - queue.expects(:batch_size).returns(2) |
49 | | - queue.expects(:queue_depth).returns(3) |
| 94 | + setup_queue LowVolumeQueue.new |
| 95 | + scheduler.expects(:bulk_generate_tasks).returns([:fake]) |
| 96 | + |
50 | 97 | ForemanTasks.expects(:async_task).with(Actions::Katello::Applicability::Scheduler) |
51 | 98 |
|
52 | 99 | Katello::Applicability::Scheduler.trigger_drain |
53 | 100 | end |
54 | 101 |
|
55 | 102 | test "does nothing when queue is empty" do |
56 | | - queue.expects(:queue_depth).returns(0) |
| 103 | + setup_queue EmptyQueue.new |
57 | 104 | ForemanTasks.expects(:async_task).never |
58 | 105 |
|
59 | 106 | Katello::Applicability::Scheduler.trigger_drain |
60 | 107 | end |
61 | 108 |
|
62 | 109 | test "spawns nothing when scheduler task is running" do |
63 | | - queue.expects(:queue_depth).returns(1) |
| 110 | + setup_queue LowVolumeQueue.new |
64 | 111 | Katello::Applicability::Scheduler.expects(:scheduler_task).returns(stub) |
65 | 112 | ForemanTasks.expects(:async_task).never |
66 | 113 |
|
|
0 commit comments