Skip to content

Commit

Permalink
Fix various flaky tests due to process-wide data changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mperham committed Feb 28, 2019
1 parent cfe53e5 commit 20f4cdb
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 87 deletions.
18 changes: 11 additions & 7 deletions test/test_util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
require 'sidekiq/util'

class TestUtil < Minitest::Test

class Helpers
include Sidekiq::Util
end

def test_event_firing
Sidekiq.options[:lifecycle_events][:startup] = [proc { raise "boom" }]
h = Helpers.new
h.fire_event(:startup)
before_handlers = Sidekiq.options[:lifecycle_events][:startup]
begin
Sidekiq.options[:lifecycle_events][:startup] = [proc { raise "boom" }]
h = Helpers.new
h.fire_event(:startup)

Sidekiq.options[:lifecycle_events][:startup] = [proc { raise "boom" }]
assert_raises RuntimeError do
h.fire_event(:startup, reraise: true)
Sidekiq.options[:lifecycle_events][:startup] = [proc { raise "boom" }]
assert_raises RuntimeError do
h.fire_event(:startup, reraise: true)
end
ensure
Sidekiq.options[:lifecycle_events][:startup] = before_handlers
end
end
end
160 changes: 80 additions & 80 deletions test/test_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ def job_params(job, score)
end

before do
ENV["RACK_ENV"] = "test"
Sidekiq.redis {|c| c.flushdb }
Sidekiq::Web.middlewares.clear
end

class WebWorker
Expand Down Expand Up @@ -634,110 +636,108 @@ def add_worker
end
end
end
end

describe 'sidekiq web with basic auth' do
include Rack::Test::Methods

def app
app = Sidekiq::Web.new
app.use(Rack::Auth::Basic) { |user, pass| user == "a" && pass == "b" }

app
end

it 'requires basic authentication' do
get '/'

assert_equal 401, last_response.status
refute_nil last_response.header["WWW-Authenticate"]
end
describe 'basic auth' do
include Rack::Test::Methods

it 'authenticates successfuly' do
basic_authorize 'a', 'b'
def app
app = Sidekiq::Web.new
app.use(Rack::Auth::Basic) { |user, pass| user == "a" && pass == "b" }

get '/'
app
end

assert_equal 200, last_response.status
end
end
it 'requires basic authentication' do
get '/'

describe 'sidekiq web with custom session' do
include Rack::Test::Methods
assert_equal 401, last_response.status
refute_nil last_response.header["WWW-Authenticate"]
end

def app
app = Sidekiq::Web.new
it 'authenticates successfuly' do
basic_authorize 'a', 'b'

app.use Rack::Session::Cookie, secret: 'v3rys3cr31', host: 'nicehost.org'
get '/'

app
assert_equal 200, last_response.status
end
end

it 'requires basic authentication' do
get '/'

session_options = last_request.env['rack.session'].options
describe 'custom session' do
include Rack::Test::Methods

assert_equal 'v3rys3cr31', session_options[:secret]
assert_equal 'nicehost.org', session_options[:host]
end
def app
app = Sidekiq::Web.new
app.use Rack::Session::Cookie, secret: 'v3rys3cr31', host: 'nicehost.org'
app
end

describe 'sessions options' do
include Rack::Test::Methods
it 'requires basic authentication' do
get '/'

describe 'using #disable' do
def app
app = Sidekiq::Web.new
app.disable(:sessions)
app
end
session_options = last_request.env['rack.session'].options

it "doesn't create sessions" do
get '/'
assert_nil last_request.env['rack.session']
end
assert_equal 'v3rys3cr31', session_options[:secret]
assert_equal 'nicehost.org', session_options[:host]
end

describe 'using #set with false argument' do
def app
app = Sidekiq::Web.new
app.set(:sessions, false)
app
end
describe 'sessions options' do
include Rack::Test::Methods

it "doesn't create sessions" do
get '/'
assert_nil last_request.env['rack.session']
end
end
describe 'using #disable' do
def app
app = Sidekiq::Web.new
app.disable(:sessions)
app
end

describe 'using #set with an hash' do
def app
app = Sidekiq::Web.new
app.set(:sessions, { domain: :all })
app
it "doesn't create sessions" do
get '/'
assert_nil last_request.env['rack.session']
end
end

it "creates sessions" do
get '/'
refute_nil last_request.env['rack.session']
refute_empty last_request.env['rack.session'].options
assert_equal :all, last_request.env['rack.session'].options[:domain]
describe 'using #set with false argument' do
def app
app = Sidekiq::Web.new
app.set(:sessions, false)
app
end

it "doesn't create sessions" do
get '/'
assert_nil last_request.env['rack.session']
end
end
end

describe 'using #enable' do
def app
app = Sidekiq::Web.new
app.enable(:sessions)
app
describe 'using #set with an hash' do
def app
app = Sidekiq::Web.new
app.set(:sessions, { domain: :all })
app
end

it "creates sessions" do
get '/'
refute_nil last_request.env['rack.session']
refute_empty last_request.env['rack.session'].options
assert_equal :all, last_request.env['rack.session'].options[:domain]
end
end

it "creates sessions" do
get '/'
refute_nil last_request.env['rack.session']
refute_empty last_request.env['rack.session'].options
refute_nil last_request.env['rack.session'].options[:secret]
describe 'using #enable' do
def app
app = Sidekiq::Web.new
app.enable(:sessions)
app
end

it "creates sessions" do
get '/'
refute_nil last_request.env['rack.session']
refute_empty last_request.env['rack.session'].options
refute_nil last_request.env['rack.session'].options[:secret]
end
end
end
end
Expand Down

0 comments on commit 20f4cdb

Please sign in to comment.