Skip to content

Commit

Permalink
test:Migrate sidekiq tests to RSpec (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcotc authored Jul 8, 2020
1 parent 16643b9 commit 64ec745
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 253 deletions.
11 changes: 1 addition & 10 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ end
namespace :test do
task all: [:main,
:rails,
:sidekiq, :monkey]
:monkey]

Rake::TestTask.new(:main) do |t|
t.libs << %w[test lib]
Expand All @@ -140,7 +140,6 @@ namespace :test do

[
:grape,
:sidekiq,
:sucker_punch
].each do |contrib|
Rake::TestTask.new(contrib) do |t|
Expand Down Expand Up @@ -199,7 +198,6 @@ task :ci do
if RUBY_PLATFORM != 'java'
# Contrib minitests
sh 'bundle exec appraisal contrib-old rake test:monkey'
sh 'bundle exec appraisal contrib-old rake test:sidekiq'
sh 'bundle exec appraisal contrib-old rake test:sucker_punch'
# Contrib specs
sh 'bundle exec appraisal contrib-old rake spec:active_model_serializers'
Expand Down Expand Up @@ -256,7 +254,6 @@ task :ci do
if RUBY_PLATFORM != 'java'
# Contrib minitests
sh 'bundle exec appraisal contrib-old rake test:monkey'
sh 'bundle exec appraisal contrib-old rake test:sidekiq'
sh 'bundle exec appraisal contrib-old rake test:sucker_punch'
# Contrib specs
sh 'bundle exec appraisal contrib-old rake spec:active_model_serializers'
Expand Down Expand Up @@ -320,7 +317,6 @@ task :ci do
if RUBY_PLATFORM != 'java'
# Contrib minitests
sh 'bundle exec appraisal contrib rake test:grape'
sh 'bundle exec appraisal contrib rake test:sidekiq'
sh 'bundle exec appraisal contrib rake test:sucker_punch'
# Contrib specs
sh 'bundle exec appraisal contrib rake spec:action_pack'
Expand Down Expand Up @@ -395,7 +391,6 @@ task :ci do
if RUBY_PLATFORM != 'java'
# Contrib minitests
sh 'bundle exec appraisal contrib rake test:grape'
sh 'bundle exec appraisal contrib rake test:sidekiq'
sh 'bundle exec appraisal contrib rake test:sucker_punch'
# Contrib specs
sh 'bundle exec appraisal contrib rake spec:action_pack'
Expand Down Expand Up @@ -475,7 +470,6 @@ task :ci do
sh 'bundle exec rake benchmark'
# Contrib minitests
sh 'bundle exec appraisal contrib rake test:grape'
sh 'bundle exec appraisal contrib rake test:sidekiq'
sh 'bundle exec appraisal contrib rake test:sucker_punch'
# Contrib specs
sh 'bundle exec appraisal contrib rake spec:action_pack'
Expand Down Expand Up @@ -540,7 +534,6 @@ task :ci do
sh 'bundle exec rake benchmark'
# Contrib minitests
sh 'bundle exec appraisal contrib rake test:grape'
sh 'bundle exec appraisal contrib rake test:sidekiq'
sh 'bundle exec appraisal contrib rake test:sucker_punch'
# Contrib specs
sh 'bundle exec appraisal contrib rake spec:action_pack'
Expand Down Expand Up @@ -615,7 +608,6 @@ task :ci do
sh 'bundle exec rake benchmark'
# Contrib minitests
sh 'bundle exec appraisal contrib rake test:grape'
sh 'bundle exec appraisal contrib rake test:sidekiq'
sh 'bundle exec appraisal contrib rake test:sucker_punch'
# Contrib specs
sh 'bundle exec appraisal contrib rake spec:action_pack'
Expand Down Expand Up @@ -689,7 +681,6 @@ task :ci do
sh 'bundle exec rake benchmark'
# Contrib minitests
sh 'bundle exec appraisal contrib rake test:grape'
sh 'bundle exec appraisal contrib rake test:sidekiq'
sh 'bundle exec appraisal contrib rake test:sucker_punch'
# Contrib specs
sh 'bundle exec appraisal contrib rake spec:action_pack'
Expand Down
70 changes: 70 additions & 0 deletions spec/ddtrace/contrib/sidekiq/client_tracer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'ddtrace/contrib/support/spec_helper'
require_relative 'support/helper'

