Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stub constants in specs #1285

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion spec/lib/appsignal/cli/install_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,6 @@ def run
it "completes the installation" do
run

puts output
expect(output).to include(*installation_instructions)
expect(output).to include_complete_install
end
Expand Down
94 changes: 34 additions & 60 deletions spec/lib/appsignal/hooks/activejob_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,33 @@
ActiveJob::Base.queue_adapter = :inline

start_agent(:options => options)
class ActiveJobTestJob < ActiveJob::Base
stub_const("ActiveJobTestJob", Class.new(ActiveJob::Base) do
def perform(*_args)
end
end
end)

class ActiveJobErrorTestJob < ActiveJob::Base
stub_const("ActiveJobErrorTestJob", Class.new(ActiveJob::Base) do
def perform
raise "uh oh"
end
end
end)

class ActiveJobErrorWithRetryTestJob < ActiveJob::Base
stub_const("ActiveJobErrorWithRetryTestJob", Class.new(ActiveJob::Base) do
retry_on StandardError, :wait => 0.seconds, :attempts => 2

def perform
raise "uh oh"
end
end
end)

class ActiveJobCustomQueueTestJob < ActiveJob::Base
stub_const("ActiveJobCustomQueueTestJob", Class.new(ActiveJob::Base) do
queue_as :custom_queue

def perform(*_args)
end
end
end)
end
around { |example| keep_transactions { example.run } }
after do
Object.send(:remove_const, :ActiveJobTestJob)
Object.send(:remove_const, :ActiveJobErrorTestJob)
Object.send(:remove_const, :ActiveJobErrorWithRetryTestJob)
Object.send(:remove_const, :ActiveJobCustomQueueTestJob)
end

it "reports the name from the ActiveJob integration" do
tags = { :queue => queue }
Expand Down Expand Up @@ -149,15 +143,12 @@ def perform(*_args)
if DependencyHelper.rails_version >= Gem::Version.new("5.0.0")
context "with priority" do
before do
class ActiveJobPriorityTestJob < ActiveJob::Base
stub_const("ActiveJobPriorityTestJob", Class.new(ActiveJob::Base) do
queue_with_priority 10

def perform(*_args)
end
end
end
after do
Object.send(:remove_const, :ActiveJobPriorityTestJob)
end)
end

it "reports the priority as tag on the transaction" do
Expand Down Expand Up @@ -264,16 +255,13 @@ def perform(*_args)
if DependencyHelper.rails_version >= Gem::Version.new("5.0.0")
context "with priority" do
before do
class ActiveJobErrorPriorityTestJob < ActiveJob::Base
stub_const("ActiveJobErrorPriorityTestJob", Class.new(ActiveJob::Base) do
queue_with_priority 10

def perform(*_args)
raise "uh oh"
end
end
end
after do
Object.send(:remove_const, :ActiveJobErrorPriorityTestJob)
end)
end

it "reports the priority as tag on the transaction" do
Expand Down Expand Up @@ -370,31 +358,28 @@ def perform(*_args)
context "with provider_job_id",
:skip => DependencyHelper.rails_version < Gem::Version.new("5.0.0") do
before do
module ActiveJob
module QueueAdapters
stub_const(
"ActiveJob::QueueAdapters::AppsignalTestAdapter",
Class.new(ActiveJob::QueueAdapters::InlineAdapter) do
# Adapter used in our test suite to add provider data to the job
# data, as is done by Rails provided ActiveJob adapters.
#
# This implementation is based on the
# `ActiveJob::QueueAdapters::InlineAdapter`.
class AppsignalTestAdapter < InlineAdapter
def enqueue(job)
Base.execute(job.serialize.merge("provider_job_id" => "my_provider_job_id"))
end
def enqueue(job)
ActiveJob::Base.execute(
job.serialize.merge("provider_job_id" => "my_provider_job_id")
)
end
end
end
)

class ProviderWrappedActiveJobTestJob < ActiveJob::Base
stub_const("ProviderWrappedActiveJobTestJob", Class.new(ActiveJob::Base) do
self.queue_adapter = :appsignal_test

def perform(*_args)
end
end
end
after do
ActiveJob::QueueAdapters.send(:remove_const, :AppsignalTestAdapter)
Object.send(:remove_const, :ProviderWrappedActiveJobTestJob)
end)
end

it "sets provider_job_id as tag" do
Expand All @@ -409,31 +394,26 @@ def perform(*_args)
context "with enqueued_at",
:skip => DependencyHelper.rails_version < Gem::Version.new("6.0.0") do
before do
module ActiveJob
module QueueAdapters
stub_const(
"ActiveJob::QueueAdapters::AppsignalTestAdapter",
Class.new(ActiveJob::QueueAdapters::InlineAdapter) do
# Adapter used in our test suite to add provider data to the job
# data, as is done by Rails provided ActiveJob adapters.
#
# This implementation is based on the
# `ActiveJob::QueueAdapters::InlineAdapter`.
class AppsignalTestAdapter < InlineAdapter
def enqueue(job)
Base.execute(job.serialize.merge("enqueued_at" => "2020-10-10T10:10:10Z"))
end
def enqueue(job)
ActiveJob::Base.execute(job.serialize.merge("enqueued_at" => "2020-10-10T10:10:10Z"))
end
end
end
)

