Skip to content

Instantly share code, notes, and snippets.

@apneadiving
Created October 8, 2015 07:47
Show Gist options
  • Save apneadiving/6adcb32ce7fec82d0653 to your computer and use it in GitHub Desktop.
Save apneadiving/6adcb32ce7fec82d0653 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe 'thread safe tenants' do
let(:db_names) { [db1, db2] }
before do
Apartment.configure do |config|
config.excluded_models = ["Company"]
config.tenant_names = lambda{ Company.pluck(:database) }
config.use_schemas = false
end
Apartment::Tenant.reload!(config)
db_names.each do |db_name|
Apartment::Tenant.create(db_name)
Company.create database: db_name
end
end
after do
db_names.each{ |db| Apartment::Tenant.drop(db) }
Apartment::Tenant.reset
Company.delete_all
end
def get_thread(tenant)
Thread.new do
Apartment::Tenant.switch(tenant) do
(0..5).each do |i|
sleep 1
expect(tenant).to eq Apartment::Tenant.current
end
end
end
end
it 'ensures each thread keeps the desired tenant' do
t1 = get_thread(db1)
sleep(1)
t2 = get_thread(db2)
t1.join
t2.join
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment