|
4 | 4 |
|
5 | 5 | class StatementRetrierTest < Minitest::Test
|
6 | 6 | def test_retry_if_lock_timeout
|
7 |
| - @migration = |
8 |
| - Class.new(ActiveRecord::Migration::Current) do |
9 |
| - def up |
10 |
| - connection.send(:retry_if_lock_timeout) do |
11 |
| - raise ActiveRecord::LockWaitTimeout, 'PG::LockNotAvailable: ERROR: canceling statement due to lock timeout' |
12 |
| - end |
13 |
| - end |
14 |
| - end.new |
| 7 | + calls = calls_for_lock_timeout_migration |
15 | 8 |
|
16 |
| - @connection.expects(:sleep).times(4) |
17 |
| - calls = |
18 |
| - record_calls(@migration, :write) do |
19 |
| - run_migration |
20 |
| - flunk 'run_migration should raise' |
21 |
| - rescue StandardError => e |
22 |
| - assert_instance_of ActiveRecord::LockWaitTimeout, e.cause |
23 |
| - assert_includes e.cause.message, 'canceling statement due to lock timeout' |
24 |
| - end |
25 | 9 | assert_equal [
|
26 | 10 | ' -> Retrying in 60 seconds...',
|
27 | 11 | ' -> Retrying now.',
|
@@ -49,7 +33,54 @@ def test_statement_retry
|
49 | 33 | ], calls[7..9]
|
50 | 34 | end
|
51 | 35 |
|
52 |
| - def lock_timout_increase_on_retry |
| 36 | + def test_lock_timout_increase_on_retry |
| 37 | + SafePgMigrations.config.lock_timeout = 0.1.seconds |
| 38 | + |
| 39 | + calls = calls_for_lock_timeout_migration |
| 40 | + |
| 41 | + assert_equal [ |
| 42 | + ' -> Retrying in 60 seconds...', |
| 43 | + ' -> Increasing the lock timeout... Currently set to 0.1', |
| 44 | + ' -> Lock timeout is now set to 0.325', |
| 45 | + ' -> Retrying now.', |
| 46 | + ' -> Retrying in 60 seconds...', |
| 47 | + ' -> Increasing the lock timeout... Currently set to 0.325', |
| 48 | + ' -> Lock timeout is now set to 0.55', |
| 49 | + ' -> Retrying now.', |
| 50 | + ' -> Retrying in 60 seconds...', |
| 51 | + ' -> Increasing the lock timeout... Currently set to 0.55', |
| 52 | + ' -> Lock timeout is now set to 0.775', |
| 53 | + ' -> Retrying now.', |
| 54 | + ' -> Retrying in 60 seconds...', |
| 55 | + ' -> Increasing the lock timeout... Currently set to 0.775', |
| 56 | + ' -> Lock timeout is now set to 1', |
| 57 | + ' -> Retrying now.', |
| 58 | + ], calls[1..-1].map(&:first) |
| 59 | + |
| 60 | + end |
| 61 | + |
| 62 | + private |
| 63 | + |
| 64 | + def calls_for_lock_timeout_migration |
| 65 | + @migration = Class.new(ActiveRecord::Migration::Current) do |
| 66 | + def up |
| 67 | + connection.send(:retry_if_lock_timeout) do |
| 68 | + raise ActiveRecord::LockWaitTimeout, 'PG::LockNotAvailable: ERROR: canceling statement due to lock timeout' |
| 69 | + end |
| 70 | + end |
| 71 | + end.new |
| 72 | + |
| 73 | + @connection.expects(:sleep).times(4) |
| 74 | + |
| 75 | + calls = |
| 76 | + record_calls(@migration, :write) do |
| 77 | + run_migration |
| 78 | + flunk 'run_migration should raise' |
| 79 | + rescue StandardError => e |
| 80 | + assert_instance_of ActiveRecord::LockWaitTimeout, e.cause |
| 81 | + assert_includes e.cause.message, 'canceling statement due to lock timeout' |
| 82 | + end |
53 | 83 |
|
| 84 | + calls |
54 | 85 | end
|
55 | 86 | end
|
0 commit comments