class ProviderWrappedActiveJobTestJob < ActiveJob::Base
stub_const("ProviderWrappedActiveJobTestJob", Class.new(ActiveJob::Base) do
self.queue_adapter = :appsignal_test

def perform(*_args)
end
end
end
after do
ActiveJob::QueueAdapters.send(:remove_const, :AppsignalTestAdapter)
Object.send(:remove_const, :ProviderWrappedActiveJobTestJob)
end)
end

it "sets queue time on transaction" do
Expand All @@ -448,13 +428,10 @@ def perform(*_args)
include ActionMailerHelpers

before do
class ActionMailerTestJob < ActionMailer::Base
stub_const("ActionMailerTestJob", Class.new(ActionMailer::Base) do
def welcome(_first_arg = nil, _second_arg = nil)
end
end
end
after do
Object.send(:remove_const, :ActionMailerTestJob)
end)
end

context "without params" do
Expand Down Expand Up @@ -524,15 +501,12 @@ def welcome(_first_arg = nil, _second_arg = nil)
include ActionMailerHelpers

before do
class ActionMailerTestMailDeliveryJob < ActionMailer::Base
stub_const("ActionMailerTestMailDeliveryJob", Class.new(ActionMailer::Base) do
self.delivery_job = ActionMailer::MailDeliveryJob

def welcome(*_args)
end
end
end
after do
Object.send(:remove_const, :ActionMailerTestMailDeliveryJob)
end)
end

it "sets the Action mailer data on the transaction" do
Expand Down
20 changes: 9 additions & 11 deletions spec/lib/appsignal/hooks/celluloid_spec.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
describe Appsignal::Hooks::CelluloidHook do
context "with celluloid" do
before :context do
module Celluloid
before do
stub_const("Celluloid", Module.new do
def self.shutdown
@shut_down = true
end

def self.shut_down?
@shut_down == true
end
end
end)
Appsignal::Hooks::CelluloidHook.new.install
end
after :context do
Object.send(:remove_const, :Celluloid)
end

describe "#dependencies_present?" do
subject { described_class.new.dependencies_present? }

it { is_expected.to be_truthy }
end

specify { expect(Appsignal).to receive(:stop) }
specify { expect(Celluloid.shut_down?).to be true }

after do
Celluloid.shutdown
describe "#install" do
it "calls Appsignal.stop on shutdown" do
expect(Appsignal).to receive(:stop)
Celluloid.shutdown
expect(Celluloid.shut_down?).to be true
end
end
end

Expand Down
17 changes: 4 additions & 13 deletions spec/lib/appsignal/hooks/data_mapper_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
describe Appsignal::Hooks::DataMapperHook do
context "with datamapper" do
before :context do
module DataMapper
end

module DataObjects
class Connection
end
end
before do
stub_const("DataMapper", Module.new)
stub_const("DataObjects", Module.new)
stub_const("DataObjects::Connection", Class.new)
Appsignal::Hooks::DataMapperHook.new.install
end

after :context do
Object.send(:remove_const, :DataMapper)
Object.send(:remove_const, :DataObjects)
end

describe "#dependencies_present?" do
subject { described_class.new.dependencies_present? }

Expand Down
22 changes: 9 additions & 13 deletions spec/lib/appsignal/hooks/delayed_job_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
describe Appsignal::Hooks::DelayedJobHook do
context "with delayed job" do
before(:context) do
module Delayed
class Plugin
def self.callbacks
end
before do
stub_const("Delayed::Plugin", Class.new do
def self.callbacks
end

class Worker
def self.plugins
@plugins ||= []
end
end)
stub_const("Delayed::Worker", Class.new do
def self.plugins
@plugins ||= []
end
end
end)
start_agent
end
after(:context) { Object.send(:remove_const, :Delayed) }
before { start_agent }

describe "#dependencies_present?" do
subject { described_class.new.dependencies_present? }
Expand Down
7 changes: 3 additions & 4 deletions spec/lib/appsignal/hooks/excon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
before { start_agent }

context "with Excon" do
before(:context) do
class Excon
before do
stub_const("Excon", Class.new do
def self.defaults
@defaults ||= {}
end
end
end)
Appsignal::Hooks::ExconHook.new.install
end
after(:context) { Object.send(:remove_const, :Excon) }

describe "#dependencies_present?" do
subject { described_class.new.dependencies_present? }
Expand Down
31 changes: 11 additions & 20 deletions spec/lib/appsignal/hooks/gvl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ def expect_gvltools_require
end

context "with old versions of GVLTools" do
before(:context) do
module GVLTools
VERSION = "0.1.0".freeze
end
before do
stub_const("GVLTools::VERSION", "0.1.0")
end

after(:context) { Object.send(:remove_const, :GVLTools) }

before(:each) { expect_gvltools_require }

describe "#dependencies_present?" do
Expand All @@ -50,24 +46,19 @@ module GVLTools
end

context "with new versions of GVLTools" do
before(:context) do
module GVLTools
VERSION = "0.2.0".freeze

module GlobalTimer
def self.enable
end
before do
stub_const("GVLTools", Module.new)
stub_const("GVLTools::VERSION", "0.2.0")
stub_const("GVLTools::GlobalTimer", Module.new do
def self.enable
end

module WaitingThreads
def self.enable
end
end)
stub_const("GVLTools::WaitingThreads", Module.new do
def self.enable
end
end
end)
end

after(:context) { Object.send(:remove_const, :GVLTools) }

describe "#dependencies_present?" do
before(:each) { expect_gvltools_require }

Expand Down
Loading
Loading