RSpec.describe 'ClientTracerTest' do
include_context 'Sidekiq testing'

subject(:perform_async) { job_class.perform_async }
let(:job_class) { EmptyWorker }

before do
Sidekiq.configure_client do |config|
config.client_middleware.clear
config.client_middleware do |chain|
chain.add(Datadog::Contrib::Sidekiq::ClientTracer)
end
end

Sidekiq::Testing.server_middleware.clear
Sidekiq::Extensions.enable_delay! if Sidekiq::VERSION > '5.0.0'
end

it 'traces job push' do
perform_async

expect(span.service).to eq('sidekiq-client')
expect(span.resource).to eq('EmptyWorker')
expect(span.get_tag('sidekiq.job.queue')).to eq('default')
expect(span.status).to eq(0)
expect(span.parent).to be_nil
expect(span.get_metric('_dd.measured')).to be_nil
end

context 'with nested trace' do
it 'traces job push' do
tracer.trace('parent.span', service: 'parent-service') do
perform_async
end

expect(spans).to have(2).items

parent_span, child_span = spans

expect(parent_span.name).to eq('parent.span')
expect(parent_span.status).to eq(0)
expect(parent_span.parent).to be_nil

expect(child_span.service).to eq('sidekiq-client')
expect(child_span.resource).to eq('EmptyWorker')
expect(child_span.get_tag('sidekiq.job.queue')).to eq('default')
expect(child_span.status).to eq(0)
expect(child_span.parent).to eq(parent_span)
expect(child_span.get_metric('_dd.measured')).to be_nil
end
end

context 'with delayed extensions' do
subject(:do_work) { DelayableClass.delay.do_work }

before do
stub_const('DelayableClass', Class.new do
def self.do_work; end
end)
end

it 'traces with correct resource' do
do_work
expect(spans.first.resource).to eq('DelayableClass.do_work')
end
end
end
23 changes: 23 additions & 0 deletions spec/ddtrace/contrib/sidekiq/disabled_tracer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'ddtrace/contrib/support/spec_helper'
require_relative 'support/helper'

RSpec.describe 'Disabled tracer' do
include_context 'Sidekiq testing'

subject(:perform_async) { job_class.perform_async }
let(:job_class) { EmptyWorker }

before do
Sidekiq::Testing.server_middleware.clear
Sidekiq::Testing.server_middleware do |chain|
Datadog.tracer.configure(enabled: false)
chain.add(Datadog::Contrib::Sidekiq::ServerTracer)
end
end

it 'does not trace' do
perform_async

expect(spans).to be_empty
end
end
119 changes: 119 additions & 0 deletions spec/ddtrace/contrib/sidekiq/server_tracer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
require 'ddtrace/contrib/support/spec_helper'
require_relative 'support/helper'

RSpec.describe 'Server tracer' do
include_context 'Sidekiq testing'

subject(:perform_async) { job_class.perform_async }
let(:job_class) { EmptyWorker }

before do
Sidekiq::Testing.server_middleware.clear
Sidekiq::Testing.server_middleware do |chain|
chain.add(Datadog::Contrib::Sidekiq::ServerTracer)
end

Sidekiq::Extensions.enable_delay! if Sidekiq::VERSION > '5.0.0'
end

it 'traces async job run' do
perform_async

expect(spans).to have(2).items

span, _push = spans
expect(span.service).to eq('sidekiq')
expect(span.resource).to eq('EmptyWorker')
expect(span.get_tag('sidekiq.job.queue')).to eq('default')
expect(span.get_tag('sidekiq.job.delay')).to_not be_nil
expect(span.status).to eq(0)
expect(span.parent).to be_nil
expect(span.get_tag('sidekiq.job.args')).to be_nil
expect(span.get_metric('_dd.measured')).to eq(1.0)
end

context 'with job run failing' do
let(:job_class) { ErrorWorker }

before do
stub_const('ErrorWorker', Class.new do
include Sidekiq::Worker

def perform
raise ZeroDivisionError, 'job error'
end
end)
end

it 'traces async job run' do
expect { perform_async }.to raise_error(ZeroDivisionError)
expect(spans).to have(2).items

span, _push = spans
expect(span.service).to eq('sidekiq')
expect(span.resource).to eq('ErrorWorker')
expect(span.get_tag('sidekiq.job.queue')).to eq('default')
expect(span.get_tag('sidekiq.job.delay')).to_not be_nil
expect(span.status).to eq(1)
expect(span.get_tag(Datadog::Ext::Errors::MSG)).to eq('job error')
expect(span.get_tag(Datadog::Ext::Errors::TYPE)).to eq('ZeroDivisionError')
expect(span.parent).to be_nil
expect(span.get_tag('sidekiq.job.args')).to be_nil
expect(span.get_metric('_dd.measured')).to eq(1.0)
end
end

context 'with custom job' do
before do
stub_const('CustomWorker', Class.new do
include Sidekiq::Worker

def self.datadog_tracer_config
{ service_name: 'sidekiq-slow', tag_args: true }
end

def perform(_) end
end)
end

it 'traces async job run' do
perform_async
CustomWorker.perform_async('random_id')

expect(spans).to have(4).items

custom, empty, _push, _push = spans

expect(empty.service).to eq('sidekiq')
expect(empty.resource).to eq('EmptyWorker')
expect(empty.get_tag('sidekiq.job.queue')).to eq('default')
expect(empty.get_tag('sidekiq.job.delay')).to_not be_nil
expect(empty.status).to eq(0)
expect(empty.parent).to be_nil
expect(empty.get_metric('_dd.measured')).to eq(1.0)

expect(custom.service).to eq('sidekiq-slow')
expect(custom.resource).to eq('CustomWorker')
expect(custom.get_tag('sidekiq.job.queue')).to eq('default')
expect(custom.status).to eq(0)
expect(custom.parent).to be_nil
expect(custom.get_tag('sidekiq.job.args')).to eq(['random_id'].to_s)
expect(custom.get_metric('_dd.measured')).to eq(1.0)
end
end

context 'with delayed extensions' do
subject(:do_work) { DelayableClass.delay.do_work }

before do
stub_const('DelayableClass', Class.new do
def self.do_work; end
end)
end

it 'traces with correct resource' do
do_work
expect(spans.first.resource).to eq('DelayableClass.do_work')
end
end
end
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@

require 'sidekiq/testing'
require 'ddtrace'
require 'ddtrace/contrib/sidekiq/client_tracer'
require 'ddtrace/contrib/sidekiq/server_tracer'
require 'helper'

class TracerTestBase < Minitest::Test
include TestTracerHelper

REDIS_HOST = ENV.fetch('TEST_REDIS_HOST', '127.0.0.1').freeze
REDIS_PORT = ENV.fetch('TEST_REDIS_PORT', 6379)
RSpec.shared_context 'Sidekiq testing' do
let(:redis_host) { ENV.fetch('TEST_REDIS_HOST', '127.0.0.1') }
let(:redis_port) { ENV.fetch('TEST_REDIS_PORT', 6379) }

def configure
before do
Datadog.configure do |c|
c.use :sidekiq
end

redis_url = "redis://#{REDIS_HOST}:#{REDIS_PORT}"
redis_url = "redis://#{redis_host}:#{redis_port}"

Sidekiq.configure_client do |config|
config.redis = { url: redis_url }
Expand All @@ -29,7 +25,10 @@ def configure
Sidekiq::Testing.inline!
end

def writer
@tracer.writer
let!(:empty_worker) do
stub_const('EmptyWorker', Class.new do
include Sidekiq::Worker
def perform; end
end)
end
end
29 changes: 29 additions & 0 deletions spec/ddtrace/contrib/sidekiq/tracer_configure_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'ddtrace/contrib/support/spec_helper'
require_relative 'support/helper'

RSpec.describe 'Tracer configuration' do
include_context 'Sidekiq testing'

subject(:perform_async) { job_class.perform_async }
let(:job_class) { EmptyWorker }

context 'with custom middleware configuration' do
before do
Sidekiq::Testing.server_middleware do |chain|
chain.add(
Datadog::Contrib::Sidekiq::ServerTracer,
service_name: 'my-service'
)
end
end

it 'instruments with custom values' do
perform_async

expect(spans).to have(2).items

span, _push = spans
expect(span.service).to eq('my-service')
end
end
end
Loading

0 comments on commit 64ec745

Please sign in to comment.