From 64a092a027d43656360af94ee3e9c6aced33656b Mon Sep 17 00:00:00 2001 From: Slava <7105077+alexeevit@users.noreply.github.com> Date: Wed, 25 Sep 2024 21:55:48 +0200 Subject: [PATCH] Fix infinite loop on Timeout while stopped (#309) --- lib/telegram/bot/client.rb | 2 +- spec/lib/telegram/bot/client_spec.rb | 45 ++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/telegram/bot/client.rb b/lib/telegram/bot/client.rb index 6e7806b..68f77ec 100644 --- a/lib/telegram/bot/client.rb +++ b/lib/telegram/bot/client.rb @@ -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) diff --git a/spec/lib/telegram/bot/client_spec.rb b/spec/lib/telegram/bot/client_spec.rb index 3fb02a6..8f0b925 100644 --- a/spec/lib/telegram/bot/client_spec.rb +++ b/spec/lib/telegram/bot/client_spec.rb @@ -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