2 require 'active_record'
4 if ENV['ACTIVERECORD_PATH'].nil?
6 Please set the ACTIVERECORD_PATH environment variable to the directory
7 containing the active_record.rb file.
10 $LOAD_PATH.unshift << ENV['ACTIVERECORD_PATH']
12 require 'active_record'
14 abort "ActiveRecord could not be found."
20 require "#{File.dirname(__FILE__)}/../lib/deadlock_retry"
23 def self.transaction(*objects, &block)
28 @logger ||= Logger.new(nil)
34 class DeadlockRetryTest < Test::Unit::TestCase
35 DEADLOCK_ERROR = "MySQL::Error: Deadlock found when trying to get lock"
36 TIMEOUT_ERROR = "MySQL::Error: Lock wait timeout exceeded"
39 assert_equal :success, MockModel.transaction { :success }
42 def test_no_errors_with_deadlock
43 errors = [ DEADLOCK_ERROR ] * 3
44 assert_equal :success, MockModel.transaction { raise ActiveRecord::StatementInvalid, errors.shift unless errors.empty?; :success }
48 def test_no_errors_with_lock_timeout
49 errors = [ TIMEOUT_ERROR ] * 3
50 assert_equal :success, MockModel.transaction { raise ActiveRecord::StatementInvalid, errors.shift unless errors.empty?; :success }
54 def test_error_if_limit_exceeded
55 assert_raise(ActiveRecord::StatementInvalid) do
56 MockModel.transaction { raise ActiveRecord::StatementInvalid, DEADLOCK_ERROR }
60 def test_error_if_unrecognized_error
61 assert_raise(ActiveRecord::StatementInvalid) do
62 MockModel.transaction { raise ActiveRecord::StatementInvalid, "Something else" }