Last active
August 8, 2017 12:17
-
-
Save mfazekas/55ba95e52d4959bfca94787481313ca7 to your computer and use it in GitHub Desktop.
test_transaction_deadlock_mc.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
begin | |
require 'bundler/inline' | |
rescue LoadError => e | |
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler' | |
raise e | |
end | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'rails', github: 'rails/rails' | |
gem 'pg' | |
gem 'byebug' | |
end | |
require 'action_controller/railtie' | |
require 'active_record/railtie' | |
require 'active_record' | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
class TestApp < Rails::Application | |
config.root = File.dirname(__FILE__) | |
config.session_store :cookie_store, key: 'cookie_store_key' | |
secrets.secret_token = 'secret_token' | |
secrets.secret_key_base = 'secret_key_base' | |
config.logger = Logger.new($stdout) | |
Rails.logger = config.logger | |
end | |
TestApp.configure do | |
config.eager_load = true | |
def config.database_configuration | |
{'development' => {'adapter' => 'postgresql', 'pool' => 4, 'host' => 'localhost', 'username' => ENV["USER"], 'database' => "test_tran_deadlock"}, | |
'development2' => {'adapter' => 'postgresql', 'pool' => 4, 'host' => 'localhost', 'username' => ENV["USER"], 'database' => "test_tran_deadlock2"} | |
} | |
end | |
end | |
TestApp.initialize! | |
TestApp.routes.draw do | |
resources :posts | |
end | |
def db_pid(conn) | |
conn.instance_variable_get("@connection").backend_pid | |
end | |
ActiveRecord::Tasks::DatabaseTasks.create_all | |
ActiveRecord::Base::establish_connection :development2 | |
ActiveRecord::Schema.define do | |
create_table :posts, force: true do |t| | |
end | |
end | |
ActiveRecord::Base::establish_connection :development | |
class Post < ActiveRecord::Base | |
establish_connection :development2 | |
has_many :comments | |
end | |
class PostsController < ActionController::Base | |
include Rails.application.routes.url_helpers | |
def index | |
conn = Post.connection | |
puts "[main] PID in req: #{db_pid(conn)}" | |
Post.create! | |
render plain: 'Home' | |
end | |
end | |
require 'minitest/autorun' | |
require 'rack/test' | |
class BugTest < Minitest::Test | |
include Rack::Test::Methods | |
def test_deadlock | |
q,r = Queue.new,Queue.new | |
Post.create! | |
Post.transaction do | |
Post.destroy_all | |
puts "[main] pid: #{db_pid(Post.connection)}" | |
get '/posts' | |
# db connection placed back to pool with open transaction | |
assert last_response.ok? | |
# start a new thread grabing connection | |
th = Thread.new do | |
conn = Post.connection | |
q.push true | |
puts "[thread] PID: #{db_pid(conn)}" | |
r.pop | |
end | |
q.pop | |
# grab connection again | |
puts "[main] PID: #{db_pid(Post.connection)}" | |
r.push true | |
puts "before destroy_all" | |
Post.destroy_all | |
puts "after destroy_all" | |
th.join | |
end | |
end | |
private | |
def app | |
Rails.application | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment