|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +# frozen_string_literal: true |
| 19 | + |
| 20 | +require 'spec_helper' |
| 21 | + |
| 22 | +# Solid Queue ships +SolidQueue::ClaimedExecution+ as an ActiveRecord model |
| 23 | +# under +app/models+, loaded via the engine through Zeitwerk. To avoid |
| 24 | +# requiring a full Rails boot here, the spec defines a stand-in constant with |
| 25 | +# the same shape and lets the spy hook it. |
| 26 | +module SolidQueue |
| 27 | + class ClaimedExecution |
| 28 | + attr_reader :job |
| 29 | + |
| 30 | + def initialize(job) |
| 31 | + @job = job |
| 32 | + end |
| 33 | + |
| 34 | + # Real solid_queue runs +ActiveJob::Base.execute+ here. For the spec we |
| 35 | + # just dispatch to the test job stub so that +super+ inside the spy's |
| 36 | + # +Ext+ module is exercised end-to-end. |
| 37 | + def perform |
| 38 | + job.run if job.respond_to?(:run) |
| 39 | + end |
| 40 | + end |
| 41 | +end |
| 42 | + |
| 43 | +require 'elastic_apm/spies/solid_queue' |
| 44 | + |
| 45 | +module ElasticAPM |
| 46 | + RSpec.describe 'Spy: SolidQueue', :intercept do |
| 47 | + class TestSolidQueueJob |
| 48 | + attr_reader :queue_name |
| 49 | + |
| 50 | + def initialize(queue_name: 'default') |
| 51 | + @queue_name = queue_name |
| 52 | + end |
| 53 | + |
| 54 | + def class_name |
| 55 | + self.class.name |
| 56 | + end |
| 57 | + |
| 58 | + def run |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + class ExplodingSolidQueueJob < TestSolidQueueJob |
| 63 | + def run |
| 64 | + raise ZeroDivisionError, 'boom' |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + it 'instruments successful job perform' do |
| 69 | + with_agent do |
| 70 | + ::SolidQueue::ClaimedExecution.new(TestSolidQueueJob.new).perform |
| 71 | + end |
| 72 | + |
| 73 | + transaction, = @intercepted.transactions |
| 74 | + expect(transaction).to_not be_nil |
| 75 | + expect(transaction.name).to eq 'ElasticAPM::TestSolidQueueJob' |
| 76 | + expect(transaction.type).to eq 'SolidQueue' |
| 77 | + expect(transaction.result).to eq 'success' |
| 78 | + expect(transaction.outcome).to eq 'success' |
| 79 | + |
| 80 | + labels = transaction.context.labels |
| 81 | + expect(labels[:queue]).to eq 'default' |
| 82 | + end |
| 83 | + |
| 84 | + it 'reports errors and marks transaction as failure' do |
| 85 | + expect do |
| 86 | + with_agent do |
| 87 | + ::SolidQueue::ClaimedExecution.new(ExplodingSolidQueueJob.new).perform |
| 88 | + end |
| 89 | + end.to raise_error(ZeroDivisionError) |
| 90 | + |
| 91 | + transaction, = @intercepted.transactions |
| 92 | + expect(transaction.name).to eq 'ElasticAPM::ExplodingSolidQueueJob' |
| 93 | + expect(transaction.type).to eq 'SolidQueue' |
| 94 | + expect(transaction.outcome).to eq 'failure' |
| 95 | + expect(transaction.result).to eq 'error' |
| 96 | + |
| 97 | + error, = @intercepted.errors |
| 98 | + expect(error.exception.type).to eq 'ZeroDivisionError' |
| 99 | + end |
| 100 | + |
| 101 | + it 'captures the queue label from the job' do |
| 102 | + with_agent do |
| 103 | + job = TestSolidQueueJob.new(queue_name: 'critical') |
| 104 | + ::SolidQueue::ClaimedExecution.new(job).perform |
| 105 | + end |
| 106 | + |
| 107 | + transaction, = @intercepted.transactions |
| 108 | + expect(transaction.context.labels[:queue]).to eq 'critical' |
| 109 | + end |
| 110 | + |
| 111 | + it 'creates a transaction for each perform call' do |
| 112 | + with_agent do |
| 113 | + ::SolidQueue::ClaimedExecution.new(TestSolidQueueJob.new).perform |
| 114 | + ::SolidQueue::ClaimedExecution.new(TestSolidQueueJob.new).perform |
| 115 | + end |
| 116 | + |
| 117 | + expect(@intercepted.transactions.size).to eq 2 |
| 118 | + end |
| 119 | + |
| 120 | + it 'prepends the Ext module onto SolidQueue::ClaimedExecution' do |
| 121 | + expect(::SolidQueue::ClaimedExecution.ancestors) |
| 122 | + .to include(Spies::SolidQueueSpy::Ext) |
| 123 | + end |
| 124 | + |
| 125 | + it "runs when the agent doesn't" do |
| 126 | + expect do |
| 127 | + ::SolidQueue::ClaimedExecution.new(TestSolidQueueJob.new).perform |
| 128 | + end.to_not raise_error |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments