Files
mtusi2026lab4/student-tests/slow_upload.py
T

33 lines
1.4 KiB
Python

"""A continuously trickling client still reaches the absolute upload deadline."""
import json
from pathlib import Path
import select
import socket
import sys
import time
import uuid
from urllib.parse import urlsplit
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client
c = Client()
alice, bob, eve, chat = c.bootstrap()
target = urlsplit(c.base)
connection = socket.create_connection((target.hostname, target.port), timeout=3)
header = 'POST /api/v1/chats/' + chat['id'] + '/attachments?filename=slow_' + uuid.uuid4().hex + '.png HTTP/1.1\r\nHost: localhost\r\nAuthorization: Bearer ' + alice['token'] + '\r\nContent-Type: image/png\r\nContent-Length: 10485760\r\nConnection: close\r\n\r\n'
started = time.monotonic()
connection.sendall(header.encode())
response = b''
while time.monotonic() - started < 20:
if select.select([connection], [], [], .15)[0]:
response += connection.recv(4096)
if b'\r\n\r\n' in response:
break
connection.sendall(b'x')
connection.close()
elapsed = time.monotonic() - started
assert response.startswith(b'HTTP/1.1 400') or response.startswith(b'HTTP/1.0 400'), response[:100]
assert 14 <= elapsed < 19
assert b'x-instance-id:' in response.lower() and b'x-request-id:' in response.lower()
print(json.dumps({'trickle_seconds': round(elapsed, 3), 'http_status': 400, 'absolute_deadline_enforced': True}))