mbtk 0.2.0__tar.gz → 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {mbtk-0.2.0 → mbtk-0.2.1}/CHANGELOG.md +8 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/PKG-INFO +1 -1
- {mbtk-0.2.0 → mbtk-0.2.1}/pyproject.toml +1 -1
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/__init__.py +1 -1
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/transport/redis_stream.py +18 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/worker.py +43 -7
- mbtk-0.2.1/tests/test_e2e_pipeline.py +289 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_worker.py +24 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/uv.lock +1 -1
- {mbtk-0.2.0 → mbtk-0.2.1}/.editorconfig +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/.github/workflows/ci.yml +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/.github/workflows/release.yml +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/.gitignore +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/.pre-commit-config.yaml +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/CONTRIBUTING.md +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/LICENSE +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/README.en.md +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/README.md +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/SECURITY.md +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/examples/echo_bot/README.md +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/examples/echo_bot/main.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/bot.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/cache/__init__.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/cache/redis.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/clock.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/di.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/dispatcher.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/errors.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/events.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/filters.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/fsm.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/max/__init__.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/max/client.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/max/keyboards.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/max/updates.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/middleware/__init__.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/middleware/errors.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/middleware/logging.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/py.typed +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/retry.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/router.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/storage/__init__.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/storage/redis.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/transport/__init__.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/webhook/__init__.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/src/mbtk/webhook/fastapi.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_cache.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_framework.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_keyboards.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_max_client.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_middleware.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_redis_fsm.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_retry.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_transport.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_version.py +0 -0
- {mbtk-0.2.0 → mbtk-0.2.1}/tests/test_webhook.py +0 -0
|
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
|
|
|
6
6
|
|
|
7
7
|
## [Unreleased]
|
|
8
8
|
|
|
9
|
+
## [0.2.1] - 2026-11-18
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- Dead-letter stream `<stream>:dead` for events dropped after permanent
|
|
14
|
+
rejection or exhausted transient retries. A failed dead-letter write
|
|
15
|
+
leaves the source event pending instead of acknowledging it.
|
|
16
|
+
|
|
9
17
|
## [0.2.0] - 2026-11-10
|
|
10
18
|
|
|
11
19
|
### Added
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: mbtk
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: A small async toolkit for MAX messenger bots, inspired by aiogram 3
|
|
5
5
|
Project-URL: Homepage, https://github.com/VladimirKrokhin/mbtk
|
|
6
6
|
Project-URL: Repository, https://github.com/VladimirKrokhin/mbtk
|
|
@@ -8,6 +8,7 @@ Requires the ``redis`` extra::
|
|
|
8
8
|
import hashlib
|
|
9
9
|
import json
|
|
10
10
|
import logging
|
|
11
|
+
import time
|
|
11
12
|
|
|
12
13
|
from ..max.updates import event_id_for
|
|
13
14
|
from . import QueuedUpdate
|
|
@@ -124,6 +125,23 @@ class RedisStreamQueue:
|
|
|
124
125
|
self.ACK_DELETE_SCRIPT, 1, self.stream, self.group, entry_id
|
|
125
126
|
)
|
|
126
127
|
|
|
128
|
+
async def dead_letter(self, entry_id, event_id, update, reason):
|
|
129
|
+
"""Persist a dropped event for manual replay/inspection."""
|
|
130
|
+
await self.client.xadd(
|
|
131
|
+
f"{self.stream}:dead",
|
|
132
|
+
{
|
|
133
|
+
"entry_id": entry_id,
|
|
134
|
+
"event_id": event_id,
|
|
135
|
+
"body": json.dumps(
|
|
136
|
+
update, sort_keys=True, separators=(",", ":")
|
|
137
|
+
),
|
|
138
|
+
"reason": reason,
|
|
139
|
+
"failed_at": str(int(time.time())),
|
|
140
|
+
},
|
|
141
|
+
maxlen=10000,
|
|
142
|
+
approximate=True,
|
|
143
|
+
)
|
|
144
|
+
|
|
127
145
|
async def record_failure(self, entry_id):
|
|
128
146
|
"""Increment the transient failure counter for an entry."""
|
|
129
147
|
key = f"{self.stream}:attempts:{entry_id}"
|
|
@@ -45,7 +45,7 @@ async def process_once(
|
|
|
45
45
|
failed = 0
|
|
46
46
|
now_ms = time.time_ns() // 1_000_000
|
|
47
47
|
oldest_lag_ms = 0
|
|
48
|
-
for entry_id,
|
|
48
|
+
for entry_id, event_id, update in records:
|
|
49
49
|
if stop_event is not None and stop_event.is_set():
|
|
50
50
|
break
|
|
51
51
|
if lag_ms := _entry_lag_ms(entry_id, now_ms):
|
|
@@ -57,13 +57,22 @@ async def process_once(
|
|
|
57
57
|
# Never log event bodies, API responses, or credentials.
|
|
58
58
|
log.error("MAX event processing failed (%s)", type(error).__name__)
|
|
59
59
|
if is_permanent_error(error):
|
|
60
|
-
# A retry cannot help.
|
|
61
|
-
#
|
|
62
|
-
#
|
|
60
|
+
# A retry cannot help. Move the event to the dead-letter
|
|
61
|
+
# stream before acknowledging: otherwise the user's action
|
|
62
|
+
# disappears without a trace.
|
|
63
|
+
reason = (
|
|
64
|
+
"permanent:"
|
|
65
|
+
f"{getattr(error, 'status', None) or 'unknown'}:"
|
|
66
|
+
f"{getattr(error, 'code', None) or 'unknown'}"
|
|
67
|
+
)
|
|
68
|
+
if not await _dead_letter(
|
|
69
|
+
queue, entry_id, event_id, update, reason, log
|
|
70
|
+
):
|
|
71
|
+
continue
|
|
63
72
|
await queue.acknowledge(entry_id)
|
|
64
73
|
await _clear_failure(queue, entry_id)
|
|
65
74
|
log.warning(
|
|
66
|
-
"MAX event
|
|
75
|
+
"MAX event moved to dead-letter after permanent rejection "
|
|
67
76
|
"status=%s code=%s",
|
|
68
77
|
getattr(error, "status", None) or "unknown",
|
|
69
78
|
getattr(error, "code", None) or "unknown",
|
|
@@ -72,11 +81,21 @@ async def process_once(
|
|
|
72
81
|
attempts = await _record_failure(queue, entry_id)
|
|
73
82
|
if attempts >= max_attempts:
|
|
74
83
|
# A transient error can be poisoned (for example, a handler
|
|
75
|
-
# bug behind a 500).
|
|
84
|
+
# bug behind a 500). Keep it in the dead-letter stream.
|
|
85
|
+
if not await _dead_letter(
|
|
86
|
+
queue,
|
|
87
|
+
entry_id,
|
|
88
|
+
event_id,
|
|
89
|
+
update,
|
|
90
|
+
f"transient:max_attempts:{type(error).__name__}",
|
|
91
|
+
log,
|
|
92
|
+
):
|
|
93
|
+
continue
|
|
76
94
|
await queue.acknowledge(entry_id)
|
|
77
95
|
await _clear_failure(queue, entry_id)
|
|
78
96
|
log.error(
|
|
79
|
-
"MAX event
|
|
97
|
+
"MAX event moved to dead-letter after %d transient "
|
|
98
|
+
"errors (%s)",
|
|
80
99
|
attempts,
|
|
81
100
|
type(error).__name__,
|
|
82
101
|
)
|
|
@@ -95,6 +114,23 @@ async def process_once(
|
|
|
95
114
|
return len(records)
|
|
96
115
|
|
|
97
116
|
|
|
117
|
+
async def _dead_letter(queue, entry_id, event_id, update, reason, log):
|
|
118
|
+
"""Persist a dropped event; return False if it could not be stored.
|
|
119
|
+
|
|
120
|
+
Without a successful dead-letter write the source entry must stay
|
|
121
|
+
pending: acknowledging it would lose the event.
|
|
122
|
+
"""
|
|
123
|
+
handler = getattr(queue, "dead_letter", None)
|
|
124
|
+
if handler is None:
|
|
125
|
+
return True
|
|
126
|
+
try:
|
|
127
|
+
await handler(entry_id, event_id, update, reason)
|
|
128
|
+
except Exception as error:
|
|
129
|
+
log.error("MAX dead-letter write failed (%s)", type(error).__name__)
|
|
130
|
+
return False
|
|
131
|
+
return True
|
|
132
|
+
|
|
133
|
+
|
|
98
134
|
async def _record_failure(queue, entry_id):
|
|
99
135
|
"""Increment the transient failure counter when the queue supports it."""
|
|
100
136
|
record = getattr(queue, "record_failure", None)
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"""End-to-end pipeline test: webhook -> queue -> worker -> dispatcher -> MAX.
|
|
2
|
+
|
|
3
|
+
The fake MAX server is a real HTTP server, so the MAX client exercises the
|
|
4
|
+
same network path as in production. The webhook is called through the ASGI
|
|
5
|
+
app, and the queue is in memory.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import json
|
|
9
|
+
import threading
|
|
10
|
+
import unittest
|
|
11
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
12
|
+
from urllib.parse import parse_qs, urlsplit
|
|
13
|
+
|
|
14
|
+
import httpx
|
|
15
|
+
|
|
16
|
+
from mbtk import Dispatcher, Router
|
|
17
|
+
from mbtk.filters import Command, Payload
|
|
18
|
+
from mbtk.max.client import MaxApiClient
|
|
19
|
+
from mbtk.max.updates import event_id_for
|
|
20
|
+
from mbtk.transport import QueuedUpdate
|
|
21
|
+
from mbtk.webhook.fastapi import create_app
|
|
22
|
+
from mbtk.worker import process_once
|
|
23
|
+
|
|
24
|
+
SECRET = "e2e-secret"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class FakeMax:
|
|
28
|
+
"""A real HTTP server that records outgoing MAX API calls."""
|
|
29
|
+
|
|
30
|
+
def __init__(self):
|
|
31
|
+
self.sent = []
|
|
32
|
+
self.answers = []
|
|
33
|
+
self.failures = []
|
|
34
|
+
self.httpd = ThreadingHTTPServer(
|
|
35
|
+
("127.0.0.1", 0), self._handler_class()
|
|
36
|
+
)
|
|
37
|
+
self.httpd.daemon_threads = True
|
|
38
|
+
self.thread = threading.Thread(
|
|
39
|
+
target=self.httpd.serve_forever, daemon=True
|
|
40
|
+
)
|
|
41
|
+
self.base_url = ""
|
|
42
|
+
|
|
43
|
+
def start(self):
|
|
44
|
+
self.thread.start()
|
|
45
|
+
host, port = self.httpd.server_address
|
|
46
|
+
self.base_url = f"http://{host}:{port}"
|
|
47
|
+
return self
|
|
48
|
+
|
|
49
|
+
def stop(self):
|
|
50
|
+
self.httpd.shutdown()
|
|
51
|
+
self.httpd.server_close()
|
|
52
|
+
self.thread.join(timeout=5)
|
|
53
|
+
|
|
54
|
+
def fail(self, method, path, status=500, times=1):
|
|
55
|
+
for _ in range(times):
|
|
56
|
+
self.failures.append((method, path, status))
|
|
57
|
+
|
|
58
|
+
def _take_error(self, method, path):
|
|
59
|
+
for index, (want_method, want_path, status) in enumerate(self.failures):
|
|
60
|
+
if want_method == method and want_path == path:
|
|
61
|
+
self.failures.pop(index)
|
|
62
|
+
return status
|
|
63
|
+
return None
|
|
64
|
+
|
|
65
|
+
def _handler_class(self):
|
|
66
|
+
fake = self
|
|
67
|
+
|
|
68
|
+
class Handler(BaseHTTPRequestHandler):
|
|
69
|
+
def do_POST(self):
|
|
70
|
+
path = urlsplit(self.path).path
|
|
71
|
+
query = parse_qs(urlsplit(self.path).query)
|
|
72
|
+
length = int(self.headers.get("Content-Length") or 0)
|
|
73
|
+
raw = self.rfile.read(length) if length else b"{}"
|
|
74
|
+
body = json.loads(raw or b"{}")
|
|
75
|
+
status = fake._take_error("POST", path)
|
|
76
|
+
if status is not None:
|
|
77
|
+
fake._reply(self, status, {"code": "test_error"})
|
|
78
|
+
return
|
|
79
|
+
if path == "/messages":
|
|
80
|
+
fake.sent.append((query, body))
|
|
81
|
+
fake._reply(
|
|
82
|
+
self,
|
|
83
|
+
200,
|
|
84
|
+
{"message": {"body": {"mid": f"mid-{len(fake.sent)}"}}},
|
|
85
|
+
)
|
|
86
|
+
return
|
|
87
|
+
if path == "/answers":
|
|
88
|
+
fake.answers.append((query, body))
|
|
89
|
+
fake._reply(self, 200, {"success": True})
|
|
90
|
+
return
|
|
91
|
+
fake._reply(self, 404, {"code": "not_found"})
|
|
92
|
+
|
|
93
|
+
def log_message(self, *args):
|
|
94
|
+
pass
|
|
95
|
+
|
|
96
|
+
return Handler
|
|
97
|
+
|
|
98
|
+
@staticmethod
|
|
99
|
+
def _reply(handler, status, payload):
|
|
100
|
+
data = json.dumps(payload).encode()
|
|
101
|
+
handler.send_response(status)
|
|
102
|
+
handler.send_header("Content-Type", "application/json")
|
|
103
|
+
handler.send_header("Content-Length", str(len(data)))
|
|
104
|
+
handler.end_headers()
|
|
105
|
+
handler.wfile.write(data)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class MemoryQueue:
|
|
109
|
+
"""At-least-once queue with redelivery of unacknowledged entries."""
|
|
110
|
+
|
|
111
|
+
def __init__(self):
|
|
112
|
+
self.new = []
|
|
113
|
+
self.in_flight = {}
|
|
114
|
+
self.acknowledged = []
|
|
115
|
+
self.failures = {}
|
|
116
|
+
self.counter = 0
|
|
117
|
+
|
|
118
|
+
async def enqueue(self, update):
|
|
119
|
+
self.counter += 1
|
|
120
|
+
record = QueuedUpdate(
|
|
121
|
+
f"entry-{self.counter}", event_id_for(update), update
|
|
122
|
+
)
|
|
123
|
+
self.new.append(record)
|
|
124
|
+
return record.event_id
|
|
125
|
+
|
|
126
|
+
async def is_ready(self):
|
|
127
|
+
return True
|
|
128
|
+
|
|
129
|
+
async def ensure_group(self):
|
|
130
|
+
return None
|
|
131
|
+
|
|
132
|
+
async def read(self, consumer, count=10):
|
|
133
|
+
records = list(self.in_flight.values())
|
|
134
|
+
if len(records) < count:
|
|
135
|
+
added = self.new[: count - len(records)]
|
|
136
|
+
self.new = self.new[len(added) :]
|
|
137
|
+
for record in added:
|
|
138
|
+
self.in_flight[record.entry_id] = record
|
|
139
|
+
records.extend(added)
|
|
140
|
+
return records[:count]
|
|
141
|
+
|
|
142
|
+
async def acknowledge(self, entry_id):
|
|
143
|
+
self.acknowledged.append(entry_id)
|
|
144
|
+
self.in_flight.pop(entry_id, None)
|
|
145
|
+
|
|
146
|
+
async def record_failure(self, entry_id):
|
|
147
|
+
self.failures[entry_id] = self.failures.get(entry_id, 0) + 1
|
|
148
|
+
return self.failures[entry_id]
|
|
149
|
+
|
|
150
|
+
async def clear_failure(self, entry_id):
|
|
151
|
+
self.failures.pop(entry_id, None)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
class DispatcherProcessor:
|
|
155
|
+
def __init__(self, dispatcher):
|
|
156
|
+
self.dispatcher = dispatcher
|
|
157
|
+
|
|
158
|
+
async def process(self, update):
|
|
159
|
+
return await self.dispatcher.feed_update(update)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def message_update(text="/ping", chat_id="10", user_id="20"):
|
|
163
|
+
return {
|
|
164
|
+
"update_id": "u-1",
|
|
165
|
+
"update_type": "message_created",
|
|
166
|
+
"message": {
|
|
167
|
+
"body": {"mid": "m-1", "text": text},
|
|
168
|
+
"recipient": {"chat_id": chat_id, "chat_type": "dialog"},
|
|
169
|
+
"sender": {"user_id": user_id},
|
|
170
|
+
},
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def callback_update():
|
|
175
|
+
return {
|
|
176
|
+
"update_id": "u-2",
|
|
177
|
+
"update_type": "message_callback",
|
|
178
|
+
"callback": {
|
|
179
|
+
"callback_id": "cb-1",
|
|
180
|
+
"payload": {"a": "ping"},
|
|
181
|
+
"chat_id": "10",
|
|
182
|
+
"chat_type": "dialog",
|
|
183
|
+
"user": {"user_id": "20"},
|
|
184
|
+
"message": {
|
|
185
|
+
"body": {"mid": "m-1"},
|
|
186
|
+
"recipient": {"chat_id": "10", "chat_type": "dialog"},
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
class PipelineTests(unittest.IsolatedAsyncioTestCase):
|
|
193
|
+
async def asyncSetUp(self):
|
|
194
|
+
self.fake_max = FakeMax().start()
|
|
195
|
+
self.addCleanup(self.fake_max.stop)
|
|
196
|
+
|
|
197
|
+
self.queue = MemoryQueue()
|
|
198
|
+
self.max_api = MaxApiClient(self.fake_max.base_url, "bot-token")
|
|
199
|
+
self.addAsyncCleanup(self.max_api.aclose)
|
|
200
|
+
|
|
201
|
+
self.router = Router("echo")
|
|
202
|
+
|
|
203
|
+
@self.router.message(Command("ping"))
|
|
204
|
+
async def ping(event):
|
|
205
|
+
await event.answer("pong")
|
|
206
|
+
|
|
207
|
+
@self.router.callback_query(Payload("ping"))
|
|
208
|
+
async def callback(event):
|
|
209
|
+
await event.answer("pong")
|
|
210
|
+
|
|
211
|
+
self.dispatcher = Dispatcher(bot=self.max_api)
|
|
212
|
+
self.dispatcher.include_router(self.router)
|
|
213
|
+
self.processor = DispatcherProcessor(self.dispatcher)
|
|
214
|
+
|
|
215
|
+
self.app = create_app(self.queue, SECRET)
|
|
216
|
+
self.webhook = httpx.AsyncClient(
|
|
217
|
+
transport=httpx.ASGITransport(app=self.app),
|
|
218
|
+
base_url="http://test",
|
|
219
|
+
)
|
|
220
|
+
self.addAsyncCleanup(self.webhook.aclose)
|
|
221
|
+
|
|
222
|
+
async def post(self, update, secret=SECRET):
|
|
223
|
+
return await self.webhook.post(
|
|
224
|
+
"/webhook",
|
|
225
|
+
content=json.dumps(update),
|
|
226
|
+
headers={"X-Max-Bot-Api-Secret": secret},
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
async def test_message_flows_from_webhook_to_max(self):
|
|
230
|
+
response = await self.post(message_update())
|
|
231
|
+
|
|
232
|
+
self.assertEqual(response.status_code, 200)
|
|
233
|
+
self.assertEqual(len(self.queue.new), 1)
|
|
234
|
+
|
|
235
|
+
processed = await process_once(self.queue, self.processor, "worker-1")
|
|
236
|
+
|
|
237
|
+
self.assertEqual(processed, 1)
|
|
238
|
+
self.assertEqual(self.queue.acknowledged, ["entry-1"])
|
|
239
|
+
query, body = self.fake_max.sent[0]
|
|
240
|
+
self.assertEqual(query["chat_id"], ["10"])
|
|
241
|
+
self.assertEqual(body["text"], "pong")
|
|
242
|
+
self.assertEqual(body["format"], "markdown")
|
|
243
|
+
self.assertNotIn("attachments", body)
|
|
244
|
+
|
|
245
|
+
async def test_callback_is_answered_with_a_notification(self):
|
|
246
|
+
response = await self.post(callback_update())
|
|
247
|
+
|
|
248
|
+
self.assertEqual(response.status_code, 200)
|
|
249
|
+
await process_once(self.queue, self.processor, "worker-1")
|
|
250
|
+
|
|
251
|
+
query, body = self.fake_max.answers[0]
|
|
252
|
+
self.assertEqual(query["callback_id"], ["cb-1"])
|
|
253
|
+
self.assertEqual(body, {"notification": "pong"})
|
|
254
|
+
self.assertEqual(self.queue.acknowledged, ["entry-1"])
|
|
255
|
+
|
|
256
|
+
async def test_transient_error_keeps_update_for_retry(self):
|
|
257
|
+
self.fake_max.fail("POST", "/messages", status=503)
|
|
258
|
+
await self.post(message_update())
|
|
259
|
+
|
|
260
|
+
await process_once(self.queue, self.processor, "worker-1")
|
|
261
|
+
|
|
262
|
+
self.assertEqual(self.queue.acknowledged, [])
|
|
263
|
+
self.assertEqual(self.queue.failures["entry-1"], 1)
|
|
264
|
+
self.assertEqual(self.fake_max.sent, [])
|
|
265
|
+
|
|
266
|
+
await process_once(self.queue, self.processor, "worker-1")
|
|
267
|
+
|
|
268
|
+
self.assertEqual(self.queue.acknowledged, ["entry-1"])
|
|
269
|
+
self.assertEqual(len(self.fake_max.sent), 1)
|
|
270
|
+
|
|
271
|
+
async def test_permanent_error_is_acknowledged_and_dropped(self):
|
|
272
|
+
self.fake_max.fail("POST", "/messages", status=403)
|
|
273
|
+
await self.post(message_update())
|
|
274
|
+
|
|
275
|
+
await process_once(self.queue, self.processor, "worker-1")
|
|
276
|
+
|
|
277
|
+
self.assertEqual(self.queue.acknowledged, ["entry-1"])
|
|
278
|
+
self.assertNotIn("entry-1", self.queue.failures)
|
|
279
|
+
self.assertEqual(self.fake_max.sent, [])
|
|
280
|
+
|
|
281
|
+
async def test_bad_webhook_secret_is_rejected(self):
|
|
282
|
+
response = await self.post(message_update(), secret="wrong")
|
|
283
|
+
|
|
284
|
+
self.assertEqual(response.status_code, 401)
|
|
285
|
+
self.assertEqual(self.queue.new, [])
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
if __name__ == "__main__":
|
|
289
|
+
unittest.main()
|
|
@@ -17,6 +17,7 @@ class FakeQueue:
|
|
|
17
17
|
self.failures = dict(failures or {})
|
|
18
18
|
self.acked = []
|
|
19
19
|
self.cleared = []
|
|
20
|
+
self.dead = []
|
|
20
21
|
self.consumer = None
|
|
21
22
|
self.count = None
|
|
22
23
|
|
|
@@ -35,6 +36,9 @@ class FakeQueue:
|
|
|
35
36
|
async def clear_failure(self, entry_id):
|
|
36
37
|
self.cleared.append(entry_id)
|
|
37
38
|
|
|
39
|
+
async def dead_letter(self, entry_id, event_id, update, reason):
|
|
40
|
+
self.dead.append((entry_id, event_id, update, reason))
|
|
41
|
+
|
|
38
42
|
|
|
39
43
|
class Processor:
|
|
40
44
|
def __init__(self, error=None):
|
|
@@ -78,6 +82,9 @@ class ProcessOnceTests(unittest.IsolatedAsyncioTestCase):
|
|
|
78
82
|
self.assertEqual(queue.acked, ["1000-0"])
|
|
79
83
|
self.assertEqual(queue.cleared, ["1000-0"])
|
|
80
84
|
self.assertNotIn("1000-0", queue.failures)
|
|
85
|
+
self.assertEqual(len(queue.dead), 1)
|
|
86
|
+
self.assertEqual(queue.dead[0][0], "1000-0")
|
|
87
|
+
self.assertIn("permanent", queue.dead[0][3])
|
|
81
88
|
|
|
82
89
|
async def test_transient_error_remains_pending(self):
|
|
83
90
|
queue = FakeQueue([record()])
|
|
@@ -100,6 +107,23 @@ class ProcessOnceTests(unittest.IsolatedAsyncioTestCase):
|
|
|
100
107
|
|
|
101
108
|
self.assertEqual(queue.acked, ["1000-0"])
|
|
102
109
|
self.assertEqual(queue.cleared, ["1000-0"])
|
|
110
|
+
self.assertEqual(len(queue.dead), 1)
|
|
111
|
+
self.assertIn("transient", queue.dead[0][3])
|
|
112
|
+
|
|
113
|
+
async def test_dead_letter_failure_does_not_ack_permanent_event(self):
|
|
114
|
+
queue = FakeQueue([record()])
|
|
115
|
+
|
|
116
|
+
async def broken_dead_letter(*_args):
|
|
117
|
+
raise RuntimeError("redis down")
|
|
118
|
+
|
|
119
|
+
queue.dead_letter = broken_dead_letter
|
|
120
|
+
processor = Processor(
|
|
121
|
+
RemoteServiceError("forbidden", 403, service="MAX")
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
await process_once(queue, processor, "worker-1")
|
|
125
|
+
|
|
126
|
+
self.assertEqual(queue.acked, [])
|
|
103
127
|
|
|
104
128
|
async def test_stop_event_leaves_unprocessed_records_pending(self):
|
|
105
129
|
stop = Event()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|