Skip to content

Instantly share code, notes, and snippets.

@zarqman
Created March 14, 2023 18:40
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 zarqman/102c6da61dc2a7fa4a24b382d8f78faa to your computer and use it in GitHub Desktop.
Save zarqman/102c6da61dc2a7fa4a24b382d8f78faa to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails", branch: "main"
gem "pg"
end
require "active_record"
require "minitest/autorun"
require "logger"
THREADS = 15 # number of parallel threads to execute
ActiveRecord::Base.establish_connection(
adapter: "postgresql",
database: "schema_cache_race",
pool: THREADS+5,
host: "localhost",
# username: "...",
# password: "...",
)
ActiveRecord::Base.logger = Logger.new(STDOUT)
## override query() to add a delay to simulate network latency
# module ActiveRecord
# module ConnectionAdapters
# module PostgreSQL
# module DatabaseStatements
#
# def query(sql, name = nil) # :nodoc:
# mark_transaction_written_if_write(sql)
#
# log(sql, name) do
# with_raw_connection do |conn|
# sleep rand(0..0.1)
# conn.async_exec(sql).map_types!(@type_map_for_results).values
# end
# end
# end
#
# end
# end
# end
# end
ActiveRecord::Schema.define do
THREADS.times do |idx|
create_table "table#{idx}s", force: true
end
end
TABLES = THREADS.times.map do |idx|
eval <<-TABLE
class Table#{idx} < ActiveRecord::Base
end
TABLE
"Table#{idx}".constantize
end
class BugTest < Minitest::Test
def test_schema_cache_access
sc = TABLES.first.connection_pool.schema_cache
TABLES.map.with_index do |klass, idx|
Thread.new do
1000.times do
# printf (idx+97).chr # view progress
klass.attribute_types
klass.send(:reload_schema_from_cache, false)
sc.clear_data_source_cache!(klass.table_name)
assert true # count completions
end
end
end.each(&:join)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment