|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +# Stubbing the constants out; these will exist in apps which have SolidQueue loaded |
| 4 | +module SolidQueue |
| 5 | + def self.process_alive_threshold |
| 6 | + 5.minutes |
| 7 | + end |
| 8 | + |
| 9 | + class Process; end |
| 10 | + class ReadyExecution; end |
| 11 | + class ScheduledExecution; end |
| 12 | + class ClaimedExecution; end |
| 13 | + class FailedExecution; end |
| 14 | +end |
| 15 | + |
| 16 | +module OkComputer |
| 17 | + describe SolidQueueCheck do |
| 18 | + it "is a Check" do |
| 19 | + expect(subject).to be_a Check |
| 20 | + end |
| 21 | + |
| 22 | + context "#check" do |
| 23 | + context "when workers and a dispatcher are alive" do |
| 24 | + before do |
| 25 | + allow(subject).to receive(:live_workers).and_return(2) |
| 26 | + allow(subject).to receive(:live_dispatchers).and_return(1) |
| 27 | + allow(subject).to receive(:stats).and_return("ready: 0, scheduled: 0, in progress: 0, failed: 0") |
| 28 | + end |
| 29 | + |
| 30 | + it { is_expected.to be_successful_check } |
| 31 | + it { is_expected.to have_message "SolidQueue is up (2 worker(s), 1 dispatcher(s) alive)." } |
| 32 | + it { is_expected.to have_message "Job Counts: ready: 0, scheduled: 0, in progress: 0, failed: 0" } |
| 33 | + end |
| 34 | + |
| 35 | + context "when no workers are alive" do |
| 36 | + before do |
| 37 | + allow(subject).to receive(:live_workers).and_return(0) |
| 38 | + allow(subject).to receive(:stats).and_return("ready: 5, scheduled: 0, in progress: 0, failed: 0") |
| 39 | + end |
| 40 | + |
| 41 | + it { is_expected.not_to be_successful_check } |
| 42 | + it { is_expected.to have_message "SolidQueue is DOWN. No workers are alive." } |
| 43 | + end |
| 44 | + |
| 45 | + context "when workers are alive but the dispatcher is down" do |
| 46 | + before do |
| 47 | + allow(subject).to receive(:live_workers).and_return(2) |
| 48 | + allow(subject).to receive(:live_dispatchers).and_return(0) |
| 49 | + allow(subject).to receive(:stats).and_return("ready: 0, scheduled: 9, in progress: 0, failed: 0") |
| 50 | + end |
| 51 | + |
| 52 | + it { is_expected.not_to be_successful_check } |
| 53 | + it { is_expected.to have_message "SolidQueue dispatcher is DOWN. Scheduled jobs will not run." } |
| 54 | + end |
| 55 | + |
| 56 | + context "when an error occurs" do |
| 57 | + before do |
| 58 | + allow(subject).to receive(:live_workers).and_raise(StandardError, "boom") |
| 59 | + end |
| 60 | + |
| 61 | + it { is_expected.not_to be_successful_check } |
| 62 | + it { is_expected.to have_message "Error: 'boom'" } |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + context "#live_workers" do |
| 67 | + it "counts worker processes with a recent heartbeat" do |
| 68 | + relation = double("relation") |
| 69 | + expect(SolidQueue::Process).to receive(:where).with("last_heartbeat_at > ?", anything).and_return(relation) |
| 70 | + expect(relation).to receive(:where).with(kind: "Worker").and_return(relation) |
| 71 | + expect(relation).to receive(:count).and_return(3) |
| 72 | + |
| 73 | + expect(subject.live_workers).to eq(3) |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + context "#live_dispatchers" do |
| 78 | + it "counts dispatcher processes with a recent heartbeat" do |
| 79 | + relation = double("relation") |
| 80 | + expect(SolidQueue::Process).to receive(:where).with("last_heartbeat_at > ?", anything).and_return(relation) |
| 81 | + expect(relation).to receive(:where).with(kind: "Dispatcher").and_return(relation) |
| 82 | + expect(relation).to receive(:count).and_return(1) |
| 83 | + |
| 84 | + expect(subject.live_dispatchers).to eq(1) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + context "#stats" do |
| 89 | + it "summarizes the current job counts" do |
| 90 | + allow(SolidQueue::ReadyExecution).to receive(:count).and_return(4) |
| 91 | + allow(SolidQueue::ScheduledExecution).to receive(:count).and_return(1) |
| 92 | + allow(SolidQueue::ClaimedExecution).to receive(:count).and_return(2) |
| 93 | + allow(SolidQueue::FailedExecution).to receive(:count).and_return(0) |
| 94 | + |
| 95 | + expect(subject.stats).to eq("ready: 4, scheduled: 1, in progress: 2, failed: 0") |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | +end |
0 commit comments