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

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
+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()