1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/test/current_attributes.rb
Mike Perham 29dca70e24
Introduce Sidekiq::Capsule (#5487)
* Initial work on Sidekiq::Config

* Initial work on Sidekiq::Config

* reduce dependencies in deploy marks

* bare sidekiq and webapp

* Modify runtime to work with Capsules

* Cleanup

* Rename test files to remove test_ prefix

* Update test suite and standard rules to be more compliant

* Move constant definition outside code, per standard formatting

* Loads of changes for introduction of Capsules

* Remove Redis adapter abstraction

* update capsule overview

* Ensure Sidekiq.redis uses the correct pool for jobs running within a Capsule

* Use default_capsule for safety

* Slow down the beat to halve its Redis overhead

* move config fixtures into cfg/

* Add capsule middleware test

* use accessor
2022-08-25 10:15:11 -07:00

67 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require_relative "./helper"
require "sidekiq/middleware/current_attributes"
require "sidekiq/fetch"
module Myapp
class Current < ActiveSupport::CurrentAttributes
attribute :user_id
end
end
describe "Current attributes" do
before do
@config = reset!
end
it "saves" do
cm = Sidekiq::CurrentAttributes::Save.new(Myapp::Current)
job = {}
with_context(:user_id, 123) do
cm.call(nil, job, nil, nil) do
assert_equal 123, job["cattr"][:user_id]
end
end
end
it "loads" do
cm = Sidekiq::CurrentAttributes::Load.new(Myapp::Current)
job = {"cattr" => {"user_id" => 123}}
assert_nil Myapp::Current.user_id
cm.call(nil, job, nil) do
assert_equal 123, Myapp::Current.user_id
end
# the Rails reloader is responsible for reseting Current after every unit of work
end
it "persists" do
Sidekiq::CurrentAttributes.persist(Myapp::Current, @config)
job_hash = {}
with_context(:user_id, 16) do
@config.client_middleware.invoke(nil, job_hash, nil, nil) do
assert_equal 16, job_hash["cattr"][:user_id]
end
end
# assert_nil Myapp::Current.user_id
# Sidekiq.server_middleware.invoke(nil, job_hash, nil) do
# assert_equal 16, job_hash["cattr"][:user_id]
# assert_equal 16, Myapp::Current.user_id
# end
# assert_nil Myapp::Current.user_id
# ensure
# Sidekiq.client_middleware.clear
# Sidekiq.server_middleware.clear
end
private
def with_context(attr, value)
Myapp::Current.send("#{attr}=", value)
yield
ensure
Myapp::Current.reset_all
end
end