athenad: fix log sort (#21703)

* athenad: fix log sort

* add test for logs to send

* use temp dir for logs

* fix changing SWAGLOG_DIR

* better way to patch SWAGLOG_DIR

* fix grammar
pull/21709/head
Greg Hogan 2021-07-24 20:11:29 -07:00 committed by GitHub
parent d74199fe81
commit 0964871239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 3 deletions

View File

@ -311,9 +311,8 @@ def get_logs_to_send_sorted():
# assume send failed and we lost the response if sent more than one hour ago
if not time_sent or curr_time - time_sent > 3600:
logs.append(log_entry)
# return logs in order they should be sent
# excluding most recent (active) log file
return sorted(logs[:-1])
return sorted(logs)[:-1]
def log_handler(end_event):
@ -332,7 +331,7 @@ def log_handler(end_event):
# send one log
curr_log = None
if len(log_files) > 0:
log_entry = log_files.pop()
log_entry = log_files.pop() # newest log file
cloudlog.debug(f"athena.log_handler.forward_request {log_entry}")
try:
curr_time = int(time.time())

View File

@ -14,6 +14,7 @@ from unittest import mock
from websocket import ABNF
from websocket._exceptions import WebSocketConnectionClosedException
from selfdrive import swaglog
from selfdrive.athena import athenad
from selfdrive.athena.athenad import dispatcher
from selfdrive.athena.tests.helpers import MockWebsocket, MockParams, MockApi, EchoSocket, with_http_server
@ -24,6 +25,7 @@ class TestAthenadMethods(unittest.TestCase):
def setUpClass(cls):
cls.SOCKET_PORT = 45454
athenad.ROOT = tempfile.mkdtemp()
athenad.SWAGLOG_DIR = swaglog.SWAGLOG_DIR = tempfile.mkdtemp()
athenad.Params = MockParams
athenad.Api = MockApi
athenad.LOCAL_PORT_WHITELIST = set([cls.SOCKET_PORT])
@ -204,5 +206,16 @@ class TestAthenadMethods(unittest.TestCase):
end_event.set()
thread.join()
def test_get_logs_to_send_sorted(self):
fl = list()
for i in range(10):
fn = os.path.join(swaglog.SWAGLOG_DIR, f'swaglog.{i:010}')
Path(fn).touch()
fl.append(os.path.basename(fn))
# ensure the list is all logs except most recent
sl = athenad.get_logs_to_send_sorted()
self.assertListEqual(sl, fl[:-1])
if __name__ == '__main__':
unittest.main()