require "spec_helper" module Sidewatch describe Sidekiq do before(:each) do allow(::Sidekiq).to receive(:redis=) allow(subject).to receive(:redis_uri){ "redis://localhost:6379/0" } end describe "#metrics" do it "returns global and per-queue metrics" do global = double("global-metric") expect(subject).to receive(:global_metrics).and_return([global]) per_queue = double("queue-metric") expect(subject).to receive(:per_queue_metrics).and_return([per_queue]) metrics = subject.metrics expect(metrics).to contain_exactly(global, per_queue) end end describe "#global_metrics" do let(:stats){ double("stats", failed: 111, enqueued: 222, processed: 333, retry_size: 444, scheduled_size: 555, ) } before(:each) do allow(subject).to receive(:sidekiq_stats){ stats } end it "produces a metric for failed jobs" do failed = subject.global_metrics.find{|m| m.name == "Failed"} expect(failed.value).to eq 111 end it "produces a metric for enqueued jobs" do failed = subject.global_metrics.find{|m| m.name == "Enqueued"} expect(failed.value).to eq 222 end it "produces a metric for processed jobs" do failed = subject.global_metrics.find{|m| m.name == "Processed"} expect(failed.value).to eq 333 end it "produces a metric for retry queue size" do failed = subject.global_metrics.find{|m| m.name == "RetrySize"} expect(failed.value).to eq 444 end it "produces a metric for scheduled queue size" do failed = subject.global_metrics.find{|m| m.name == "ScheduledSize"} expect(failed.value).to eq 555 end end describe "#per_queue_metrics" do let(:queues){ [ double("queue1", name: "high-priority", size: 50, latency: 5), double("queue2", name: "low-priority", size: 100, latency: 10), ] } before(:each) do allow(subject).to receive(:sidekiq_queues){ queues } end it "produces queue length and latency metrics for each queue" do metrics = subject.per_queue_metrics.map(&:name).uniq expect(metrics).to contain_exactly("Enqueued", "Latency") end it "produces at least one metric for every queue" do metrics = subject.per_queue_metrics queues = metrics.map{|metric| metric.dimensions["QueueName"]}.uniq expect(queues).to contain_exactly("high-priority", "low-priority") end end end end