diff --git a/lib/telegram/bot/client.rb b/lib/telegram/bot/client.rb index c2f07af..d4a84df 100644 --- a/lib/telegram/bot/client.rb +++ b/lib/telegram/bot/client.rb @@ -36,7 +36,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