Skip to content

Commit 27e37e2

Browse files
add logger to client system tests
Make system tests more stable.
1 parent 1a7589e commit 27e37e2

7 files changed

Lines changed: 63 additions & 10 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""Configure logger for class of Transport Interface."""
2+
3+
import logging
4+
5+
from uds.transport_interface import AbstractTransportInterface, TransportLogger
6+
7+
8+
# configure your logging
9+
# https://docs.python.org/3/library/logging.html
10+
logger = logging.getLogger("UDS") # example logger name
11+
logger.setLevel(logging.INFO) # example logging level
12+
# optionally configure logging to file
13+
file_handler = logging.FileHandler(filename="uds.log", encoding="utf-8")
14+
file_handler.setLevel(logging.INFO)
15+
logger.addHandler(file_handler)
16+
# optionally configure logging to python console
17+
stream_handler = logging.StreamHandler()
18+
stream_handler.setLevel(logging.INFO)
19+
logger.addHandler(stream_handler)
20+
21+
# configure your logger
22+
# https://uds.readthedocs.io/en/stable/pages/user_guide/logging.html#configuration
23+
transport_logger = TransportLogger(logger_name="UDS", # the same name as previously configured logger
24+
message_logging_level=logging.INFO,
25+
packet_logging_level=logging.INFO,
26+
log_sending=True,
27+
log_receiving=True)
28+
29+
# activate your logger
30+
# https://uds.readthedocs.io/en/stable/pages/user_guide/logging.html#decorating-transport-interface-class
31+
@transport_logger
32+
class CustomTransportInterface(AbstractTransportInterface):
33+
... # TODO: implement your interface class

examples/transport_interface/instance_logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
log_receiving=True)
3131

3232
# activate your logger
33-
# http://localhost:63342/uds/docs/build/html/pages/user_guide/logging.html#decorating-transport-interface-instance
33+
# https://uds.readthedocs.io/en/stable/pages/user_guide/logging.html#decorating-transport-interface-instance
3434
transport_interface_with_logger = transport_logger(transport_interface)
3535

3636
# TODO: use `transport_interface_with_logger` the same way as `transport_interface`

tests/software_tests/transport_interface/test_logger.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from asyncio import iscoroutine, iscoroutinefunction
2-
from inspect import *
3-
41
import pytest
52
from mock import AsyncMock, MagicMock, Mock, call, patch
63

tests/system_tests/client/can/python_can.py renamed to tests/system_tests/client/can/test_python_can_kvaser.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from random import choice
2+
from time import sleep
23

34
from tests.conftest import make_can_addressing_information
45

56
from can import Bus
67
from uds.can import CanAddressingFormat, DefaultFlowControlParametersGenerator, PyCanTransportInterface
78
from uds.utilities import TimeMillisecondsAlias
89

9-
from ..test_client import (
10+
from ..client import (
1011
AbstractBaseClientFunctionalityTests,
1112
AbstractClientErrorGuessing,
1213
AbstractClientTests,
@@ -32,9 +33,10 @@ def _define_transport_interfaces(self):
3233
self.can_interface_2 = Bus(interface="kvaser",
3334
channel=1,
3435
fd=True)
35-
self.transport_interface_1 = PyCanTransportInterface(
36+
transport_interface_1 = PyCanTransportInterface(
3637
network_manager=self.can_interface_1,
3738
addressing_information=addressing_information)
39+
self.transport_interface_1 = self.transport_logger(transport_interface_1)
3840
self.transport_interface_2 = PyCanTransportInterface(
3941
network_manager=self.can_interface_2,
4042
addressing_information=addressing_information.get_other_end())
@@ -46,6 +48,7 @@ def teardown_method(self):
4648
super().teardown_method()
4749
self.can_interface_1.shutdown()
4850
self.can_interface_2.shutdown()
51+
sleep(0.1)
4952

5053
def configure_slow_message_reception(self):
5154
"""Change configuration of Transport Interfaces to reach timeouts easily."""
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from abc import ABC, abstractmethod
99
from time import perf_counter, sleep
10+
import logging
11+
from datetime import datetime
1012

1113
import pytest
1214
from tests.system_tests import BaseSystemTests
@@ -15,12 +17,16 @@
1517
from uds.client import Client
1618
from uds.message import RequestSID, UdsMessage, UdsMessageRecord
1719
from uds.translator import BASE_TRANSLATOR
18-
from uds.transport_interface import AbstractTransportInterface
20+
from uds.transport_interface import AbstractTransportInterface, TransportLogger
1921

2022

2123
class AbstractClientTests(BaseSystemTests, ABC):
2224
"""Abstract definition of System tests for UDS Client."""
2325

26+
LOGGER_NAME: str = "UDS"
27+
28+
logger: logging.Logger
29+
transport_logger: TransportLogger
2430
transport_interface_1: AbstractTransportInterface
2531
transport_interface_2: AbstractTransportInterface
2632

@@ -36,6 +42,20 @@ def _define_transport_interfaces(self):
3642
def configure_slow_message_reception(self):
3743
"""Change configuration of Transport Interfaces to reach timeouts easily."""
3844

45+
@classmethod
46+
def configure_logger(cls):
47+
cls.logger = logging.getLogger(cls.LOGGER_NAME)
48+
cls.logger.setLevel(logging.INFO)
49+
date_and_time = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
50+
file_handler = logging.FileHandler(filename=f"{date_and_time}.log", encoding="utf-8")
51+
file_handler.setLevel(logging.INFO)
52+
cls.logger.addHandler(file_handler)
53+
cls.transport_logger = TransportLogger(logger_name=cls.LOGGER_NAME)
54+
55+
@classmethod
56+
def setup_class(cls):
57+
cls.configure_logger()
58+
3959
def setup_method(self):
4060
"""
4161
Prepare for testing:

uds/can/transport_interface/python_can.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def __teardown_sync_listening(self, suppress_warning: bool = False) -> None:
205205
:param suppress_warning: Do not warn about mixing Synchronous and Asynchronous implementation.
206206
"""
207207
if self.notifier is not None:
208-
self.notifier.stop(self._MIN_NOTIFIER_TIMEOUT)
208+
self.notifier.stop()
209209
self.notifier = None
210210
if not suppress_warning:
211211
warn(message="Notifier (python-can) was stopped. "
@@ -222,7 +222,7 @@ def __teardown_async_listening(self, suppress_warning: bool = False) -> None:
222222
:param suppress_warning: Do not warn about mixing Synchronous and Asynchronous implementation.
223223
"""
224224
if self.async_notifier is not None:
225-
self.async_notifier.stop(self._MIN_NOTIFIER_TIMEOUT)
225+
self.async_notifier.stop()
226226
self.async_notifier = None
227227
if not suppress_warning:
228228
warn(message="Async notifier (python-can) was stopped. "

uds/transport_interface/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def log_receiving(self) -> bool:
121121

122122
@log_receiving.setter
123123
def log_receiving(self, value: bool) -> None:
124-
"""Set whether outgoing traffic shall be logged."""
124+
"""Set whether incoming traffic shall be logged."""
125125
self.__log_receiving = bool(value)
126126

127127
@property

0 commit comments

Comments
 (0)