Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8d7ec88a authored by Martin Brabham's avatar Martin Brabham
Browse files

Topshim Testing: Further simplification

All of the jobs get serialized to the same asyncio handler
without all of the test code boiler plate.

Bug: 234756905
Test: system/gd/cert/run --clean --topshim
Tag: #floss
Change-Id: I93c0afa0d92793d11f34293bb6c88141c7c0a977
parent 1811a0e5
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -19,11 +19,12 @@ import grpc

from blueberry.facade.topshim import facade_pb2
from blueberry.facade.topshim import facade_pb2_grpc
from blueberry.tests.topshim.lib.async_closable import AsyncClosable

from google.protobuf import empty_pb2 as empty_proto


class AdapterClient():
class AdapterClient(AsyncClosable):
    """
    Wrapper gRPC interface to the Topshim/BTIF layer
    """
@@ -39,7 +40,7 @@ class AdapterClient():
        self.__adapter_stub = facade_pb2_grpc.AdapterServiceStub(self.__channel)
        self.__adapter_event_stream = self.__adapter_stub.FetchEvents(facade_pb2.FetchEventsRequest())

    async def terminate(self):
    async def close(self):
        for task in self.__task_list:
            task.cancel()
            task = None
+50 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3
#
#   Copyright 2022 - The Android Open Source Project
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import asyncio
import time
from abc import ABC, abstractmethod
import logging


class AsyncClosable(ABC):

    async def __async_exit(self, type=None, value=None, traceback=None):
        try:
            return await self.close()
        except Exception:
            logging.warning("Failed to close or already closed")

    def __enter__(self):
        return self

    def __exit__(self, type, value, traceback):
        asyncio.run_until_complete(self.__async_exit(type, value, traceback))
        return traceback is None

    def __del__(self):
        asyncio.get_event_loop().run_until_complete(self.__async_exit())

    @abstractmethod
    async def close(self):
        pass


async def asyncSafeClose(closable):
    if closable is None:
        logging.warn("Tried to close an object that is None")
        return
    await closable.close()
+4 −2
Original line number Diff line number Diff line
@@ -19,11 +19,13 @@ import grpc

from blueberry.facade.topshim import facade_pb2
from blueberry.facade.topshim import facade_pb2_grpc
from blueberry.tests.topshim.lib.async_closable import AsyncClosable
from blueberry.tests.topshim.lib.async_closable import asyncSafeClose

from google.protobuf import empty_pb2 as empty_proto


class GattClient():
class GattClient(AsyncClosable):
    """
    Wrapper gRPC interface to the GATT Service
    """
@@ -39,7 +41,7 @@ class GattClient():
        self.__gatt_stub = facade_pb2_grpc.GattServiceStub(self.__channel)
        #self.__gatt_event_stream = self.__gatt_stub.FetchEvents(facade_pb2.FetchEventsRequest())

    async def terminate(self):
    async def close(self):
        """
        Terminate the current tasks
        """
+16 −18
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ from blueberry.tests.gd.cert.os_utils import TerminalColor
from blueberry.tests.gd.cert.tracelogger import TraceLogger
from blueberry.tests.gd.cert.truth import assertThat
from blueberry.tests.topshim.lib.adapter_client import AdapterClient
from blueberry.tests.topshim.lib.async_closable import asyncSafeClose
from blueberry.tests.topshim.lib.gatt_client import GattClient
from blueberry.tests.topshim.lib.topshim_device import TopshimDevice

@@ -161,31 +162,28 @@ class TopshimBaseTest(base_test.BaseTestClass):
    __dut = None
    __cert = None

    # TODO(optedoblivion): Make TopshimTestStack class
    dut_adapter = None
    dut_gatt = None

    cert_adapter = None
    cert_gatt = None
    async def __safe_terminate(self, object):
        if object != None:
            return await object.terminate()
        else:
            self.log.warn("Object was already null!")

    async def _setup_adapter(self):
        self.dut_adapter = AdapterClient(port=self.dut_port)
        self.cert_adapter = AdapterClient(port=self.cert_port)
        started = await self.dut_adapter._verify_adapter_started()
        dut_adapter = AdapterClient(port=self.dut_port)
        # TODO(optedoblivion): Remove hack
        self.dut_adapter = dut_adapter
        cert_adapter = AdapterClient(port=self.cert_port)
        started = await dut_adapter._verify_adapter_started()
        assertThat(started).isTrue()
        started = started and await self.cert_adapter._verify_adapter_started()
        started = started and await cert_adapter._verify_adapter_started()
        assertThat(started).isTrue()
        self.dut_gatt = GattClient(port=self.dut_port)
        self.cert_gatt = GattClient(port=self.cert_port)
        self.__dut = TopshimDevice(self.dut_adapter, self.dut_gatt)
        self.__cert = TopshimDevice(self.cert_adapter, self.cert_gatt)
        self.__dut = TopshimDevice(dut_adapter, GattClient(port=self.dut_port))
        self.__cert = TopshimDevice(cert_adapter, GattClient(port=self.cert_port))
        return started

    async def _teardown_adapter(self):
        await self.dut_adapter.terminate()
        await self.dut_gatt.terminate()
        await self.cert_adapter.terminate()
        await self.cert_gatt.terminate()
        await asyncSafeClose(self.__dut)
        await asyncSafeClose(self.__cert)

    def dut(self):
        """
+63 −3
Original line number Diff line number Diff line
#!/usr/bin/env python3
#
#   Copyright 2019 - The Android Open Source Project
#
#   Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,6 +16,8 @@
import asyncio
import logging

from blueberry.tests.topshim.lib.async_closable import AsyncClosable
from blueberry.tests.topshim.lib.async_closable import asyncSafeClose
from blueberry.tests.gd.cert.gd_device import GdHostOnlyDevice
from blueberry.tests.gd.cert.gd_device import MOBLY_CONTROLLER_CONFIG_NAME
from blueberry.tests.gd.cert.os_utils import get_gd_root
@@ -65,19 +66,78 @@ def get_instances_with_configs(configs):
    return devices


class TopshimDevice():
class TopshimDevice(AsyncClosable):
    __adapter = None
    __gatt = None

    async def __le_rand_wrapper(self, async_fn):
        await async_fn


#        await self.__adapter.le_rand()

    def __post(self, async_fn):
        asyncio.get_event_loop().run_until_complete(async_fn)
        asyncio.get_event_loop().run_until_complete(self.__le_rand_wrapper(async_fn))

    def __init__(self, adapter, gatt):
        self.__adapter = adapter
        self.__gatt = gatt

    async def close(self):
        """
        Implement abstract method to close out any streams or jobs.
        """
        await asyncSafeClose(self.__adapter)
        await asyncSafeClose(self.__gatt)

    def enable_page_scan(self):
        self.__post(self.__adapter.enable_page_scan())

    def disable_page_scan(self):
        self.__post(self.__adapter.disable_page_scan())

    def start_advertising(self):
        """
        Starts BLE Advertiser for the stack.
        Assumes stack defaults.  Which in our case would be RRPA
        """
        self.__post(self.__gatt.advertising_enable())

    def stop_advertising(self):
        """
        Stop BLE Advertiser.
        """
        self.__post(self.__gatt.advertising_disable())

    def start_scanning(self):
        pass

    def stop_scanning(self):
        pass

    def clear_event_mask(self):
        self.__post(self.__adapter.clear_event_mask())

    def clear_event_filter(self):
        self.__post(self.__adapter.clear_event_filter())

    def clear_filter_accept_list(self):
        self.__post(self.__adapter.clear_filter_accept_list())

    def disconnect_all_acls(self):
        self.__post(self.__adapter.disconnect_all_acls())

    def allow_wake_by_hid(self):
        self.__post(self.__adapter.allow_wake_by_hid())

    def set_default_event_mask(self):
        self.__post(self.__adapter.set_default_event_mask())

    def set_event_filter_inquiry_result_all_devices(self):
        self.__post(self.__adapter.set_event_filter_inquiry_result_all_devices())

    def set_event_filter_connection_setup_all_devices(self):
        self.__post(self.__adapter.set_event_filter_connection_setup_all_devices())

    def le_rand(self):
        self.__post(self.__adapter.le_rand())
Loading