Created
April 10, 2016 20:12
-
-
Save arthurnn/fbb59a684a75a415a2a8032c0ee99f54 to your computer and use it in GitHub Desktop.
This file contains 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
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 | |
routes.draw do | |
get '/' => 'test#index' | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
class TestController < ActionController::Base | |
include Rails.application.routes.url_helpers | |
def index | |
Post.create! | |
render plain: 'Home' | |
end | |
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"}} | |
end | |
end | |
TestApp.initialize! | |
ActiveRecord::Tasks::DatabaseTasks.create_current | |
ActiveRecord::Schema.define do | |
create_table :posts, force: true do |t| | |
end | |
end | |
require 'minitest/autorun' | |
require 'rack/test' | |
class BugTest < ActionDispatch::IntegrationTest | |
def test_returns_success | |
q,r = Queue.new,Queue.new | |
Post.create! | |
ActiveRecord::Base.transaction do | |
Post.destroy_all | |
puts "[main] pid: #{ActiveRecord::Base.connection.instance_variable_get("@connection").backend_pid}" | |
get '/' | |
# db connection placed back to pool with open transaction | |
assert_response :success #last_response.ok? | |
# start a new thread grabing connection | |
th = Thread.new do | |
conn = ActiveRecord::Base.connection | |
q.push true | |
puts "[thread] PID: #{conn.instance_variable_get("@connection").backend_pid}" | |
r.pop | |
end | |
q.pop | |
puts "[main] PID: #{ActiveRecord::Base.connection.instance_variable_get("@connection").backend_pid}" | |
r.push true | |
Post.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