Skip to content

Instantly share code, notes, and snippets.

@bpardee
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpardee/b700fa1da841a9b933ca to your computer and use it in GitHub Desktop.
Save bpardee/b700fa1da841a9b933ca to your computer and use it in GitHub Desktop.
ActiveRecord threadsafe test
require 'active_record'
puts "Active Record #{ActiveRecord::VERSION::STRING}"
# TODO: How to get past sqlite3_connection undefined error?
# ActiveRecord::Base.establish_connection(
# :adapter => 'jdbcsqlite3',
# :database => ':memory:'
# )
ActiveRecord::Base.establish_connection(
adapter: 'mysql2',
database: 'foobar',
# Needed for Rails 4.2
pool: 100
)
ActiveRecord::Schema.define do
create_table :foos, force: true do |t|
100.times do |i|
t.boolean "field#{i}".to_sym, :boolean
end
end
end
class Foo < ActiveRecord::Base
if ENV['FIX_FOO']
after_initialize :mutexize_attributes
def mutexize_attributes
mutex = Mutex.new
class << attributes
def []=(attr, val)
mutex.synchronize { super }
end
end
end
end
end
10.times do
foo = Foo.new
(0..99).map do |i|
Thread.new(i) do |i|
foo.send("field#{i}=", true)
end
end.each {|t| t.join}
attrs = foo.attributes.reject {|k,v| v==true || v==1 || ['id','boolean'].include?(k)}
puts attrs.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment