Измеряйте загрузку до подтверждения приёма вложения

This commit is contained in:
2026-11-20 10:54:35 +03:00
parent c27b681689
commit b05b33824f
3 changed files with 39 additions and 12 deletions
+1 -4
View File
@@ -140,10 +140,7 @@ def experiment(args, content, report):
report['download'] = {'ok': False}
if accepted:
try:
response = bootstrap.call('GET', '/api/v1/attachments/' + accepted[0]['attachment_id'] + '/content', actor=reader)
body = response['bytes']
report['download'] = {'ok': body == content and response['headers'].get_content_type() == args.mime,
'status': response['status'], 'bytes': len(body), 'sha256': hashlib.sha256(body).hexdigest()}
report['download'] = memory.verify_download(bootstrap, reader, accepted[0]['attachment_id'], content, args.mime)
except Exception as exc:
report['download']['error'] = str(exc) if isinstance(exc, module.Failure) else type(exc).__name__
report['ok'] = (len(accepted) == args.concurrency and report['download']['ok'] and stable
+26 -1
View File
@@ -6,6 +6,9 @@ import struct
import unittest
from types import SimpleNamespace
import io
from email.message import Message
from unittest.mock import Mock
import threading
import zlib
@@ -127,13 +130,35 @@ class UploadMemoryTests(unittest.TestCase):
response.code, response.headers = 415, {}
return response
client = SimpleNamespace(base='http://localhost', opener=Opener(), timeout=1)
import threading
result = self.module.upload(None, client, {'token': 'synthetic'}, {'id': 'chat'}, b'jpeg', 'hash',
threading.Barrier(1), 0, 'decoder', mime='image/jpeg')
self.assertEqual(captured[0].get_header('Content-type'), 'image/jpeg')
self.assertTrue(captured[0].full_url.endswith('decoder-0.jpg'))
self.assertEqual(result['status'], 415)
def test_upload_counts_the_queued_snapshot_as_accepted(self):
attachment = {'id': 'a1', 'sha256': 'hash', 'size_bytes': 3, 'status': 'queued',
'owner_id': 'user', 'chat_id': 'chat'}
response = io.BytesIO(json.dumps(attachment).encode())
response.code, response.headers = 202, {}
client = SimpleNamespace(base='http://localhost', opener=Mock(), timeout=1)
client.opener.open.return_value = response
result = self.module.upload(Mock(), client, {'id': 'user', 'token': 'synthetic'},
{'id': 'chat'}, b'png', 'hash', threading.Barrier(1), 0, 'small')
self.assertTrue(result['ok'], result)
self.assertEqual(result['attachment_id'], 'a1')
def test_sample_download_waits_for_publication(self):
client = Mock()
headers = Message()
headers['Content-Type'] = 'image/jpeg'
client.call.return_value = {'bytes': b'jpeg', 'headers': headers, 'status': 200}
result = self.module.verify_download(client, 'reader', 'a1', b'jpeg', 'image/jpeg')
self.assertEqual([call[0] for call in client.method_calls], ['wait_ready', 'call'])
client.wait_ready.assert_called_once_with('reader', {'id': 'a1'})
client.call.assert_called_once_with('GET', '/api/v1/attachments/a1/content', actor='reader')
self.assertTrue(result['ok'])
if __name__ == '__main__':
unittest.main()
+12 -7
View File
@@ -217,7 +217,7 @@ def upload(client_module, client, actor, chat, content, checksum, barrier, index
result['status'] = response.code
result['instance_id'] = response.headers.get('X-Instance-Id')
body = response.read(65537)
if result['status'] != 201:
if result['status'] != 202:
result['error'] = 'http_error'
elif len(body) > 65536:
result['error'] = 'oversized_metadata'
@@ -225,7 +225,7 @@ def upload(client_module, client, actor, chat, content, checksum, barrier, index
attachment = json.loads(body)
client_module.shape(attachment, 'Attachment')
valid = (attachment['sha256'] == checksum and attachment['size_bytes'] == len(content)
and attachment['status'] == 'ready' and attachment['owner_id'] == actor['id']
and attachment['status'] == 'queued' and attachment['owner_id'] == actor['id']
and attachment['chat_id'] == chat['id'])
result['ok'] = valid
result['attachment_id'] = attachment['id']
@@ -239,6 +239,15 @@ def upload(client_module, client, actor, chat, content, checksum, barrier, index
return result
def verify_download(client, reader, attachment_id, content, mime):
# Poll after the upload measurements so latency ends at the durable handoff.
client.wait_ready(reader, {'id': attachment_id})
response = client.call('GET', '/api/v1/attachments/' + attachment_id + '/content', actor=reader)
body = response['bytes']
return {'ok': body == content and response['headers'].get_content_type() == mime,
'bytes': len(body), 'sha256': hashlib.sha256(body).hexdigest(), 'status': response['status']}
def run_case(module, bootstrap_client, actor, reader, chat, stats, args, case):
name, width, height, size, seed = case
content = payload_for(case)
@@ -273,11 +282,7 @@ def run_case(module, bootstrap_client, actor, reader, chat, stats, args, case):
download = {'ok': False}
if successes:
try:
response = bootstrap_client.call('GET', '/api/v1/attachments/' + successes[0]['attachment_id'] + '/content', actor=reader)
downloaded = response['bytes']
download = {'ok': downloaded == content and response['headers'].get_content_type() == 'image/png',
'bytes': len(downloaded), 'sha256': hashlib.sha256(downloaded).hexdigest(),
'status': response['status']}
download = verify_download(bootstrap_client, reader, successes[0]['attachment_id'], content, 'image/png')
except Exception as exc:
download['error'] = type(exc).__name__
distinct = len({r['attachment_id'] for r in successes})