|
|
@ -9,10 +9,11 @@ import queue |
|
|
|
import unittest |
|
|
|
import unittest |
|
|
|
from dataclasses import asdict, replace |
|
|
|
from dataclasses import asdict, replace |
|
|
|
from datetime import datetime, timedelta |
|
|
|
from datetime import datetime, timedelta |
|
|
|
|
|
|
|
from parameterized import parameterized |
|
|
|
from typing import Optional |
|
|
|
from typing import Optional |
|
|
|
|
|
|
|
|
|
|
|
from multiprocessing import Process |
|
|
|
from multiprocessing import Process |
|
|
|
from pathlib import Path |
|
|
|
from pympler.tracker import SummaryTracker |
|
|
|
from unittest import mock |
|
|
|
from unittest import mock |
|
|
|
from websocket import ABNF |
|
|
|
from websocket import ABNF |
|
|
|
from websocket._exceptions import WebSocketConnectionClosedException |
|
|
|
from websocket._exceptions import WebSocketConnectionClosedException |
|
|
@ -57,10 +58,11 @@ class TestAthenadMethods(unittest.TestCase): |
|
|
|
break |
|
|
|
break |
|
|
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
@staticmethod |
|
|
|
def _create_file(file: str, parent: Optional[str] = None) -> str: |
|
|
|
def _create_file(file: str, parent: Optional[str] = None, data: bytes = b'') -> str: |
|
|
|
fn = os.path.join(Paths.log_root() if parent is None else parent, file) |
|
|
|
fn = os.path.join(Paths.log_root() if parent is None else parent, file) |
|
|
|
os.makedirs(os.path.dirname(fn), exist_ok=True) |
|
|
|
os.makedirs(os.path.dirname(fn), exist_ok=True) |
|
|
|
Path(fn).touch() |
|
|
|
with open(fn, 'wb') as f: |
|
|
|
|
|
|
|
f.write(data) |
|
|
|
return fn |
|
|
|
return fn |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -137,19 +139,31 @@ class TestAthenadMethods(unittest.TestCase): |
|
|
|
if fn.endswith('.bz2'): |
|
|
|
if fn.endswith('.bz2'): |
|
|
|
self.assertEqual(athenad.strip_bz2_extension(fn), fn[:-4]) |
|
|
|
self.assertEqual(athenad.strip_bz2_extension(fn), fn[:-4]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@parameterized.expand([(True,), (False,)]) |
|
|
|
@with_http_server |
|
|
|
@with_http_server |
|
|
|
def test_do_upload(self, host): |
|
|
|
def test_do_upload(self, compress, host): |
|
|
|
fn = self._create_file('qlog.bz2') |
|
|
|
# random bytes to ensure rather large object post-compression |
|
|
|
|
|
|
|
fn = self._create_file('qlog', data=os.urandom(10000 * 1024)) |
|
|
|
|
|
|
|
|
|
|
|
item = athenad.UploadItem(path=fn, url="http://localhost:1238", headers={}, created_at=int(time.time()*1000), id='') |
|
|
|
# warm up object tracker |
|
|
|
|
|
|
|
tracker = SummaryTracker() |
|
|
|
|
|
|
|
for _ in range(5): |
|
|
|
|
|
|
|
tracker.diff() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
upload_fn = fn + ('.bz2' if compress else '') |
|
|
|
|
|
|
|
item = athenad.UploadItem(path=upload_fn, url="http://localhost:1238", headers={}, created_at=int(time.time()*1000), id='') |
|
|
|
with self.assertRaises(requests.exceptions.ConnectionError): |
|
|
|
with self.assertRaises(requests.exceptions.ConnectionError): |
|
|
|
athenad._do_upload(item) |
|
|
|
athenad._do_upload(item) |
|
|
|
|
|
|
|
|
|
|
|
item = athenad.UploadItem(path=fn, url=f"{host}/qlog.bz2", headers={}, created_at=int(time.time()*1000), id='') |
|
|
|
item = athenad.UploadItem(path=upload_fn, url=f"{host}/qlog.bz2", headers={}, created_at=int(time.time()*1000), id='') |
|
|
|
resp = athenad._do_upload(item) |
|
|
|
resp = athenad._do_upload(item) |
|
|
|
self.assertEqual(resp.status_code, 201) |
|
|
|
self.assertEqual(resp.status_code, 201) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# assert memory cleaned up |
|
|
|
|
|
|
|
for _type, num_objects, total_size in tracker.diff(): |
|
|
|
|
|
|
|
with self.subTest(_type=_type): |
|
|
|
|
|
|
|
self.assertLess(total_size / 1024, 10, f'Object {_type} ({num_objects=}) grew larger than 10 kB while uploading file') |
|
|
|
|
|
|
|
|
|
|
|
@with_http_server |
|
|
|
@with_http_server |
|
|
|
def test_uploadFileToUrl(self, host): |
|
|
|
def test_uploadFileToUrl(self, host): |
|
|
|
fn = self._create_file('qlog.bz2') |
|
|
|
fn = self._create_file('qlog.bz2') |
|
|
|