Skip to content

Commit

Permalink
Fix infinite loop on Timeout while stopped (atipugin#309)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeevit authored Sep 25, 2024
1 parent 64704ef commit 64a092a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/telegram/bot/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def fetch_updates
yield handle_update(update)
end
rescue Faraday::TimeoutError, Faraday::ConnectionFailed
retry
retry if @running
end

def handle_update(update)
Expand Down
45 changes: 36 additions & 9 deletions spec/lib/telegram/bot/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,48 @@
end

describe '#stop' do
before do
allow(client.api).to receive(:getUpdates).and_return [Telegram::Bot::Types::Update.new(update_id: 111_111)]
context 'when bot receives messages' do
before do
allow(client.api).to receive(:getUpdates).and_return [Telegram::Bot::Types::Update.new(update_id: 111_111)]

current_times = 0
current_times = 0

client.listen do |_message|
current_times += 1
client.stop if current_times == expected_times
client.listen do |_message|
current_times += 1
client.stop if current_times == expected_times
end
end

let(:expected_times) { 3 }

specify do
expect(client.api).to have_received(:getUpdates).exactly(expected_times).times
end
end

let(:expected_times) { 3 }
context 'when bot does not receive any messages' do
before do
current_times = 0

allow(client.api).to receive(:getUpdates) do
if current_times >= expected_times - 1
client.stop
raise Faraday::TimeoutError
end

specify do
expect(client.api).to have_received(:getUpdates).exactly(expected_times).times
[Telegram::Bot::Types::Update.new(update_id: 111_111)]
end

client.listen do |_message|
current_times += 1
end
end

let(:expected_times) { 3 }

specify do
expect(client.api).to have_received(:getUpdates).exactly(expected_times).times
end
end
end

Expand Down

0 comments on commit 64a092a

Please sign in to comment.