• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3"""
4Copyright (c) 2024 Huawei Device Co., Ltd.
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9    http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Description: Python Runtime Domain Interfaces
18"""
19
20import sys
21import json
22from pathlib import Path
23
24sys.path.append(str(Path(__file__).parent.parent)) # add aw path to sys.path
25
26from all_utils import CommonUtils
27from implement_api.protocol_api import ProtocolImpl
28from cdp import runtime
29from customized_types import ProtocolType
30
31comm_with_debugger_server = CommonUtils.communicate_with_debugger_server
32
33
34class RuntimeImpl(ProtocolImpl):
35
36    def __init__(self, id_generator, websocket):
37        super().__init__(id_generator, websocket)
38        self.dispatch_table = {"enable": (self.enable, ProtocolType.send),
39                               "runIfWaitingForDebugger": (self.run_if_waiting_for_debugger, ProtocolType.send),
40                               "getProperties": (self.get_properties, ProtocolType.send),
41                               "getHeapUsage": (self.get_heap_usage, ProtocolType.send)}
42
43    async def enable(self, message_id, connection, params):
44        response = await comm_with_debugger_server(self.websocket, connection,
45                                                   runtime.enable(), message_id)
46        CommonUtils.assert_equal(json.loads(response), {"id": message_id, "result": {"protocol": []}})
47
48    async def run_if_waiting_for_debugger(self, message_id, connection, params):
49        response = await comm_with_debugger_server(self.websocket, connection,
50                                                   runtime.run_if_waiting_for_debugger(), message_id)
51        CommonUtils.assert_equal(json.loads(response), {"id": message_id, "result": {}})
52
53    async def get_properties(self, message_id, connection, params):
54        response = await comm_with_debugger_server(self.websocket, connection,
55                                                   runtime.get_properties(params), message_id)
56        response = json.loads(response)
57        CommonUtils.assert_equal(response['id'], message_id)
58        return response
59
60    async def get_heap_usage(self, message_id, connection, params):
61        response = await comm_with_debugger_server(self.websocket, connection,
62                                                   runtime.get_heap_usage(), message_id)
63        while response.startswith('{"method":"HeapProfiler.lastSeenObjectId"') \
64                or response.startswith('{"method":"HeapProfiler.heapStatsUpdate"'):
65            response = await self.websocket.recv_msg_of_debugger_server(connection.instance_id,
66                                                                        connection.received_msg_queue)
67        response = json.loads(response)
68        CommonUtils.assert_equal(response['id'], message_id)
69        assert response['result']['usedSize'] > 0, f"{response['result']['usedSize']} is not greater than 0"
70        assert response['result']['totalSize'] > 0, f"{response['result']['totalSize']} is not greater than 0"
71        return response
72