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 4 years ago committed by GitHub
parent d74199fe81
commit 0964871239
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      selfdrive/athena/athenad.py
  2. 13
      selfdrive/athena/tests/test_athenad.py

@ -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 # 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: if not time_sent or curr_time - time_sent > 3600:
logs.append(log_entry) logs.append(log_entry)
# return logs in order they should be sent
# excluding most recent (active) log file # excluding most recent (active) log file
return sorted(logs[:-1]) return sorted(logs)[:-1]
def log_handler(end_event): def log_handler(end_event):
@ -332,7 +331,7 @@ def log_handler(end_event):
# send one log # send one log
curr_log = None curr_log = None
if len(log_files) > 0: 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}") cloudlog.debug(f"athena.log_handler.forward_request {log_entry}")
try: try:
curr_time = int(time.time()) curr_time = int(time.time())

@ -14,6 +14,7 @@ from unittest import mock
from websocket import ABNF from websocket import ABNF
from websocket._exceptions import WebSocketConnectionClosedException from websocket._exceptions import WebSocketConnectionClosedException
from selfdrive import swaglog
from selfdrive.athena import athenad from selfdrive.athena import athenad
from selfdrive.athena.athenad import dispatcher from selfdrive.athena.athenad import dispatcher
from selfdrive.athena.tests.helpers import MockWebsocket, MockParams, MockApi, EchoSocket, with_http_server from selfdrive.athena.tests.helpers import MockWebsocket, MockParams, MockApi, EchoSocket, with_http_server
@ -24,6 +25,7 @@ class TestAthenadMethods(unittest.TestCase):
def setUpClass(cls): def setUpClass(cls):
cls.SOCKET_PORT = 45454 cls.SOCKET_PORT = 45454
athenad.ROOT = tempfile.mkdtemp() athenad.ROOT = tempfile.mkdtemp()
athenad.SWAGLOG_DIR = swaglog.SWAGLOG_DIR = tempfile.mkdtemp()
athenad.Params = MockParams athenad.Params = MockParams
athenad.Api = MockApi athenad.Api = MockApi
athenad.LOCAL_PORT_WHITELIST = set([cls.SOCKET_PORT]) athenad.LOCAL_PORT_WHITELIST = set([cls.SOCKET_PORT])
@ -204,5 +206,16 @@ class TestAthenadMethods(unittest.TestCase):
end_event.set() end_event.set()
thread.join() 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__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save