fix: keep VK bot alive across long poll failures

This commit is contained in:
nyan404 committed 2026-10-08 14:09:20 +03:00
1 parent e33de2114a
commit 2d9aa2126c
8 files changed
+157 -15

No files matched your search

+24
View File
@@ -1,5 +1,7 @@
import unittest
import json
from unittest.mock import patch
from urllib.error import URLError
from app.belki.vk_bot import (
BelkiDialog,
@@ -13,6 +15,28 @@ from app.belki.vk_bot import (
class VkBotTests(unittest.TestCase):
def test_long_poll_reconnects_after_ssl_error(self):
class Client:
def __init__(self):
self.calls = 0
def call(self, method, params):
self.calls += 1
return {"server": "https://vk.example/longpoll", "key": "key", "ts": str(self.calls)}
class Response:
def __enter__(self): return self
def __exit__(self, *args): return False
def __iter__(self): return iter(())
def read(self): return b'{"ts":"2","updates":[{"type":"message_new"}]}'
bot = BelkiBot.__new__(BelkiBot)
bot.client = Client()
bot.group_id = 1
events = bot.events()
with patch("app.belki.vk_bot.urlopen", side_effect=[URLError("handshake timeout"), Response()]), patch("app.belki.vk_bot.time.sleep"):
self.assertEqual(next(events)["type"], "message_new")
self.assertEqual(bot.client.calls, 2)
def test_public_chat_messages_are_ignored_but_invite_is_registered(self):
class Client:
def __init__(self): self.sent = []