|
| 1 | +# frozen_string_literal: true |
| 2 | +require 'test_helper' |
| 3 | + |
| 4 | +class CI::Queue::Redis::Base::HeartbeatProcessTest < Minitest::Test |
| 5 | + MAX = CI::Queue::Redis::Base::HeartbeatProcess::MAX_RESTART_ATTEMPTS |
| 6 | + |
| 7 | + def setup |
| 8 | + @hp = CI::Queue::Redis::Base::HeartbeatProcess.new( |
| 9 | + 'redis://localhost:6379/0', |
| 10 | + 'zset', 'owners', 'leases' |
| 11 | + ) |
| 12 | + # boot! and restart! must not spawn real processes |
| 13 | + @hp.stubs(:boot!) |
| 14 | + @hp.stubs(:restart!) |
| 15 | + end |
| 16 | + |
| 17 | + def test_tick_retries_after_pipe_ioerror |
| 18 | + @hp.expects(:send_message).twice.raises(IOError, "closed stream").then.returns(nil) |
| 19 | + |
| 20 | + @hp.tick!("test_id", "lease_id") |
| 21 | + end |
| 22 | + |
| 23 | + def test_tick_retries_after_epipe |
| 24 | + @hp.expects(:send_message).twice.raises(Errno::EPIPE).then.returns(nil) |
| 25 | + |
| 26 | + @hp.tick!("test_id", "lease_id") |
| 27 | + end |
| 28 | + |
| 29 | + def test_tick_calls_restart_on_pipe_error |
| 30 | + @hp.stubs(:send_message).raises(IOError, "closed stream").then.returns(nil) |
| 31 | + @hp.expects(:restart!).once |
| 32 | + |
| 33 | + @hp.tick!("test_id", "lease_id") |
| 34 | + end |
| 35 | + |
| 36 | + def test_tick_raises_after_max_restart_attempts |
| 37 | + @hp.stubs(:send_message).raises(IOError, "closed stream") |
| 38 | + |
| 39 | + assert_raises(IOError) do |
| 40 | + (MAX + 1).times { @hp.tick!("test_id", "lease_id") } |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + def test_restart_counter_resets_after_success |
| 45 | + # Build a sequence: [raise, return] * (MAX+1). |
| 46 | + # Without @restart_attempts = 0 on success, the (MAX+1)th failure would exceed MAX and raise. |
| 47 | + stub = @hp.stubs(:send_message) |
| 48 | + (MAX + 1).times do |i| |
| 49 | + stub = stub.raises(IOError, "closed stream").then.returns(nil) |
| 50 | + stub = stub.then unless i == MAX |
| 51 | + end |
| 52 | + |
| 53 | + (MAX + 1).times { @hp.tick!("test_id", "lease_id") } |
| 54 | + end |
| 55 | +end |
0 commit comments